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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  33. uint32_t duty = (4095 / valueMax) * min(value, valueMax);
  34. ledcWrite(channel, duty);
  35. }
  36. SPIClass mySpi = SPIClass(HSPI);
  37. XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
  38. TFT_eSPI tft = TFT_eSPI();
  39. void ui_init(void) {
  40. mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
  41. ts.begin(mySpi);
  42. ts.setRotation(1);
  43. tft.init();
  44. tft.setRotation(1);
  45. ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
  46. ledcAttachPin(TFT_BL, LEDC_CHANNEL_0);
  47. ledcAnalogWrite(LEDC_CHANNEL_0, 255);
  48. tft.fillScreen(TFT_BLACK);
  49. int x = LCD_WIDTH / 2;
  50. int y = LCD_HEIGHT / 2;
  51. int fontSize = 2;
  52. tft.drawCentreString("Initializing ESP-ENV", x, y - 16, fontSize);
  53. tft.drawCentreString("xythobuz.de", x, y + 16, fontSize);
  54. }
  55. void printTouchToDisplay(TS_Point p) {
  56. tft.fillScreen(TFT_BLACK);
  57. tft.setTextColor(TFT_WHITE, TFT_BLACK);
  58. int x = LCD_WIDTH / 2;
  59. int y = 100;
  60. int fontSize = 2;
  61. String temp = "Pressure = " + String(p.z);
  62. tft.drawCentreString(temp, x, y, fontSize);
  63. y += 16;
  64. temp = "X = " + String(p.x);
  65. tft.drawCentreString(temp, x, y, fontSize);
  66. y += 16;
  67. temp = "Y = " + String(p.y);
  68. tft.drawCentreString(temp, x, y, fontSize);
  69. }
  70. void ui_run(void) {
  71. if (ts.tirqTouched() && ts.touched()) {
  72. TS_Point p = ts.getPoint();
  73. printTouchToDisplay(p);
  74. if (p.x < 1000) {
  75. writeMQTTtopic("livingroom/light_kitchen", "off");
  76. } else if (p.x > 3000) {
  77. writeMQTTtopic("livingroom/light_kitchen", "on");
  78. }
  79. delay(1000);
  80. }
  81. }
  82. #endif // FEATURE_UI