/ PyPortal_Titano_ArduinoSelfTest / PyPortal_Titano_ArduinoSelfTest.ino
PyPortal_Titano_ArduinoSelfTest.ino
1 // SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // This program does a test of all the PyPortal Titano hardware 6 // so you can get an example of how to read 7 // sensors, touchscreen, and display stuff! 8 9 #include "SPI.h" 10 #include "Adafruit_GFX.h" 11 #include "Adafruit_HX8357.h" 12 #include <Adafruit_SPIFlash.h> 13 #include "TouchScreen.h" 14 #include <SdFat.h> 15 #include <WiFiNINA.h> 16 #include "click.h" 17 18 #define RED_LED 13 19 #define TFT_RESET 24 20 #define TFT_BACKLIGHT 25 21 #define LIGHT_SENSOR A2 22 #define SD_CS 32 23 #define SPKR_SHUTDOWN 50 24 25 #define TFT_D0 34 // Data bit 0 pin (MUST be on PORT byte boundary) 26 #define TFT_WR 26 // Write-strobe pin (CCL-inverted timer output) 27 #define TFT_DC 10 // Data/command pin 28 #define TFT_CS 11 // Chip-select pin 29 #define TFT_RST 24 // Reset pin 30 #define TFT_RD 9 // Read-strobe pin 31 #define TFT_BACKLIGHT 25 32 33 // PyPortal Titano 34 Adafruit_HX8357 tft = Adafruit_HX8357(tft8bitbus, TFT_D0, TFT_WR, TFT_DC, TFT_CS, TFT_RST, TFT_RD); 35 36 Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3); 37 Adafruit_SPIFlash flash(&flashTransport); 38 39 #define YP A4 // must be an analog pin, use "An" notation! 40 #define XM A7 // must be an analog pin, use "An" notation! 41 #define YM A6 // can be a digital pin 42 #define XP A5 // can be a digital pin 43 TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 44 #define X_MIN 760 45 #define X_MAX 320 46 #define Y_MIN 875 47 #define Y_MAX 193 48 49 Adafruit_GFX_Button soundBtn = Adafruit_GFX_Button(); 50 SdFat SD; 51 52 char text[25]=" "; 53 54 void setup() { 55 Serial.begin(115200); 56 //while (!Serial); 57 58 Serial.println("All Test!"); 59 60 pinMode(RED_LED, OUTPUT); 61 pinMode(TFT_BACKLIGHT, OUTPUT); 62 digitalWrite(TFT_BACKLIGHT, HIGH); 63 64 pinMode(TFT_RESET, OUTPUT); 65 digitalWrite(TFT_RESET, HIGH); 66 delay(10); 67 digitalWrite(TFT_RESET, LOW); 68 delay(10); 69 digitalWrite(TFT_RESET, HIGH); 70 delay(10); 71 72 tft.begin(); 73 74 tft.fillScreen(HX8357_BLACK); 75 tft.setTextSize(2); 76 tft.setTextColor(HX8357_GREEN); 77 tft.setTextWrap(true); 78 tft.setCursor(0, 0); 79 80 tft.print("QSPI Flash..."); 81 if (!flash.begin()){ 82 Serial.println("Could not find flash on QSPI bus!"); 83 tft.setTextColor(HX8357_RED); 84 tft.println("FAILED"); 85 while (1); 86 } 87 Serial.println("Reading QSPI ID"); 88 Serial.print("JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX); 89 tft.setTextColor(HX8357_GREEN); 90 tft.print("QSPI Flash JEDEC 0x"); tft.println(flash.getJEDECID(), HEX); 91 92 /*************** SD CARD */ 93 tft.setCursor(0, 48); 94 tft.print("SD Card..."); 95 if (!SD.begin(SD_CS)) { 96 Serial.println("Card init. failed!"); 97 tft.setTextColor(HX8357_RED); 98 tft.println("FAILED"); 99 tft.setTextColor(HX8357_GREEN); 100 } else { 101 tft.println("OK!"); 102 } 103 104 /*************** WiFi Module */ 105 106 tft.setCursor(0, 64); 107 tft.print("WiFi Module..."); 108 WiFi.status(); 109 delay(100); 110 if (WiFi.status() == WL_NO_MODULE) { 111 Serial.println("ESP32 SPI not found"); 112 tft.setTextColor(HX8357_RED); 113 tft.println("FAILED"); 114 tft.setTextColor(HX8357_GREEN); 115 } else { 116 Serial.println("ESP32 SPI mode found"); 117 tft.println("OK!"); 118 } 119 120 121 soundBtn.initButton(&tft, 150, 280, 150, 50, HX8357_WHITE, HX8357_YELLOW, HX8357_BLACK, "Sound", 2); 122 soundBtn.drawButton(); 123 124 analogWriteResolution(12); 125 analogWrite(A0, 128); 126 pinMode(SPKR_SHUTDOWN, OUTPUT); 127 digitalWrite(SPKR_SHUTDOWN, LOW); 128 } 129 130 void loop() { 131 digitalWrite(RED_LED, HIGH); 132 tft.setTextColor(HX8357_WHITE); 133 // read light sensor 134 tft.fillRect(160, 100, 240, 16, HX8357_BLACK); 135 tft.setCursor(0, 100); 136 uint16_t light = analogRead(LIGHT_SENSOR); 137 Serial.print("light sensor: "); Serial.println(light); 138 tft.print("Light sensor: "); tft.println(light); 139 140 // externals 141 tft.fillRect(40, 130, 60, 34, HX8357_BLUE); 142 tft.setCursor(0, 132); 143 float d3 = (float)analogRead(A1) * 3.3 / 1024; 144 float d4 = (float)analogRead(A3) * 3.3 / 1024; 145 Serial.print("STEMMA: "); 146 Serial.print(d3,1); Serial.print(", "); 147 Serial.print(d4,1); Serial.println(); 148 tft.print("D3: "); tft.println(d3,1); 149 tft.print("D4: "); tft.println(d4,1); 150 151 tft.fillRect(80, 164, 240, 16, HX8357_BLACK); 152 tft.setCursor(0, 164); 153 tft.print("Touch: "); 154 155 TSPoint p = ts.getPoint(); 156 // we have some minimum pressure we consider 'valid' 157 // pressure of 0 means no pressing! 158 if (p.z > (ts.pressureThreshhold +200)) { 159 Serial.print("X = "); Serial.print(p.x); 160 Serial.print("\tY = "); Serial.print(p.y); 161 Serial.print("\tPressure = "); Serial.println(p.z); 162 163 // map touchscreen coordinates to screen coordinates 164 int16_t x = map(p.x, X_MIN, X_MAX, 320, 0); // inverted X compared to screen 165 int16_t y = map(p.y, Y_MIN, Y_MAX, 0, 480); 166 // raw position 167 //int16_t tx = p.x; 168 //int16_t ty = p.y; 169 //tft.print("("); tft.print(tx); tft.print(", "); tft.print(ty); tft.println(")"); 170 tft.fillRect(10, 180, 240, 16, HX8357_BLACK); 171 tft.print("X:"); 172 tft.print(itoa(x,text,10)); tft.print(" Y:"); 173 tft.print(itoa(y,text,10)); tft.print(" Z:"); 174 tft.print(itoa(p.z,text,10)); 175 //tft.println(")"); 176 177 if (soundBtn.contains(x, y)) { 178 Serial.println("Ding!"); 179 soundBtn.press(true); 180 } else { 181 soundBtn.press(false); 182 } 183 } else { 184 soundBtn.press(false); 185 } 186 if (soundBtn.justPressed()) { 187 soundBtn.drawButton(true); 188 digitalWrite(SPKR_SHUTDOWN, HIGH); 189 190 uint32_t i, prior, usec = 1000000L / SAMPLE_RATE; 191 prior = micros(); 192 for (uint32_t i=0; i<sizeof(clickaudio); i++) { 193 uint32_t t; 194 while((t = micros()) - prior < usec); 195 analogWrite(A0, (uint16_t)clickaudio[i]); 196 prior = t; 197 } 198 digitalWrite(SPKR_SHUTDOWN, LOW); 199 } 200 if (soundBtn.justReleased()) { 201 soundBtn.drawButton(false); 202 } 203 digitalWrite(RED_LED, LOW); 204 delay(20); 205 }