Adding more server.
This commit is contained in:
@ -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 >$@
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
{{opcodes}}
|
||||
|
||||
#include "SPIFFS.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include "SPIFFS.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
@ -12,7 +15,8 @@
|
||||
#include <sys/select.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
146
ueforth/arduino/arduino_server.fs
Normal file
146
ueforth/arduino/arduino_server.fs
Normal file
@ -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|
|
||||
<!html>
|
||||
<head>
|
||||
<title>esp32forth</title>
|
||||
<style>
|
||||
body {
|
||||
padding: 5px;
|
||||
background-color: #111;
|
||||
color: #2cf;
|
||||
}
|
||||
#prompt {
|
||||
width: 100%;
|
||||
padding: 5px;
|
||||
font-family: monospace;
|
||||
background-color: #ff8;
|
||||
}
|
||||
#output {
|
||||
width: 100%;
|
||||
height: 80%;
|
||||
resize: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<h2>uEforth 1</h2>
|
||||
<link rel="icon" href="data:,">
|
||||
<body>
|
||||
Upload File: <input id="filepick" type="file" name="files[]"></input><br/>
|
||||
<button onclick="ask('hex')">hex</button>
|
||||
<button onclick="ask('decimal')">decimal</button>
|
||||
<button onclick="ask('words')">words</button>
|
||||
<button onclick="ask('$100 init hush')">init</button>
|
||||
<button onclick="ask('ride')">ride</button>
|
||||
<button onclick="ask('blow')">blow</button>
|
||||
<button onclick="ask('$50000 p0')">fore</button>
|
||||
<button onclick="ask('$a0000 p0')">back</button>
|
||||
<button onclick="ask('$10000 p0')">left</button>
|
||||
<button onclick="ask('$40000 p0')">right</button>
|
||||
<button onclick="ask('$90000 p0')">spin</button>
|
||||
<button onclick="ask('0 p0')">stop</button>
|
||||
<button onclick="ask('4 p0s')">LED</button>
|
||||
<button onclick="ask('$24 ADC . $27 ADC . $22 ADC . $23 ADC .')">ADC</button>
|
||||
<br/>
|
||||
<textarea id="output" readonly></textarea>
|
||||
<input id="prompt" type="prompt"></input><br/>
|
||||
<script>
|
||||
var prompt = document.getElementById('prompt');
|
||||
var filepick = document.getElementById('filepick');
|
||||
var output = document.getElementById('output');
|
||||
function httpPost(url, items, callback) {
|
||||
var fd = new FormData();
|
||||
for (k in items) {
|
||||
fd.append(k, items[k]);
|
||||
}
|
||||
var r = new XMLHttpRequest();
|
||||
r.onreadystatechange = function() {
|
||||
if (this.readyState == XMLHttpRequest.DONE) {
|
||||
if (this.status === 200) {
|
||||
callback(this.responseText);
|
||||
} else {
|
||||
callback(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
r.open('POST', url);
|
||||
r.send(fd);
|
||||
}
|
||||
function ask(cmd, callback) {
|
||||
httpPost('/input',
|
||||
{cmd: cmd + '\\n'}, function(data) {
|
||||
if (data !== null) { output.value += data; }
|
||||
output.scrollTop = output.scrollHeight; // Scroll to the bottom
|
||||
if (callback !== undefined) { callback(); }
|
||||
});
|
||||
}
|
||||
prompt.onkeyup = function(event) {
|
||||
if (event.keyCode === 13) {
|
||||
event.preventDefault();
|
||||
ask(prompt.value);
|
||||
prompt.value = '';
|
||||
}
|
||||
};
|
||||
filepick.onchange = function(event) {
|
||||
if (event.target.files.length > 0) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
var parts = e.target.result.split('\\n');
|
||||
function upload() {
|
||||
if (parts.length === 0) { filepick.value = ''; return; }
|
||||
ask(parts.shift(), upload);
|
||||
}
|
||||
upload();
|
||||
}
|
||||
reader.readAsText(event.target.files[0]);
|
||||
}
|
||||
};
|
||||
window.onload = function() {
|
||||
ask('');
|
||||
prompt.focus();
|
||||
};
|
||||
</script>
|
||||
| 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 ;
|
||||
@ -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;
|
||||
|
||||
@ -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');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user