From 2b0b5f198af50641cffc6bf95acda1e2a9e2ffe7 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Fri, 8 Jan 2021 01:21:42 -0800 Subject: [PATCH] Adding more server. --- ueforth/Makefile | 1 + ueforth/arduino/arduino.fs | 6 ++ ueforth/arduino/arduino.template.ino | 63 ++++++++++-- ueforth/arduino/arduino_server.fs | 146 +++++++++++++++++++++++++++ ueforth/common/core.h | 1 + ueforth/common/source_to_string.js | 2 +- 6 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 ueforth/arduino/arduino_server.fs diff --git a/ueforth/Makefile b/ueforth/Makefile index c954dd4..97eee11 100644 --- a/ueforth/Makefile +++ b/ueforth/Makefile @@ -81,6 +81,7 @@ $(GEN)/windows_boot.h: common/source_to_string.js $(WINDOWS_BOOT) | $(GEN) ARDUINO_BOOT = common/boot.fs arduino/arduino.fs \ posix/posix_highlevel.fs common/filetools.fs \ + arduino/arduino_server.fs \ arduino/autoboot.fs $(GEN)/arduino_boot.h: common/source_to_string.js $(ARDUINO_BOOT) | $(GEN) echo "ok" | cat $(ARDUINO_BOOT) - | $< boot >$@ diff --git a/ueforth/arduino/arduino.fs b/ueforth/arduino/arduino.fs index 84e2e8e..63ac531 100644 --- a/ueforth/arduino/arduino.fs +++ b/ueforth/arduino/arduino.fs @@ -15,6 +15,12 @@ : freq ( n n -- ) 1000 * 13 ledcSetup drop ; : tone ( n n -- ) 1000 * ledcWriteTone drop ; +( WiFi Modes ) +0 constant WIFI_MODE_NULL +1 constant WIFI_MODE_STA +2 constant WIFI_MODE_AP +3 constant WIFI_MODE_APSTA + ( Startup Setup ) -1 echo ! 115200 Serial.begin diff --git a/ueforth/arduino/arduino.template.ino b/ueforth/arduino/arduino.template.ino index b735f34..08f9ca4 100644 --- a/ueforth/arduino/arduino.template.ino +++ b/ueforth/arduino/arduino.template.ino @@ -1,8 +1,11 @@ {{opcodes}} -#include "SPIFFS.h" + #include +#include #include +#include +#include "SPIFFS.h" #include #include @@ -12,7 +15,8 @@ #include #if defined(ESP32) -# define HEAP_SIZE (100 * 1024) +//# define HEAP_SIZE (100 * 1024) +# define HEAP_SIZE (50 * 1024) # define STACK_SIZE 512 #elif defined(ESP8266) # define HEAP_SIZE (40 * 1024) @@ -22,7 +26,7 @@ # define STACK_SIZE 32 #endif -#define PUSH(v) (DUP, tos = (v)) +#define PUSH(v) (DUP, tos = (cell_t) (v)) #define PLATFORM_OPCODE_LIST \ /* Allocation and Strings */ \ @@ -86,6 +90,7 @@ X("WiFi.status", WIFI_STATUS, DUP; tos = WiFi.status()) \ X("WiFi.macAddress", WIFI_MAC_ADDRESS, WiFi.macAddress((uint8_t *) tos); DROP) \ X("WiFi.localIP", WIFI_LOCAL_IPS, DUP; tos = FromIP(WiFi.localIP())) \ + X("WiFi.mode", WIFI_MODE, WiFi.mode((wifi_mode_t) tos); DROP) \ /* SPIFFS */ \ X("SPIFFS.begin", SPIFFS_BEGIN, \ tos = SPIFFS.begin(sp[-1], (const char *) *sp, tos); sp -=2) \ @@ -101,9 +106,37 @@ X("WebServer.stop", WEBSERVER_STOP, \ WebServer *ws = (WebServer *) tos; DROP; ws->stop()) \ X("WebServer.on", WEBSERVER_ON, \ - WebServer *ws = (WebServer *) tos; DROP; \ - const char *url = (const char *) tos; DROP; \ - InvokeWebServerOn(ws, url, tos); DROP) \ + InvokeWebServerOn((WebServer *) tos, (const char *) sp[-1], *sp); \ + sp -= 2; DROP) \ + X("WebServer.hasArg", WEBSERVER_HAS_ARG, \ + tos = ((WebServer *) tos)->hasArg((const char *) *sp); DROP) \ + X("WebServer.arg", WEBSERVER_ARG, \ + String v = ((WebServer *) tos)->arg((const char *) *sp); \ +Serial.println(v); \ +Serial.println(v.length()); \ + *sp = (cell_t) v.c_str(); tos = v.length()) \ + X("WebServer.argi", WEBSERVER_ARGI, \ + String v = ((WebServer *) tos)->arg(*sp); \ + *sp = (cell_t) v.c_str(); tos = v.length()) \ + X("WebServer.argName", WEBSERVER_ARG_NAME, \ + String v = ((WebServer *) tos)->argName(*sp); \ + *sp = (cell_t) v.c_str(); tos = v.length()) \ + X("WebServer.args", WEBSERVER_ARGS, tos = ((WebServer *) tos)->args()) \ + X("WebServer.setContentLength", WEBSERVER_SET_CONTENT_LENGTH, \ + ((WebServer *) tos)->setContentLength(*sp); --sp; DROP) \ + X("WebServer.sendHeader", WEBSERVER_SEND_HEADER, \ + ((WebServer *) tos)->sendHeader((const char *) sp[-2], (const char *) sp[-1], *sp); \ + sp -= 3; DROP) \ + X("WebServer.send", WEBSERVER_SEND, \ + ((WebServer *) tos)->send(sp[-2], (const char *) sp[-1], (const char *) *sp); \ + sp -= 3; DROP) \ + X("WebServer.sendContent", WEBSERVER_SEND_CONTENT, \ + WebServerSendContent((WebServer *) tos, (const char *) sp[-1], *sp); \ + sp -= 2; DROP) \ + X("WebServer.method", WEBSERVER_METHOD, \ + tos = (cell_t) ((WebServer *) tos)->method()) \ + X("WebServer.handleClient", WEBSERVER_HANDLE_CLIENT, \ + ((WebServer *) tos)->handleClient(); DROP) \ // TODO: Why doesn't ftruncate exist? // X("RESIZE-FILE", RESIZE_FILE, cell_t fd = tos; DROP; \ @@ -147,10 +180,28 @@ static void InvokeWebServerOn(WebServer *ws, const char *url, cell_t xt) { }); } +static void WebServerSendContent(WebServer *ws, const char *data, cell_t len) { + char buffer[256]; + while (len) { + if (len < sizeof(buffer) - 1) { + memcpy(buffer, data, len); + buffer[len] = 0; + ws->sendContent(buffer); + len = 0; + } else { + memcpy(buffer, data, sizeof(buffer) - 1); + buffer[sizeof(buffer)] = 0; + ws->sendContent(buffer); + len -= (sizeof(buffer) - 1); + } + } +} + void setup() { cell_t *heap = (cell_t *) malloc(HEAP_SIZE); ueforth(0, 0, heap, boot, sizeof(boot)); } void loop() { + ueforth_run(); } diff --git a/ueforth/arduino/arduino_server.fs b/ueforth/arduino/arduino_server.fs new file mode 100644 index 0000000..d175ed7 --- /dev/null +++ b/ueforth/arduino/arduino_server.fs @@ -0,0 +1,146 @@ +( Server Terminal ) +: n. ( n -- ) <# #s #> type ; : ip# dup 255 and n. [char] . emit 256 / ; +: ip. ( n -- ) ip# ip# ip# 255 and . ; +: r| ( -- z ) [char] | parse s>z ; + +r| + + +esp32forth + + +

uEforth 1

+ + +Upload File:
+ + + + + + + + + + + + + + +
+ +
+ +| constant index-html + +variable webserver + +: handle-index + ( ." Handling index.html content length" cr + index-html z>s nip webserver @ WebServer.setContentLength ) + ." Handling index.html content length" cr + 200 z" text/html" index-html webserver @ WebServer.send + ." Done! Handling index.html" cr +; + +: handle-input + ." Handling input" cr + z" cmd" webserver @ WebServer.hasArg if + ." hasarg" cr + z" cmd" webserver @ WebServer.arg + ." Got: " cr dup . + 2dup type cr + ['] evaluate catch drop + 200 z" text/plain" z" nop" webserver @ WebServer.send + else + ." not hasarg" cr + 500 z" text/plain" z" Missing Input" webserver @ WebServer.send + then +; + +: serve + 80 WebServer.new webserver ! + z" /" ['] handle-index webserver @ WebServer.on + z" /input" ['] handle-input webserver @ WebServer.on + webserver @ WebServer.begin + begin + webserver @ WebServer.handleClient + 1 ms + yield + again +; + +: wifi ( z z -- ) WIFI_MODE_STA Wifi.mode WiFi.begin 1000 ms WiFi.localIP ip. ; +: webui ( z z -- ) wifi serve ; diff --git a/ueforth/common/core.h b/ueforth/common/core.h index 9683cff..500d523 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -107,6 +107,7 @@ static cell_t *evaluate1(cell_t *sp) { } else { #if PRINT_ERRORS write(2, (void *) name, len); + write(2, "\n", 1); #endif *++sp = name; *++sp = len; diff --git a/ueforth/common/source_to_string.js b/ueforth/common/source_to_string.js index d5c6bb1..f735615 100755 --- a/ueforth/common/source_to_string.js +++ b/ueforth/common/source_to_string.js @@ -6,7 +6,7 @@ var source = fs.readFileSync(process.stdin.fd).toString(); var name = process.argv[2]; source = source.replace(/["]/g, '\\"'); -source = '" ' + source.split('\n').join(' "\n" ') + ' "'; +source = '"' + source.split('\n').join('\\n"\n"') + '\\n"'; source = source.replace(/["] ["]/g, ''); source = source.replace(/["] [(] ([^)]*)[)] ["]/g, '// $1');