Browse Source

draw some experimental ui buttons

Thomas Buck 5 months ago
parent
commit
d27e72e4b2
3 changed files with 61 additions and 43 deletions
  1. 1
    0
      include/ui.h
  2. 9
    4
      src/main.cpp
  3. 51
    39
      src/ui.cpp

+ 1
- 0
include/ui.h View File

15
 #define __UI_H__
15
 #define __UI_H__
16
 
16
 
17
 void ui_init(void);
17
 void ui_init(void);
18
+void ui_draw_menu(void);
18
 void ui_run(void);
19
 void ui_run(void);
19
 
20
 
20
 #endif // __UI_H__
21
 #endif // __UI_H__

+ 9
- 4
src/main.cpp View File

54
 
54
 
55
 void setup() {
55
 void setup() {
56
     pinMode(BUILTIN_LED_PIN, OUTPUT);
56
     pinMode(BUILTIN_LED_PIN, OUTPUT);
57
-    
57
+
58
     Serial.begin(115200);
58
     Serial.begin(115200);
59
 
59
 
60
     debug.println(F("Initializing..."));
60
     debug.println(F("Initializing..."));
110
 
110
 
111
     // Set hostname workaround
111
     // Set hostname workaround
112
     WiFi.hostname(hostname);
112
     WiFi.hostname(hostname);
113
-    
113
+
114
 #elif defined(ARDUINO_ARCH_ESP32)
114
 #elif defined(ARDUINO_ARCH_ESP32)
115
 
115
 
116
     // Set hostname workaround
116
     // Set hostname workaround
117
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
117
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
118
     WiFi.setHostname(hostname.c_str());
118
     WiFi.setHostname(hostname.c_str());
119
-    
119
+
120
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
120
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
121
         /*
121
         /*
122
          * was initially: workaround for WiFi connecting only every 2nd reset
122
          * was initially: workaround for WiFi connecting only every 2nd reset
146
         debug.print(F("."));
146
         debug.print(F("."));
147
     }
147
     }
148
     debug.println(F("\nWiFi connected!"));
148
     debug.println(F("\nWiFi connected!"));
149
-    
149
+
150
     // Set hostname workaround
150
     // Set hostname workaround
151
     WiFi.setHostname(hostname.c_str());
151
     WiFi.setHostname(hostname.c_str());
152
 
152
 
180
     initServers(hostname);
180
     initServers(hostname);
181
 
181
 
182
     debug.println(F("Ready! Starting..."));
182
     debug.println(F("Ready! Starting..."));
183
+
184
+#ifdef FEATURE_UI
185
+    debug.println(F("UI Go"));
186
+    ui_draw_menu();
187
+#endif // FEATURE_UI
183
 }
188
 }
184
 
189
 
185
 void loop() {
190
 void loop() {

+ 51
- 39
src/ui.cpp View File

42
 #define TOUCH_TOP 230
42
 #define TOUCH_TOP 230
43
 #define TOUCH_BOTTOM 3800
43
 #define TOUCH_BOTTOM 3800
44
 
44
 
45
-TS_Point touchToScreen(TS_Point p) {
45
+static SPIClass mySpi = SPIClass(HSPI);
46
+static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
47
+
48
+static TFT_eSPI tft = TFT_eSPI();
49
+
50
+static TS_Point touchToScreen(TS_Point p) {
46
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
51
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
47
     p.y = map(p.y, TOUCH_TOP, TOUCH_BOTTOM, 0, LCD_HEIGHT);
52
     p.y = map(p.y, TOUCH_TOP, TOUCH_BOTTOM, 0, LCD_HEIGHT);
48
     return p;
53
     return p;
49
 }
54
 }
50
 
55
 
51
-void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
56
+static void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
52
     uint32_t duty = (4095 / valueMax) * min(value, valueMax);
57
     uint32_t duty = (4095 / valueMax) * min(value, valueMax);
53
     ledcWrite(channel, duty);
58
     ledcWrite(channel, duty);
54
 }
59
 }
55
 
60
 
56
-SPIClass mySpi = SPIClass(HSPI);
57
-XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
61
+#define BTN_W 120
62
+#define BTN_H 60
63
+#define BTN_GAP 20
64
+
65
+#define BTNS_OFF_X ((LCD_WIDTH - (2 * BTN_W) - (1 * BTN_GAP)) / 2)
66
+#define BTNS_OFF_Y ((LCD_HEIGHT - (3 * BTN_H) - (2 * BTN_GAP)) / 2)
67
+
68
+static void draw_button(const char *name, uint32_t x, uint32_t y, bool state) {
69
+    tft.fillRect(x - BTN_W / 2, y - BTN_H / 2, BTN_W, BTN_H, state ? TFT_GREEN : TFT_RED);
70
+
71
+    tft.setTextDatum(MC_DATUM); // middle center
72
+    tft.drawString(name, x, y, 2);
73
+}
74
+
75
+static bool lc, lw, lk, sa, lt, lp;
76
+
77
+static void draw_livingroom(void) {
78
+    draw_button("Lights Corner", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2, lc);
79
+    draw_button("Lights Workspace", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, lw);
80
+    draw_button("Lights Kitchen", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, lk);
58
 
81
 
59
-TFT_eSPI tft = TFT_eSPI();
82
+    draw_button("Sound Amp.", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2, sa);
83
+    draw_button("Lights TV", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, lt);
84
+    draw_button("Lights PC", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, lp);
85
+}
60
 
86
 
61
 void ui_init(void) {
87
 void ui_init(void) {
62
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
88
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
72
 
98
 
73
     tft.fillScreen(TFT_BLACK);
99
     tft.fillScreen(TFT_BLACK);
74
 
100
 
75
-    int x = LCD_WIDTH / 2;
76
-    int y = LCD_HEIGHT / 2;
77
-    int fontSize = 2;
78
-    tft.drawCentreString("Initializing ESP-ENV", x, y - 16, fontSize);
79
-    tft.drawCentreString("xythobuz.de", x, y + 16, fontSize);
101
+    tft.setTextDatum(MC_DATUM); // middle center
102
+    tft.drawString("Initializing ESP-ENV", LCD_WIDTH / 2, LCD_HEIGHT / 2 - 16, 2);
103
+    tft.drawString("xythobuz.de", LCD_WIDTH / 2, LCD_HEIGHT / 2 + 16, 2);
80
 }
104
 }
81
 
105
 
82
-void printTouchToDisplay(TS_Point p) {
106
+void ui_draw_menu(void) {
83
     tft.fillScreen(TFT_BLACK);
107
     tft.fillScreen(TFT_BLACK);
84
-
85
-    tft.fillRect(0, 0, 100, LCD_HEIGHT, TFT_RED);
86
-    tft.fillRect(LCD_WIDTH - 100, 0, 100, LCD_HEIGHT, TFT_GREEN);
87
-
88
-    tft.setTextColor(TFT_WHITE, TFT_BLACK);
89
-
90
-    int x = LCD_WIDTH / 2;
91
-    int y = 100;
92
-    int fontSize = 2;
93
-
94
-    String temp = "Pressure = " + String(p.z);
95
-    tft.drawCentreString(temp, x, y, fontSize);
96
-
97
-    y += 16;
98
-    temp = "X = " + String(p.x);
99
-    tft.drawCentreString(temp, x, y, fontSize);
100
-
101
-    y += 16;
102
-    temp = "Y = " + String(p.y);
103
-    tft.drawCentreString(temp, x, y, fontSize);
108
+    draw_livingroom();
104
 }
109
 }
105
 
110
 
106
 void ui_run(void) {
111
 void ui_run(void) {
107
     if (ts.tirqTouched() && ts.touched()) {
112
     if (ts.tirqTouched() && ts.touched()) {
108
         TS_Point p = touchToScreen(ts.getPoint());
113
         TS_Point p = touchToScreen(ts.getPoint());
109
-        printTouchToDisplay(p);
110
-
111
-        if (p.x < 100) {
112
-            writeMQTTtopic("livingroom/light_kitchen", "off");
113
-            tft.drawCentreString("Off", LCD_WIDTH / 2, 100 + 4 * 16, 2);
114
-        } else if (p.x > (LCD_WIDTH - 100)) {
115
-            writeMQTTtopic("livingroom/light_kitchen", "on");
116
-            tft.drawCentreString("On", LCD_WIDTH / 2, 100 + 4 * 16, 2);
114
+
115
+        if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= BTNS_OFF_Y) && (p.y <= BTNS_OFF_Y + BTN_H)) {
116
+            lc = !lc;
117
+        } else if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H + BTN_GAP)) && (p.y <= (BTNS_OFF_Y + BTN_H + BTN_GAP + BTN_H))) {
118
+            lw = !lw;
119
+        } else if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2)) && (p.y <= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2 + BTN_H))) {
120
+            lk = !lk;
121
+        } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= BTNS_OFF_Y) && (p.y <= BTNS_OFF_Y + BTN_H)) {
122
+            sa = !sa;
123
+        } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H + BTN_GAP)) && (p.y <= (BTNS_OFF_Y + BTN_H + BTN_GAP + BTN_H))) {
124
+            lt = !lt;
125
+        } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2)) && (p.y <= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2 + BTN_H))) {
126
+            lp = !lp;
117
         }
127
         }
118
 
128
 
129
+        // TODO
130
+        draw_livingroom();
119
         delay(100);
131
         delay(100);
120
     }
132
     }
121
 }
133
 }

Loading…
Cancel
Save