瀏覽代碼

add simple volcano_run state

Thomas Buck 1 年之前
父節點
當前提交
e78ad09f27
共有 11 個文件被更改,包括 139 次插入2 次删除
  1. 1
    0
      CMakeLists.txt
  2. 2
    0
      include/menu.h
  3. 1
    0
      include/state.h
  4. 31
    0
      include/state_volcano_run.h
  5. 1
    0
      include/workflow.h
  6. 4
    0
      src/menu.c
  7. 15
    0
      src/state.c
  8. 3
    0
      src/state_scan.c
  9. 70
    0
      src/state_volcano_run.c
  10. 7
    2
      src/state_volcano_workflow.c
  11. 4
    0
      src/workflow.c

+ 1
- 0
CMakeLists.txt 查看文件

@@ -68,6 +68,7 @@ target_sources(gadget PUBLIC
68 68
     src/workflow.c
69 69
     src/menu.c
70 70
     src/state_volcano_workflow.c
71
+    src/state_volcano_run.c
71 72
 
72 73
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
73 74
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 2
- 0
include/menu.h 查看文件

@@ -30,6 +30,8 @@ struct menu_state {
30 30
 };
31 31
 
32 32
 void menu_init(void (*cb)(int));
33
+void menu_deinit(void);
34
+
33 35
 void menu_run(void (*cb)(struct menu_state *));
34 36
 
35 37
 #endif // __MENU_H__

+ 1
- 0
include/state.h 查看文件

@@ -23,6 +23,7 @@ enum system_state {
23 23
     STATE_INIT = 0,
24 24
     STATE_SCAN,
25 25
     STATE_VOLCANO_WORKFLOW,
26
+    STATE_VOLCANO_RUN,
26 27
 };
27 28
 
28 29
 void state_switch(enum system_state next);

+ 31
- 0
include/state_volcano_run.h 查看文件

@@ -0,0 +1,31 @@
1
+/*
2
+ * state_volcano_run.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_VOLCANO_RUN_H__
20
+#define __STATE_VOLCANO_RUN_H__
21
+
22
+#include <ble.h>
23
+
24
+void state_volcano_run_index(uint16_t index);
25
+void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type);
26
+
27
+void state_volcano_run_enter(void);
28
+void state_volcano_run_exit(void);
29
+void state_volcano_run_run(void);
30
+
31
+#endif // __STATE_VOLCANO_RUN_H__

+ 1
- 0
include/workflow.h 查看文件

@@ -40,6 +40,7 @@ const char *wf_author(uint16_t index);
40 40
 struct wf_state wf_status(void);
41 41
 void wf_start(uint16_t index);
42 42
 
43
+void wf_reset(void);
43 44
 void wf_run(void);
44 45
 
45 46
 #endif // __WORKFLOW_H__

+ 4
- 0
src/menu.c 查看文件

@@ -83,6 +83,10 @@ void menu_init(void (*cb)(int)) {
83 83
     buttons_callback(menu_buttons);
84 84
 }
85 85
 
86
+void menu_deinit(void) {
87
+    buttons_callback(NULL);
88
+}
89
+
86 90
 void menu_run(void (*draw)(struct menu_state *)) {
87 91
     if (draw) {
88 92
         draw(&menu);

+ 15
- 0
src/state.c 查看文件

@@ -20,6 +20,7 @@
20 20
 #include "log.h"
21 21
 #include "state_scan.h"
22 22
 #include "state_volcano_workflow.h"
23
+#include "state_volcano_run.h"
23 24
 #include "state.h"
24 25
 
25 26
 static enum system_state state = STATE_INIT;
@@ -41,6 +42,11 @@ void state_switch(enum system_state next) {
41 42
         state_volcano_wf_exit();
42 43
         break;
43 44
 
45
+    case STATE_VOLCANO_RUN:
46
+        debug("leaving STATE_VOLCANO_RUN");
47
+        state_volcano_run_exit();
48
+        break;
49
+
44 50
     default:
45 51
         break;
46 52
     }
@@ -57,6 +63,11 @@ void state_switch(enum system_state next) {
57 63
         state_volcano_wf_enter();
58 64
         break;
59 65
 
66
+    case STATE_VOLCANO_RUN:
67
+        debug("entering STATE_VOLCANO_RUN");
68
+        state_volcano_run_enter();
69
+        break;
70
+
60 71
     default:
61 72
         break;
62 73
     }
@@ -77,6 +88,10 @@ void state_run(void) {
77 88
         state_volcano_wf_run();
78 89
         break;
79 90
 
91
+    case STATE_VOLCANO_RUN:
92
+        state_volcano_run_run();
93
+        break;
94
+
80 95
     default:
81 96
         debug("invalid main state %d", state);
82 97
         state_switch(STATE_SCAN);

+ 3
- 0
src/state_scan.c 查看文件

@@ -24,6 +24,7 @@
24 24
 #include "models.h"
25 25
 #include "menu.h"
26 26
 #include "state.h"
27
+#include "state_volcano_run.h"
27 28
 #include "state_scan.h"
28 29
 
29 30
 static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
@@ -39,6 +40,7 @@ static void enter_cb(int selection) {
39 40
 
40 41
         if (devs++ == selection) {
41 42
             if (dev == DEV_VOLCANO) {
43
+                state_volcano_run_target(results[i].addr, results[i].type);
42 44
                 state_switch(STATE_VOLCANO_WORKFLOW);
43 45
             }
44 46
             return;
@@ -52,6 +54,7 @@ void state_scan_enter(void) {
52 54
 }
53 55
 
54 56
 void state_scan_exit(void) {
57
+    menu_deinit();
55 58
     ble_scan(BLE_SCAN_OFF);
56 59
 }
57 60
 

+ 70
- 0
src/state_volcano_run.c 查看文件

@@ -0,0 +1,70 @@
1
+/*
2
+ * state_volcano_run.c
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
+#include <stdio.h>
20
+#include <string.h>
21
+
22
+#include "config.h"
23
+#include "workflow.h"
24
+#include "state.h"
25
+#include "state_volcano_run.h"
26
+
27
+#include "menu.h"
28
+
29
+static uint16_t wf_index;
30
+static bd_addr_t ble_addr;
31
+static bd_addr_type_t ble_type;
32
+static bool wait_for_connect = false;
33
+
34
+void state_volcano_run_index(uint16_t index) {
35
+    wf_index = index;
36
+}
37
+
38
+void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
39
+    memcpy(ble_addr, addr, sizeof(bd_addr_t));
40
+    ble_type = type;
41
+}
42
+
43
+void state_volcano_run_enter(void) {
44
+    ble_connect(ble_addr, ble_type);
45
+    wait_for_connect = true;
46
+}
47
+
48
+void state_volcano_run_exit(void) {
49
+    wf_reset();
50
+    ble_disconnect();
51
+}
52
+
53
+static void draw(struct menu_state *menu) {
54
+    struct wf_state state = wf_status();
55
+    snprintf(menu->buff, MENU_MAX_LEN, "%d / %d", state.step, state.count);
56
+}
57
+
58
+void state_volcano_run_run(void) {
59
+    if (wait_for_connect && ble_is_connected()) {
60
+        wait_for_connect = false;
61
+        wf_start(wf_index);
62
+    }
63
+
64
+    menu_run(draw);
65
+
66
+    struct wf_state state = wf_status();
67
+    if (state.status == WF_IDLE) {
68
+        state_switch(STATE_SCAN);
69
+    }
70
+}

+ 7
- 2
src/state_volcano_workflow.c 查看文件

@@ -22,10 +22,15 @@
22 22
 #include "config.h"
23 23
 #include "menu.h"
24 24
 #include "workflow.h"
25
+#include "state.h"
26
+#include "state_volcano_run.h"
25 27
 #include "state_volcano_workflow.h"
26 28
 
27 29
 static void enter_cb(int selection) {
28
-    (void)(selection); // TODO
30
+    if ((selection >= 0) && (selection < wf_count())) {
31
+        state_volcano_run_index(selection);
32
+        state_switch(STATE_VOLCANO_RUN);
33
+    }
29 34
 }
30 35
 
31 36
 void state_volcano_wf_enter(void) {
@@ -33,7 +38,7 @@ void state_volcano_wf_enter(void) {
33 38
 }
34 39
 
35 40
 void state_volcano_wf_exit(void) {
36
-    // nothing to do
41
+    menu_deinit();
37 42
 }
38 43
 
39 44
 static void draw(struct menu_state *menu) {

+ 4
- 0
src/workflow.c 查看文件

@@ -240,6 +240,10 @@ void wf_start(uint16_t index) {
240 240
     do_step();
241 241
 }
242 242
 
243
+void wf_reset(void) {
244
+    status = WF_IDLE;
245
+}
246
+
243 247
 void wf_run(void) {
244 248
     if (status == WF_IDLE) {
245 249
         return;

Loading…
取消
儲存