/ PyPortal_ArduinoSelfTest / PyPortal_ArduinoSelfTest.ino
PyPortal_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 hardware so you can get an example of how to read
  6  // sensors, touchscreen, and display stuff!
  7  
  8  #include "SPI.h"
  9  #include "Adafruit_GFX.h"
 10  #include "Adafruit_ILI9341.h"
 11  #include <Adafruit_SPIFlash.h>
 12  #include "Adafruit_ADT7410.h"
 13  #include "TouchScreen.h"
 14  #include <SdFat.h>
 15  #include <WiFiNINA.h>
 16  #include "coin.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  // ILI9341 with 8-bit parallel interface:
 33  Adafruit_ILI9341 tft = Adafruit_ILI9341(tft8bitbus, TFT_D0, TFT_WR, TFT_DC, TFT_CS, TFT_RST, TFT_RD);
 34  
 35  Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
 36  Adafruit_SPIFlash flash(&flashTransport);
 37  
 38  Adafruit_ADT7410 tempsensor = Adafruit_ADT7410();
 39  
 40  #define YP A4  // must be an analog pin, use "An" notation!
 41  #define XM A7  // must be an analog pin, use "An" notation!
 42  #define YM A6   // can be a digital pin
 43  #define XP A5   // can be a digital pin
 44  TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
 45  #define X_MIN  750
 46  #define X_MAX  325
 47  #define Y_MIN  840
 48  #define Y_MAX  240
 49  
 50  Adafruit_GFX_Button coin = Adafruit_GFX_Button();
 51  SdFat SD;
 52  
 53  void setup() {
 54    Serial.begin(115200);
 55    //while (!Serial);
 56  
 57    Serial.println("All Test!");
 58  
 59    pinMode(RED_LED, OUTPUT);
 60    pinMode(TFT_BACKLIGHT, OUTPUT);
 61    digitalWrite(TFT_BACKLIGHT, HIGH);
 62  
 63    pinMode(TFT_RESET, OUTPUT);
 64    digitalWrite(TFT_RESET, HIGH);
 65    delay(10);
 66    digitalWrite(TFT_RESET, LOW);
 67    delay(10);
 68    digitalWrite(TFT_RESET, HIGH);
 69    delay(10);
 70  
 71    tft.begin();
 72  
 73    tft.fillScreen(ILI9341_BLACK);
 74    tft.setTextSize(2);
 75    tft.setTextColor(ILI9341_GREEN);
 76    tft.setTextWrap(true);
 77    tft.setCursor(0, 0);
 78  
 79    tft.print("QSPI Flash...");
 80    if (!flash.begin()){
 81      Serial.println("Could not find flash on QSPI bus!");
 82      tft.setTextColor(ILI9341_RED);
 83      tft.println("FAILED");
 84      while (1);
 85    }
 86    Serial.println("Reading QSPI ID");
 87    Serial.print("JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);
 88    tft.setTextColor(ILI9341_GREEN);
 89    tft.print("QSPI Flash JEDEC 0x"); tft.println(flash.getJEDECID(), HEX);
 90  
 91    /*************** SD CARD */
 92    tft.setCursor(0, 48);
 93    tft.print("SD Card...");
 94    if (!SD.begin(SD_CS)) {
 95      Serial.println("Card init. failed!");
 96      tft.setTextColor(ILI9341_RED);
 97      tft.println("FAILED");
 98      tft.setTextColor(ILI9341_GREEN);
 99    } else {
100      tft.println("OK!");
101    }
102  
103    /*************** WiFi Module */
104      
105    tft.setCursor(0, 64);
106    tft.print("WiFi Module...");
107    WiFi.status();
108    delay(100);
109    if (WiFi.status() == WL_NO_MODULE) {
110      Serial.println("ESP32 SPI not found");
111      tft.setTextColor(ILI9341_RED);
112      tft.println("FAILED");
113      tft.setTextColor(ILI9341_GREEN);
114    } else {
115      Serial.println("ESP32 SPI mode found");
116      tft.println("OK!");
117    }
118  
119     /*************** Temperature sensor */
120     tft.setCursor(0, 80);
121     tft.print("ADT7410...");
122     if (!tempsensor.begin()) {
123      Serial.println("Couldn't find ADT7410!");
124      tft.setTextColor(ILI9341_RED);
125      tft.println("FAILED");
126      tft.setTextColor(ILI9341_GREEN);
127    } else {
128      Serial.println("ADT7410 found");
129      tft.println("OK!");
130    }
131   
132    coin.initButton(&tft, 120, 280, 100, 50, ILI9341_WHITE, ILI9341_YELLOW, ILI9341_BLACK, "Sound", 2);
133    coin.drawButton();
134  
135    analogWriteResolution(12);
136    analogWrite(A0, 128);
137    pinMode(SPKR_SHUTDOWN, OUTPUT);
138    digitalWrite(SPKR_SHUTDOWN, LOW);
139  }
140  
141  void loop() {
142    digitalWrite(RED_LED, HIGH);
143    tft.setTextColor(ILI9341_WHITE);
144    // read light sensor
145    tft.fillRect(160, 100, 240, 16, ILI9341_BLACK);
146    tft.setCursor(0, 100);
147    uint16_t light = analogRead(LIGHT_SENSOR);
148    Serial.print("light sensor: "); Serial.println(light);
149    tft.print("Light sensor: "); tft.println(light);
150  
151    // read temp sensor
152    tft.fillRect(150, 116, 240, 16, ILI9341_BLACK);
153    tft.setCursor(0, 116);
154    float temp = tempsensor.readTempC();
155    Serial.print("temp sensor: "); Serial.println(temp, 2);
156    tft.print("Temp sensor: "); tft.println(temp, 2);
157  
158    // externals
159    tft.fillRect(0, 132, 240, 32, ILI9341_BLACK);
160    tft.setCursor(0, 132);
161    float d3 = (float)analogRead(A1) * 3.3 / 1024;
162    float d4 = (float)analogRead(A3) * 3.3 / 1024;
163    Serial.print("STEMMA: "); 
164    Serial.print(d3,1); Serial.print(", ");
165    Serial.print(d4,1); Serial.println();
166    tft.print("D3: "); tft.println(d3,1);
167    tft.print("D4: "); tft.println(d4,1); 
168  
169    tft.fillRect(80, 164, 240, 16, ILI9341_BLACK);
170    tft.setCursor(0, 164);
171    tft.print("Touch: ");
172    
173    TSPoint p = ts.getPoint();
174    // we have some minimum pressure we consider 'valid'
175    // pressure of 0 means no pressing!
176    if (p.z > ts.pressureThreshhold) {
177       Serial.print("X = "); Serial.print(p.x);
178       Serial.print("\tY = "); Serial.print(p.y);
179       Serial.print("\tPressure = "); Serial.println(p.z);
180       int16_t x = map(p.x, X_MIN, X_MAX, 0, 240);
181       int16_t y = map(p.y, Y_MIN, Y_MAX, 0, 320);
182       tft.print("("); tft.print(x); tft.print(", "); tft.print(y); tft.println(")");
183      if (coin.contains(x, y)) {
184        Serial.println("Ding!");
185        coin.press(true);
186      } else {
187        coin.press(false);
188      }
189    } else {
190      coin.press(false);
191    }
192    if (coin.justPressed()) {
193      coin.drawButton(true);
194      digitalWrite(SPKR_SHUTDOWN, HIGH);
195  
196      uint32_t i, prior, usec = 1000000L / SAMPLE_RATE;
197      prior = micros();
198      for (uint32_t i=0; i<sizeof(coinaudio); i++) {
199        uint32_t t;
200        while((t = micros()) - prior < usec);
201        analogWrite(A0, (uint16_t)coinaudio[i]);
202        prior = t;
203      }
204      digitalWrite(SPKR_SHUTDOWN, LOW);
205    }
206    if (coin.justReleased()) {
207      coin.drawButton(false);
208    }
209    digitalWrite(RED_LED, LOW);
210    delay(20);
211  }