Преглед изворни кода

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

Thomas Buck пре 5 месеци
родитељ
комит
91d5783ac8
3 измењених фајлова са 66 додато и 48 уклоњено
  1. 2
    2
      src/DebugLog.cpp
  2. 53
    40
      src/mqtt.cpp
  3. 11
    6
      src/ui.cpp

+ 2
- 2
src/DebugLog.cpp Прегледај датотеку

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

+ 53
- 40
src/mqtt.cpp Прегледај датотеку

@@ -49,6 +49,10 @@ PubSubClient mqtt(mqttClient);
49 49
 static unsigned long last_mqtt_reconnect_time = 0;
50 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 56
 static void writeMQTT() {
53 57
     if (!mqtt.connected()) {
54 58
         return;
@@ -117,73 +121,44 @@ static void mqttCallback(char* topic, byte* payload, unsigned int length) {
117 121
     } else if (ps.indexOf("none") != -1) {
118 122
         state = 4;
119 123
     } else {
124
+        debug.println(F("parse abort"));
120 125
         return;
121 126
     }
122 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 129
 #ifdef FEATURE_UI
163 130
     // store new topic values for display
164 131
     if (ts == "livingroom/light_kitchen") {
165 132
         ui_status.light_kitchen = state ? true : false;
133
+        prev_status.light_kitchen = ui_status.light_kitchen;
166 134
         ui_progress(UI_UPDATE);
167 135
     } else if (ts == "livingroom/light_pc") {
168 136
         ui_status.light_pc = state ? true : false;
137
+        prev_status.light_pc = ui_status.light_pc;
169 138
         ui_progress(UI_UPDATE);
170 139
     } else if (ts == "livingroom/light_bench") {
171 140
         ui_status.light_bench = state ? true : false;
141
+        prev_status.light_bench = ui_status.light_bench;
172 142
         ui_progress(UI_UPDATE);
173 143
     } else if (ts == "livingroom/light_amp") {
174 144
         ui_status.light_amp = state ? true : false;
145
+        prev_status.light_amp = ui_status.light_amp;
175 146
         ui_progress(UI_UPDATE);
176 147
     } else if (ts == "livingroom/light_box") {
177 148
         ui_status.light_box = state ? true : false;
149
+        prev_status.light_box = ui_status.light_box;
178 150
         ui_progress(UI_UPDATE);
179 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 154
         ui_progress(UI_UPDATE);
182 155
     } else if (ts == "livingroom/workbench/cmnd/POWER") {
183 156
         ui_status.light_workspace = state ? true : false;
157
+        prev_status.light_workspace = ui_status.light_workspace;
184 158
         ui_progress(UI_UPDATE);
185 159
     } else if (ts == "livingroom/amp/cmnd/POWER") {
186 160
         ui_status.sound_amplifier = state ? true : false;
161
+        prev_status.sound_amplifier = ui_status.sound_amplifier;
187 162
         ui_progress(UI_UPDATE);
188 163
     } else if (ts == "bathroom/force_light") {
189 164
         if (state == 0) {
@@ -197,9 +172,48 @@ static void mqttCallback(char* topic, byte* payload, unsigned int length) {
197 172
         } else if (state == 4) {
198 173
             ui_status.bathroom_lights = BATH_LIGHT_NONE;
199 174
         }
175
+        prev_status.bathroom_lights = ui_status.bathroom_lights;
200 176
         ui_progress(UI_UPDATE);
201 177
     }
202 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 219
 static void mqttReconnect() {
@@ -259,7 +273,6 @@ void runMQTT() {
259 273
 }
260 274
 
261 275
 #ifdef FEATURE_UI
262
-static struct ui_status prev_status = ui_status;
263 276
 
264 277
 static void mqttPublish(const char* ts, const char *ps, bool retained) {
265 278
     debug.print(F("MQTT  Tx&gt; @ \""));

+ 11
- 6
src/ui.cpp Прегледај датотеку

@@ -52,12 +52,11 @@
52 52
 #define BTNS_OFF_X ((LCD_WIDTH - (2 * BTN_W) - (1 * BTN_GAP)) / 2)
53 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 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 61
 static SPIClass mySpi = SPIClass(HSPI);
63 62
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
@@ -79,6 +78,7 @@ static enum ui_pages ui_page = UI_START;
79 78
 static bool is_touched = false;
80 79
 static unsigned long last_ldr = 0;
81 80
 static int ldr_value = 0;
81
+static unsigned long last_touch_time = 0;
82 82
 
83 83
 static TS_Point touchToScreen(TS_Point p) {
84 84
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
@@ -305,11 +305,15 @@ void ui_progress(enum ui_state state) {
305 305
 
306 306
 void ui_run(void) {
307 307
     unsigned long now = millis();
308
+
308 309
     if (now >= (last_ldr + LDR_CHECK_MS)) {
309 310
         last_ldr = now;
310 311
         int ldr = analogRead(LDR_PIN);
312
+
313
+        // TODO lowpass?
311 314
         //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
312 315
         ldr_value = ldr;
316
+
313 317
         if (ui_page == UI_INFO) {
314 318
             ui_draw_menu();
315 319
         }
@@ -322,13 +326,14 @@ void ui_run(void) {
322 326
         p = touchToScreen(ts.getPoint());
323 327
 
324 328
         // minimum pressure
325
-        if (p.z < TOUCH_PRESSURE_MIN_DOWN) {
329
+        if (p.z < TOUCH_PRESSURE_MIN) {
326 330
             touched = false;
327 331
         }
328 332
     }
329 333
 
330 334
     if (touched && (!is_touched)) {
331 335
         is_touched = true;
336
+        last_touch_time = millis();
332 337
 
333 338
         if (ui_page == UI_INFO) {
334 339
             // switch to next page
@@ -406,7 +411,7 @@ void ui_run(void) {
406 411
         }
407 412
 
408 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 415
         is_touched = false;
411 416
     }
412 417
 }

Loading…
Откажи
Сачувај