Browse Source

add commands for heater and pump state

Thomas Buck 1 year ago
parent
commit
722d6e6d8f
4 changed files with 96 additions and 14 deletions
  1. 7
    0
      include/volcano.h
  2. 9
    14
      src/ble.c
  3. 46
    0
      src/console.c
  4. 34
    0
      src/volcano.c

+ 7
- 0
include/volcano.h View File

19
 #ifndef __VOLCANO_H__
19
 #ifndef __VOLCANO_H__
20
 #define __VOLCANO_H__
20
 #define __VOLCANO_H__
21
 
21
 
22
+#include <stdint.h>
23
+#include <stdbool.h>
24
+
22
 // in 1/10th degrees C, or < 0 on error
25
 // in 1/10th degrees C, or < 0 on error
23
 int16_t volcano_get_current_temp(void);
26
 int16_t volcano_get_current_temp(void);
24
 int16_t volcano_get_target_temp(void);
27
 int16_t volcano_get_target_temp(void);
26
 // v in 1/10th degrees C, returns < 0 on error
29
 // v in 1/10th degrees C, returns < 0 on error
27
 int8_t volcano_set_target_temp(uint16_t v);
30
 int8_t volcano_set_target_temp(uint16_t v);
28
 
31
 
32
+// returns < 0 on error
33
+int8_t volcano_set_heater_state(bool value);
34
+int8_t volcano_set_pump_state(bool value);
35
+
29
 #endif // __VOLCANO_H__
36
 #endif // __VOLCANO_H__

+ 9
- 14
src/ble.c View File

421
     cyw43_thread_enter();
421
     cyw43_thread_enter();
422
 
422
 
423
     switch (state) {
423
     switch (state) {
424
-    case TC_OFF:
425
-        cyw43_thread_exit();
426
-        return;
427
-
428
     case TC_W4_SCAN:
424
     case TC_W4_SCAN:
429
         cyw43_thread_exit();
425
         cyw43_thread_exit();
430
         ble_scan(0);
426
         ble_scan(0);
435
         gap_disconnect(connection_handle);
431
         gap_disconnect(connection_handle);
436
         break;
432
         break;
437
 
433
 
438
-    default:
434
+    case TC_IDLE:
439
         break;
435
         break;
436
+
437
+    default:
438
+        debug("invalid state for connect %d", state);
439
+        cyw43_thread_exit();
440
+        return;
440
     }
441
     }
441
 
442
 
442
     debug("connecting to %s", bd_addr_to_str(addr));
443
     debug("connecting to %s", bd_addr_to_str(addr));
461
     cyw43_thread_enter();
462
     cyw43_thread_enter();
462
 
463
 
463
     if (state == TC_READY) {
464
     if (state == TC_READY) {
465
+        debug("disconnecting");
464
         gap_disconnect(connection_handle);
466
         gap_disconnect(connection_handle);
467
+    } else {
468
+        debug("invalid state for disconnect %d", state);
465
     }
469
     }
466
 
470
 
467
     cyw43_thread_exit();
471
     cyw43_thread_exit();
578
         service_idx = srvc;
582
         service_idx = srvc;
579
         cyw43_thread_exit();
583
         cyw43_thread_exit();
580
 
584
 
581
-        debug("waiting for service discovery");
582
-
583
         uint32_t start_time = to_ms_since_boot(get_absolute_time());
585
         uint32_t start_time = to_ms_since_boot(get_absolute_time());
584
         while (1) {
586
         while (1) {
585
             sleep_ms(1);
587
             sleep_ms(1);
598
             cyw43_thread_exit();
600
             cyw43_thread_exit();
599
 
601
 
600
             if (state_cached == TC_READY) {
602
             if (state_cached == TC_READY) {
601
-            debug("service discovery done");
602
                 break;
603
                 break;
603
             }
604
             }
604
         }
605
         }
650
         characteristic_idx = ch;
651
         characteristic_idx = ch;
651
         cyw43_thread_exit();
652
         cyw43_thread_exit();
652
 
653
 
653
-        debug("waiting for characteristic discovery");
654
-
655
         uint32_t start_time = to_ms_since_boot(get_absolute_time());
654
         uint32_t start_time = to_ms_since_boot(get_absolute_time());
656
         while (1) {
655
         while (1) {
657
             sleep_ms(1);
656
             sleep_ms(1);
670
             cyw43_thread_exit();
669
             cyw43_thread_exit();
671
 
670
 
672
             if (state_cached == TC_READY) {
671
             if (state_cached == TC_READY) {
673
-                debug("characteristic discovery done");
674
                 break;
672
                 break;
675
             }
673
             }
676
         }
674
         }
699
     state = TC_W4_WRITE;
697
     state = TC_W4_WRITE;
700
     cyw43_thread_exit();
698
     cyw43_thread_exit();
701
 
699
 
702
-    debug("waiting for write");
703
-
704
     uint32_t start_time = to_ms_since_boot(get_absolute_time());
700
     uint32_t start_time = to_ms_since_boot(get_absolute_time());
705
     while (1) {
701
     while (1) {
706
         sleep_ms(1);
702
         sleep_ms(1);
719
         cyw43_thread_exit();
715
         cyw43_thread_exit();
720
 
716
 
721
         if ((state_cached == TC_WRITE_COMPLETE) || (state_cached == TC_READY)) {
717
         if ((state_cached == TC_WRITE_COMPLETE) || (state_cached == TC_READY)) {
722
-            debug("write done (%s)", (state_cached == TC_READY) ? "error" : "success");
723
             break;
718
             break;
724
         }
719
         }
725
     }
720
     }

+ 46
- 0
src/console.c View File

110
         println("   vrct - Volcano read current temperature");
110
         println("   vrct - Volcano read current temperature");
111
         println("   vrtt - Volcano read target temperature");
111
         println("   vrtt - Volcano read target temperature");
112
         println(" vwtt X - Volcano write target temperature");
112
         println(" vwtt X - Volcano write target temperature");
113
+        println("  vwh X - Set heater to 1 or 0");
114
+        println("  vwp X - Set heater to 1 or 0");
113
         println("");
115
         println("");
114
         println("Press Enter with no input to repeat last command.");
116
         println("Press Enter with no input to repeat last command.");
115
         println("Use repeat to continuously execute last command.");
117
         println("Use repeat to continuously execute last command.");
260
                 println("success");
262
                 println("success");
261
             }
263
             }
262
         }
264
         }
265
+    } else if (str_startswith(line, "vwh ")) {
266
+        int val;
267
+        int r = sscanf(line, "vwh %d", &val);
268
+        if ((r != 1) || ((val != 0) && (val != 1))) {
269
+            println("invalid input (%d %d)", r, val);
270
+        } else {
271
+#ifdef TEST_VOLCANO_AUTO_CONNECT
272
+            VOLCANO_AUTO_CONNECT
273
+#endif // TEST_VOLCANO_AUTO_CONNECT
274
+
275
+            int8_t r = volcano_set_heater_state(val == 1);
276
+
277
+#ifdef TEST_VOLCANO_AUTO_CONNECT
278
+            ble_disconnect();
279
+#endif // TEST_VOLCANO_AUTO_CONNECT
280
+
281
+            if (r < 0) {
282
+                println("error writing heater state %d", r);
283
+            } else {
284
+                println("success");
285
+            }
286
+        }
287
+    } else if (str_startswith(line, "vwp ")) {
288
+        int val;
289
+        int r = sscanf(line, "vwp %d", &val);
290
+        if ((r != 1) || ((val != 0) && (val != 1))) {
291
+            println("invalid input (%d %d)", r, val);
292
+        } else {
293
+#ifdef TEST_VOLCANO_AUTO_CONNECT
294
+            VOLCANO_AUTO_CONNECT
295
+#endif // TEST_VOLCANO_AUTO_CONNECT
296
+
297
+            int8_t r = volcano_set_pump_state(val == 1);
298
+
299
+#ifdef TEST_VOLCANO_AUTO_CONNECT
300
+            ble_disconnect();
301
+#endif // TEST_VOLCANO_AUTO_CONNECT
302
+
303
+            if (r < 0) {
304
+                println("error writing pump state %d", r);
305
+            } else {
306
+                println("success");
307
+            }
308
+        }
263
     } else {
309
     } else {
264
         println("unknown command \"%s\"", line);
310
         println("unknown command \"%s\"", line);
265
     }
311
     }

+ 34
- 0
src/volcano.c View File

83
     }
83
     }
84
     return r;
84
     return r;
85
 }
85
 }
86
+
87
+int8_t volcano_set_heater_state(bool value) {
88
+    uuid_base[3] = UUID_WRITE_SRVC;
89
+
90
+    if (value) {
91
+        uuid_base2[3] = UUID_HEATER_ON;
92
+    } else {
93
+        uuid_base2[3] = UUID_HEATER_OFF;
94
+    }
95
+
96
+    uint8_t d = 0;
97
+    int8_t r = ble_write(uuid_base, uuid_base2, &d, 1);
98
+    if (r != 0) {
99
+        debug("ble_write unexpected value %d", r);
100
+    }
101
+    return r;
102
+}
103
+
104
+int8_t volcano_set_pump_state(bool value) {
105
+    uuid_base[3] = UUID_WRITE_SRVC;
106
+
107
+    if (value) {
108
+        uuid_base2[3] = UUID_PUMP_ON;
109
+    } else {
110
+        uuid_base2[3] = UUID_PUMP_OFF;
111
+    }
112
+
113
+    uint8_t d = 0;
114
+    int8_t r = ble_write(uuid_base, uuid_base2, &d, 1);
115
+    if (r != 0) {
116
+        debug("ble_write unexpected value %d", r);
117
+    }
118
+    return r;
119
+}

Loading…
Cancel
Save