Browse Source

add volcano auto connect

Thomas Buck 6 months ago
parent
commit
9a1b4f229b
12 changed files with 123 additions and 13 deletions
  1. 4
    0
      include/config.h
  2. 2
    1
      include/mem.h
  3. 5
    1
      include/menu.h
  4. 2
    0
      src/buttons.c
  5. 6
    1
      src/console.c
  6. 1
    1
      src/log.c
  7. 14
    2
      src/mem.c
  8. 12
    5
      src/menu.c
  9. 38
    1
      src/state_scan.c
  10. 1
    0
      src/state_volcano_run.c
  11. 36
    1
      src/state_volcano_workflow.c
  12. 2
    0
      src/text.c

+ 4
- 0
include/config.h View File

22
 #define MENU_PREFER_VOLCANO
22
 #define MENU_PREFER_VOLCANO
23
 //#define MENU_PREFER_CRAFTY
23
 //#define MENU_PREFER_CRAFTY
24
 
24
 
25
+#ifdef MENU_PREFER_VOLCANO
26
+#define VOLCANO_AUTO_CONNECT_TIMEOUT_MS 2000
27
+#endif // MENU_PREFER_VOLCANO
28
+
25
 #ifdef NDEBUG
29
 #ifdef NDEBUG
26
 // Release build
30
 // Release build
27
 #define AUTO_MOUNT_MASS_STORAGE
31
 #define AUTO_MOUNT_MASS_STORAGE

+ 2
- 1
include/mem.h View File

26
 
26
 
27
 struct mem_data {
27
 struct mem_data {
28
     uint16_t backlight;
28
     uint16_t backlight;
29
+
29
     uint16_t wf_count;
30
     uint16_t wf_count;
30
     struct workflow wf[WF_MAX_FLOWS];
31
     struct workflow wf[WF_MAX_FLOWS];
31
 } __attribute__((packed));
32
 } __attribute__((packed));
32
 
33
 
33
-
34
 // wf and wf_count are assigned in mem_init()
34
 // wf and wf_count are assigned in mem_init()
35
 #define MEM_DATA_INIT {           \
35
 #define MEM_DATA_INIT {           \
36
     .backlight = (0xFF00 >> 1),   \
36
     .backlight = (0xFF00 >> 1),   \
39
 void mem_init(void);
39
 void mem_init(void);
40
 void mem_write(void);
40
 void mem_write(void);
41
 struct mem_data *mem_data(void);
41
 struct mem_data *mem_data(void);
42
+void mem_load_defaults(void);
42
 
43
 
43
 #endif // __MEM_H__
44
 #endif // __MEM_H__

+ 5
- 1
include/menu.h View File

28
     int off;
28
     int off;
29
     int selection;
29
     int selection;
30
     int length;
30
     int length;
31
-    char *buff;
31
+    char buff[MENU_MAX_LEN];
32
 };
32
 };
33
 
33
 
34
 void menu_init(void (*enter)(int),
34
 void menu_init(void (*enter)(int),
39
 
39
 
40
 void menu_run(void (*cb)(struct menu_state *), bool centered);
40
 void menu_run(void (*cb)(struct menu_state *), bool centered);
41
 
41
 
42
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
43
+extern bool menu_got_input;
44
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
45
+
42
 #endif // __MENU_H__
46
 #endif // __MENU_H__

+ 2
- 0
src/buttons.c View File

19
 #include "pico/stdlib.h"
19
 #include "pico/stdlib.h"
20
 
20
 
21
 #include "config.h"
21
 #include "config.h"
22
+#include "log.h"
22
 #include "buttons.h"
23
 #include "buttons.h"
23
 
24
 
24
 static const uint gpio_num[NUM_BTNS] = {
25
 static const uint gpio_num[NUM_BTNS] = {
69
         if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
70
         if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
70
             if (state != buttons[i].current_state) {
71
             if (state != buttons[i].current_state) {
71
                 buttons[i].current_state = state;
72
                 buttons[i].current_state = state;
73
+                debug("btn %d now %s", i, state ? "pressed" : "released");
72
                 if (callback) {
74
                 if (callback) {
73
                     callback(i, state);
75
                     callback(i, state);
74
                 }
76
                 }

+ 6
- 1
src/console.c View File

40
 #include "main.h"
40
 #include "main.h"
41
 #include "models.h"
41
 #include "models.h"
42
 #include "workflow.h"
42
 #include "workflow.h"
43
-#include "console.h"
44
 #include "crafty.h"
43
 #include "crafty.h"
44
+#include "mem.h"
45
+#include "console.h"
45
 
46
 
46
 #define CNSL_BUFF_SIZE 64
47
 #define CNSL_BUFF_SIZE 64
47
 #define CNSL_REPEAT_MS 500
48
 #define CNSL_REPEAT_MS 500
99
         println("   help - print this message");
100
         println("   help - print this message");
100
         println("  mount - make mass storage medium (un)available");
101
         println("  mount - make mass storage medium (un)available");
101
         println("  power - show Lipo battery status");
102
         println("  power - show Lipo battery status");
103
+        println("   memr - reset flash memory config");
102
         println("");
104
         println("");
103
         println("   scan - start or stop BLE scan");
105
         println("   scan - start or stop BLE scan");
104
         println("scanres - print list of found BLE devices");
106
         println("scanres - print list of found BLE devices");
145
         println("Battery: %.2fV = %.1f%% @ %s",
147
         println("Battery: %.2fV = %.1f%% @ %s",
146
                 volt, lipo_percentage(volt),
148
                 volt, lipo_percentage(volt),
147
                 lipo_charging() ? "charging" : "draining");
149
                 lipo_charging() ? "charging" : "draining");
150
+    } else if (strcmp(line, "memr") == 0) {
151
+        mem_load_defaults();
152
+        mem_write();
148
     } else if (strcmp(line, "scan") == 0) {
153
     } else if (strcmp(line, "scan") == 0) {
149
         ble_scan(BLE_SCAN_TOGGLE);
154
         ble_scan(BLE_SCAN_TOGGLE);
150
     } else if (strcmp(line, "scanres") == 0) {
155
     } else if (strcmp(line, "scanres") == 0) {

+ 1
- 1
src/log.c View File

31
 static uint8_t log_buff[4096] = {0};
31
 static uint8_t log_buff[4096] = {0};
32
 static struct ring_buffer log = RB_INIT(log_buff, sizeof(log_buff));
32
 static struct ring_buffer log = RB_INIT(log_buff, sizeof(log_buff));
33
 
33
 
34
-static uint8_t line_buff[128] = {0};
34
+static uint8_t line_buff[256] = {0};
35
 static volatile bool got_input = false;
35
 static volatile bool got_input = false;
36
 static FIL log_file_fat;
36
 static FIL log_file_fat;
37
 
37
 

+ 14
- 2
src/mem.c View File

77
     return ~c;
77
     return ~c;
78
 }
78
 }
79
 
79
 
80
-void mem_init(void) {
80
+void mem_load_defaults(void) {
81
     data_ram.data.wf_count = wf_default_count;
81
     data_ram.data.wf_count = wf_default_count;
82
+    debug("preparing %d default workflows", data_ram.data.wf_count);
82
     for (uint16_t i = 0; i < wf_default_count; i++) {
83
     for (uint16_t i = 0; i < wf_default_count; i++) {
83
         data_ram.data.wf[i] = wf_default_data[i];
84
         data_ram.data.wf[i] = wf_default_data[i];
85
+        debug("\"%s\" by \"%s\"", data_ram.data.wf[i].name, data_ram.data.wf[i].author);
84
     }
86
     }
87
+}
88
+
89
+void mem_init(void) {
90
+    mem_load_defaults();
85
 
91
 
86
     if (!flash_safe_execute_core_init()) {
92
     if (!flash_safe_execute_core_init()) {
87
         debug("error calling flash_safe_execute_core_init");
93
         debug("error calling flash_safe_execute_core_init");
96
         if (checksum != flash_ptr->checksum) {
102
         if (checksum != flash_ptr->checksum) {
97
             debug("invalid checksum (0x%08lX != 0x%08lX)", flash_ptr->checksum, checksum);
103
             debug("invalid checksum (0x%08lX != 0x%08lX)", flash_ptr->checksum, checksum);
98
         } else {
104
         } else {
105
+            debug("loading from flash (0x%08lX)", checksum);
99
             data_ram = *flash_ptr;
106
             data_ram = *flash_ptr;
100
         }
107
         }
101
     } else {
108
     } else {
102
         debug("invalid config (0x%02X != 0x%02X)", flash_ptr->version, MEM_VERSION);
109
         debug("invalid config (0x%02X != 0x%02X)", flash_ptr->version, MEM_VERSION);
103
     }
110
     }
111
+
112
+    debug("found %d workflows", data_ram.data.wf_count);
113
+    for (uint16_t i = 0; i < data_ram.data.wf_count; i++) {
114
+        debug("\"%s\" by \"%s\"", data_ram.data.wf[i].name, data_ram.data.wf[i].author);
115
+    }
104
 }
116
 }
105
 
117
 
106
 static void mem_write_flash(void *param) {
118
 static void mem_write_flash(void *param) {
118
 
130
 
119
     data_ram.checksum = calc_checksum(&data_ram);
131
     data_ram.checksum = calc_checksum(&data_ram);
120
 
132
 
121
-    debug("writing new data");
133
+    debug("writing new data (0x%08lX)", data_ram.checksum);
122
     int r = flash_safe_execute(mem_write_flash, &data_ram, FLASH_LOCK_TIMEOUT_MS);
134
     int r = flash_safe_execute(mem_write_flash, &data_ram, FLASH_LOCK_TIMEOUT_MS);
123
     if (r != PICO_OK) {
135
     if (r != PICO_OK) {
124
         debug("error calling mem_write_flash: %d", r);
136
         debug("error calling mem_write_flash: %d", r);

+ 12
- 5
src/menu.c View File

26
 #include "menu.h"
26
 #include "menu.h"
27
 
27
 
28
 static char prev_buff[MENU_MAX_LEN] = {0};
28
 static char prev_buff[MENU_MAX_LEN] = {0};
29
-static char buff[MENU_MAX_LEN] = {0};
30
-static struct menu_state menu = { .off = 0, .selection = -1, .length = 0, .buff = buff };
29
+static struct menu_state menu = { .off = 0, .selection = -1, .length = 0, .buff = {0} };
31
 static void (*enter_callback)(int) = NULL;
30
 static void (*enter_callback)(int) = NULL;
32
 static void (*up_callback)(int) = NULL;
31
 static void (*up_callback)(int) = NULL;
33
 static void (*down_callback)(int) = NULL;
32
 static void (*down_callback)(int) = NULL;
34
 static void (*exit_callback)(void) = NULL;
33
 static void (*exit_callback)(void) = NULL;
35
 
34
 
35
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
36
+bool menu_got_input = false;
37
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
38
+
36
 static void menu_buttons(enum buttons btn, bool state) {
39
 static void menu_buttons(enum buttons btn, bool state) {
40
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
41
+    menu_got_input = true;
42
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
43
+
37
     if (state && (btn == BTN_LEFT)) {
44
     if (state && (btn == BTN_LEFT)) {
38
         uint16_t backlight_value = lcd_get_backlight();
45
         uint16_t backlight_value = lcd_get_backlight();
39
         if (backlight_value > 0x00FF) {
46
         if (backlight_value > 0x00FF) {
128
         draw(&menu);
135
         draw(&menu);
129
     }
136
     }
130
 
137
 
131
-    if (strncmp(buff, prev_buff, MENU_MAX_LEN) != 0) {
132
-        strncpy(prev_buff, buff, MENU_MAX_LEN);
133
-        text_box(buff, centered);
138
+    if (strncmp(menu.buff, prev_buff, MENU_MAX_LEN) != 0) {
139
+        strncpy(prev_buff, menu.buff, MENU_MAX_LEN);
140
+        text_box(menu.buff, centered);
134
     }
141
     }
135
 }
142
 }

+ 38
- 1
src/state_scan.c View File

19
 #include <stdio.h>
19
 #include <stdio.h>
20
 #include <string.h>
20
 #include <string.h>
21
 
21
 
22
+#include "pico/stdlib.h"
23
+
22
 #include "config.h"
24
 #include "config.h"
23
 #include "ble.h"
25
 #include "ble.h"
24
 #include "models.h"
26
 #include "models.h"
32
 static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
34
 static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
33
 static int result_count = 0;
35
 static int result_count = 0;
34
 
36
 
37
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
38
+static uint32_t auto_connect_time = 0;
39
+static int auto_connect_idx = 0;
40
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
41
+
35
 static void enter_cb(int selection) {
42
 static void enter_cb(int selection) {
36
     int devs = 0;
43
     int devs = 0;
37
     for (int i = 0; i < result_count; i++) {
44
     for (int i = 0; i < result_count; i++) {
93
 #ifdef MENU_PREFER_VOLCANO
100
 #ifdef MENU_PREFER_VOLCANO
94
             if (dev == DEV_VOLCANO) {
101
             if (dev == DEV_VOLCANO) {
95
                 menu->selection = devs - 1;
102
                 menu->selection = devs - 1;
103
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
104
+                auto_connect_time = to_ms_since_boot(get_absolute_time());
105
+                auto_connect_idx = i;
106
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
96
             }
107
             }
97
 #endif // MENU_PREFER_VOLCANO
108
 #endif // MENU_PREFER_VOLCANO
98
 #ifdef MENU_PREFER_CRAFTY
109
 #ifdef MENU_PREFER_CRAFTY
104
 #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
115
 #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
105
 
116
 
106
         if ((devs - 1) == menu->selection) {
117
         if ((devs - 1) == menu->selection) {
107
-            pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
118
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
119
+            if ((auto_connect_time != 0) && (!menu_got_input)) {
120
+                uint32_t now = to_ms_since_boot(get_absolute_time());
121
+                uint32_t diff = now - auto_connect_time;
122
+                pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
123
+                                "%ld ",
124
+                                (VOLCANO_AUTO_CONNECT_TIMEOUT_MS / 1000) - (diff / 1000));
125
+            } else {
126
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
127
+                pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
128
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
129
+            }
130
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
108
         } else {
131
         } else {
109
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
132
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
110
         }
133
         }
138
 
161
 
139
 void state_scan_run(void) {
162
 void state_scan_run(void) {
140
     menu_run(draw, false);
163
     menu_run(draw, false);
164
+
165
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
166
+    if ((auto_connect_time != 0) && (!menu_got_input)) {
167
+        uint32_t now = to_ms_since_boot(get_absolute_time());
168
+        if ((now - auto_connect_time) >= VOLCANO_AUTO_CONNECT_TIMEOUT_MS) {
169
+            state_volcano_run_target(results[auto_connect_idx].addr,
170
+                                     results[auto_connect_idx].type);
171
+            state_volcano_wf_edit(false);
172
+            state_switch(STATE_VOLCANO_WORKFLOW);
173
+
174
+            auto_connect_time = 0;
175
+        }
176
+    }
177
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
141
 }
178
 }

+ 1
- 0
src/state_volcano_run.c View File

41
 }
41
 }
42
 
42
 
43
 void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
43
 void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
44
+    debug("%s %d", bd_addr_to_str(addr), type);
44
     memcpy(ble_addr, addr, sizeof(bd_addr_t));
45
     memcpy(ble_addr, addr, sizeof(bd_addr_t));
45
     ble_type = type;
46
     ble_type = type;
46
 }
47
 }

+ 36
- 1
src/state_volcano_workflow.c View File

19
 #include <stdio.h>
19
 #include <stdio.h>
20
 #include <string.h>
20
 #include <string.h>
21
 
21
 
22
+#include "pico/stdlib.h"
23
+
22
 #include "config.h"
24
 #include "config.h"
23
 #include "menu.h"
25
 #include "menu.h"
24
 #include "workflow.h"
26
 #include "workflow.h"
29
 
31
 
30
 static bool edit_mode = false;
32
 static bool edit_mode = false;
31
 
33
 
34
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
35
+static uint32_t auto_connect_time = 0;
36
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
37
+
32
 static void enter_cb(int selection) {
38
 static void enter_cb(int selection) {
33
     if ((selection >= 0) && (selection < wf_count())) {
39
     if ((selection >= 0) && (selection < wf_count())) {
34
         if (edit_mode) {
40
         if (edit_mode) {
75
     } else {
81
     } else {
76
         menu_init(enter_cb, NULL, NULL, exit_cb);
82
         menu_init(enter_cb, NULL, NULL, exit_cb);
77
     }
83
     }
84
+
85
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
86
+    auto_connect_time = to_ms_since_boot(get_absolute_time());
87
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
78
 }
88
 }
79
 
89
 
80
 void state_volcano_wf_exit(void) {
90
 void state_volcano_wf_exit(void) {
92
         }
102
         }
93
 
103
 
94
         if (i == menu->selection) {
104
         if (i == menu->selection) {
95
-            pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
105
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
106
+            if ((auto_connect_time != 0) && (!menu_got_input)) {
107
+                uint32_t now = to_ms_since_boot(get_absolute_time());
108
+                uint32_t diff = now - auto_connect_time;
109
+                pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
110
+                                "%ld ",
111
+                                (VOLCANO_AUTO_CONNECT_TIMEOUT_MS / 1000) - (diff / 1000));
112
+            } else {
113
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
114
+                pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
115
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
116
+            }
117
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
96
         } else {
118
         } else {
97
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
119
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
98
         }
120
         }
112
 
134
 
113
 void state_volcano_wf_run(void) {
135
 void state_volcano_wf_run(void) {
114
     menu_run(draw, false);
136
     menu_run(draw, false);
137
+
138
+#ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
139
+    if ((auto_connect_time != 0) && (!menu_got_input)) {
140
+        uint32_t now = to_ms_since_boot(get_absolute_time());
141
+        if ((now - auto_connect_time) >= VOLCANO_AUTO_CONNECT_TIMEOUT_MS) {
142
+            state_volcano_run_index(0);
143
+            state_switch(STATE_VOLCANO_RUN);
144
+
145
+            auto_connect_time = 0;
146
+            menu_got_input = true;
147
+        }
148
+    }
149
+#endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
115
 }
150
 }

+ 2
- 0
src/text.c View File

115
         return;
115
         return;
116
     }
116
     }
117
 
117
 
118
+    debug("searching for \"%s\"", tf->fontname);
119
+
118
     const struct mf_font_s *font = mf_find_font(tf->fontname);
120
     const struct mf_font_s *font = mf_find_font(tf->fontname);
119
     if (!font) {
121
     if (!font) {
120
         debug("No such font: %s", tf->fontname);
122
         debug("No such font: %s", tf->fontname);

Loading…
Cancel
Save