IoT - NOTHaSP

From The TinkerNet Wiki
Jump to navigation Jump to search

And so it begins...

The display in use

2021-04-28 15.52.58.jpg

2021-04-28 15.53.14.jpg

So far, just the buttons, but MQTT is coming.

The code as it stands

  1 #include <Arduino.h>
  2 #include <SPI.h>
  3 
  4 #include <Adafruit_ILI9341esp.h>
  5 #include <Adafruit_GFX.h>
  6 #include <XPT2046.h>
  7 
  8 void BUTTONHandler(int By, int Bx);
  9 
 10 // Modify the following two lines to match your hardware
 11 // Also, update calibration parameters below, as necessary
 12 
 13 // For the esp shield, these are the default.
 14 #define TFT_DC 2
 15 #define TFT_CS 15
 16 
 17 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
 18 XPT2046 touch(/*cs=*/4, /*irq=*/5);
 19 
 20 // 8 rows of 3 buttons...
 21 // (on a portrait mode screen)
 22 Adafruit_GFX_Button buttonS[8][3];
 23 
 24 void setup()
 25 {
 26   Serial.begin(115200);
 27   delay(500);
 28   Serial.println("Test FOUR!");
 29 
 30   SPI.setFrequency(ESP_SPI_FREQ);
 31 
 32   tft.begin();
 33   touch.begin(tft.width(), tft.height()); // Must be done before setting rotation
 34   Serial.print("tftx =");
 35   Serial.print(tft.width());
 36   Serial.print(" tfty =");
 37   Serial.println(tft.height());
 38   tft.fillScreen(ILI9341_BLACK);
 39 
 40   // Replace these for your screen module
 41   touch.setCalibration(1834, 256, 264, 1783);
 42 
 43   for (int Bx = 0; Bx < 3; Bx++)
 44     for (int By = 0; By < 8; By++)
 45     {
 46       buttonS[By][Bx].initButton(&tft,
 47                                  40 + (Bx * 80),      // X position
 48                                  20 + (By * 40),      // Y position
 49                                  80,                  // Width
 50                                  40,                  // Height
 51                                  ILI9341_DARKCYAN,    // Border Colour
 52                                  ILI9341_OLIVE,       // Background Colour
 53                                  ILI9341_GREENYELLOW, // Text Colour
 54                                  "Bork",              // Label
 55                                  2                    // Text Size
 56       );
 57 
 58       buttonS[By][Bx].drawButton();
 59     }
 60 }
 61 
 62 static uint16_t prev_x = 0xffff, prev_y = 0xffff;
 63 
 64 void loop()
 65 {
 66   uint16_t x, y;
 67   if (touch.isTouching())
 68   {
 69     touch.getPosition(x, y);
 70     //	Serial.print("x ="); Serial.print(x); Serial.print(" y ="); Serial.println(y);
 71 
 72     prev_x = x;
 73     prev_y = y;
 74   }
 75   else
 76   {
 77     prev_x = prev_y = 0xffff;
 78   }
 79 
 80   for (int Bx = 0; Bx < 3; Bx++)
 81     for (int By = 0; By < 8; By++)
 82     {
 83       buttonS[By][Bx].press(buttonS[By][Bx].contains(x, y)); // tell the button it is pressed
 84 
 85       BUTTONHandler(By, Bx);
 86     }
 87 
 88   delay(20);
 89 }
 90 
 91 void BUTTONHandler(int By, int Bx)
 92 {
 93   if (buttonS[By][Bx].justReleased())
 94   {
 95     buttonS[By][Bx].drawButton(); // draw normal
 96   }
 97 
 98   if (buttonS[By][Bx].justPressed())
 99   {
100     buttonS[By][Bx].drawButton(true); // draw invert!
101   }
102 }