Difference between revisions of "IoT - NOTHaSP"

From The TinkerNet Wiki
Jump to navigation Jump to search
(Created page with "460x460px left|460x460px")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
And so it begins...
 +
 +
[[MCU - Display - TFT|The display in use]]
 +
 +
[[File:2021-04-28 15.52.58.jpg|left|460x460px]]
 
[[File:2021-04-28 15.53.14.jpg|460x460px]]
 
[[File:2021-04-28 15.53.14.jpg|460x460px]]
[[File:2021-04-28 15.52.58.jpg|left|460x460px]]
+
 
 +
So far, just the buttons, but MQTT is coming.
 +
 
 +
== The code as it stands ==
 +
<syntaxhighlight lang="c++" line="1">
 +
#include <Arduino.h>
 +
#include <SPI.h>
 +
 
 +
#include <Adafruit_ILI9341esp.h>
 +
#include <Adafruit_GFX.h>
 +
#include <XPT2046.h>
 +
 
 +
void BUTTONHandler(int By, int Bx);
 +
 
 +
// Modify the following two lines to match your hardware
 +
// Also, update calibration parameters below, as necessary
 +
 
 +
// For the esp shield, these are the default.
 +
#define TFT_DC 2
 +
#define TFT_CS 15
 +
 
 +
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
 +
XPT2046 touch(/*cs=*/4, /*irq=*/5);
 +
 
 +
// 8 rows of 3 buttons...
 +
// (on a portrait mode screen)
 +
Adafruit_GFX_Button buttonS[8][3];
 +
 
 +
void setup()
 +
{
 +
  Serial.begin(115200);
 +
  delay(500);
 +
  Serial.println("Test FOUR!");
 +
 
 +
  SPI.setFrequency(ESP_SPI_FREQ);
 +
 
 +
  tft.begin();
 +
  touch.begin(tft.width(), tft.height()); // Must be done before setting rotation
 +
  Serial.print("tftx =");
 +
  Serial.print(tft.width());
 +
  Serial.print(" tfty =");
 +
  Serial.println(tft.height());
 +
  tft.fillScreen(ILI9341_BLACK);
 +
 
 +
  // Replace these for your screen module
 +
  touch.setCalibration(1834, 256, 264, 1783);
 +
 
 +
  for (int Bx = 0; Bx < 3; Bx++)
 +
    for (int By = 0; By < 8; By++)
 +
    {
 +
      buttonS[By][Bx].initButton(&tft,
 +
                                40 + (Bx * 80),      // X position
 +
                                20 + (By * 40),      // Y position
 +
                                80,                  // Width
 +
                                40,                  // Height
 +
                                ILI9341_DARKCYAN,    // Border Colour
 +
                                ILI9341_OLIVE,      // Background Colour
 +
                                ILI9341_GREENYELLOW, // Text Colour
 +
                                "Bork",              // Label
 +
                                2                    // Text Size
 +
      );
 +
 
 +
      buttonS[By][Bx].drawButton();
 +
    }
 +
}
 +
 
 +
static uint16_t prev_x = 0xffff, prev_y = 0xffff;
 +
 
 +
void loop()
 +
{
 +
  uint16_t x, y;
 +
  if (touch.isTouching())
 +
  {
 +
    touch.getPosition(x, y);
 +
    // Serial.print("x ="); Serial.print(x); Serial.print(" y ="); Serial.println(y);
 +
 
 +
    prev_x = x;
 +
    prev_y = y;
 +
  }
 +
  else
 +
  {
 +
    prev_x = prev_y = 0xffff;
 +
  }
 +
 
 +
  for (int Bx = 0; Bx < 3; Bx++)
 +
    for (int By = 0; By < 8; By++)
 +
    {
 +
      buttonS[By][Bx].press(buttonS[By][Bx].contains(x, y)); // tell the button it is pressed
 +
 
 +
      BUTTONHandler(By, Bx);
 +
    }
 +
 
 +
  delay(20);
 +
}
 +
 
 +
void BUTTONHandler(int By, int Bx)
 +
{
 +
  if (buttonS[By][Bx].justReleased())
 +
  {
 +
    buttonS[By][Bx].drawButton(); // draw normal
 +
  }
 +
 
 +
  if (buttonS[By][Bx].justPressed())
 +
  {
 +
    buttonS[By][Bx].drawButton(true); // draw invert!
 +
  }
 +
}
 +
</syntaxhighlight>

Latest revision as of 23:13, 29 April 2021

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 }