Browse Source

string editing ui

Thomas Buck 11 months ago
parent
commit
8b5ea7054b
2 changed files with 46 additions and 7 deletions
  1. 1
    1
      src/mem.c
  2. 45
    6
      src/state_string.c

+ 1
- 1
src/mem.c View File

86
         data_ram.data.wf[i] = wf_default_data[i];
86
         data_ram.data.wf[i] = wf_default_data[i];
87
     }
87
     }
88
 
88
 
89
-    // TODO
89
+    // TODO better way to pre-define WiFi credentials
90
     data_ram.data.net_count = 1;
90
     data_ram.data.net_count = 1;
91
     strcpy(data_ram.data.net[0].name, DEFAULT_WIFI_SSID);
91
     strcpy(data_ram.data.net[0].name, DEFAULT_WIFI_SSID);
92
     strcpy(data_ram.data.net[0].pass, DEFAULT_WIFI_PASS);
92
     strcpy(data_ram.data.net[0].pass, DEFAULT_WIFI_PASS);

+ 45
- 6
src/state_string.c View File

17
  */
17
  */
18
 
18
 
19
 #include <stdio.h>
19
 #include <stdio.h>
20
+#include <string.h>
20
 
21
 
21
 #include "config.h"
22
 #include "config.h"
22
 #include "buttons.h"
23
 #include "buttons.h"
30
 static size_t str_len = 0;
31
 static size_t str_len = 0;
31
 static const char *str_name = NULL;
32
 static const char *str_name = NULL;
32
 static enum system_state str_ret_state = STATE_SCAN;
33
 static enum system_state str_ret_state = STATE_SCAN;
34
+static size_t edit = 0, offset = 0;
33
 
35
 
34
 void state_string_set(char *value, size_t length,
36
 void state_string_set(char *value, size_t length,
35
                       const char *name) {
37
                       const char *name) {
48
     if ((str_p == NULL) || (str_len <= 0) || (str_name == NULL)) {
50
     if ((str_p == NULL) || (str_len <= 0) || (str_name == NULL)) {
49
         snprintf(buff, sizeof(buff), "error");
51
         snprintf(buff, sizeof(buff), "error");
50
     } else {
52
     } else {
51
-        snprintf(buff, sizeof(buff), "%s:\n\n%s", str_name, str_p);
53
+        snprintf(buff, sizeof(buff), "%s:\n\n'%s'", str_name, str_p + offset);
52
     }
54
     }
53
 
55
 
54
-    text_box(buff, true,
56
+    text_box(buff, false,
55
              "fixed_10x20",
57
              "fixed_10x20",
56
              0, LCD_WIDTH,
58
              0, LCD_WIDTH,
57
              50, MENU_BOX_HEIGHT(MENU_MAX_LINES, 20, 2),
59
              50, MENU_BOX_HEIGHT(MENU_MAX_LINES, 20, 2),
58
              0);
60
              0);
61
+
62
+    size_t ch = edit - offset + 1;
63
+    lcd_write_rect(ch * 10,
64
+                   50 + 3 * 22,
65
+                   ch * 10 + 10,
66
+                   50 + 3 * 22 + 3,
67
+                   LCD_WHITE);
59
 }
68
 }
60
 
69
 
61
 static void string_buttons(enum buttons btn, bool state) {
70
 static void string_buttons(enum buttons btn, bool state) {
62
     if (state && (btn == BTN_Y)) {
71
     if (state && (btn == BTN_Y)) {
63
         state_switch(str_ret_state);
72
         state_switch(str_ret_state);
64
     } else if (state && (btn == BTN_LEFT)) {
73
     } else if (state && (btn == BTN_LEFT)) {
65
-        // TODO
74
+        if (edit > 0) {
75
+            edit--;
76
+        }
66
     } else if (state && (btn == BTN_RIGHT)) {
77
     } else if (state && (btn == BTN_RIGHT)) {
67
-        // TODO
78
+        if (edit < (str_len - 1)) {
79
+            edit++;
80
+        }
81
+        size_t l = strlen(str_p);
82
+        while (edit >= l) {
83
+            str_p[l++] = ' ';
84
+        }
68
     } else if (state && (btn == BTN_UP)) {
85
     } else if (state && (btn == BTN_UP)) {
69
-        // TODO
86
+        char *c = str_p + edit;
87
+        if ((*c >= ' ') && (*c < '~')) {
88
+            (*c)++;
89
+        }
70
     } else if (state && (btn == BTN_DOWN)) {
90
     } else if (state && (btn == BTN_DOWN)) {
71
-        // TODO
91
+        char *c = str_p + edit;
92
+        if ((*c > ' ') && (*c <= '~')) {
93
+            (*c)--;
94
+        }
95
+    } else if (state && (btn == BTN_B)) {
96
+        char *c = str_p + edit;
97
+        *c = '\0';
98
+    } else {
99
+        return;
100
+    }
101
+
102
+    while (edit < offset) {
103
+        offset -= 1;
72
     }
104
     }
105
+    while (edit >= (offset + (LCD_WIDTH / 10 - 2))) {
106
+        offset += 1;
107
+    }
108
+
109
+    draw();
73
 }
110
 }
74
 
111
 
75
 void state_string_enter(void) {
112
 void state_string_enter(void) {
76
     buttons_callback(string_buttons);
113
     buttons_callback(string_buttons);
114
+    edit = 0;
115
+    offset = 0;
77
     draw();
116
     draw();
78
 }
117
 }
79
 
118
 

Loading…
Cancel
Save