/* PyBadge/PyGamer Display Read - see http://www.technoblogy.com/show?332T David Johnson-Davies - www.technoblogy.com - v2 21st April 2020 For Adafruit PyBadge/PyGamer ST7735 TFT displays CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include // Core graphics library #include // Hardware-specific library for ST7735 #include // Adafruit PyBadge/PyGamer #define TFT_CS 44 // Chip select #define TFT_RST 46 // Display reset #define TFT_DC 45 // Display data/command select #define TFT_BACKLIGHT 47 // Display backlight pin #define TFT_MOSI 41 // Data out #define TFT_SCLK 42 // Clock out class Technoblogy_ST7735 : public Adafruit_ST7735 { public: Technoblogy_ST7735(int8_t cs, int8_t dc, int8_t mosi, int8_t sclk, int8_t rst); uint16_t getPixel(uint16_t x, uint16_t y); void xorPixel(uint16_t x, uint16_t y, uint16_t color); }; Technoblogy_ST7735::Technoblogy_ST7735(int8_t cs, int8_t dc, int8_t mosi, int8_t sclk, int8_t rst) : Adafruit_ST7735(cs, dc, mosi, sclk, rst) {} Technoblogy_ST7735 tft = Technoblogy_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); uint16_t Technoblogy_ST7735::getPixel (uint16_t x, uint16_t y) { uint32_t ret = 0; startWrite(); setAddrWindow(x, y, 1, 1); writeCommand(ST77XX_RAMRD); pinMode(TFT_MOSI, INPUT); pinMode(TFT_SCLK, OUTPUT); for (int i=0; i<33; i++) { digitalWrite(TFT_SCLK, HIGH); ret = ret<<1 | digitalRead(TFT_MOSI); digitalWrite(TFT_SCLK, LOW); } pinMode(TFT_MOSI, OUTPUT); endWrite(); return ((ret & 0xf80000)>>8 | (ret & 0xfc00)>>5 | (ret & 0xf8)>>3); } void Technoblogy_ST7735::xorPixel (uint16_t x, uint16_t y, uint16_t color) { uint16_t lastcolor = getPixel(x, y); if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) { startWrite(); writeCommand(ST77XX_RAMWR); SPI_WRITE16(color ^ lastcolor); endWrite(); } } // Simple bouncing-ball example ********************************************** const uint16_t Barrier = ST77XX_RED; const uint16_t Ball = ST77XX_CYAN; void drawBall (int x, int y) { tft.xorPixel(x, y, Ball); tft.xorPixel(x+1, y, Ball); tft.xorPixel(x-1, y, Ball); tft.xorPixel(x, y+1, Ball); tft.xorPixel(x, y-1, Ball); } void setup() { tft.initR(INITR_BLACKTAB); tft.setRotation(1); pinMode(TFT_BACKLIGHT, OUTPUT); digitalWrite(TFT_BACKLIGHT, HIGH); tft.fillScreen(ST77XX_BLACK); // // Draw arena tft.fillRect(0, 0, 160, 2, Barrier); tft.fillRect(0, 126, 160, 2, Barrier); tft.fillRect(0, 0, 2, 128, Barrier); tft.fillRect(158, 0, 2, 128, Barrier); tft.fillRect(50, 32, 2, 64, Barrier); tft.fillRect(108, 32, 2, 64, Barrier); tft.setCursor(44, 4); tft.setTextColor(ST77XX_WHITE); tft.print("Bouncing Ball"); } void loop() { int ballX = 80, ballY = 64; int dirX = -1, dirY = -1; drawBall(ballX, ballY); for (;;) { drawBall(ballX, ballY); // Move ball ballX = ballX + dirX; ballY = ballY + dirY; drawBall(ballX, ballY); // Check if collision if (tft.getPixel(ballX + (dirX*2), ballY) == Barrier) dirX = -dirX; if (tft.getPixel(ballX, ballY + (dirY*2)) == Barrier) dirY = -dirY; delay(10); } }