瀏覽代碼

only show relevant devices in scan state

Thomas Buck 1 年之前
父節點
當前提交
0769496400
共有 5 個檔案被更改,包括 130 行新增48 行删除
  1. 1
    0
      CMakeLists.txt
  2. 26
    0
      include/state_scan.h
  3. 1
    1
      src/models.c
  4. 5
    47
      src/state.c
  5. 97
    0
      src/state_scan.c

+ 1
- 0
CMakeLists.txt 查看文件

@@ -64,6 +64,7 @@ target_sources(gadget PUBLIC
64 64
     src/serial.c
65 65
     src/ring.c
66 66
     src/models.c
67
+    src/state_scan.c
67 68
 
68 69
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
69 70
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 26
- 0
include/state_scan.h 查看文件

@@ -0,0 +1,26 @@
1
+/*
2
+ * state_scan.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_SCAN_H__
20
+#define __STATE_SCAN_H__
21
+
22
+void state_scan_enter(void);
23
+void state_scan_exit(void);
24
+void state_scan_run(void);
25
+
26
+#endif // __STATE_SCAN_H__

+ 1
- 1
src/models.c 查看文件

@@ -30,7 +30,7 @@ enum known_devices models_filter_name(const char *name) {
30 30
     } else if (strcmp(name, "S&B VOLCANO H") == 0) {
31 31
         return DEV_VOLCANO;
32 32
     } else if (strcmp(name, "STORZ&BICKEL") == 0) {
33
-        return DEV_VOLCANO;
33
+        return DEV_CRAFTY;
34 34
     } else {
35 35
         return DEV_UNKNOWN;
36 36
     }

+ 5
- 47
src/state.c 查看文件

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * state.c
3 3
  *
4
- * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
4
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
5 5
  *
6 6
  * This program is free software: you can redistribute it and/or modify
7 7
  * it under the terms of the GNU General Public License as published by
@@ -16,14 +16,9 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
-#include <stdio.h>
20
-
21 19
 #include "config.h"
22 20
 #include "log.h"
23
-#include "buttons.h"
24
-#include "ble.h"
25
-#include "lcd.h"
26
-#include "text.h"
21
+#include "state_scan.h"
27 22
 #include "state.h"
28 23
 
29 24
 static enum system_state state = STATE_INIT;
@@ -37,7 +32,7 @@ void state_switch(enum system_state next) {
37 32
     switch (state) {
38 33
     case STATE_SCAN:
39 34
         debug("leaving STATE_SCAN");
40
-        ble_scan(BLE_SCAN_OFF);
35
+        state_scan_exit();
41 36
         break;
42 37
 
43 38
     default:
@@ -48,7 +43,7 @@ void state_switch(enum system_state next) {
48 43
     switch (next) {
49 44
     case STATE_SCAN:
50 45
         debug("entering STATE_SCAN");
51
-        ble_scan(BLE_SCAN_ON);
46
+        state_scan_enter();
52 47
         break;
53 48
 
54 49
     default:
@@ -67,49 +62,12 @@ void state_run(void) {
67 62
     }
68 63
     last_heartbeat = now;
69 64
 
70
-    static struct text_font font = {
71
-        .fontname = "fixed_10x20",
72
-        .font = NULL,
73
-    };
74
-    if (font.font == NULL) {
75
-        text_prepare_font(&font);
76
-    }
77
-
78
-    struct text_conf text = {
79
-        .text = "",
80
-        .x = 0,
81
-        .y = 50,
82
-        .justify = false,
83
-        .alignment = MF_ALIGN_CENTER,
84
-        .width = 240,
85
-        .height = 240 - 80,
86
-        .margin = 2,
87
-        .fg = RGB_565(0xFF, 0xFF, 0xFF),
88
-        .bg = RGB_565(0x00, 0x00, 0x00),
89
-        .font = &font,
90
-    };
91
-
92 65
     switch (state) {
93 66
     case STATE_INIT:
94 67
         break;
95 68
 
96 69
     case STATE_SCAN: {
97
-        struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
98
-        int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
99
-        if (n <= 0) {
100
-            text.text = "N\nO\nN\nE";
101
-            text_draw(&text);
102
-        } else {
103
-            char buff[1024] = {0};
104
-            uint pos = 0;
105
-
106
-            for (int i = 0; i < n; i++) {
107
-                pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", results[i].name);
108
-            }
109
-
110
-            text.text = buff;
111
-            text_draw(&text);
112
-        }
70
+        state_scan_run();
113 71
         break;
114 72
     }
115 73
 

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

@@ -0,0 +1,97 @@
1
+/*
2
+ * state_scan.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
+
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 "models.h"
28
+#include "state.h"
29
+
30
+void state_scan_enter(void) {
31
+    ble_scan(BLE_SCAN_ON);
32
+}
33
+
34
+void state_scan_exit(void) {
35
+    ble_scan(BLE_SCAN_OFF);
36
+}
37
+
38
+void state_scan_run(void) {
39
+    static char prev_buff[512] = {0};
40
+    char buff[512] = {0};
41
+
42
+    struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
43
+    int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
44
+
45
+    uint pos = 0;
46
+    for (int i = 0; i < n; i++) {
47
+        enum known_devices dev = models_filter_name(results[i].name);
48
+        if (dev == DEV_UNKNOWN) {
49
+            continue;
50
+        }
51
+
52
+        if (dev == DEV_VOLCANO) {
53
+            pos += snprintf(buff + pos, sizeof(buff) - pos, "Volcano ");
54
+        } else if (dev == DEV_CRAFTY) {
55
+            pos += snprintf(buff + pos, sizeof(buff) - pos, "Crafty+ ");
56
+        }
57
+
58
+        char info[32] = "";
59
+        models_get_serial(results[i].data, results[i].data_len,
60
+                          info, sizeof(info));
61
+        pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", info);
62
+    }
63
+
64
+    if (pos == 0) {
65
+        strncpy(buff, "NONE", sizeof(buff));
66
+    }
67
+
68
+    if (strncmp(buff, prev_buff, sizeof(buff)) == 0) {
69
+        return;
70
+    }
71
+    strncpy(prev_buff, buff, sizeof(prev_buff));
72
+
73
+    static struct text_font font = {
74
+        .fontname = "fixed_10x20",
75
+        .font = NULL,
76
+    };
77
+    if (font.font == NULL) {
78
+        text_prepare_font(&font);
79
+    }
80
+
81
+    struct text_conf text = {
82
+        .text = "",
83
+        .x = 0,
84
+        .y = 50,
85
+        .justify = false,
86
+        .alignment = MF_ALIGN_CENTER,
87
+        .width = 240,
88
+        .height = 240 - 80,
89
+        .margin = 2,
90
+        .fg = RGB_565(0xFF, 0xFF, 0xFF),
91
+        .bg = RGB_565(0x00, 0x00, 0x00),
92
+        .font = &font,
93
+    };
94
+
95
+    text.text = buff;
96
+    text_draw(&text);
97
+}

Loading…
取消
儲存