TFT Shield test program (ST7735) Using 1.8" Adafruit TFT Shield with Joystick and microSD v1.0 Verified by WIZwiki-W7500 platform

Dependencies:   Adafruit_GFX Adafruit_ST7735 SDFileSystem mbed

Committer:
justinkim
Date:
Thu Jul 30 22:59:40 2015 +0000
Revision:
1:a2c6c864e335
Parent:
0:e3a8eb7dada7
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:e3a8eb7dada7 1 #include "mbed.h"
justinkim 0:e3a8eb7dada7 2 #include "Adafruit_ST7735.h"
justinkim 0:e3a8eb7dada7 3 #include "SDFileSystem.h"
justinkim 0:e3a8eb7dada7 4
justinkim 0:e3a8eb7dada7 5 #define BUTTON_NONE 0
justinkim 0:e3a8eb7dada7 6 #define BUTTON_DOWN 1
justinkim 0:e3a8eb7dada7 7 #define BUTTON_RIGHT 2
justinkim 0:e3a8eb7dada7 8 #define BUTTON_SELECT 3
justinkim 0:e3a8eb7dada7 9 #define BUTTON_UP 4
justinkim 0:e3a8eb7dada7 10 #define BUTTON_LEFT 5
justinkim 0:e3a8eb7dada7 11
justinkim 0:e3a8eb7dada7 12 #define BUFFPIXEL 20
justinkim 0:e3a8eb7dada7 13 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "SD"); // the pinout on the mbed
justinkim 0:e3a8eb7dada7 14 Adafruit_ST7735 tft(D11, D12, D13, D10, D8, D9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
justinkim 0:e3a8eb7dada7 15 AnalogIn joystick(A3);
justinkim 0:e3a8eb7dada7 16
justinkim 0:e3a8eb7dada7 17 uint8_t readButton(void);
justinkim 0:e3a8eb7dada7 18 void bmpDraw(char *filename, uint8_t x, uint8_t y);
justinkim 0:e3a8eb7dada7 19
justinkim 0:e3a8eb7dada7 20 void main(void)
justinkim 0:e3a8eb7dada7 21 {
justinkim 0:e3a8eb7dada7 22 uint8_t buttonhistory = 0;
justinkim 0:e3a8eb7dada7 23
justinkim 0:e3a8eb7dada7 24 // Initialize 1.8" TFT
justinkim 0:e3a8eb7dada7 25 tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
justinkim 0:e3a8eb7dada7 26
justinkim 0:e3a8eb7dada7 27 printf("OK!\r\n");
justinkim 0:e3a8eb7dada7 28 tft.fillScreen(ST7735_BLACK);
justinkim 0:e3a8eb7dada7 29
justinkim 0:e3a8eb7dada7 30 while(1)
justinkim 0:e3a8eb7dada7 31 {
justinkim 0:e3a8eb7dada7 32 uint8_t b = readButton();
justinkim 0:e3a8eb7dada7 33 tft.setTextSize(3);
justinkim 0:e3a8eb7dada7 34 if (b == BUTTON_DOWN) {
justinkim 0:e3a8eb7dada7 35 tft.setTextColor(ST7735_RED);
justinkim 0:e3a8eb7dada7 36 tft.setCursor(0, 10);
justinkim 0:e3a8eb7dada7 37 tft.printf("Down ");
justinkim 0:e3a8eb7dada7 38 buttonhistory |= 1;
justinkim 0:e3a8eb7dada7 39 }
justinkim 0:e3a8eb7dada7 40 if (b == BUTTON_LEFT) {
justinkim 0:e3a8eb7dada7 41 tft.setTextColor(ST7735_YELLOW);
justinkim 0:e3a8eb7dada7 42 tft.setCursor(0, 35);
justinkim 0:e3a8eb7dada7 43 tft.printf("Left ");
justinkim 0:e3a8eb7dada7 44 buttonhistory |= 2;
justinkim 0:e3a8eb7dada7 45 }
justinkim 0:e3a8eb7dada7 46 if (b == BUTTON_UP) {
justinkim 0:e3a8eb7dada7 47 tft.setTextColor(ST7735_GREEN);
justinkim 0:e3a8eb7dada7 48 tft.setCursor(0, 60);
justinkim 0:e3a8eb7dada7 49 tft.printf("Up");
justinkim 0:e3a8eb7dada7 50 buttonhistory |= 4;
justinkim 0:e3a8eb7dada7 51 }
justinkim 0:e3a8eb7dada7 52 if (b == BUTTON_RIGHT) {
justinkim 0:e3a8eb7dada7 53 tft.setTextColor(ST7735_BLUE);
justinkim 0:e3a8eb7dada7 54 tft.setCursor(0, 85);
justinkim 0:e3a8eb7dada7 55 tft.printf("Right");
justinkim 0:e3a8eb7dada7 56 buttonhistory |= 8;
justinkim 0:e3a8eb7dada7 57 }
justinkim 0:e3a8eb7dada7 58 if ((b == BUTTON_SELECT) && (buttonhistory == 0xF)) {
justinkim 0:e3a8eb7dada7 59 tft.setTextColor(ST7735_MAGENTA);
justinkim 0:e3a8eb7dada7 60 tft.setCursor(0, 110);
justinkim 0:e3a8eb7dada7 61 tft.printf("SELECT");
justinkim 0:e3a8eb7dada7 62 buttonhistory |= 8;
justinkim 0:e3a8eb7dada7 63 wait_ms(2000);
justinkim 0:e3a8eb7dada7 64 printf("Initializing SD card...\r\n");
justinkim 0:e3a8eb7dada7 65 // if(tft.BMP_16(0, 0, "/SD/parrot.bmp"))
justinkim 0:e3a8eb7dada7 66 printf("a\r\n");
justinkim 0:e3a8eb7dada7 67 tft.DrawBitmapFile("/SD/parrot.bmp");
justinkim 0:e3a8eb7dada7 68 // else
justinkim 0:e3a8eb7dada7 69 printf("b\r\n");
justinkim 0:e3a8eb7dada7 70 }
justinkim 0:e3a8eb7dada7 71 wait_ms(100);
justinkim 0:e3a8eb7dada7 72 }
justinkim 0:e3a8eb7dada7 73 }
justinkim 0:e3a8eb7dada7 74
justinkim 0:e3a8eb7dada7 75 uint8_t readButton(void) {
justinkim 0:e3a8eb7dada7 76 float a = joystick.read();
justinkim 0:e3a8eb7dada7 77
justinkim 0:e3a8eb7dada7 78 a *= 5.0;
justinkim 0:e3a8eb7dada7 79
justinkim 0:e3a8eb7dada7 80 printf("Button read analog = %f\r\n",a);
justinkim 0:e3a8eb7dada7 81 if (a < 0.2) return BUTTON_DOWN;
justinkim 0:e3a8eb7dada7 82 if (a < 1.0) return BUTTON_RIGHT;
justinkim 0:e3a8eb7dada7 83 if (a < 1.7) return BUTTON_SELECT;
justinkim 0:e3a8eb7dada7 84 if (a < 2.6) return BUTTON_UP;
justinkim 0:e3a8eb7dada7 85 if (a < 4.6) return BUTTON_LEFT;
justinkim 0:e3a8eb7dada7 86 else return BUTTON_NONE;
justinkim 0:e3a8eb7dada7 87 }