+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanAdd variable mouse speedee76735

master
14.1 KiB416 linesraw
1#include "interpreter.h"
2#include "ADC.h"
3#include "OS.h"
4#include "esp8266.h"
5#include "heap.h"
6#include "io.h"
7#include "launchpad.h"
8#include "littlefs.h"
9#include "mouse.h"
10#include "printf.h"
11#include "std.h"
12#include "timer.h"
13#include <stdint.h>
14
15#define ERROR(...)                                                             \
16    printf(RED "ERROR: " NORMAL __VA_ARGS__);                                  \
17    return;
18
19const size_t COMMAND_BUF_LEN = 128;
20static bool wifi = false;
21
22static bool next_token(char** current, char* token) {
23    while (is_whitespace(**current)) { ++*current; }
24    if (!**current) {
25        return false;
26    }
27    uint8_t tok_idx = 0;
28    do {
29        token[tok_idx++] = *(*current)++;
30    } while (**current && !is_whitespace(**current));
31    token[tok_idx] = '\0';
32    return true;
33}
34
35static char* HELPSTRING =
36    "Available commands:\n\n\r"
37    "led COLOR [on, off, or toggle]\tcontrol the onboard RGB led\n\r"
38    "adc\t\t\t\tread a single sample from the ADC\n\r"
39    "temp\t\t\t\tget the internal system temperature\n\r"
40    "time [get/reset]\t\tOS time helpers\n\n\r"
41
42#ifdef TRACK_JITTER
43    "jitter\t\t\t\tshow periodic task jitter stats\n\r"
44#endif
45    "heap\t\t\t\tshow heap usage information\n\n\r"
46
47    "mount\t\t\t\tmount the sd card\n\r"
48    "unmount\t\t\t\tunmount the sd card\n\r"
49    "format yes really\t\tformat the sd card\n\r"
50    "upload FILENAME\t\t\ttransfer a file over UART\n\r"
51    "exec FILENAME\t\t\tload and run process from file\n\r"
52    "touch FILENAME\t\t\tcreates a new file\n\r"
53    "cat FILENAME\t\t\tdisplay the contents of a file\n\r"
54    "append FILENAME WORD\t\tappend a word to a file\n\r"
55    "ls\t\t\t\tlist the files in the directory\n\r"
56    "mv FILENAME NEWNAME\t\tmove a file\n\r"
57    "cp FILENAME NEWNAME\t\tcopy a file\n\r"
58    "rm FILENAME\t\t\tdelete a file\n\r"
59    "checksum FILENAME\t\tcompute a checksum of a file\n\n\r"
60
61    "connect [SSID PASS]\t\tconnect to a wifi network.\n\r"
62    "server\t\t\t\tspawn remote interpreter\n\r"
63    "client IPV4\t\t\tspawn remote client\n\r"
64    "mouse_server\t\t\tspawn remote mouse server\n\r"
65    "mouse_client IPV4\t\tspawn remote mouse controller\n\r"
66    "mouse\t\t\t\tcontrol the mouse locally\n\r"
67    "exit\t\t\t\tleave this session\n\r";
68
69static char* MOUSEHELP =
70    "\n\nMouse Controls:\n\r"
71    "q w e      These keys all make the mouse move a straight direction\n\r"
72    "a s d      Holding shift allows you to make slight adjustments to\n\r"
73    "z x c      the cursor position. s stops this movement.\n\n\r"
74
75    ", . and / act as the left, middle, and right mouse buttons\n\r"
76    "normally they perform a single click, but holding shift (so < > and ?)\n\r"
77    "makes them toggle holding the button.\n\n\r"
78
79    "+ and - increase and decrease the speed of continuous movements\n\n\r"
80
81    "o makes the mouse start circling (press again to stop)\n\n\r"
82
83    "Space makes the cursor move to the center of the screen,\n\r"
84    "stops all mouse movement, and releases all mouse buttons\n\r";
85
86static uint32_t server_id = 0;
87static void server(void) {
88    server_id = OS_Id();
89    if (!ESP8266_StartServer(23, 600)) { // port 23, 5min timeout
90        ERROR("Server failure");
91        OS_Kill();
92    }
93    puts("Server started");
94    while (true) {
95        ESP8266_WaitForConnection();
96        puts("Connected");
97        interpreter(true);
98        ESP8266_CloseTCPConnection();
99    }
100}
101
102static void mouse_server(void) {
103    server_id = OS_Id();
104    if (!ESP8266_StartServer(23, 600)) {
105        ERROR("Server failure");
106        OS_Kill();
107    }
108    puts("Server started");
109    ESP8266_WaitForConnection();
110    puts("Connected");
111
112    mouse_init();
113    char msg[] = "   ";
114    ESP8266_Receive(msg, 3);
115    while (mouse_cmd(msg[0])) { ESP8266_Receive(msg, 3); }
116    ESP8266_CloseTCPConnection();
117    OS_Kill();
118}
119
120static char* temp_ip;
121static void client(void) {
122    if (!ESP8266_MakeTCPConnection(temp_ip, 23)) {
123        puts("Client failure");
124        OS_Kill();
125    }
126    free(temp_ip);
127    puts("Client started");
128
129    char* raw_command = malloc(COMMAND_BUF_LEN);
130    while (true) {
131        ESP8266_ReceiveEcho();
132        int len = readline(raw_command, COMMAND_BUF_LEN - 1);
133        raw_command[len] = '\n';
134        raw_command[len + 1] = '\0';
135        ESP8266_Send(raw_command);
136    }
137}
138
139void interpret_command(char* raw_command, char* token, bool remote) {
140    char* current = raw_command;
141    if (!next_token(&current, token)) {
142        ERROR("enter a command\n\r");
143    } else if (streq(token, "help") || streq(token, "h")) {
144        puts(HELPSTRING);
145    } else if (streq(token, "led")) {
146        uint8_t led;
147        if (!next_token(&current, token)) {
148            ERROR("expected another argument for color\n\r");
149        } else if (streq(token, "red") || streq(token, "r")) {
150            led = RED_LED;
151        } else if (streq(token, "blue") || streq(token, "b")) {
152            led = BLUE_LED;
153        } else if (streq(token, "green") || streq(token, "g")) {
154            led = GREEN_LED;
155        } else {
156            ERROR("invalid color '%s'\n\rTry red, green, or blue\n\r", token);
157        }
158        if (!next_token(&current, token) || streq(token, "toggle") ||
159            streq(token, "t")) {
160            led_toggle(led);
161        } else if (streq(token, "on") || streq(token, "1")) {
162            led_write(led, true);
163        } else if (streq(token, "off") || streq(token, "0")) {
164            led_write(led, false);
165        } else {
166            ERROR("invalid action '%s'\n\rTry on, off, or toggle\n\r", token);
167        }
168    } else if (streq(token, "adc")) {
169        printf("ADC reading: %d\n\r", adc_in());
170    } else if (streq(token, "temp")) {
171        printf("%d degrees F\n\r", (int32_t)temperature());
172    } else if (streq(token, "jitter")) {
173        OS_ReportJitter();
174    } else if (streq(token, "heap")) {
175        heap_stats();
176    } else if (streq(token, "time")) {
177        if (!next_token(&current, token) || streq(token, "get")) {
178            printf("Current time: %dms\n\r", (uint32_t)to_ms(OS_Time()));
179        } else if (streq(token, "reset")) {
180            puts("OS time reset");
181            OS_ClearTime();
182        } else {
183            ERROR("expected 'get' or 'reset', got '%s'\n\r", token);
184        }
185    } else if (streq(token, "mount")) {
186        if (!littlefs_init() || !littlefs_mount()) {
187            ERROR("mount failed");
188        }
189    } else if (streq(token, "unmount")) {
190        littlefs_unmount();
191    } else if (streq(token, "format")) {
192        if (next_token(&current, token) && streq(token, "yes") &&
193            next_token(&current, token) && streq(token, "really")) {
194            if (!littlefs_format()) {
195                ERROR("formatting failed\n\r");
196            }
197        } else {
198            ERROR("You need to indicate that you're REALLY sure\n\r");
199        }
200    } else if (streq(token, "ls")) {
201        littlefs_ls();
202    } else if (streq(token, "touch")) {
203        if (!next_token(&current, token)) {
204            ERROR("must pass a filename\n\r");
205        } else if (!(littlefs_open_file(token, true) &&
206                     littlefs_close_file())) {
207            ERROR("couldn't create file\n\r");
208        }
209    } else if (streq(token, "cat")) {
210        if (!next_token(&current, token)) {
211            ERROR("must pass a filename\n\r");
212        } else if (!littlefs_open_file(token, false)) {
213            ERROR("couldn't open file '%s'\n\r", token);
214        }
215        char temp;
216        while (littlefs_read((uint8_t*)&temp)) { putchar(temp); }
217        printf("\n\r");
218        littlefs_close_file();
219    } else if (streq(token, "append")) {
220        if (!next_token(&current, token)) {
221            ERROR("must pass a filename\n\r");
222        } else if (!littlefs_open_file_append(token, true)) {
223            ERROR("couldn't open file\n\r", token);
224        } else if (!next_token(&current, token)) {
225            littlefs_close_file();
226            ERROR("must pass some characters to append\n\r");
227        }
228        uint8_t len = strlen(token);
229        bool ret = littlefs_write_buffer(token, len);
230        littlefs_close_file();
231        if (!ret) {
232            ERROR("failed to write to the file\n\r");
233        }
234        printf("Wrote %d bytes\n\r", len);
235    } else if (streq(token, "rm")) {
236        if (!next_token(&current, token)) {
237            ERROR("must pass a filename\n\r");
238        } else if (!littlefs_remove(token)) {
239            ERROR("couldn't remove file '%s'\n\r", token);
240        }
241    } else if (streq(token, "mv")) {
242        if (!next_token(&current, token)) {
243            ERROR("must pass a filename\n\r");
244        }
245        char filename[32];
246        memcpy(filename, token, sizeof(filename));
247        if (!next_token(&current, token)) {
248            ERROR("must pass another filename\n\r");
249        } else if (!littlefs_move(filename, token)) {
250            ERROR("failed to move file\n\r");
251        }
252    } else if (streq(token, "cp")) {
253        // TODO
254        ERROR("unimplemented\n\r");
255    } else if (streq(token, "exec")) {
256        if (!next_token(&current, token)) {
257            ERROR("must pass a filename\n\r");
258        }
259        OS_LoadProgram(token);
260        OS_Sleep(ms(1000));
261    } else if (streq(token, "upload")) {
262        if (!next_token(&current, token)) {
263            ERROR("must pass a filename\n\r");
264        }
265        if (!littlefs_open_file(token, true)) {
266            ERROR("failed to open file\n\r");
267        }
268        puts("Are you using the file transfer utility? [Y/n]");
269        bool file_transfer = !(getchar() == 'n');
270        bool binary = false;
271        if (file_transfer) {
272            puts("Is the file you're transferring binary? [y/N]");
273            binary = getchar() == 'y';
274        }
275        char sizebuf[16];
276        if (file_transfer) {
277            puts("Close this session and run the file transfer utility...");
278            uart_change_speed(9600);
279        } else {
280            printf("Enter your file's size in bytes: ");
281        }
282        readline(sizebuf, sizeof(sizebuf));
283        uint32_t size = atoi(sizebuf);
284        if (!file_transfer) {
285            puts("Now paste the contents of your file...");
286        }
287        while (size--) {
288            char temp = getchar();
289            if (!littlefs_write(temp)) {
290                littlefs_close_file();
291                ERROR("failed to write to the file\n\r");
292                if (file_transfer) {
293                    __asm("BKPT");
294                }
295            }
296            // translate to CRLF line endings
297            if (!binary && (temp == '\r' || temp == '\n')) {
298                littlefs_write(temp == '\n' ? '\r' : '\n');
299            }
300        }
301        if (file_transfer) {
302            uart_change_speed(115200);
303        }
304        puts("Successfully Uploaded!");
305        littlefs_close_file();
306    } else if (streq(token, "checksum")) {
307        if (!next_token(&current, token)) {
308            ERROR("must pass a filename\n\r");
309        } else if (!littlefs_open_file(token, false)) {
310            ERROR("couldn't open file '%s'\n\r", token);
311        }
312        char temp;
313        uint32_t checksum = 0;
314        while (littlefs_read((uint8_t*)&temp)) { checksum += temp; }
315        printf("0x%08x\n\r", checksum);
316        littlefs_close_file();
317    } else if (streq(token, "connect")) {
318        if (next_token(&current, token)) {
319            ERROR("Unimplimented\n\r");
320        } else if (wifi) {
321            ERROR("Already connected\n\r");
322        }
323        if (!ESP8266_Init(false, false)) {
324            ERROR("No Wifi adapter\n\r");
325        }
326        ESP8266_GetVersionNumber();
327        if (!ESP8266_Connect(true)) {
328            ERROR("No Wifi network\n\r");
329        }
330        wifi = true;
331        puts("Wifi connected");
332    } else if (streq(token, "server")) {
333        if (server_id) {
334            ERROR("Server already running\n\r");
335        } else if (!wifi) {
336            ERROR("Connect to a wifi network\n\r");
337        }
338        OS_AddThread(server, "Remote Intepreter", 2048, 2);
339    } else if (streq(token, "client")) {
340        if (!next_token(&current, token)) {
341            ERROR("Must pass an IPV4 address\n\r");
342        } else if (!wifi) {
343            ERROR("Connect to a wifi network\n\r");
344        }
345        temp_ip = calloc(strlen(token) + 1);
346        strcpy(temp_ip, token);
347        OS_AddThread(client, "Remote Client", 2048, 1);
348    } else if (streq(token, "exit")) {
349        free(token);
350        free(raw_command);
351        OS_Kill();
352    } else if (streq(token, "mouse_server")) {
353        if (!wifi) {
354            ERROR("Connect to a wifi network\n\r");
355        }
356        OS_AddThread(mouse_server, "mouse_server", 2048, 2);
357    } else if (streq(token, "mouse_client")) {
358        if (!next_token(&current, token)) {
359            ERROR("must pass an IPV4 address\n\r");
360        } else if (!wifi) {
361            ERROR("Connect to a wifi network\n\r");
362        }
363        temp_ip = calloc(strlen(token) + 1);
364        strcpy(temp_ip, token);
365        if (!ESP8266_MakeTCPConnection(temp_ip, 23)) {
366            ERROR("Client failure");
367        }
368        puts("Client started");
369        free(temp_ip);
370        puts(MOUSEHELP);
371        char msg[2] = " ";
372        while (true) {
373            char c = getchar();
374            msg[0] = c;
375            ESP8266_Send(msg);
376            if (c == 127)
377                break;
378        }
379    } else if (streq(token, "mouse")) {
380        mouse_init();
381        puts(MOUSEHELP);
382        char c;
383        while ((c = getchar()) != 127) { mouse_cmd(c); }
384    } else {
385        ERROR("unrecognized command '%s', try 'help'\n\r", token);
386    }
387}
388
389void interpreter(bool remote) {
390    char* token = malloc(32);
391    char* raw_command = malloc(COMMAND_BUF_LEN);
392    if (remote) {
393        OS_RedirectOutput(ESP);
394    }
395    printf("\x1b[1;1H\x1b[2JPress Enter to begin...\x03");
396    if (remote) {
397        ESP8266_Receive(raw_command, COMMAND_BUF_LEN);
398    } else {
399        readline(raw_command, COMMAND_BUF_LEN);
400    }
401    puts(HELPSTRING);
402    if (littlefs_init() && littlefs_mount()) {
403        puts(GREEN "Filesystem is mounted" NORMAL);
404    } else {
405        puts(RED "Filesystem failed to mount" NORMAL);
406    }
407    while (true) {
408        printf("\n\r\xF0\x9F\x8D\x8D> \x03");
409        if (remote) {
410            ESP8266_Receive(raw_command, COMMAND_BUF_LEN);
411        } else {
412            readline(raw_command, COMMAND_BUF_LEN);
413        }
414        interpret_command(raw_command, token, true);
415    }
416}