ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ui.cpp
  3. *
  4. * https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/2-TouchTest/2-TouchTest.ino
  5. * https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/4-BacklightControlTest/4-BacklightControlTest.ino
  6. *
  7. * ESP8266 / ESP32 Environmental Sensor
  8. *
  9. * ----------------------------------------------------------------------------
  10. * "THE BEER-WARE LICENSE" (Revision 42):
  11. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  12. * you can do whatever you want with this stuff. If we meet some day, and you
  13. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  14. * ----------------------------------------------------------------------------
  15. */
  16. #include <Arduino.h>
  17. #include "config.h"
  18. #include "mqtt.h"
  19. #include "ui.h"
  20. #ifdef FEATURE_UI
  21. #include <SPI.h>
  22. #include <XPT2046_Touchscreen.h>
  23. #include <TFT_eSPI.h>
  24. #define XPT2046_IRQ 36
  25. #define XPT2046_MOSI 32
  26. #define XPT2046_MISO 39
  27. #define XPT2046_CLK 25
  28. #define XPT2046_CS 33
  29. #define LEDC_CHANNEL_0 0
  30. #define LEDC_TIMER_12_BIT 12
  31. #define LEDC_BASE_FREQ 5000
  32. #define TOUCH_LEFT 180
  33. #define TOUCH_RIGHT 3750
  34. #define TOUCH_TOP 230
  35. #define TOUCH_BOTTOM 3800
  36. TS_Point touchToScreen(TS_Point p) {
  37. p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
  38. p.y = map(p.y, TOUCH_TOP, TOUCH_BOTTOM, 0, LCD_HEIGHT);
  39. return p;
  40. }
  41. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  42. uint32_t duty = (4095 / valueMax) * min(value, valueMax);
  43. ledcWrite(channel, duty);
  44. }
  45. SPIClass mySpi = SPIClass(HSPI);
  46. XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
  47. TFT_eSPI tft = TFT_eSPI();
  48. void ui_init(void) {
  49. mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
  50. ts.begin(mySpi);
  51. ts.setRotation(1);
  52. tft.init();
  53. tft.setRotation(1);
  54. ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
  55. ledcAttachPin(TFT_BL, LEDC_CHANNEL_0);
  56. ledcAnalogWrite(LEDC_CHANNEL_0, 255);
  57. tft.fillScreen(TFT_BLACK);
  58. int x = LCD_WIDTH / 2;
  59. int y = LCD_HEIGHT / 2;
  60. int fontSize = 2;
  61. tft.drawCentreString("Initializing ESP-ENV", x, y - 16, fontSize);
  62. tft.drawCentreString("xythobuz.de", x, y + 16, fontSize);
  63. }
  64. void printTouchToDisplay(TS_Point p) {
  65. tft.fillScreen(TFT_BLACK);
  66. tft.fillRect(0, 0, 100, LCD_HEIGHT, TFT_RED);
  67. tft.fillRect(LCD_WIDTH - 100, 0, 100, LCD_HEIGHT, TFT_GREEN);
  68. tft.setTextColor(TFT_WHITE, TFT_BLACK);
  69. int x = LCD_WIDTH / 2;
  70. int y = 100;
  71. int fontSize = 2;
  72. String temp = "Pressure = " + String(p.z);
  73. tft.drawCentreString(temp, x, y, fontSize);
  74. y += 16;
  75. temp = "X = " + String(p.x);
  76. tft.drawCentreString(temp, x, y, fontSize);
  77. y += 16;
  78. temp = "Y = " + String(p.y);
  79. tft.drawCentreString(temp, x, y, fontSize);
  80. }
  81. void ui_run(void) {
  82. if (ts.tirqTouched() && ts.touched()) {
  83. TS_Point p = touchToScreen(ts.getPoint());
  84. printTouchToDisplay(p);
  85. if (p.x < 100) {
  86. writeMQTTtopic("livingroom/light_kitchen", "off");
  87. tft.drawCentreString("Off", LCD_WIDTH / 2, 100 + 4 * 16, 2);
  88. } else if (p.x > (LCD_WIDTH - 100)) {
  89. writeMQTTtopic("livingroom/light_kitchen", "on");
  90. tft.drawCentreString("On", LCD_WIDTH / 2, 100 + 4 * 16, 2);
  91. }
  92. delay(100);
  93. }
  94. }
  95. #endif // FEATURE_UI