Browse Source

prefetch volcano ble characteristics

Thomas Buck 11 months ago
parent
commit
7680476df9
7 changed files with 119 additions and 22 deletions
  1. 9
    0
      README.md
  2. 3
    1
      include/ble.h
  3. 3
    0
      include/volcano.h
  4. 59
    14
      src/ble.c
  5. 2
    2
      src/state_crafty.c
  6. 37
    0
      src/volcano.c
  7. 6
    5
      src/workflow.c

+ 9
- 0
README.md View File

@@ -1,9 +1,18 @@
1 1
 # Pi Pico Volcano Remote Control Gadget
2 2
 
3
+Supports:
4
+
5
+ * workflows for the Volcano Hybrid
6
+ * basic status and settings for Crafty+
7
+
3 8
 For use with Raspberry Pi Pico W boards with the [Waveshare Pico LCD 1.3](https://www.waveshare.com/wiki/Pico-LCD-1.3) and the [Pimoroni Pico Lipo Shim](https://shop.pimoroni.com/products/pico-lipo-shim).
4 9
 
5 10
 Adapted from the [tinyusb-cdc-example](https://github.com/hathach/tinyusb/blob/master/examples/device/cdc_msc/src/main.c), [adc example](https://github.com/raspberrypi/pico-examples/tree/master/adc/read_vsys), [standalone client example](https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/standalone/client.c) and my [Trackball firmware](https://git.xythobuz.de/thomas/Trackball).
6 11
 
12
+`python-test` contains a similar app to the C version in the top level of the repo, but instead written for MicroPython on the Pico W.
13
+Unfortunately I had many performance and space problems with this, so I decided to rewrite it.
14
+`web-app` contains a script to conveniently fetch the original web app JS sources, for "reverse engineering".
15
+
7 16
 ## Quick Start
8 17
 
9 18
 When compiling for the first time, check out the required git submodules.

+ 3
- 1
include/ble.h View File

@@ -54,8 +54,10 @@ void ble_connect(bd_addr_t addr, bd_addr_type_t type);
54 54
 bool ble_is_connected(void);
55 55
 void ble_disconnect(void);
56 56
 
57
+int8_t ble_discover(const uint8_t *service, const uint8_t *characteristic);
58
+
57 59
 int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len);
58 60
 int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
59
-                 uint8_t *buff, uint16_t buff_len);
61
+                 const uint8_t *buff, uint16_t buff_len);
60 62
 
61 63
 #endif // __BLE_H__

+ 3
- 0
include/volcano.h View File

@@ -22,6 +22,9 @@
22 22
 #include <stdint.h>
23 23
 #include <stdbool.h>
24 24
 
25
+// returns < 0 on error
26
+int8_t volcano_discover_characteristics(void);
27
+
25 28
 // in 1/10th degrees C, or < 0 on error
26 29
 int16_t volcano_get_current_temp(void);
27 30
 int16_t volcano_get_target_temp(void);

+ 59
- 14
src/ble.c View File

@@ -28,10 +28,10 @@
28 28
 #include "util.h"
29 29
 #include "ble.h"
30 30
 
31
-#define BLE_READ_TIMEOUT_MS (2 * 500)
32
-#define BLE_SRVC_TIMEOUT_MS (2 * 500)
33
-#define BLE_CHAR_TIMEOUT_MS (2 * 2000)
34
-#define BLE_WRTE_TIMEOUT_MS (2 * 500)
31
+#define BLE_READ_TIMEOUT_MS (3 * 500)
32
+#define BLE_SRVC_TIMEOUT_MS (3 * 500)
33
+#define BLE_CHAR_TIMEOUT_MS (3 * 2000)
34
+#define BLE_WRTE_TIMEOUT_MS (3 * 500)
35 35
 #define BLE_MAX_SCAN_AGE_MS (10 * 1000)
36 36
 #define BLE_MAX_SERVICES 8
37 37
 #define BLE_MAX_CHARACTERISTICS 8
@@ -576,16 +576,7 @@ int32_t ble_read(const uint8_t *characteristic, uint8_t *buff, uint16_t buff_len
576 576
     return read_len;
577 577
 }
578 578
 
579
-int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
580
-                  uint8_t *buff, uint16_t buff_len) {
581
-    cyw43_thread_enter();
582
-
583
-    if (state != TC_READY) {
584
-        cyw43_thread_exit();
585
-        debug("invalid state for write (%d)", state);
586
-        return -1;
587
-    }
588
-
579
+static int discover_service(const uint8_t *service) {
589 580
     // check if service has already been discovered
590 581
     int srvc = -1, free_srvc = -1;
591 582
     for (int i = 0; i < BLE_MAX_SERVICES; i++) {
@@ -652,6 +643,10 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
652 643
         cyw43_thread_enter();
653 644
     }
654 645
 
646
+    return srvc;
647
+}
648
+
649
+static int discover_characteristic(int srvc, const uint8_t *characteristic) {
655 650
     // check if characteristic has already been discovered
656 651
     int ch = -1, free_ch = -1;
657 652
     for (int i = 0; i < BLE_MAX_CHARACTERISTICS; i++) {
@@ -719,6 +714,31 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
719 714
         cyw43_thread_enter();
720 715
     }
721 716
 
717
+    return ch;
718
+}
719
+
720
+int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
721
+                 const uint8_t *buff, uint16_t buff_len) {
722
+    cyw43_thread_enter();
723
+
724
+    if (state != TC_READY) {
725
+        cyw43_thread_exit();
726
+        debug("invalid state for write (%d)", state);
727
+        return -1;
728
+    }
729
+
730
+    int srvc = discover_service(service);
731
+    if (srvc < 0) {
732
+        debug("error discovering service (%d)", srvc);
733
+        return srvc;
734
+    }
735
+
736
+    int ch = discover_characteristic(srvc, characteristic);
737
+    if (ch < 0) {
738
+        debug("error discovering characteristic (%d)", ch);
739
+        return ch;
740
+    }
741
+
722 742
     if (buff_len > BLE_MAX_VALUE_LEN) {
723 743
         buff_len = BLE_MAX_VALUE_LEN;
724 744
     }
@@ -768,3 +788,28 @@ int8_t ble_write(const uint8_t *service, const uint8_t *characteristic,
768 788
     cyw43_thread_exit();
769 789
     return ret;
770 790
 }
791
+
792
+int8_t ble_discover(const uint8_t *service, const uint8_t *characteristic) {
793
+    cyw43_thread_enter();
794
+
795
+    if (state != TC_READY) {
796
+        cyw43_thread_exit();
797
+        debug("invalid state for discovery (%d)", state);
798
+        return -1;
799
+    }
800
+
801
+    int srvc = discover_service(service);
802
+    if (srvc < 0) {
803
+        debug("error discovering service (%d)", srvc);
804
+        return srvc;
805
+    }
806
+
807
+    int ch = discover_characteristic(srvc, characteristic);
808
+    if (ch < 0) {
809
+        debug("error discovering characteristic (%d)", ch);
810
+        return ch;
811
+    }
812
+
813
+    cyw43_thread_exit();
814
+    return 0;
815
+}

+ 2
- 2
src/state_crafty.c View File

@@ -28,7 +28,7 @@
28 28
 
29 29
 #include "menu.h"
30 30
 
31
-#define CRAFTY_UPDATE_TIME_MS 1000
31
+#define CRAFTY_UPDATE_TIME_MS 3000
32 32
 
33 33
 static bd_addr_t ble_addr = {0};
34 34
 static bd_addr_type_t ble_type = 0;
@@ -115,8 +115,8 @@ void state_crafty_run(void) {
115 115
     static uint32_t last = 0;
116 116
     uint32_t now = to_ms_since_boot(get_absolute_time());
117 117
     if (((now - last) >= CRAFTY_UPDATE_TIME_MS) || (last == 0)) {
118
-        last = now;
119 118
         menu_run(draw, true);
119
+        last = to_ms_since_boot(get_absolute_time());
120 120
     }
121 121
 
122 122
     if (wait_for_disconnect && !ble_is_connected()) {

+ 37
- 0
src/volcano.c View File

@@ -40,6 +40,43 @@ static uint8_t uuid_base2[16] = {
40 40
     0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
41 41
 };
42 42
 
43
+int8_t volcano_discover_characteristics(void) {
44
+    uuid_base[3] = UUID_WRITE_SRVC;
45
+    int8_t r;
46
+
47
+    uuid_base2[3] = UUID_TARGET_TEMP;
48
+    r = ble_discover(uuid_base, uuid_base2);
49
+    if (r < 0) {
50
+        return r;
51
+    }
52
+
53
+    uuid_base2[3] = UUID_HEATER_ON;
54
+    r = ble_discover(uuid_base, uuid_base2);
55
+    if (r < 0) {
56
+        return r;
57
+    }
58
+
59
+    uuid_base2[3] = UUID_HEATER_OFF;
60
+    r = ble_discover(uuid_base, uuid_base2);
61
+    if (r < 0) {
62
+        return r;
63
+    }
64
+
65
+    uuid_base2[3] = UUID_PUMP_ON;
66
+    r = ble_discover(uuid_base, uuid_base2);
67
+    if (r < 0) {
68
+        return r;
69
+    }
70
+
71
+    uuid_base2[3] = UUID_PUMP_OFF;
72
+    r = ble_discover(uuid_base, uuid_base2);
73
+    if (r < 0) {
74
+        return r;
75
+    }
76
+
77
+    return 0;
78
+}
79
+
43 80
 int16_t volcano_get_current_temp(void) {
44 81
     uuid_base[3] = UUID_CURRENT_TEMP;
45 82
 

+ 6
- 5
src/workflow.c View File

@@ -199,12 +199,13 @@ void wf_start(uint16_t index) {
199 199
     wf_i = index;
200 200
     step = 0;
201 201
 
202
-    // discover characteristics
203
-    volcano_set_pump_state(false);
204
-    volcano_set_heater_state(false);
205
-    volcano_set_target_temp(1850);
206
-
202
+    /*
203
+     * first turn on heater, then do discovery, to save some time.
204
+     * this means we heat for some seconds before changing the setpoint.
205
+     * should not be a problem in practice.
206
+     */
207 207
     volcano_set_heater_state(true);
208
+    volcano_discover_characteristics();
208 209
 
209 210
     do_step();
210 211
 }

Loading…
Cancel
Save