|
@@ -27,11 +27,92 @@
|
27
|
27
|
#include "models.h"
|
28
|
28
|
#include "state.h"
|
29
|
29
|
|
|
30
|
+static const int max_lines = 5;
|
|
31
|
+
|
|
32
|
+static int menu_off = 0;
|
|
33
|
+static int menu_selection = -1;
|
|
34
|
+static int menu_length = 0;
|
|
35
|
+
|
|
36
|
+static void text_box(const char *s) {
|
|
37
|
+ static struct text_font font = {
|
|
38
|
+ .fontname = "fixed_10x20",
|
|
39
|
+ .font = NULL,
|
|
40
|
+ };
|
|
41
|
+ if (font.font == NULL) {
|
|
42
|
+ text_prepare_font(&font);
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ struct text_conf text = {
|
|
46
|
+ .text = "",
|
|
47
|
+ .x = 0,
|
|
48
|
+ .y = 50,
|
|
49
|
+ .justify = false,
|
|
50
|
+ .alignment = MF_ALIGN_CENTER,
|
|
51
|
+ .width = 240,
|
|
52
|
+ .height = 240 - 80,
|
|
53
|
+ .margin = 2,
|
|
54
|
+ .fg = RGB_565(0xFF, 0xFF, 0xFF),
|
|
55
|
+ .bg = RGB_565(0x00, 0x00, 0x00),
|
|
56
|
+ .font = &font,
|
|
57
|
+ };
|
|
58
|
+
|
|
59
|
+ // TODO clear background?!
|
|
60
|
+
|
|
61
|
+ text.text = s;
|
|
62
|
+ text_draw(&text);
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+static void state_scan_buttons(enum buttons btn, bool state) {
|
|
66
|
+ if (state && (btn == BTN_LEFT)) {
|
|
67
|
+ // TODO brightness down
|
|
68
|
+ return;
|
|
69
|
+ } else if (state && (btn == BTN_RIGHT)) {
|
|
70
|
+ // TODO brightness up
|
|
71
|
+ return;
|
|
72
|
+ } else if (state && ((btn == BTN_ENTER) || (btn == BTN_A))) {
|
|
73
|
+ // TODO menu_selection
|
|
74
|
+ return;
|
|
75
|
+ } else if ((!state) || ((btn != BTN_UP) && (btn != BTN_DOWN))) {
|
|
76
|
+ return;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ if (state && (btn == BTN_UP)) {
|
|
80
|
+ if (menu_selection < 0) {
|
|
81
|
+ menu_selection = menu_length - 1;
|
|
82
|
+ } else {
|
|
83
|
+ menu_selection -= 1;
|
|
84
|
+ }
|
|
85
|
+ } else if (state && (btn == BTN_DOWN)) {
|
|
86
|
+ if (menu_selection < 0) {
|
|
87
|
+ menu_selection = 0;
|
|
88
|
+ } else {
|
|
89
|
+ menu_selection += 1;
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ if (menu_selection < 0) {
|
|
94
|
+ menu_selection += menu_length;
|
|
95
|
+ }
|
|
96
|
+ if (menu_selection >= menu_length) {
|
|
97
|
+ menu_selection -= menu_length;
|
|
98
|
+ }
|
|
99
|
+ if (menu_selection >= 0) {
|
|
100
|
+ while (menu_selection < menu_off) {
|
|
101
|
+ menu_off -= 1;
|
|
102
|
+ }
|
|
103
|
+ while (menu_selection >= (menu_off + max_lines)) {
|
|
104
|
+ menu_off += 1;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+}
|
|
108
|
+
|
30
|
109
|
void state_scan_enter(void) {
|
|
110
|
+ buttons_callback(state_scan_buttons);
|
31
|
111
|
ble_scan(BLE_SCAN_ON);
|
32
|
112
|
}
|
33
|
113
|
|
34
|
114
|
void state_scan_exit(void) {
|
|
115
|
+ buttons_callback(NULL);
|
35
|
116
|
ble_scan(BLE_SCAN_OFF);
|
36
|
117
|
}
|
37
|
118
|
|
|
@@ -42,13 +123,41 @@ void state_scan_run(void) {
|
42
|
123
|
struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
|
43
|
124
|
int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
|
44
|
125
|
|
45
|
|
- uint pos = 0;
|
|
126
|
+ int pos = 0, devs = 0;
|
46
|
127
|
for (int i = 0; i < n; i++) {
|
47
|
128
|
enum known_devices dev = models_filter_name(results[i].name);
|
48
|
129
|
if (dev == DEV_UNKNOWN) {
|
49
|
130
|
continue;
|
50
|
131
|
}
|
51
|
132
|
|
|
133
|
+ devs++;
|
|
134
|
+
|
|
135
|
+ if (((devs - 1) < menu_off)
|
|
136
|
+ || ((devs - 1 - menu_off) >= max_lines)) {
|
|
137
|
+ continue;
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+#if defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
|
|
141
|
+ if (menu_selection < 0) {
|
|
142
|
+#ifdef MENU_PREFER_VOLCANO
|
|
143
|
+ if (dev == DEV_VOLCANO) {
|
|
144
|
+ menu_selection = devs - 1;
|
|
145
|
+ }
|
|
146
|
+#endif // MENU_PREFER_VOLCANO
|
|
147
|
+#ifdef MENU_PREFER_CRAFTY
|
|
148
|
+ if (dev == DEV_CRAFTY) {
|
|
149
|
+ menu_selection = devs - 1;
|
|
150
|
+ }
|
|
151
|
+#endif // MENU_PREFER_CRAFTY
|
|
152
|
+ }
|
|
153
|
+#endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
|
|
154
|
+
|
|
155
|
+ if ((devs - 1) == menu_selection) {
|
|
156
|
+ pos += snprintf(buff + pos, sizeof(buff) - pos, "> ");
|
|
157
|
+ } else {
|
|
158
|
+ pos += snprintf(buff + pos, sizeof(buff) - pos, " ");
|
|
159
|
+ }
|
|
160
|
+
|
52
|
161
|
if (dev == DEV_VOLCANO) {
|
53
|
162
|
pos += snprintf(buff + pos, sizeof(buff) - pos, "Volcano ");
|
54
|
163
|
} else if (dev == DEV_CRAFTY) {
|
|
@@ -56,42 +165,27 @@ void state_scan_run(void) {
|
56
|
165
|
}
|
57
|
166
|
|
58
|
167
|
char info[32] = "";
|
59
|
|
- models_get_serial(results[i].data, results[i].data_len,
|
60
|
|
- info, sizeof(info));
|
|
168
|
+ if (models_get_serial(results[i].data, results[i].data_len,
|
|
169
|
+ info, sizeof(info)) < 0) {
|
|
170
|
+ strcpy(info, "-error-");
|
|
171
|
+ }
|
61
|
172
|
pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", info);
|
62
|
173
|
}
|
63
|
174
|
|
64
|
|
- if (pos == 0) {
|
65
|
|
- strncpy(buff, "NONE", sizeof(buff));
|
66
|
|
- }
|
|
175
|
+ menu_length = devs;
|
67
|
176
|
|
68
|
|
- if (strncmp(buff, prev_buff, sizeof(buff)) == 0) {
|
69
|
|
- return;
|
|
177
|
+#if !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
|
|
178
|
+ if ((menu_selection < 0) && (menu_length > 0)) {
|
|
179
|
+ menu_selection = 0;
|
70
|
180
|
}
|
71
|
|
- strncpy(prev_buff, buff, sizeof(prev_buff));
|
|
181
|
+#endif // !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
|
72
|
182
|
|
73
|
|
- static struct text_font font = {
|
74
|
|
- .fontname = "fixed_10x20",
|
75
|
|
- .font = NULL,
|
76
|
|
- };
|
77
|
|
- if (font.font == NULL) {
|
78
|
|
- text_prepare_font(&font);
|
|
183
|
+ if (menu_length == 0) {
|
|
184
|
+ strncpy(buff, "NONE", sizeof(buff));
|
79
|
185
|
}
|
80
|
186
|
|
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);
|
|
187
|
+ if (strncmp(buff, prev_buff, sizeof(buff)) != 0) {
|
|
188
|
+ strncpy(prev_buff, buff, sizeof(prev_buff));
|
|
189
|
+ text_box(buff);
|
|
190
|
+ }
|
97
|
191
|
}
|