|
@@ -0,0 +1,112 @@
|
|
1
|
+/*
|
|
2
|
+ * state_wifi.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 "mem.h"
|
|
24
|
+#include "menu.h"
|
|
25
|
+#include "state.h"
|
|
26
|
+#include "state_wifi.h"
|
|
27
|
+
|
|
28
|
+static void enter_cb(int selection) {
|
|
29
|
+ if ((selection >= 0) && (selection < mem_data()->net_count)) {
|
|
30
|
+ //state_volcano_run_index(selection);
|
|
31
|
+ //state_switch(STATE_VOLCANO_RUN);
|
|
32
|
+ }
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+static void wifi_move_down(uint16_t index) {
|
|
36
|
+ if ((index < 1) || (index >= mem_data()->net_count)) {
|
|
37
|
+ return;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ struct net_credentials tmp = mem_data()->net[index - 1];
|
|
41
|
+ mem_data()->net[index - 1] = mem_data()->net[index];
|
|
42
|
+ mem_data()->net[index] = tmp;
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+static void wifi_move_up(uint16_t index) {
|
|
46
|
+ if (index >= (mem_data()->net_count - 1)) {
|
|
47
|
+ return;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ struct net_credentials tmp = mem_data()->net[index + 1];
|
|
51
|
+ mem_data()->net[index + 1] = mem_data()->net[index];
|
|
52
|
+ mem_data()->net[index] = tmp;
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+static void lower_cb(int selection) {
|
|
56
|
+ if ((selection > 0) && (selection < mem_data()->net_count)) {
|
|
57
|
+ wifi_move_down(selection);
|
|
58
|
+ selection--;
|
|
59
|
+ }
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+static void upper_cb(int selection) {
|
|
63
|
+ if ((selection >= 0) && (selection < (mem_data()->net_count - 1))) {
|
|
64
|
+ wifi_move_up(selection);
|
|
65
|
+ selection++;
|
|
66
|
+ }
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+static void exit_cb(void) {
|
|
70
|
+ state_switch(STATE_SETTINGS);
|
|
71
|
+}
|
|
72
|
+
|
|
73
|
+void state_wifi_enter(void) {
|
|
74
|
+ menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+void state_wifi_exit(void) {
|
|
78
|
+ menu_deinit();
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+static void draw(struct menu_state *menu) {
|
|
82
|
+ menu->length = mem_data()->net_count;
|
|
83
|
+
|
|
84
|
+ int pos = 0;
|
|
85
|
+ for (uint16_t i = 0; i < menu->length; i++) {
|
|
86
|
+ if ((i < menu->off)
|
|
87
|
+ || ((i - menu->off) >= MENU_MAX_LINES)) {
|
|
88
|
+ continue;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ if (i == menu->selection) {
|
|
92
|
+ pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
|
|
93
|
+ } else {
|
|
94
|
+ pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
|
|
98
|
+ "%s\n", mem_data()->net[i].name);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ if ((menu->selection < 0) && (menu->length > 0)) {
|
|
102
|
+ menu->selection = 0;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ if (menu->length == 0) {
|
|
106
|
+ strncpy(menu->buff, "NONE", MENU_MAX_LEN);
|
|
107
|
+ }
|
|
108
|
+}
|
|
109
|
+
|
|
110
|
+void state_wifi_run(void) {
|
|
111
|
+ menu_run(draw, false);
|
|
112
|
+}
|