Browse Source

add state for wifi settings

Thomas Buck 1 year ago
parent
commit
919d147804
6 changed files with 153 additions and 1 deletions
  1. 1
    0
      CMakeLists.txt
  2. 1
    0
      include/state.h
  3. 26
    0
      include/state_wifi.h
  4. 6
    0
      src/state.c
  5. 7
    1
      src/state_settings.c
  6. 112
    0
      src/state_wifi.c

+ 1
- 0
CMakeLists.txt View File

@@ -98,6 +98,7 @@ target_sources(gadget PUBLIC
98 98
     src/wifi.c
99 99
     src/venty.c
100 100
     src/state_venty.c
101
+    src/state_wifi.c
101 102
 
102 103
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
103 104
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 1
- 0
include/state.h View File

@@ -31,6 +31,7 @@ enum system_state {
31 31
     STATE_VALUE,
32 32
     STATE_VOLCANO_CONF,
33 33
     STATE_VENTY,
34
+    STATE_WIFI_NETS,
34 35
 
35 36
     STATE_INVALID,
36 37
 };

+ 26
- 0
include/state_wifi.h View File

@@ -0,0 +1,26 @@
1
+/*
2
+ * state_wifi.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_WIFI_H__
20
+#define __STATE_WIFI_H__
21
+
22
+void state_wifi_enter(void);
23
+void state_wifi_exit(void);
24
+void state_wifi_run(void);
25
+
26
+#endif // __STATE_WIFI_H__

+ 6
- 0
src/state.c View File

@@ -28,6 +28,7 @@
28 28
 #include "state_value.h"
29 29
 #include "state_volcano_conf.h"
30 30
 #include "state_venty.h"
31
+#include "state_wifi.h"
31 32
 #include "state.h"
32 33
 
33 34
 #define stringify(name) # name
@@ -96,6 +97,11 @@ static const struct state states[STATE_INVALID + 1] = {
96 97
         .exit = state_venty_exit,
97 98
         .run = state_venty_run,
98 99
     }, {
100
+        .name = stringify(STATE_WIFI_NETS),
101
+        .enter = state_wifi_enter,
102
+        .exit = state_wifi_exit,
103
+        .run = state_wifi_run,
104
+    }, {
99 105
         .name = stringify(STATE_INVALID),
100 106
         .enter = NULL,
101 107
         .exit = NULL,

+ 7
- 1
src/state_settings.c View File

@@ -69,11 +69,16 @@ static void enter_cb(int selection) {
69 69
         break;
70 70
 
71 71
     case 4:
72
+        // WiFi Networks
73
+        state_switch(STATE_WIFI_NETS);
74
+        break;
75
+
76
+    case 5:
72 77
         // Factory Reset
73 78
         mem_load_defaults();
74 79
         break;
75 80
 
76
-    case 5:
81
+    case 6:
77 82
         // OTA Update
78 83
         picowota_reboot(true);
79 84
         break;
@@ -112,6 +117,7 @@ static void draw(struct menu_state *menu) {
112 117
     ADD_STATIC_ELEMENT("Brightness (%d)", __builtin_ffs(mem_data()->backlight));
113 118
     ADD_STATIC_ELEMENT("Edit Workflows");
114 119
     ADD_STATIC_ELEMENT("Enable WiFi (%d)", mem_data()->enable_wifi);
120
+    ADD_STATIC_ELEMENT("WiFi Networks");
115 121
     ADD_STATIC_ELEMENT("Factory Reset");
116 122
     ADD_STATIC_ELEMENT("OTA Update");
117 123
 

+ 112
- 0
src/state_wifi.c View File

@@ -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
+}

Loading…
Cancel
Save