Browse Source

draw some experimental ui buttons

Thomas Buck 6 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,6 +15,7 @@
15 15
 #define __UI_H__
16 16
 
17 17
 void ui_init(void);
18
+void ui_draw_menu(void);
18 19
 void ui_run(void);
19 20
 
20 21
 #endif // __UI_H__

+ 9
- 4
src/main.cpp View File

@@ -54,7 +54,7 @@ void onDisconnected(const WiFiEventStationModeDisconnected& event) {
54 54
 
55 55
 void setup() {
56 56
     pinMode(BUILTIN_LED_PIN, OUTPUT);
57
-    
57
+
58 58
     Serial.begin(115200);
59 59
 
60 60
     debug.println(F("Initializing..."));
@@ -110,13 +110,13 @@ void setup() {
110 110
 
111 111
     // Set hostname workaround
112 112
     WiFi.hostname(hostname);
113
-    
113
+
114 114
 #elif defined(ARDUINO_ARCH_ESP32)
115 115
 
116 116
     // Set hostname workaround
117 117
     WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
118 118
     WiFi.setHostname(hostname.c_str());
119
-    
119
+
120 120
     WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
121 121
         /*
122 122
          * was initially: workaround for WiFi connecting only every 2nd reset
@@ -146,7 +146,7 @@ void setup() {
146 146
         debug.print(F("."));
147 147
     }
148 148
     debug.println(F("\nWiFi connected!"));
149
-    
149
+
150 150
     // Set hostname workaround
151 151
     WiFi.setHostname(hostname.c_str());
152 152
 
@@ -180,6 +180,11 @@ void setup() {
180 180
     initServers(hostname);
181 181
 
182 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 190
 void loop() {

+ 51
- 39
src/ui.cpp View File

@@ -42,21 +42,47 @@
42 42
 #define TOUCH_TOP 230
43 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 51
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
47 52
     p.y = map(p.y, TOUCH_TOP, TOUCH_BOTTOM, 0, LCD_HEIGHT);
48 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 57
     uint32_t duty = (4095 / valueMax) * min(value, valueMax);
53 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 87
 void ui_init(void) {
62 88
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
@@ -72,50 +98,36 @@ void ui_init(void) {
72 98
 
73 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 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 111
 void ui_run(void) {
107 112
     if (ts.tirqTouched() && ts.touched()) {
108 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 131
         delay(100);
120 132
     }
121 133
 }

Loading…
Cancel
Save