Browse Source

basic state machine for app logic

Thomas Buck 1 year ago
parent
commit
2c330fd03f
6 changed files with 166 additions and 1 deletions
  1. 1
    0
      CMakeLists.txt
  2. 1
    0
      include/ble.h
  3. 30
    0
      include/state.h
  4. 15
    1
      src/ble.c
  5. 9
    0
      src/main.c
  6. 110
    0
      src/state.c

+ 1
- 0
CMakeLists.txt View File

@@ -59,6 +59,7 @@ target_sources(gadget PUBLIC
59 59
     src/lcd.c
60 60
     src/text.c
61 61
     src/image.c
62
+    src/state.c
62 63
 
63 64
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
64 65
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 1
- 0
include/ble.h View File

@@ -41,6 +41,7 @@ struct ble_scan_result {
41 41
 };
42 42
 
43 43
 void ble_init(void);
44
+bool ble_is_ready(void);
44 45
 void ble_scan(enum ble_scan_mode mode);
45 46
 int ble_get_scan_results(struct ble_scan_result *buf, uint len);
46 47
 

+ 30
- 0
include/state.h View File

@@ -0,0 +1,30 @@
1
+/*
2
+ * state.h
3
+ *
4
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#ifndef __STATE_H__
20
+#define __STATE_H__
21
+
22
+enum system_state {
23
+    STATE_INIT = 0,
24
+    STATE_SCAN,
25
+};
26
+
27
+void state_switch(enum system_state next);
28
+void state_run(void);
29
+
30
+#endif // __STATE_H__

+ 15
- 1
src/ble.c View File

@@ -203,9 +203,14 @@ static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa
203 203
 }
204 204
 
205 205
 void ble_init(void) {
206
+    cyw43_thread_enter();
207
+
206 208
     for (uint i = 0; i < BLE_MAX_SCAN_RESULTS; i++) {
207 209
         scans[i].set = false;
208 210
     }
211
+    state = TC_OFF;
212
+
213
+    cyw43_thread_exit();
209 214
 
210 215
     l2cap_init();
211 216
     sm_init();
@@ -219,14 +224,23 @@ void ble_init(void) {
219 224
     hci_power_control(HCI_POWER_ON);
220 225
 }
221 226
 
227
+bool ble_is_ready(void) {
228
+    cyw43_thread_enter();
229
+
230
+    bool v = (state != TC_OFF);
231
+
232
+    cyw43_thread_exit();
233
+    return v;
234
+}
235
+
222 236
 void ble_scan(enum ble_scan_mode mode) {
223 237
     cyw43_thread_enter();
224 238
 
225 239
     switch (mode) {
226 240
     case BLE_SCAN_OFF:
227 241
         debug("stopping BLE scan");
228
-        state = TC_IDLE;
229 242
         gap_stop_scan();
243
+        state = TC_IDLE;
230 244
         break;
231 245
 
232 246
     case BLE_SCAN_ON:

+ 9
- 0
src/main.c View File

@@ -32,6 +32,7 @@
32 32
 #include "lcd.h"
33 33
 #include "text.h"
34 34
 #include "image.h"
35
+#include "state.h"
35 36
 
36 37
 int main(void) {
37 38
     // required for debug console
@@ -75,6 +76,14 @@ int main(void) {
75 76
     debug("init done");
76 77
     lcd_set_backlight(0x8000);
77 78
 
79
+    // wait for BLE stack to be ready before using it
80
+    while (!ble_is_ready()) {
81
+        sleep_ms(10);
82
+    }
83
+
84
+    debug("starting app");
85
+    state_switch(STATE_SCAN);
86
+
78 87
     while (1) {
79 88
         watchdog_update();
80 89
 

+ 110
- 0
src/state.c View File

@@ -0,0 +1,110 @@
1
+/*
2
+ * state.c
3
+ *
4
+ * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#include <stdio.h>
20
+
21
+#include "config.h"
22
+#include "log.h"
23
+#include "buttons.h"
24
+#include "ble.h"
25
+#include "lcd.h"
26
+#include "text.h"
27
+#include "state.h"
28
+
29
+static enum system_state state = STATE_INIT;
30
+
31
+void state_switch(enum system_state next) {
32
+    if (state == next) {
33
+        return;
34
+    }
35
+
36
+    // clean up old state when leaving it
37
+    switch (state) {
38
+    case STATE_SCAN:
39
+        debug("leaving STATE_SCAN");
40
+        ble_scan(BLE_SCAN_OFF);
41
+        break;
42
+
43
+    default:
44
+        break;
45
+    }
46
+
47
+    // prepare new state on entering
48
+    switch (next) {
49
+    case STATE_SCAN:
50
+        debug("entering STATE_SCAN");
51
+        ble_scan(BLE_SCAN_ON);
52
+        break;
53
+
54
+    default:
55
+        break;
56
+    }
57
+
58
+    state = next;
59
+}
60
+
61
+void state_run(void) {
62
+    static struct text_font font = {
63
+        .fontname = "fixed_10x20",
64
+        .font = NULL,
65
+    };
66
+    if (font.font == NULL) {
67
+        text_prepare_font(&font);
68
+    }
69
+
70
+    struct text_conf text = {
71
+        .text = "",
72
+        .x = 0,
73
+        .y = 50,
74
+        .justify = false,
75
+        .alignment = MF_ALIGN_CENTER,
76
+        .width = 240,
77
+        .height = 240 - 80,
78
+        .margin = 2,
79
+        .fg = RGB_565(0xFF, 0xFF, 0xFF),
80
+        .bg = RGB_565(0x00, 0x00, 0x00),
81
+        .font = &font,
82
+    };
83
+
84
+    switch (state) {
85
+    case STATE_SCAN: {
86
+        struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
87
+        int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
88
+        if (n <= 0) {
89
+            text.text = "N\nO\nN\nE";
90
+            text_draw(&text);
91
+        } else {
92
+            char buff[1024] = {0};
93
+            uint pos = 0;
94
+
95
+            for (int i = 0; i < n; i++) {
96
+                pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", results[i].name);
97
+            }
98
+
99
+            text.text = buff;
100
+            text_draw(&text);
101
+        }
102
+        break;
103
+    }
104
+
105
+    default:
106
+        debug("invalid main state %d", state);
107
+        state_switch(STATE_SCAN);
108
+        break;
109
+    }
110
+}

Loading…
Cancel
Save