Browse Source

add simple volcano_run state

Thomas Buck 1 year ago
parent
commit
e78ad09f27
11 changed files with 139 additions and 2 deletions
  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 View File

68
     src/workflow.c
68
     src/workflow.c
69
     src/menu.c
69
     src/menu.c
70
     src/state_volcano_workflow.c
70
     src/state_volcano_workflow.c
71
+    src/state_volcano_run.c
71
 
72
 
72
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
73
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
73
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
74
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 2
- 0
include/menu.h View File

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

+ 1
- 0
include/state.h View File

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

+ 31
- 0
include/state_volcano_run.h View File

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 View File

40
 struct wf_state wf_status(void);
40
 struct wf_state wf_status(void);
41
 void wf_start(uint16_t index);
41
 void wf_start(uint16_t index);
42
 
42
 
43
+void wf_reset(void);
43
 void wf_run(void);
44
 void wf_run(void);
44
 
45
 
45
 #endif // __WORKFLOW_H__
46
 #endif // __WORKFLOW_H__

+ 4
- 0
src/menu.c View File

83
     buttons_callback(menu_buttons);
83
     buttons_callback(menu_buttons);
84
 }
84
 }
85
 
85
 
86
+void menu_deinit(void) {
87
+    buttons_callback(NULL);
88
+}
89
+
86
 void menu_run(void (*draw)(struct menu_state *)) {
90
 void menu_run(void (*draw)(struct menu_state *)) {
87
     if (draw) {
91
     if (draw) {
88
         draw(&menu);
92
         draw(&menu);

+ 15
- 0
src/state.c View File

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

+ 3
- 0
src/state_scan.c View File

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

+ 70
- 0
src/state_volcano_run.c View File

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 View File

22
 #include "config.h"
22
 #include "config.h"
23
 #include "menu.h"
23
 #include "menu.h"
24
 #include "workflow.h"
24
 #include "workflow.h"
25
+#include "state.h"
26
+#include "state_volcano_run.h"
25
 #include "state_volcano_workflow.h"
27
 #include "state_volcano_workflow.h"
26
 
28
 
27
 static void enter_cb(int selection) {
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
 void state_volcano_wf_enter(void) {
36
 void state_volcano_wf_enter(void) {
33
 }
38
 }
34
 
39
 
35
 void state_volcano_wf_exit(void) {
40
 void state_volcano_wf_exit(void) {
36
-    // nothing to do
41
+    menu_deinit();
37
 }
42
 }
38
 
43
 
39
 static void draw(struct menu_state *menu) {
44
 static void draw(struct menu_state *menu) {

+ 4
- 0
src/workflow.c View File

240
     do_step();
240
     do_step();
241
 }
241
 }
242
 
242
 
243
+void wf_reset(void) {
244
+    status = WF_IDLE;
245
+}
246
+
243
 void wf_run(void) {
247
 void wf_run(void) {
244
     if (status == WF_IDLE) {
248
     if (status == WF_IDLE) {
245
         return;
249
         return;

Loading…
Cancel
Save