Browse Source

settings ui improvements

Thomas Buck 11 months ago
parent
commit
c5f54827dd

+ 2
- 0
CMakeLists.txt View File

99
     src/venty.c
99
     src/venty.c
100
     src/state_venty.c
100
     src/state_venty.c
101
     src/state_wifi.c
101
     src/state_wifi.c
102
+    src/state_wifi_edit.c
103
+    src/state_string.c
102
 
104
 
103
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
105
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
104
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
106
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c

+ 2
- 0
include/state.h View File

32
     STATE_VOLCANO_CONF,
32
     STATE_VOLCANO_CONF,
33
     STATE_VENTY,
33
     STATE_VENTY,
34
     STATE_WIFI_NETS,
34
     STATE_WIFI_NETS,
35
+    STATE_WIFI_EDIT,
36
+    STATE_STRING,
35
 
37
 
36
     STATE_INVALID,
38
     STATE_INVALID,
37
 };
39
 };

+ 33
- 0
include/state_string.h View File

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 View File

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 View File

114
     } else {
114
     } else {
115
         debug("invalid config (0x%02X != 0x%02X)", flash_ptr->version, MEM_VERSION);
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
 static void mem_write_flash(void *param) {
138
 static void mem_write_flash(void *param) {

+ 12
- 0
src/state.c View File

29
 #include "state_volcano_conf.h"
29
 #include "state_volcano_conf.h"
30
 #include "state_venty.h"
30
 #include "state_venty.h"
31
 #include "state_wifi.h"
31
 #include "state_wifi.h"
32
+#include "state_wifi_edit.h"
33
+#include "state_string.h"
32
 #include "state.h"
34
 #include "state.h"
33
 
35
 
34
 #define stringify(name) # name
36
 #define stringify(name) # name
102
         .exit = state_wifi_exit,
104
         .exit = state_wifi_exit,
103
         .run = state_wifi_run,
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
         .name = stringify(STATE_INVALID),
117
         .name = stringify(STATE_INVALID),
106
         .enter = NULL,
118
         .enter = NULL,
107
         .exit = NULL,
119
         .exit = NULL,

+ 8
- 4
src/state_edit_workflow.c View File

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

+ 10
- 4
src/state_settings.c View File

30
 #include "state_workflow.h"
30
 #include "state_workflow.h"
31
 #include "state_settings.h"
31
 #include "state_settings.h"
32
 
32
 
33
+static void exit_cb(void) {
34
+    state_switch(STATE_SCAN);
35
+}
36
+
33
 static void enter_cb(int selection) {
37
 static void enter_cb(int selection) {
34
     switch (selection) {
38
     switch (selection) {
35
     case 0:
39
     case 0:
82
         // OTA Update
86
         // OTA Update
83
         picowota_reboot(true);
87
         picowota_reboot(true);
84
         break;
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
 void state_settings_enter(void) {
96
 void state_settings_enter(void) {
121
     ADD_STATIC_ELEMENT("Factory Reset");
125
     ADD_STATIC_ELEMENT("Factory Reset");
122
     ADD_STATIC_ELEMENT("OTA Update");
126
     ADD_STATIC_ELEMENT("OTA Update");
123
 
127
 
128
+    ADD_STATIC_ELEMENT("... go back");
129
+
124
     if (menu->selection < 0) {
130
     if (menu->selection < 0) {
125
         menu->selection = 0;
131
         menu->selection = 0;
126
     }
132
     }

+ 84
- 0
src/state_string.c View File

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 View File

58
 
58
 
59
 static void draw(void) {
59
 static void draw(void) {
60
     static char buff[100];
60
     static char buff[100];
61
-    static size_t pos = 0;
62
 
61
 
63
     if ((val_p == NULL) || (val_len <= 0) || (val_name == NULL)) {
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
     } else {
65
     } else {
67
         if (val_mode == VAL_STEP_INCREMENT) {
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
         } else {
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 View File

16
  * See <http://www.gnu.org/licenses/>.
16
  * See <http://www.gnu.org/licenses/>.
17
  */
17
  */
18
 
18
 
19
+// TODO adding and removing whole networks
20
+
19
 #include <stdio.h>
21
 #include <stdio.h>
20
 #include <string.h>
22
 #include <string.h>
21
 
23
 
23
 #include "mem.h"
25
 #include "mem.h"
24
 #include "menu.h"
26
 #include "menu.h"
25
 #include "state.h"
27
 #include "state.h"
28
+#include "state_wifi_edit.h"
26
 #include "state_wifi.h"
29
 #include "state_wifi.h"
27
 
30
 
31
+static void exit_cb(void) {
32
+    state_switch(STATE_SETTINGS);
33
+}
34
+
28
 static void enter_cb(int selection) {
35
 static void enter_cb(int selection) {
29
     if ((selection >= 0) && (selection < mem_data()->net_count)) {
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
     }
75
     }
67
 }
76
 }
68
 
77
 
69
-static void exit_cb(void) {
70
-    state_switch(STATE_SETTINGS);
71
-}
72
-
73
 void state_wifi_enter(void) {
78
 void state_wifi_enter(void) {
74
     menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
79
     menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
75
 }
80
 }
95
         }
100
         }
96
 
101
 
97
         pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
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 View File

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 View File

35
 static bool edit_mode = false;
35
 static bool edit_mode = false;
36
 static uint32_t auto_connect_time = 0;
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
 static void enter_cb(int selection) {
46
 static void enter_cb(int selection) {
39
     if ((selection >= 0) && (selection < wf_count())) {
47
     if ((selection >= 0) && (selection < wf_count())) {
40
         if (edit_mode) {
48
         if (edit_mode) {
44
             state_volcano_run_index(selection);
52
             state_volcano_run_index(selection);
45
             state_switch(STATE_VOLCANO_RUN);
53
             state_switch(STATE_VOLCANO_RUN);
46
         }
54
         }
55
+    } else {
56
+        exit_cb();
47
     }
57
     }
48
 }
58
 }
49
 
59
 
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
 void state_wf_edit(bool edit) {
80
 void state_wf_edit(bool edit) {
79
     edit_mode = edit;
81
     edit_mode = edit;
80
 }
82
 }
123
                         "'%s' by %s\n", wf_name(i), wf_author(i));
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
 

Loading…
Cancel
Save