Browse Source

wait some time after a registered touch and react properly to external mqtt events

Thomas Buck 5 months ago
parent
commit
91d5783ac8
3 changed files with 66 additions and 48 deletions
  1. 2
    2
      src/DebugLog.cpp
  2. 53
    40
      src/mqtt.cpp
  3. 11
    6
      src/ui.cpp

+ 2
- 2
src/DebugLog.cpp View File

38
 
38
 
39
 void DebugLog::sendToTargets(String s) {
39
 void DebugLog::sendToTargets(String s) {
40
     Serial.print(s);
40
     Serial.print(s);
41
-    
41
+
42
     s = "log:" + s;
42
     s = "log:" + s;
43
     wifi_send_websocket(s);
43
     wifi_send_websocket(s);
44
 }
44
 }
51
 #ifdef ENABLE_DEBUGLOG
51
 #ifdef ENABLE_DEBUGLOG
52
     addToBuffer(s);
52
     addToBuffer(s);
53
 #endif // ENABLE_DEBUGLOG
53
 #endif // ENABLE_DEBUGLOG
54
-    
54
+
55
     sendToTargets(s);
55
     sendToTargets(s);
56
 }
56
 }
57
 
57
 

+ 53
- 40
src/mqtt.cpp View File

49
 static unsigned long last_mqtt_reconnect_time = 0;
49
 static unsigned long last_mqtt_reconnect_time = 0;
50
 static unsigned long last_mqtt_write_time = 0;
50
 static unsigned long last_mqtt_write_time = 0;
51
 
51
 
52
+#ifdef FEATURE_UI
53
+static struct ui_status prev_status = ui_status;
54
+#endif // FEATURE_UI
55
+
52
 static void writeMQTT() {
56
 static void writeMQTT() {
53
     if (!mqtt.connected()) {
57
     if (!mqtt.connected()) {
54
         return;
58
         return;
117
     } else if (ps.indexOf("none") != -1) {
121
     } else if (ps.indexOf("none") != -1) {
118
         state = 4;
122
         state = 4;
119
     } else {
123
     } else {
124
+        debug.println(F("parse abort"));
120
         return;
125
         return;
121
     }
126
     }
122
 #endif
127
 #endif
123
 
128
 
124
-#ifdef FEATURE_RELAIS
125
-    int id = -1;
126
-
127
-    String our_topic(SENSOR_LOCATION);
128
-    our_topic += "/";
129
-
130
-    if (!ts.startsWith(our_topic)) {
131
-        debug.print(F("Unknown MQTT room "));
132
-        debug.println(ts);
133
-        return;
134
-    }
135
-
136
-    String ids = ts.substring(our_topic.length());
137
-    for (int i = 0; i < relais_count(); i++) {
138
-        if (ids == relais_name(i)) {
139
-            id = i;
140
-            break;
141
-        }
142
-    }
143
-
144
-    if (id < 0) {
145
-        debug.print(F("Unknown MQTT topic "));
146
-        debug.println(ts);
147
-        return;
148
-    }
149
-
150
-    if ((id >= 0) && (id < relais_count())) {
151
-        debug.print(F("Turning "));
152
-        debug.print(state ? "on" : "off");
153
-        debug.print(F(" relais "));
154
-        debug.println(id);
155
-
156
-        relais_set(id, state);
157
-
158
-        writeDatabase();
159
-    }
160
-#endif // FEATURE_RELAIS
161
-
162
 #ifdef FEATURE_UI
129
 #ifdef FEATURE_UI
163
     // store new topic values for display
130
     // store new topic values for display
164
     if (ts == "livingroom/light_kitchen") {
131
     if (ts == "livingroom/light_kitchen") {
165
         ui_status.light_kitchen = state ? true : false;
132
         ui_status.light_kitchen = state ? true : false;
133
+        prev_status.light_kitchen = ui_status.light_kitchen;
166
         ui_progress(UI_UPDATE);
134
         ui_progress(UI_UPDATE);
167
     } else if (ts == "livingroom/light_pc") {
135
     } else if (ts == "livingroom/light_pc") {
168
         ui_status.light_pc = state ? true : false;
136
         ui_status.light_pc = state ? true : false;
137
+        prev_status.light_pc = ui_status.light_pc;
169
         ui_progress(UI_UPDATE);
138
         ui_progress(UI_UPDATE);
170
     } else if (ts == "livingroom/light_bench") {
139
     } else if (ts == "livingroom/light_bench") {
171
         ui_status.light_bench = state ? true : false;
140
         ui_status.light_bench = state ? true : false;
141
+        prev_status.light_bench = ui_status.light_bench;
172
         ui_progress(UI_UPDATE);
142
         ui_progress(UI_UPDATE);
173
     } else if (ts == "livingroom/light_amp") {
143
     } else if (ts == "livingroom/light_amp") {
174
         ui_status.light_amp = state ? true : false;
144
         ui_status.light_amp = state ? true : false;
145
+        prev_status.light_amp = ui_status.light_amp;
175
         ui_progress(UI_UPDATE);
146
         ui_progress(UI_UPDATE);
176
     } else if (ts == "livingroom/light_box") {
147
     } else if (ts == "livingroom/light_box") {
177
         ui_status.light_box = state ? true : false;
148
         ui_status.light_box = state ? true : false;
149
+        prev_status.light_box = ui_status.light_box;
178
         ui_progress(UI_UPDATE);
150
         ui_progress(UI_UPDATE);
179
     } else if (ts == "livingroom/light_corner/cmnd/POWER") {
151
     } else if (ts == "livingroom/light_corner/cmnd/POWER") {
180
-        ui_status.light_corner = state ? true: false;
152
+        ui_status.light_corner = state ? true : false;
153
+        prev_status.light_corner = ui_status.light_corner;
181
         ui_progress(UI_UPDATE);
154
         ui_progress(UI_UPDATE);
182
     } else if (ts == "livingroom/workbench/cmnd/POWER") {
155
     } else if (ts == "livingroom/workbench/cmnd/POWER") {
183
         ui_status.light_workspace = state ? true : false;
156
         ui_status.light_workspace = state ? true : false;
157
+        prev_status.light_workspace = ui_status.light_workspace;
184
         ui_progress(UI_UPDATE);
158
         ui_progress(UI_UPDATE);
185
     } else if (ts == "livingroom/amp/cmnd/POWER") {
159
     } else if (ts == "livingroom/amp/cmnd/POWER") {
186
         ui_status.sound_amplifier = state ? true : false;
160
         ui_status.sound_amplifier = state ? true : false;
161
+        prev_status.sound_amplifier = ui_status.sound_amplifier;
187
         ui_progress(UI_UPDATE);
162
         ui_progress(UI_UPDATE);
188
     } else if (ts == "bathroom/force_light") {
163
     } else if (ts == "bathroom/force_light") {
189
         if (state == 0) {
164
         if (state == 0) {
197
         } else if (state == 4) {
172
         } else if (state == 4) {
198
             ui_status.bathroom_lights = BATH_LIGHT_NONE;
173
             ui_status.bathroom_lights = BATH_LIGHT_NONE;
199
         }
174
         }
175
+        prev_status.bathroom_lights = ui_status.bathroom_lights;
200
         ui_progress(UI_UPDATE);
176
         ui_progress(UI_UPDATE);
201
     }
177
     }
202
 #endif // FEATURE_UI
178
 #endif // FEATURE_UI
179
+
180
+#ifdef FEATURE_RELAIS
181
+    int id = -1;
182
+
183
+    String our_topic(SENSOR_LOCATION);
184
+    our_topic += "/";
185
+
186
+    if (!ts.startsWith(our_topic)) {
187
+        debug.print(F("Unknown MQTT room "));
188
+        debug.println(ts);
189
+        return;
190
+    }
191
+
192
+    String ids = ts.substring(our_topic.length());
193
+    for (int i = 0; i < relais_count(); i++) {
194
+        if (ids == relais_name(i)) {
195
+            id = i;
196
+            break;
197
+        }
198
+    }
199
+
200
+    if (id < 0) {
201
+        debug.print(F("Unknown MQTT topic "));
202
+        debug.println(ts);
203
+        return;
204
+    }
205
+
206
+    if ((id >= 0) && (id < relais_count())) {
207
+        debug.print(F("Turning "));
208
+        debug.print(state ? "on" : "off");
209
+        debug.print(F(" relais "));
210
+        debug.println(id);
211
+
212
+        relais_set(id, state);
213
+
214
+        writeDatabase();
215
+    }
216
+#endif // FEATURE_RELAIS
203
 }
217
 }
204
 
218
 
205
 static void mqttReconnect() {
219
 static void mqttReconnect() {
259
 }
273
 }
260
 
274
 
261
 #ifdef FEATURE_UI
275
 #ifdef FEATURE_UI
262
-static struct ui_status prev_status = ui_status;
263
 
276
 
264
 static void mqttPublish(const char* ts, const char *ps, bool retained) {
277
 static void mqttPublish(const char* ts, const char *ps, bool retained) {
265
     debug.print(F("MQTT  Tx&gt; @ \""));
278
     debug.print(F("MQTT  Tx&gt; @ \""));

+ 11
- 6
src/ui.cpp View File

52
 #define BTNS_OFF_X ((LCD_WIDTH - (2 * BTN_W) - (1 * BTN_GAP)) / 2)
52
 #define BTNS_OFF_X ((LCD_WIDTH - (2 * BTN_W) - (1 * BTN_GAP)) / 2)
53
 #define BTNS_OFF_Y ((LCD_HEIGHT - (3 * BTN_H) - (2 * BTN_GAP)) / 2)
53
 #define BTNS_OFF_Y ((LCD_HEIGHT - (3 * BTN_H) - (2 * BTN_GAP)) / 2)
54
 
54
 
55
-#define INVERT_BOOL(x) (x) = !(x);
55
+#define INVERT_BOOL(x) (x) = !(x)
56
 
56
 
57
 #define LDR_CHECK_MS 1000
57
 #define LDR_CHECK_MS 1000
58
-
59
-#define TOUCH_PRESSURE_MIN_DOWN 200
60
-#define TOUCH_PRESSURE_MIN_UP 10
58
+#define MIN_TOUCH_DELAY_MS 200
59
+#define TOUCH_PRESSURE_MIN 200
61
 
60
 
62
 static SPIClass mySpi = SPIClass(HSPI);
61
 static SPIClass mySpi = SPIClass(HSPI);
63
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
62
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
79
 static bool is_touched = false;
78
 static bool is_touched = false;
80
 static unsigned long last_ldr = 0;
79
 static unsigned long last_ldr = 0;
81
 static int ldr_value = 0;
80
 static int ldr_value = 0;
81
+static unsigned long last_touch_time = 0;
82
 
82
 
83
 static TS_Point touchToScreen(TS_Point p) {
83
 static TS_Point touchToScreen(TS_Point p) {
84
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
84
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
305
 
305
 
306
 void ui_run(void) {
306
 void ui_run(void) {
307
     unsigned long now = millis();
307
     unsigned long now = millis();
308
+
308
     if (now >= (last_ldr + LDR_CHECK_MS)) {
309
     if (now >= (last_ldr + LDR_CHECK_MS)) {
309
         last_ldr = now;
310
         last_ldr = now;
310
         int ldr = analogRead(LDR_PIN);
311
         int ldr = analogRead(LDR_PIN);
312
+
313
+        // TODO lowpass?
311
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
314
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
312
         ldr_value = ldr;
315
         ldr_value = ldr;
316
+
313
         if (ui_page == UI_INFO) {
317
         if (ui_page == UI_INFO) {
314
             ui_draw_menu();
318
             ui_draw_menu();
315
         }
319
         }
322
         p = touchToScreen(ts.getPoint());
326
         p = touchToScreen(ts.getPoint());
323
 
327
 
324
         // minimum pressure
328
         // minimum pressure
325
-        if (p.z < TOUCH_PRESSURE_MIN_DOWN) {
329
+        if (p.z < TOUCH_PRESSURE_MIN) {
326
             touched = false;
330
             touched = false;
327
         }
331
         }
328
     }
332
     }
329
 
333
 
330
     if (touched && (!is_touched)) {
334
     if (touched && (!is_touched)) {
331
         is_touched = true;
335
         is_touched = true;
336
+        last_touch_time = millis();
332
 
337
 
333
         if (ui_page == UI_INFO) {
338
         if (ui_page == UI_INFO) {
334
             // switch to next page
339
             // switch to next page
406
         }
411
         }
407
 
412
 
408
         ui_draw_menu();
413
         ui_draw_menu();
409
-    } else if ((!touched) && is_touched && (p.z < TOUCH_PRESSURE_MIN_UP)) {
414
+    } else if ((!touched) && is_touched && ((now - last_touch_time) >= MIN_TOUCH_DELAY_MS)) {
410
         is_touched = false;
415
         is_touched = false;
411
     }
416
     }
412
 }
417
 }

Loading…
Cancel
Save