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,6 +19,9 @@
19 19
 #ifndef __VOLCANO_H__
20 20
 #define __VOLCANO_H__
21 21
 
22
+#include <stdint.h>
23
+#include <stdbool.h>
24
+
22 25
 // in 1/10th degrees C, or < 0 on error
23 26
 int16_t volcano_get_current_temp(void);
24 27
 int16_t volcano_get_target_temp(void);
@@ -26,4 +29,8 @@ int16_t volcano_get_target_temp(void);
26 29
 // v in 1/10th degrees C, returns < 0 on error
27 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 36
 #endif // __VOLCANO_H__

+ 9
- 14
src/ble.c View File

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

+ 46
- 0
src/console.c View File

@@ -110,6 +110,8 @@ static void cnsl_interpret(const char *line) {
110 110
         println("   vrct - Volcano read current temperature");
111 111
         println("   vrtt - Volcano read target temperature");
112 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 115
         println("");
114 116
         println("Press Enter with no input to repeat last command.");
115 117
         println("Use repeat to continuously execute last command.");
@@ -260,6 +262,50 @@ static void cnsl_interpret(const char *line) {
260 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 309
     } else {
264 310
         println("unknown command \"%s\"", line);
265 311
     }

+ 34
- 0
src/volcano.c View File

@@ -83,3 +83,37 @@ int8_t volcano_set_target_temp(uint16_t value) {
83 83
     }
84 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