/* PyGamer/PyBadge TFT Screenshot - see http://www.technoblogy.com/show?398X David Johnson-Davies - www.technoblogy.com - 8th August 2020 For Adafruit PyGamer/PyBadge 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 #include #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); private: uint16_t readCol16(); void readInit(); }; 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); // Helper functions void Technoblogy_ST7735::readInit () { writeCommand(ST77XX_RAMRD); pinMode(TFT_MOSI, INPUT); pinMode(TFT_SCLK, OUTPUT); for (int i=0; i<9; i++) { digitalWrite(TFT_SCLK, HIGH); digitalWrite(TFT_SCLK, LOW); } } uint16_t Technoblogy_ST7735::readCol16 () { uint32_t col; for (int i=0; i<24; i++) { digitalWrite(TFT_SCLK, HIGH); col = col<<1 | digitalRead(TFT_MOSI); digitalWrite(TFT_SCLK, LOW); } return ((col & 0xf80000)>>8 | (col & 0xfc00)>>5 | (col & 0xf8)>>3); } // Return the colour of the pixel at x, y uint16_t Technoblogy_ST7735::getPixel (uint16_t x, uint16_t y) { uint16_t col16; startWrite(); setAddrWindow(x, y, 1, 1); readInit(); col16 = readCol16(); pinMode(TFT_MOSI, OUTPUT); endWrite(); return col16; } // Save screen in BMP format ********************************************** File bmpFile; // Write two bytes, least significant byte first void writeTwo (uint16_t word) { bmpFile.write(word & 0xFF); bmpFile.write((word >> 8) & 0xFF); } // Write four bytes, least significant byte first void writeFour (uint32_t word) { bmpFile.write(word & 0xFF); bmpFile.write((word >> 8) & 0xFF); bmpFile.write((word >> 16) & 0xFF); bmpFile.write((word >> 24) & 0xFF); } void bmpSave () { uint32_t filesize, offset; uint16_t width = tft.width(), height = tft.height(); char filename[11] = "image1.bmp"; SD.begin(); while (SD.exists(filename)) { filename[5]++; } bmpFile = SD.open(filename, FILE_WRITE); // On error hang up if (!bmpFile) for (;;); digitalWrite(LED_BUILTIN, HIGH); // // File header: 14 bytes bmpFile.write('B'); bmpFile.write('M'); writeFour(14+40+12+width*height*2); // File size in bytes writeFour(0); writeFour(14+40+12); // Offset to image data from start // // Image header: 40 bytes writeFour(40); // Header size writeFour(width); // Image width writeFour(height); // Image height writeTwo(1); // Planes writeTwo(16); // Bits per pixel writeFour(0); // Compression (none) writeFour(0); // Image size (0 for uncompressed) writeFour(0); // Preferred X resolution (ignore) writeFour(0); // Preferred Y resolution (ignore) writeFour(0); // Colour map entries (ignore) writeFour(0); // Important colours (ignore) // // Colour masks: 12 bytes writeFour(0b0000011111100000); // Green writeFour(0b1111100000000000); // Red writeFour(0b0000000000011111); // Blue // // Image data: width * height * 2 bytes for (int y=height-1; y>=0; y--) { for (int x=0; x>3; } void plotDemo () { int xsize = tft.width(), ysize = tft.height(), x1 = 15, y1 = ysize-11-1, h; tft.setCursor((xsize-x1-90)/2+x1, ysize-1-(ysize-8)); tft.print("Sensor Readings"); // Horizontal axis int xinc = (xsize-x1)/20; tft.drawLine(x1, y1, xsize-1, y1, ST77XX_WHITE); for (int i=0; i<=20; i=i+4) { int mark = x1+i*xinc; tft.drawLine(mark, y1, mark, y1+2, ST77XX_WHITE); // Draw histogram if (i != 20) { int bar = xinc*4/3; h = 5+random(ysize-31); tft.fillRect(mark+bar*2-1, y1-h, bar, h, Colour(0, 0, 255)); h = 5+random(ysize-31); tft.fillRect(mark+bar, y1-h, bar, h, Colour(0, 255, 0)); h = 5+random(ysize-31); tft.fillRect(mark+1, y1-h, bar, h, Colour(255, 0, 0)); } int tens = i/10; if (tens != 0) { tft.setCursor(mark-7, y1+4); tft.print(tens); tft.setCursor(mark-1, y1+4); tft.print(i%10); } else { tft.setCursor(mark-3, y1+4); tft.print(i%10); } } // Vertical axis int yinc = (ysize-11)/20; tft.drawLine(x1, y1, x1, 0, ST77XX_WHITE); for (int i=0; i<=20; i=i+5) { int mark = y1-i*yinc; tft.drawLine(x1, mark, x1-2, mark, ST77XX_WHITE); int tens = i/10; if (tens != 0) { tft.setCursor(x1-15, mark-3); tft.print(tens); } tft.setCursor(x1-9, mark-3); tft.print(i%10); } } // Setup ********************************************** void setup () { // Use LED as error light pinMode(LED_BUILTIN, OUTPUT); tft.initR(INITR_BLACKTAB); tft.setRotation(1); pinMode(TFT_BACKLIGHT, OUTPUT); digitalWrite(TFT_BACKLIGHT, HIGH); tft.fillScreen(ST77XX_BLACK); } void loop () { plotDemo(); // Save file bmpSave(); for(;;); }