selftest.ino
  1  // SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <Adafruit_DotStar.h>
  6  #include <Adafruit_GFX.h>    // Core graphics library
  7  #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  8  #include <Adafruit_DPS310.h>
  9  #include <Adafruit_AHTX0.h>
 10  
 11  #define NUM_DOTSTAR 5
 12  #define BG_COLOR ST77XX_BLACK
 13  #define ST77XX_GREY 0x8410   // Colors are in RGB565 format
 14  
 15  // display!
 16  Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RESET);
 17  // LEDs!
 18  Adafruit_DotStar pixels(NUM_DOTSTAR, PIN_DOTSTAR_DATA, PIN_DOTSTAR_CLOCK, DOTSTAR_BRG);
 19  // sensors!
 20  Adafruit_DPS310 dps;
 21  Adafruit_AHTX0 aht;
 22  
 23  uint8_t LED_dutycycle = 0;
 24  uint16_t firstPixelHue = 0;
 25  
 26  void setup() {
 27    //while (!Serial);
 28    Serial.begin(115200);
 29    delay(100);
 30    
 31    pixels.begin(); // Initialize pins for output
 32    pixels.show();  // Turn all LEDs off ASAP
 33    pixels.setBrightness(20);
 34  
 35    pinMode(BUTTON_DOWN, INPUT_PULLDOWN);
 36    pinMode(BUTTON_SELECT, INPUT_PULLDOWN);
 37    pinMode(BUTTON_UP, INPUT_PULLDOWN);
 38  
 39    //analogReadResolution(13);
 40    
 41    tft.init(240, 240);                // Initialize ST7789 screen
 42    pinMode(TFT_BACKLIGHT, OUTPUT);
 43    digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
 44  
 45    tft.fillScreen(BG_COLOR);
 46    tft.setTextSize(2);
 47    tft.setTextColor(ST77XX_YELLOW);
 48    tft.setTextWrap(false);
 49  
 50    // check DPS!
 51    tft.setCursor(0, 0);
 52    tft.setTextColor(ST77XX_YELLOW);
 53    tft.print("DP310? ");
 54  
 55    
 56    if (! dps.begin_I2C()) {  
 57      tft.setTextColor(ST77XX_RED);
 58      tft.println("FAIL!");
 59      while (1) delay(100);
 60    }
 61    tft.setTextColor(ST77XX_GREEN);
 62    tft.println("OK!");
 63    dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
 64    dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
 65  
 66    // check AHT!
 67    tft.setCursor(0, 20);
 68    tft.setTextColor(ST77XX_YELLOW);
 69    tft.print("AHT20? ");
 70    
 71    if (! aht.begin()) {  
 72      tft.setTextColor(ST77XX_RED);
 73      tft.println("FAIL!");
 74      while (1) delay(100);
 75    }
 76    tft.setTextColor(ST77XX_GREEN);
 77    tft.println("OK!");
 78  
 79    pinMode(LED_BUILTIN, OUTPUT);
 80    pinMode(SPEAKER, OUTPUT);
 81  
 82    ledcSetup(0, 2000, 8);
 83    ledcAttachPin(LED_BUILTIN, 0);
 84  
 85    ledcSetup(1, 2000, 8);
 86    ledcAttachPin(SPEAKER, 1);
 87    ledcWrite(1, 0);
 88  }
 89  
 90  
 91  
 92  void loop() {
 93  
 94  
 95    /********************* sensors    */
 96    sensors_event_t humidity, temp, pressure;
 97    
 98    tft.setCursor(0, 0);
 99    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
100    dps.getEvents(&temp, &pressure);
101    
102    tft.print("DP310: ");
103    tft.print(temp.temperature, 0);
104    tft.print(" C ");
105    tft.print(pressure.pressure, 0);
106    tft.print(" hPa");
107    tft.println("              ");
108    Serial.printf("DPS310: %0.1f *C  %0.2f hPa\n", temp.temperature, pressure.pressure);
109  
110  
111    tft.setCursor(0, 20);
112    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
113    aht.getEvent(&humidity, &temp);
114  
115    tft.print("AHT20: ");
116    tft.print(temp.temperature, 0);
117    tft.print(" C ");
118    tft.print(humidity.relative_humidity, 0);
119    tft.print(" %");
120    tft.println("              ");
121    Serial.printf("AHT20: %0.1f *C  %0.2f rH\n", temp.temperature, humidity.relative_humidity);
122  
123  
124    /****************** BUTTONS */
125    tft.setCursor(0, 40);
126    tft.setTextColor(ST77XX_YELLOW);
127    tft.print("Buttons: ");
128    if (! digitalRead(BUTTON_DOWN)) {  
129      tft.setTextColor(ST77XX_GREY);
130    } else {
131      Serial.println("DOWN pressed");
132      tft.setTextColor(ST77XX_WHITE);
133    }
134    tft.print("DOWN ");
135  
136    if (! digitalRead(BUTTON_SELECT)) {  
137      tft.setTextColor(ST77XX_GREY);
138    } else {
139      Serial.println("SELECT pressed");
140      tft.setTextColor(ST77XX_WHITE);
141    }
142    tft.print("SEL ");
143    
144    if (! digitalRead(BUTTON_UP)) {  
145      tft.setTextColor(ST77XX_GREY);
146    } else {
147      Serial.println("UP pressed");
148      tft.setTextColor(ST77XX_WHITE);
149    }
150    tft.println("UP");
151  
152    /************************** CAPACITIVE */
153    uint16_t touchread;
154    
155    tft.setCursor(0, 60);
156    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
157    tft.print("Captouch 6: ");
158    touchread = touchRead(6);
159    if (touchread < 10000 ) {  
160      tft.setTextColor(ST77XX_GREY, BG_COLOR);
161    } else {
162      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
163    }
164    tft.print(touchread);
165    tft.println("          ");
166    Serial.printf("Captouch #6 reading: %d\n", touchread);
167    
168    tft.setCursor(0, 80);
169    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
170    tft.print("Captouch 7: ");
171    touchread = touchRead(7);
172    if (touchread < 20000 ) {  
173      tft.setTextColor(ST77XX_GREY, BG_COLOR);
174    } else {
175      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
176    }
177    tft.print(touchread);
178    tft.println("          ");
179    Serial.printf("Captouch #7 reading: %d\n", touchread);
180  
181  
182    tft.setCursor(0, 100);
183    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
184    tft.print("Captouch 8: ");
185    touchread = touchRead(8);
186    if (touchread < 20000 ) {  
187      tft.setTextColor(ST77XX_GREY, BG_COLOR);
188    } else {
189      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
190    }
191    tft.print(touchread);
192    tft.println("          ");
193    Serial.printf("Captouch #8 reading: %d\n", touchread);
194  
195  
196    /************************** ANALOG READ */
197    uint16_t analogread;
198  
199    tft.setCursor(0, 120);
200    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
201    tft.print("Analog 0: ");
202    analogread = analogRead(A0);
203    if (analogread < 8000 ) {  
204      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
205    } else {
206      tft.setTextColor(ST77XX_RED, BG_COLOR);
207    }
208    tft.print(analogread);
209    tft.println("    ");
210    Serial.printf("Analog A0 reading: %d\n", analogread);
211  
212  
213    tft.setCursor(0, 140);
214    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
215    tft.print("Analog 1: ");
216    analogread = analogRead(A1);
217    if (analogread < 8000 ) {  
218      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
219    } else {
220      tft.setTextColor(ST77XX_RED, BG_COLOR);
221    }
222    tft.print(analogread);
223    tft.println("    ");
224    Serial.printf("Analog A1 reading: %d\n", analogread);
225  
226    
227    tft.setCursor(0, 160);
228    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
229    tft.print("Analog 2: ");
230    analogread = analogRead(A2);
231    if (analogread < 8000 ) {  
232      tft.setTextColor(ST77XX_WHITE, BG_COLOR);
233    } else {
234      tft.setTextColor(ST77XX_RED, BG_COLOR);
235    }
236    tft.print(analogread);
237    tft.println("    ");
238    Serial.printf("Analog A2 reading: %d\n", analogread);
239  
240    tft.setCursor(0, 180);
241    tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
242    tft.print("Light: ");
243    analogread = analogRead(A3);
244    tft.setTextColor(ST77XX_WHITE, BG_COLOR);
245    tft.print(analogread);
246    tft.println("    ");
247    Serial.printf("Light sensor reading: %d\n", analogread);
248    
249    /************************** Beep! */
250    if (digitalRead(BUTTON_SELECT)) {  
251       Serial.println("** Beep! ***");
252       fhtone(SPEAKER, 988.0, 100.0);  // tone1 - B5
253       fhtone(SPEAKER, 1319.0, 200.0); // tone2 - E6
254       delay(100);
255       //fhtone(SPEAKER, 2000.0, 100.0);
256    }
257    
258    /************************** LEDs */
259    // pulse red LED
260    ledcWrite(0, LED_dutycycle);
261    LED_dutycycle += 32;
262    
263    // rainbow dotstars
264    for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
265        int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
266        pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
267    }
268    pixels.show(); // Update strip with new contents
269    firstPixelHue += 256;
270  }
271  
272  
273  void fhtone(uint8_t pin, float frequency, float duration) {
274    ledcSetup(1, frequency, 8);
275    ledcAttachPin(pin, 1);
276    ledcWrite(1, 128);
277    delay(duration);
278    ledcWrite(1, 0);
279  }