瀏覽代碼

ui now functioning for livingroom

Thomas Buck 5 月之前
父節點
當前提交
d47220cc33
共有 4 個檔案被更改,包括 159 行新增49 行删除
  1. 1
    1
      include/mqtt.h
  2. 1
    1
      include/ui.h
  3. 37
    6
      src/mqtt.cpp
  4. 120
    41
      src/ui.cpp

+ 1
- 1
include/mqtt.h 查看文件

@@ -18,7 +18,7 @@ void initMQTT();
18 18
 void runMQTT();
19 19
 
20 20
 #ifdef FEATURE_UI
21
-void writeMQTTtopic(const char *topic, const char *data, bool retain = true);
21
+void writeMQTT_UI(void);
22 22
 #endif // FEATURE_UI
23 23
 
24 24
 #endif // __MQTT_H__

+ 1
- 1
include/ui.h 查看文件

@@ -33,10 +33,10 @@ enum ui_state {
33 33
     UI_WIFI_CONNECTING,
34 34
     UI_WIFI_CONNECTED,
35 35
     UI_READY,
36
+    UI_UPDATE,
36 37
 };
37 38
 
38 39
 void ui_init(void);
39
-void ui_draw_menu(void);
40 40
 void ui_run(void);
41 41
 
42 42
 void ui_progress(enum ui_state state);

+ 37
- 6
src/mqtt.cpp 查看文件

@@ -157,20 +157,28 @@ static void mqttCallback(char* topic, byte* payload, unsigned int length) {
157 157
     // store new topic values for display
158 158
     if (ts == "livingroom/light_kitchen") {
159 159
         ui_status.light_kitchen = state ? true : false;
160
+        ui_progress(UI_UPDATE);
160 161
     } else if (ts == "livingroom/light_pc") {
161 162
         ui_status.light_pc = state ? true : false;
163
+        ui_progress(UI_UPDATE);
162 164
     } else if (ts == "livingroom/light_bench") {
163 165
         ui_status.light_bench = state ? true : false;
166
+        ui_progress(UI_UPDATE);
164 167
     } else if (ts == "livingroom/light_amp") {
165 168
         ui_status.light_amp = state ? true : false;
169
+        ui_progress(UI_UPDATE);
166 170
     } else if (ts == "livingroom/light_box") {
167 171
         ui_status.light_box = state ? true : false;
168
-    } else if (ts == "livingroom/TODO") {
172
+        ui_progress(UI_UPDATE);
173
+    } else if (ts == "livingroom/light_corner/cmnd/POWER") {
169 174
         ui_status.light_corner = state ? true: false;
170
-    } else if (ts == "livingroom/TODO") {
175
+        ui_progress(UI_UPDATE);
176
+    } else if (ts == "livingroom/workbench/cmnd/POWER") {
171 177
         ui_status.light_workspace = state ? true : false;
172
-    } else if (ts == "livingroom/TODO") {
178
+        ui_progress(UI_UPDATE);
179
+    } else if (ts == "livingroom/amp/cmnd/POWER") {
173 180
         ui_status.sound_amplifier = state ? true : false;
181
+        ui_progress(UI_UPDATE);
174 182
     }
175 183
 #endif // FEATURE_UI
176 184
 }
@@ -202,7 +210,9 @@ static void mqttReconnect() {
202 210
         mqtt.subscribe("livingroom/light_bench");
203 211
         mqtt.subscribe("livingroom/light_amp");
204 212
         mqtt.subscribe("livingroom/light_box");
205
-        // TODO corner lights, workspace, amp
213
+        mqtt.subscribe("livingroom/light_corner/cmnd/POWER");
214
+        mqtt.subscribe("livingroom/workbench/cmnd/POWER");
215
+        mqtt.subscribe("livingroom/amp/cmnd/POWER");
206 216
 #endif // FEATURE_UI
207 217
     }
208 218
 }
@@ -229,8 +239,29 @@ void runMQTT() {
229 239
 }
230 240
 
231 241
 #ifdef FEATURE_UI
232
-void writeMQTTtopic(const char *topic, const char *data, bool retain) {
233
-    mqtt.publish(topic, data, retain);
242
+static struct ui_status prev_status = ui_status;
243
+
244
+void writeMQTT_UI(void) {
245
+    struct ui_status curr_status = ui_status;
246
+    if (curr_status.light_amp != prev_status.light_amp) {
247
+        mqtt.publish("livingroom/light_amp", curr_status.light_amp ? "on" : "off", true);
248
+    } else if (curr_status.light_bench != prev_status.light_bench) {
249
+        mqtt.publish("livingroom/light_bench", curr_status.light_bench ? "on" : "off", true);
250
+    } else if (curr_status.light_box != prev_status.light_box) {
251
+        mqtt.publish("livingroom/light_box", curr_status.light_box ? "on" : "off", true);
252
+    } else if (curr_status.light_corner != prev_status.light_corner) {
253
+        mqtt.publish("livingroom/light_corner/cmnd/POWER", curr_status.light_corner ? "on" : "off", true);
254
+    } else if (curr_status.light_kitchen != prev_status.light_kitchen) {
255
+        mqtt.publish("livingroom/light_kitchen", curr_status.light_kitchen ? "on" : "off", true);
256
+    } else if (curr_status.light_pc != prev_status.light_pc) {
257
+        mqtt.publish("livingroom/light_pc", curr_status.light_pc ? "on" : "off", true);
258
+    } else if (curr_status.light_workspace != prev_status.light_workspace) {
259
+        mqtt.publish("livingroom/workbench/cmnd/POWER", curr_status.light_workspace ? "on" : "off", true);
260
+    } else if (curr_status.sound_amplifier != prev_status.sound_amplifier) {
261
+        mqtt.publish("livingroom/amp/cmnd/POWER", curr_status.sound_amplifier ? "on" : "off", true);
262
+    }
263
+
264
+    prev_status = curr_status;
234 265
 }
235 266
 #endif
236 267
 

+ 120
- 41
src/ui.cpp 查看文件

@@ -86,21 +86,61 @@ static void draw_button(const char *name, uint32_t x, uint32_t y, uint32_t color
86 86
 }
87 87
 
88 88
 static void draw_livingroom1(void) {
89
-    draw_button("Lights Corner", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2, ui_status.light_corner ? TFT_GREEN : TFT_RED);
90
-    draw_button("Lights Workspace", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, ui_status.light_workspace ? TFT_GREEN : TFT_RED);
91
-    draw_button("Lights Kitchen", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, ui_status.light_kitchen ? TFT_GREEN : TFT_RED);
92
-
93
-    draw_button("Sound Amp.", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2, ui_status.sound_amplifier ? TFT_GREEN : TFT_RED);
94
-    draw_button("All Lights Off", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, TFT_RED);
89
+    // 1
90
+    draw_button("Lights Corner",
91
+                BTNS_OFF_X + BTN_W / 2,
92
+                BTNS_OFF_Y + BTN_H / 2,
93
+                ui_status.light_corner ? TFT_GREEN : TFT_RED);
94
+
95
+    // 2
96
+    draw_button("Lights Workspace",
97
+                BTNS_OFF_X + BTN_W / 2,
98
+                BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP,
99
+                ui_status.light_workspace ? TFT_GREEN : TFT_RED);
100
+
101
+    // 3
102
+    draw_button("Lights Kitchen",
103
+                BTNS_OFF_X + BTN_W / 2,
104
+                BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2,
105
+                ui_status.light_kitchen ? TFT_GREEN : TFT_RED);
106
+
107
+    // 4
108
+    draw_button("Sound Amp.",
109
+                BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP,
110
+                BTNS_OFF_Y + BTN_H / 2,
111
+                ui_status.sound_amplifier ? TFT_GREEN : TFT_RED);
112
+
113
+    // 5
114
+    draw_button("All Lights Off",
115
+                BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP,
116
+                BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP,
117
+                TFT_MAGENTA);
95 118
 }
96 119
 
97 120
 static void draw_livingroom2(void) {
98
-    draw_button("Lights PC", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2, ui_status.light_corner ? TFT_GREEN : TFT_RED);
99
-    draw_button("Lights Bench", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, ui_status.light_workspace ? TFT_GREEN : TFT_RED);
100
-    //draw_button("Lights Amp.", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, ui_status.light_kitchen ? TFT_GREEN : TFT_RED);
101
-
102
-    draw_button("Lights Amp.", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2, ui_status.sound_amplifier ? TFT_GREEN : TFT_RED);
103
-    draw_button("Lights Box", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, TFT_RED); // TODO both
121
+    // 1
122
+    draw_button("Lights PC",
123
+                BTNS_OFF_X + BTN_W / 2,
124
+                BTNS_OFF_Y + BTN_H / 2,
125
+                ui_status.light_pc ? TFT_GREEN : TFT_RED);
126
+
127
+    // 2
128
+    draw_button("Lights Bench",
129
+                BTNS_OFF_X + BTN_W / 2,
130
+                BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP,
131
+                ui_status.light_bench ? TFT_GREEN : TFT_RED);
132
+
133
+    // 4
134
+    draw_button("Lights Amp.",
135
+                BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP,
136
+                BTNS_OFF_Y + BTN_H / 2,
137
+                ui_status.light_amp ? TFT_GREEN : TFT_RED);
138
+
139
+    // 5
140
+    draw_button("Lights Box",
141
+                BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP,
142
+                BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP,
143
+                ui_status.light_box ? TFT_GREEN : TFT_RED);
104 144
 }
105 145
 
106 146
 void ui_init(void) {
@@ -118,6 +158,30 @@ void ui_init(void) {
118 158
     ui_progress(UI_INIT);
119 159
 }
120 160
 
161
+static void ui_draw_menu(void) {
162
+    switch (ui_page) {
163
+        case UI_START:
164
+            tft.fillScreen(TFT_BLACK);
165
+            ui_page = UI_LIVINGROOM1;
166
+            // fall-through
167
+
168
+        case UI_LIVINGROOM1:
169
+            draw_livingroom1();
170
+            break;
171
+
172
+        case UI_LIVINGROOM2:
173
+            draw_livingroom2();
174
+            break;
175
+
176
+        default:
177
+            ui_page = UI_START;
178
+            ui_draw_menu();
179
+            return;
180
+    }
181
+
182
+    draw_button("Next...", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, TFT_CYAN);
183
+}
184
+
121 185
 void ui_progress(enum ui_state state) {
122 186
     int x = LCD_WIDTH / 2;
123 187
     int y = LCD_HEIGHT / 2;
@@ -153,33 +217,14 @@ void ui_progress(enum ui_state state) {
153 217
         } break;
154 218
 
155 219
         case UI_READY: {
156
-            tft.fillScreen(TFT_BLACK);
157
-            tft.setTextDatum(MC_DATUM); // middle center
158
-            tft.drawString("Ready", x, y, fontSize);
220
+            ui_page = UI_START;
221
+            ui_draw_menu();
159 222
         } break;
160
-    }
161
-}
162 223
 
163
-void ui_draw_menu(void) {
164
-    switch (ui_page) {
165
-        case UI_START:
166
-            tft.fillScreen(TFT_BLACK);
167
-            ui_page = UI_LIVINGROOM1;
168
-            // fall-through
169
-
170
-        case UI_LIVINGROOM1:
171
-            draw_livingroom1();
172
-            break;
173
-
174
-        case UI_LIVINGROOM2:
175
-            draw_livingroom2();
176
-            break;
177
-
178
-        default:
179
-            ui_page = UI_START;
224
+        case UI_UPDATE: {
225
+            ui_draw_menu();
226
+        } break;
180 227
     }
181
-
182
-    draw_button("Next...", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, TFT_MAGENTA);
183 228
 }
184 229
 
185 230
 void ui_run(void) {
@@ -187,15 +232,49 @@ void ui_run(void) {
187 232
         TS_Point p = touchToScreen(ts.getPoint());
188 233
 
189 234
         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)) {
190
-            INVERT_BOOL(ui_status.light_corner);
235
+            // 1
236
+            if (ui_page == UI_LIVINGROOM1) {
237
+                INVERT_BOOL(ui_status.light_corner);
238
+            } else if (ui_page == UI_LIVINGROOM2) {
239
+                INVERT_BOOL(ui_status.light_pc);
240
+            }
241
+            writeMQTT_UI();
191 242
         } 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))) {
192
-            INVERT_BOOL(ui_status.light_workspace);
243
+            // 2
244
+            if (ui_page == UI_LIVINGROOM1) {
245
+                INVERT_BOOL(ui_status.light_workspace);
246
+            } else if (ui_page == UI_LIVINGROOM2) {
247
+                INVERT_BOOL(ui_status.light_bench);
248
+            }
249
+            writeMQTT_UI();
193 250
         } 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))) {
194
-            INVERT_BOOL(ui_status.light_kitchen);
251
+            // 3
252
+            if (ui_page == UI_LIVINGROOM1) {
253
+                INVERT_BOOL(ui_status.light_kitchen);
254
+            }
255
+            writeMQTT_UI();
195 256
         } 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)) {
196
-            INVERT_BOOL(ui_status.sound_amplifier);
257
+            // 4
258
+            if (ui_page == UI_LIVINGROOM1) {
259
+                INVERT_BOOL(ui_status.sound_amplifier);
260
+            } else if (ui_page == UI_LIVINGROOM2) {
261
+                INVERT_BOOL(ui_status.light_amp);
262
+            }
263
+            writeMQTT_UI();
197 264
         } 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))) {
198
-            // TODO should act on both TV lights (box and amp)
265
+            // 5
266
+            if (ui_page == UI_LIVINGROOM1) {
267
+                ui_status.light_amp = false;
268
+                ui_status.light_kitchen = false;
269
+                ui_status.light_bench= false;
270
+                ui_status.light_workspace = false;
271
+                ui_status.light_pc = false;
272
+                ui_status.light_corner = false;
273
+                ui_status.light_box = false;
274
+            } else if (ui_page == UI_LIVINGROOM2) {
275
+                INVERT_BOOL(ui_status.light_box);
276
+            }
277
+            writeMQTT_UI();
199 278
         } 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))) {
200 279
             // switch to next page
201 280
             ui_page = (enum ui_pages)((ui_page + 1) % UI_NUM_PAGES);

Loading…
取消
儲存