PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
8.6KB

  1. /***************************************************
  2. This is an example sketch for the Adafruit 1.8" TFT shield with joystick
  3. ----> http://www.adafruit.com/products/802
  4. Check out the links above for our tutorials and wiring diagrams
  5. These displays use SPI to communicate, 4 pins are required to
  6. interface
  7. One pin is also needed for the joystick, we use analog 3
  8. Adafruit invests time and resources providing this open source code,
  9. please support Adafruit and open-source hardware by purchasing
  10. products from Adafruit!
  11. Written by Limor Fried/Ladyada for Adafruit Industries.
  12. MIT license, all text above must be included in any redistribution
  13. ****************************************************/
  14. #include <Adafruit_GFX.h>
  15. #include <ST7735_t3.h>
  16. #include <SD.h>
  17. #include <SPI.h>
  18. // This Teensy3 native optimized version requires specific pins
  19. //
  20. #define TFT_SCLK 13 // SCLK can also use pin 14
  21. #define TFT_MOSI 11 // MOSI can also use pin 7
  22. #define TFT_CS 10 // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
  23. #define TFT_DC 9 // but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
  24. #define TFT_RST 8 // RST can use any pin
  25. #define SD_CS 4 // CS for SD card, can use any pin
  26. ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  27. #define BUTTON_NONE 0
  28. #define BUTTON_DOWN 1
  29. #define BUTTON_RIGHT 2
  30. #define BUTTON_SELECT 3
  31. #define BUTTON_UP 4
  32. #define BUTTON_LEFT 5
  33. void setup(void) {
  34. pinMode(SD_CS, INPUT_PULLUP); // keep SD CS high when not using SD card
  35. Serial.begin(9600);
  36. // Use this initializer if you're using a 1.8" TFT
  37. tft.initR(INITR_BLACKTAB);
  38. // Use this initializer (uncomment) if you're using a 1.44" TFT
  39. //tft.initR(INITR_144GREENTAB);
  40. Serial.println("OK!");
  41. tft.fillScreen(ST7735_BLACK);
  42. }
  43. uint8_t readButton(void) {
  44. float a = analogRead(3);
  45. a *= 5.0;
  46. a /= 1024.0;
  47. Serial.print("Button read analog = ");
  48. Serial.println(a);
  49. if (a < 0.2) return BUTTON_DOWN;
  50. if (a < 1.0) return BUTTON_RIGHT;
  51. if (a < 1.5) return BUTTON_SELECT;
  52. if (a < 2.0) return BUTTON_UP;
  53. if (a < 3.2) return BUTTON_LEFT;
  54. else return BUTTON_NONE;
  55. }
  56. uint8_t buttonhistory = 0;
  57. void loop() {
  58. uint8_t b = readButton();
  59. tft.setTextSize(3);
  60. if (b == BUTTON_DOWN) {
  61. tft.setTextColor(ST7735_RED);
  62. tft.setCursor(0, 10);
  63. tft.print("Down ");
  64. buttonhistory |= 1;
  65. }
  66. if (b == BUTTON_LEFT) {
  67. tft.setTextColor(ST7735_YELLOW);
  68. tft.setCursor(0, 35);
  69. tft.print("Left ");
  70. buttonhistory |= 2;
  71. }
  72. if (b == BUTTON_UP) {
  73. tft.setTextColor(ST7735_GREEN);
  74. tft.setCursor(0, 60);
  75. tft.print("Up");
  76. buttonhistory |= 4;
  77. }
  78. if (b == BUTTON_RIGHT) {
  79. tft.setTextColor(ST7735_BLUE);
  80. tft.setCursor(0, 85);
  81. tft.print("Right");
  82. buttonhistory |= 8;
  83. }
  84. if ((b == BUTTON_SELECT) && (buttonhistory == 0xF)) {
  85. tft.setTextColor(ST7735_MAGENTA);
  86. tft.setCursor(0, 110);
  87. tft.print("SELECT");
  88. buttonhistory |= 8;
  89. delay(2000);
  90. Serial.print("Initializing SD card...");
  91. if (!SD.begin(SD_CS)) {
  92. tft.fillScreen(ST7735_BLACK);
  93. tft.setCursor(5, tft.height()/2 - 6);
  94. tft.print("Unable to access");
  95. tft.setCursor(32, tft.height()/2 + 6);
  96. tft.print("SD card");
  97. Serial.println("failed!");
  98. return;
  99. }
  100. bmpDraw("parrot.bmp", 0, 0);
  101. while (1);
  102. }
  103. delay(100);
  104. }
  105. // This function opens a Windows Bitmap (BMP) file and
  106. // displays it at the given coordinates. It's sped up
  107. // by reading many pixels worth of data at a time
  108. // (rather than pixel by pixel). Increasing the buffer
  109. // size takes more of the Arduino's precious RAM but
  110. // makes loading a little faster. 20 pixels seems a
  111. // good balance.
  112. #define BUFFPIXEL 20
  113. void bmpDraw(const char *filename, uint8_t x, uint8_t y) {
  114. File bmpFile;
  115. int bmpWidth, bmpHeight; // W+H in pixels
  116. uint8_t bmpDepth; // Bit depth (currently must be 24)
  117. uint32_t bmpImageoffset; // Start of image data in file
  118. uint32_t rowSize; // Not always = bmpWidth; may have padding
  119. uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  120. uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  121. boolean goodBmp = false; // Set to true on valid header parse
  122. boolean flip = true; // BMP is stored bottom-to-top
  123. int w, h, row, col;
  124. uint8_t r, g, b;
  125. uint32_t pos = 0, startTime = millis();
  126. if((x >= tft.width()) || (y >= tft.height())) return;
  127. Serial.println();
  128. Serial.print("Loading image '");
  129. Serial.print(filename);
  130. Serial.println('\'');
  131. // Open requested file on SD card
  132. bmpFile = SD.open(filename);
  133. if (!bmpFile) {
  134. tft.fillScreen(ST7735_BLACK);
  135. tft.setCursor(12, tft.height()/2 - 12);
  136. tft.print("Unable to");
  137. tft.setCursor(12, tft.height()/2 - 0);
  138. tft.print("read file: ");
  139. tft.setCursor(12, tft.height()/2 + 12);
  140. tft.setTextColor(ST7735_YELLOW);
  141. tft.print(filename);
  142. Serial.print("File not found");
  143. return;
  144. }
  145. // Parse BMP header
  146. if(read16(bmpFile) == 0x4D42) { // BMP signature
  147. Serial.print("File size: "); Serial.println(read32(bmpFile));
  148. (void)read32(bmpFile); // Read & ignore creator bytes
  149. bmpImageoffset = read32(bmpFile); // Start of image data
  150. Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
  151. // Read DIB header
  152. Serial.print("Header size: "); Serial.println(read32(bmpFile));
  153. bmpWidth = read32(bmpFile);
  154. bmpHeight = read32(bmpFile);
  155. if(read16(bmpFile) == 1) { // # planes -- must be '1'
  156. bmpDepth = read16(bmpFile); // bits per pixel
  157. Serial.print("Bit Depth: "); Serial.println(bmpDepth);
  158. if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  159. goodBmp = true; // Supported BMP format -- proceed!
  160. Serial.print("Image size: ");
  161. Serial.print(bmpWidth);
  162. Serial.print('x');
  163. Serial.println(bmpHeight);
  164. // BMP rows are padded (if needed) to 4-byte boundary
  165. rowSize = (bmpWidth * 3 + 3) & ~3;
  166. // If bmpHeight is negative, image is in top-down order.
  167. // This is not canon but has been observed in the wild.
  168. if(bmpHeight < 0) {
  169. bmpHeight = -bmpHeight;
  170. flip = false;
  171. }
  172. // Crop area to be loaded
  173. w = bmpWidth;
  174. h = bmpHeight;
  175. if((x+w-1) >= tft.width()) w = tft.width() - x;
  176. if((y+h-1) >= tft.height()) h = tft.height() - y;
  177. // Set TFT address window to clipped image bounds
  178. tft.setAddrWindow(x, y, x+w-1, y+h-1);
  179. for (row=0; row<h; row++) { // For each scanline...
  180. // Seek to start of scan line. It might seem labor-
  181. // intensive to be doing this on every line, but this
  182. // method covers a lot of gritty details like cropping
  183. // and scanline padding. Also, the seek only takes
  184. // place if the file position actually needs to change
  185. // (avoids a lot of cluster math in SD library).
  186. if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  187. pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  188. else // Bitmap is stored top-to-bottom
  189. pos = bmpImageoffset + row * rowSize;
  190. if(bmpFile.position() != pos) { // Need seek?
  191. bmpFile.seek(pos);
  192. buffidx = sizeof(sdbuffer); // Force buffer reload
  193. }
  194. for (col=0; col<w; col++) { // For each pixel...
  195. // Time to read more pixel data?
  196. if (buffidx >= sizeof(sdbuffer)) { // Indeed
  197. bmpFile.read(sdbuffer, sizeof(sdbuffer));
  198. buffidx = 0; // Set index to beginning
  199. }
  200. // Convert pixel from BMP to TFT format, push to display
  201. b = sdbuffer[buffidx++];
  202. g = sdbuffer[buffidx++];
  203. r = sdbuffer[buffidx++];
  204. tft.pushColor(tft.Color565(r,g,b));
  205. } // end pixel
  206. } // end scanline
  207. Serial.print("Loaded in ");
  208. Serial.print(millis() - startTime);
  209. Serial.println(" ms");
  210. } // end goodBmp
  211. }
  212. }
  213. bmpFile.close();
  214. if(!goodBmp) Serial.println("BMP format not recognized.");
  215. }
  216. // These read 16- and 32-bit types from the SD card file.
  217. // BMP data is stored little-endian, Arduino is little-endian too.
  218. // May need to reverse subscript order if porting elsewhere.
  219. uint16_t read16(File f) {
  220. uint16_t result;
  221. ((uint8_t *)&result)[0] = f.read(); // LSB
  222. ((uint8_t *)&result)[1] = f.read(); // MSB
  223. return result;
  224. }
  225. uint32_t read32(File f) {
  226. uint32_t result;
  227. ((uint8_t *)&result)[0] = f.read(); // LSB
  228. ((uint8_t *)&result)[1] = f.read();
  229. ((uint8_t *)&result)[2] = f.read();
  230. ((uint8_t *)&result)[3] = f.read(); // MSB
  231. return result;
  232. }