Thomas Buck 11 месяцев назад
Родитель
Сommit
c5f54827dd
13 измененных файлов: 329 добавлений и 45 удалений
  1. 2
    0
      CMakeLists.txt
  2. 2
    0
      include/state.h
  3. 33
    0
      include/state_string.h
  4. 30
    0
      include/state_wifi_edit.h
  5. 19
    0
      src/mem.c
  6. 12
    0
      src/state.c
  7. 8
    4
      src/state_edit_workflow.c
  8. 10
    4
      src/state_settings.c
  9. 84
    0
      src/state_string.c
  10. 14
    12
      src/state_value.c
  11. 15
    12
      src/state_wifi.c
  12. 87
    0
      src/state_wifi_edit.c
  13. 13
    13
      src/state_workflow.c

+ 2
- 0
CMakeLists.txt Просмотреть файл

@@ -99,6 +99,8 @@ target_sources(gadget PUBLIC
99 99
     src/venty.c
100 100
     src/state_venty.c
101 101
     src/state_wifi.c
102
+    src/state_wifi_edit.c
103
+    src/state_string.c
102 104
 
103 105
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
104 106
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 2
- 0
include/state.h Просмотреть файл

@@ -32,6 +32,8 @@ enum system_state {
32 32
     STATE_VOLCANO_CONF,
33 33
     STATE_VENTY,
34 34
     STATE_WIFI_NETS,
35
+    STATE_WIFI_EDIT,
36
+    STATE_STRING,
35 37
 
36 38
     STATE_INVALID,
37 39
 };

+ 33
- 0
include/state_string.h Просмотреть файл

@@ -0,0 +1,33 @@
1
+/*
2
+ * state_string.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_STRING_H__
20
+#define __STATE_STRING_H__
21
+
22
+#include <sys/types.h>
23
+#include "state.h"
24
+
25
+void state_string_set(char *value, size_t length,
26
+                      const char *name);
27
+void state_string_return(enum system_state state);
28
+
29
+void state_string_enter(void);
30
+void state_string_exit(void);
31
+void state_string_run(void);
32
+
33
+#endif // __STATE_STRING_H__

+ 30
- 0
include/state_wifi_edit.h Просмотреть файл

@@ -0,0 +1,30 @@
1
+/*
2
+ * state_wifi_edit.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_EDIT_H__
20
+#define __STATE_WIFI_EDIT_H__
21
+
22
+#include <stdint.h>
23
+
24
+void state_wifi_edit_index(uint16_t index);
25
+
26
+void state_wifi_edit_enter(void);
27
+void state_wifi_edit_exit(void);
28
+void state_wifi_edit_run(void);
29
+
30
+#endif // __STATE_WIFI_EDIT_H__

+ 19
- 0
src/mem.c Просмотреть файл

@@ -114,6 +114,25 @@ void mem_load(void) {
114 114
     } else {
115 115
         debug("invalid config (0x%02X != 0x%02X)", flash_ptr->version, MEM_VERSION);
116 116
     }
117
+
118
+    // add default WiFi from #define to flash config, if it is not there yet
119
+    bool found = false;
120
+    for (uint16_t i = 0; i < data_ram.data.net_count; i++) {
121
+        if (strcmp(data_ram.data.net[i].name, DEFAULT_WIFI_SSID) == 0) {
122
+            if (strcmp(data_ram.data.net[i].pass, DEFAULT_WIFI_PASS) != 0) {
123
+                debug("warning: restoring wifi password for '%s'", DEFAULT_WIFI_SSID);
124
+                strcpy(data_ram.data.net[i].pass, DEFAULT_WIFI_PASS);
125
+            }
126
+            found = true;
127
+            break;
128
+        }
129
+    }
130
+    if ((!found) && (data_ram.data.net_count < WIFI_MAX_NET_COUNT)) {
131
+        debug("info: adding wifi password for '%s'", DEFAULT_WIFI_SSID);
132
+        strcpy(data_ram.data.net[data_ram.data.net_count].name, DEFAULT_WIFI_SSID);
133
+        strcpy(data_ram.data.net[data_ram.data.net_count].pass, DEFAULT_WIFI_PASS);
134
+        data_ram.data.net_count++;
135
+    }
117 136
 }
118 137
 
119 138
 static void mem_write_flash(void *param) {

+ 12
- 0
src/state.c Просмотреть файл

@@ -29,6 +29,8 @@
29 29
 #include "state_volcano_conf.h"
30 30
 #include "state_venty.h"
31 31
 #include "state_wifi.h"
32
+#include "state_wifi_edit.h"
33
+#include "state_string.h"
32 34
 #include "state.h"
33 35
 
34 36
 #define stringify(name) # name
@@ -102,6 +104,16 @@ static const struct state states[STATE_INVALID + 1] = {
102 104
         .exit = state_wifi_exit,
103 105
         .run = state_wifi_run,
104 106
     }, {
107
+        .name = stringify(STATE_WIFI_EDIT),
108
+        .enter = state_wifi_edit_enter,
109
+        .exit = state_wifi_edit_exit,
110
+        .run = state_wifi_edit_run,
111
+    }, {
112
+        .name = stringify(STATE_STRING),
113
+        .enter = state_string_enter,
114
+        .exit = state_string_exit,
115
+        .run = state_string_run,
116
+    }, {
105 117
         .name = stringify(STATE_INVALID),
106 118
         .enter = NULL,
107 119
         .exit = NULL,

+ 8
- 4
src/state_edit_workflow.c Просмотреть файл

@@ -31,6 +31,10 @@
31 31
 
32 32
 static uint16_t wf_index = 0;
33 33
 
34
+static void exit_cb(void) {
35
+    state_switch(STATE_WORKFLOW);
36
+}
37
+
34 38
 static void enter_cb(int selection) {
35 39
     static char buff[20];
36 40
 
@@ -62,6 +66,8 @@ static void enter_cb(int selection) {
62 66
 
63 67
         state_value_return(STATE_EDIT_WORKFLOW);
64 68
         state_switch(STATE_VALUE);
69
+    } else {
70
+        exit_cb();
65 71
     }
66 72
 }
67 73
 
@@ -77,10 +83,6 @@ static void upper_cb(int selection) {
77 83
     }
78 84
 }
79 85
 
80
-static void exit_cb(void) {
81
-    state_switch(STATE_WORKFLOW);
82
-}
83
-
84 86
 void state_edit_wf_index(uint16_t index) {
85 87
     wf_index = index;
86 88
 }
@@ -114,6 +116,8 @@ static void draw(struct menu_state *menu) {
114 116
                         "% 2d: %s\n", i, wf_step_str(step));
115 117
     }
116 118
 
119
+    ADD_STATIC_ELEMENT("... go back");
120
+
117 121
     if (menu->selection < 0) {
118 122
         menu->selection = 0;
119 123
     }

+ 10
- 4
src/state_settings.c Просмотреть файл

@@ -30,6 +30,10 @@
30 30
 #include "state_workflow.h"
31 31
 #include "state_settings.h"
32 32
 
33
+static void exit_cb(void) {
34
+    state_switch(STATE_SCAN);
35
+}
36
+
33 37
 static void enter_cb(int selection) {
34 38
     switch (selection) {
35 39
     case 0:
@@ -82,11 +86,11 @@ static void enter_cb(int selection) {
82 86
         // OTA Update
83 87
         picowota_reboot(true);
84 88
         break;
85
-    }
86
-}
87 89
 
88
-static void exit_cb(void) {
89
-    state_switch(STATE_SCAN);
90
+    default:
91
+        exit_cb();
92
+        break;
93
+    }
90 94
 }
91 95
 
92 96
 void state_settings_enter(void) {
@@ -121,6 +125,8 @@ static void draw(struct menu_state *menu) {
121 125
     ADD_STATIC_ELEMENT("Factory Reset");
122 126
     ADD_STATIC_ELEMENT("OTA Update");
123 127
 
128
+    ADD_STATIC_ELEMENT("... go back");
129
+
124 130
     if (menu->selection < 0) {
125 131
         menu->selection = 0;
126 132
     }

+ 84
- 0
src/state_string.c Просмотреть файл

@@ -0,0 +1,84 @@
1
+/*
2
+ * state_string.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 "buttons.h"
23
+#include "lcd.h"
24
+#include "menu.h"
25
+#include "text.h"
26
+#include "textbox.h"
27
+#include "state_string.h"
28
+
29
+static char *str_p = NULL;
30
+static size_t str_len = 0;
31
+static const char *str_name = NULL;
32
+static enum system_state str_ret_state = STATE_SCAN;
33
+
34
+void state_string_set(char *value, size_t length,
35
+                      const char *name) {
36
+    str_p = value;
37
+    str_len = length;
38
+    str_name = name;
39
+}
40
+
41
+void state_string_return(enum system_state state) {
42
+    str_ret_state = state;
43
+}
44
+
45
+static void draw(void) {
46
+    static char buff[42];
47
+
48
+    if ((str_p == NULL) || (str_len <= 0) || (str_name == NULL)) {
49
+        snprintf(buff, sizeof(buff), "error");
50
+    } else {
51
+        snprintf(buff, sizeof(buff), "%s:\n\n%s", str_name, str_p);
52
+    }
53
+
54
+    text_box(buff, true,
55
+             "fixed_10x20",
56
+             0, LCD_WIDTH,
57
+             50, MENU_BOX_HEIGHT(MENU_MAX_LINES, 20, 2),
58
+             0);
59
+}
60
+
61
+static void string_buttons(enum buttons btn, bool state) {
62
+    if (state && (btn == BTN_Y)) {
63
+        state_switch(str_ret_state);
64
+    } else if (state && (btn == BTN_LEFT)) {
65
+        // TODO
66
+    } else if (state && (btn == BTN_RIGHT)) {
67
+        // TODO
68
+    } else if (state && (btn == BTN_UP)) {
69
+        // TODO
70
+    } else if (state && (btn == BTN_DOWN)) {
71
+        // TODO
72
+    }
73
+}
74
+
75
+void state_string_enter(void) {
76
+    buttons_callback(string_buttons);
77
+    draw();
78
+}
79
+
80
+void state_string_exit(void) {
81
+    buttons_callback(NULL);
82
+}
83
+
84
+void state_string_run(void) { }

+ 14
- 12
src/state_value.c Просмотреть файл

@@ -58,23 +58,25 @@ void state_value_return(enum system_state state) {
58 58
 
59 59
 static void draw(void) {
60 60
     static char buff[100];
61
-    static size_t pos = 0;
62 61
 
63 62
     if ((val_p == NULL) || (val_len <= 0) || (val_name == NULL)) {
64
-        pos += snprintf(buff, sizeof(buff),
65
-                        "error");
63
+        snprintf(buff, sizeof(buff),
64
+                 "error");
66 65
     } else {
67 66
         if (val_mode == VAL_STEP_INCREMENT) {
68
-            pos += snprintf(buff, sizeof(buff),
69
-                            "%s:\n\n%d -> %d -> %d",
70
-                            val_name, val_min / val_step, val / val_step, val_max / val_step);
67
+            snprintf(buff, sizeof(buff),
68
+                     "%s:\n\n%d -> %d -> %d",
69
+                     val_name,
70
+                     val_min / val_step,
71
+                     val / val_step,
72
+                     val_max / val_step);
71 73
         } else {
72
-            pos += snprintf(buff, sizeof(buff),
73
-                            "%s:\n\n%d -> %d -> %d",
74
-                            val_name,
75
-                            __builtin_ffs(val_min),
76
-                            __builtin_ffs(val),
77
-                            __builtin_ffs(val_max));
74
+            snprintf(buff, sizeof(buff),
75
+                     "%s:\n\n%d -> %d -> %d",
76
+                     val_name,
77
+                     __builtin_ffs(val_min),
78
+                     __builtin_ffs(val),
79
+                     __builtin_ffs(val_max));
78 80
         }
79 81
     }
80 82
 

+ 15
- 12
src/state_wifi.c Просмотреть файл

@@ -16,6 +16,8 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
+// TODO adding and removing whole networks
20
+
19 21
 #include <stdio.h>
20 22
 #include <string.h>
21 23
 
@@ -23,12 +25,19 @@
23 25
 #include "mem.h"
24 26
 #include "menu.h"
25 27
 #include "state.h"
28
+#include "state_wifi_edit.h"
26 29
 #include "state_wifi.h"
27 30
 
31
+static void exit_cb(void) {
32
+    state_switch(STATE_SETTINGS);
33
+}
34
+
28 35
 static void enter_cb(int selection) {
29 36
     if ((selection >= 0) && (selection < mem_data()->net_count)) {
30
-        //state_volcano_run_index(selection);
31
-        //state_switch(STATE_VOLCANO_RUN);
37
+        state_wifi_edit_index(selection);
38
+        state_switch(STATE_WIFI_EDIT);
39
+    } else {
40
+        exit_cb();
32 41
     }
33 42
 }
34 43
 
@@ -66,10 +75,6 @@ static void upper_cb(int selection) {
66 75
     }
67 76
 }
68 77
 
69
-static void exit_cb(void) {
70
-    state_switch(STATE_SETTINGS);
71
-}
72
-
73 78
 void state_wifi_enter(void) {
74 79
     menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
75 80
 }
@@ -95,15 +100,13 @@ static void draw(struct menu_state *menu) {
95 100
         }
96 101
 
97 102
         pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
98
-                        "%s\n", mem_data()->net[i].name);
103
+                        "'%s'\n", mem_data()->net[i].name);
99 104
     }
100 105
 
101
-    if ((menu->selection < 0) && (menu->length > 0)) {
102
-        menu->selection = 0;
103
-    }
106
+    ADD_STATIC_ELEMENT("... go back");
104 107
 
105
-    if (menu->length == 0) {
106
-        strncpy(menu->buff, "NONE", MENU_MAX_LEN);
108
+    if (menu->selection < 0) {
109
+        menu->selection = 0;
107 110
     }
108 111
 }
109 112
 

+ 87
- 0
src/state_wifi_edit.c Просмотреть файл

@@ -0,0 +1,87 @@
1
+/*
2
+ * state_wifi_edit.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 <stddef.h>
20
+#include <stdio.h>
21
+
22
+#include "config.h"
23
+#include "menu.h"
24
+#include "mem.h"
25
+#include "state.h"
26
+#include "state_string.h"
27
+#include "state_wifi_edit.h"
28
+
29
+static uint16_t wifi_index = 0;
30
+
31
+static void exit_cb(void) {
32
+    state_switch(STATE_SETTINGS);
33
+}
34
+
35
+static void enter_cb(int selection) {
36
+    switch (selection) {
37
+    case 0:
38
+        // SSID
39
+        state_string_set(mem_data()->net[wifi_index].name,
40
+                         WIFI_MAX_NAME_LEN, "SSID");
41
+        state_string_return(STATE_WIFI_EDIT);
42
+        state_switch(STATE_STRING);
43
+        break;
44
+
45
+    case 1:
46
+        // Password
47
+        state_string_set(mem_data()->net[wifi_index].pass,
48
+                         WIFI_MAX_NAME_LEN, "Password");
49
+        state_string_return(STATE_WIFI_EDIT);
50
+        state_switch(STATE_STRING);
51
+        break;
52
+
53
+    default:
54
+        exit_cb();
55
+        break;
56
+    }
57
+}
58
+
59
+void state_wifi_edit_index(uint16_t index) {
60
+    wifi_index = index;
61
+}
62
+
63
+void state_wifi_edit_enter(void) {
64
+    menu_init(enter_cb, NULL, NULL, exit_cb);
65
+}
66
+
67
+void state_wifi_edit_exit(void) {
68
+    menu_deinit();
69
+}
70
+
71
+static void draw(struct menu_state *menu) {
72
+    int pos = 0;
73
+    menu->length = 0;
74
+
75
+    ADD_STATIC_ELEMENT("SSID: '%s'", mem_data()->net[wifi_index].name);
76
+    ADD_STATIC_ELEMENT("Pass: '%s'", mem_data()->net[wifi_index].pass);
77
+
78
+    ADD_STATIC_ELEMENT("... go back");
79
+
80
+    if (menu->selection < 0) {
81
+        menu->selection = 0;
82
+    }
83
+}
84
+
85
+void state_wifi_edit_run(void) {
86
+    menu_run(draw, false);
87
+}

+ 13
- 13
src/state_workflow.c Просмотреть файл

@@ -35,6 +35,14 @@
35 35
 static bool edit_mode = false;
36 36
 static uint32_t auto_connect_time = 0;
37 37
 
38
+static void exit_cb(void) {
39
+    if (edit_mode) {
40
+        state_switch(STATE_SETTINGS);
41
+    } else {
42
+        state_switch(STATE_SCAN);
43
+    }
44
+}
45
+
38 46
 static void enter_cb(int selection) {
39 47
     if ((selection >= 0) && (selection < wf_count())) {
40 48
         if (edit_mode) {
@@ -44,6 +52,8 @@ static void enter_cb(int selection) {
44 52
             state_volcano_run_index(selection);
45 53
             state_switch(STATE_VOLCANO_RUN);
46 54
         }
55
+    } else {
56
+        exit_cb();
47 57
     }
48 58
 }
49 59
 
@@ -67,14 +77,6 @@ static void upper_cb(int selection) {
67 77
     }
68 78
 }
69 79
 
70
-static void exit_cb(void) {
71
-    if (edit_mode) {
72
-        state_switch(STATE_SETTINGS);
73
-    } else {
74
-        state_switch(STATE_SCAN);
75
-    }
76
-}
77
-
78 80
 void state_wf_edit(bool edit) {
79 81
     edit_mode = edit;
80 82
 }
@@ -123,12 +125,10 @@ static void draw(struct menu_state *menu) {
123 125
                         "'%s' by %s\n", wf_name(i), wf_author(i));
124 126
     }
125 127
 
126
-    if ((menu->selection < 0) && (menu->length > 0)) {
127
-        menu->selection = 0;
128
-    }
128
+    ADD_STATIC_ELEMENT("... go back");
129 129
 
130
-    if (menu->length == 0) {
131
-        strncpy(menu->buff, "NONE", MENU_MAX_LEN);
130
+    if (menu->selection < 0) {
131
+        menu->selection = 0;
132 132
     }
133 133
 }
134 134
 

Загрузка…
Отмена
Сохранить