Browse Source

add volcano auto connect

Thomas Buck 11 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,6 +22,10 @@
22 22
 #define MENU_PREFER_VOLCANO
23 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 29
 #ifdef NDEBUG
26 30
 // Release build
27 31
 #define AUTO_MOUNT_MASS_STORAGE

+ 2
- 1
include/mem.h View File

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

+ 5
- 1
include/menu.h View File

@@ -28,7 +28,7 @@ struct menu_state {
28 28
     int off;
29 29
     int selection;
30 30
     int length;
31
-    char *buff;
31
+    char buff[MENU_MAX_LEN];
32 32
 };
33 33
 
34 34
 void menu_init(void (*enter)(int),
@@ -39,4 +39,8 @@ void menu_deinit(void);
39 39
 
40 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 46
 #endif // __MENU_H__

+ 2
- 0
src/buttons.c View File

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

+ 6
- 1
src/console.c View File

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

+ 1
- 1
src/log.c View File

@@ -31,7 +31,7 @@
31 31
 static uint8_t log_buff[4096] = {0};
32 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 35
 static volatile bool got_input = false;
36 36
 static FIL log_file_fat;
37 37
 

+ 14
- 2
src/mem.c View File

@@ -77,11 +77,17 @@ static uint32_t calc_checksum(const struct mem_contents *data) {
77 77
     return ~c;
78 78
 }
79 79
 
80
-void mem_init(void) {
80
+void mem_load_defaults(void) {
81 81
     data_ram.data.wf_count = wf_default_count;
82
+    debug("preparing %d default workflows", data_ram.data.wf_count);
82 83
     for (uint16_t i = 0; i < wf_default_count; i++) {
83 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 92
     if (!flash_safe_execute_core_init()) {
87 93
         debug("error calling flash_safe_execute_core_init");
@@ -96,11 +102,17 @@ void mem_init(void) {
96 102
         if (checksum != flash_ptr->checksum) {
97 103
             debug("invalid checksum (0x%08lX != 0x%08lX)", flash_ptr->checksum, checksum);
98 104
         } else {
105
+            debug("loading from flash (0x%08lX)", checksum);
99 106
             data_ram = *flash_ptr;
100 107
         }
101 108
     } else {
102 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 118
 static void mem_write_flash(void *param) {
@@ -118,7 +130,7 @@ void mem_write(void) {
118 130
 
119 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 134
     int r = flash_safe_execute(mem_write_flash, &data_ram, FLASH_LOCK_TIMEOUT_MS);
123 135
     if (r != PICO_OK) {
124 136
         debug("error calling mem_write_flash: %d", r);

+ 12
- 5
src/menu.c View File

@@ -26,14 +26,21 @@
26 26
 #include "menu.h"
27 27
 
28 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 30
 static void (*enter_callback)(int) = NULL;
32 31
 static void (*up_callback)(int) = NULL;
33 32
 static void (*down_callback)(int) = NULL;
34 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 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 44
     if (state && (btn == BTN_LEFT)) {
38 45
         uint16_t backlight_value = lcd_get_backlight();
39 46
         if (backlight_value > 0x00FF) {
@@ -128,8 +135,8 @@ void menu_run(void (*draw)(struct menu_state *), bool centered) {
128 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,6 +19,8 @@
19 19
 #include <stdio.h>
20 20
 #include <string.h>
21 21
 
22
+#include "pico/stdlib.h"
23
+
22 24
 #include "config.h"
23 25
 #include "ble.h"
24 26
 #include "models.h"
@@ -32,6 +34,11 @@
32 34
 static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
33 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 42
 static void enter_cb(int selection) {
36 43
     int devs = 0;
37 44
     for (int i = 0; i < result_count; i++) {
@@ -93,6 +100,10 @@ static void draw(struct menu_state *menu) {
93 100
 #ifdef MENU_PREFER_VOLCANO
94 101
             if (dev == DEV_VOLCANO) {
95 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 108
 #endif // MENU_PREFER_VOLCANO
98 109
 #ifdef MENU_PREFER_CRAFTY
@@ -104,7 +115,19 @@ static void draw(struct menu_state *menu) {
104 115
 #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
105 116
 
106 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 131
         } else {
109 132
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
110 133
         }
@@ -138,4 +161,18 @@ static void draw(struct menu_state *menu) {
138 161
 
139 162
 void state_scan_run(void) {
140 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,6 +41,7 @@ void state_volcano_run_index(uint16_t index) {
41 41
 }
42 42
 
43 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 45
     memcpy(ble_addr, addr, sizeof(bd_addr_t));
45 46
     ble_type = type;
46 47
 }

+ 36
- 1
src/state_volcano_workflow.c View File

@@ -19,6 +19,8 @@
19 19
 #include <stdio.h>
20 20
 #include <string.h>
21 21
 
22
+#include "pico/stdlib.h"
23
+
22 24
 #include "config.h"
23 25
 #include "menu.h"
24 26
 #include "workflow.h"
@@ -29,6 +31,10 @@
29 31
 
30 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 38
 static void enter_cb(int selection) {
33 39
     if ((selection >= 0) && (selection < wf_count())) {
34 40
         if (edit_mode) {
@@ -75,6 +81,10 @@ void state_volcano_wf_enter(void) {
75 81
     } else {
76 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 90
 void state_volcano_wf_exit(void) {
@@ -92,7 +102,19 @@ static void draw(struct menu_state *menu) {
92 102
         }
93 103
 
94 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 118
         } else {
97 119
             pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "  ");
98 120
         }
@@ -112,4 +134,17 @@ static void draw(struct menu_state *menu) {
112 134
 
113 135
 void state_volcano_wf_run(void) {
114 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,6 +115,8 @@ void text_prepare_font(struct text_font *tf) {
115 115
         return;
116 116
     }
117 117
 
118
+    debug("searching for \"%s\"", tf->fontname);
119
+
118 120
     const struct mf_font_s *font = mf_find_font(tf->fontname);
119 121
     if (!font) {
120 122
         debug("No such font: %s", tf->fontname);

Loading…
Cancel
Save