diff --git a/nodemcu/http/outputbuff.lua b/nodemcu/http/outputbuff.lua new file mode 100644 index 0000000..5be63dc --- /dev/null +++ b/nodemcu/http/outputbuff.lua @@ -0,0 +1,31 @@ +return function (connection, req, args) + connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") + connection:send('Reprogram') + connection:send('') + connection:send('

Arguments

') + + local form = [===[ +
+ + +
+ ]===] + + if req.method == "GET" then + connection:send(form) + elseif req.method == "POST" then + local rd = req.getRequestData() + -- connection:send(cjson.encode(rd)) + connection:send('

Received the following values:

') + connection:send("\n") + else + connection:send("NOT IMPLEMENTED") + end + + connection:send('') +end diff --git a/nodemcu/http/reprog.lua b/nodemcu/http/reprog.lua new file mode 100644 index 0000000..5be63dc --- /dev/null +++ b/nodemcu/http/reprog.lua @@ -0,0 +1,31 @@ +return function (connection, req, args) + connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") + connection:send('Reprogram') + connection:send('') + connection:send('

Arguments

') + + local form = [===[ +
+ + +
+ ]===] + + if req.method == "GET" then + connection:send(form) + elseif req.method == "POST" then + local rd = req.getRequestData() + -- connection:send(cjson.encode(rd)) + connection:send('

Received the following values:

') + connection:send("\n") + else + connection:send("NOT IMPLEMENTED") + end + + connection:send('') +end diff --git a/nodemcu/http/wifi_settings.lua b/nodemcu/http/wifi_settings.lua index 7426db5..1dc2f2b 100644 --- a/nodemcu/http/wifi_settings.lua +++ b/nodemcu/http/wifi_settings.lua @@ -16,6 +16,20 @@ end connection:send("SSID: "..staConfig.ssid) connection:send("\nPassword: "..staConfig.pwd) + connection:send("\nSTA IP: "..wifi.sta.getip()) + connection:send("\nMAC: "..wifi.sta.getmac()) + connection:send("\nstatus: "..wifi.sta.status()) + connection:send([===[ + + Statuses: + 0: IDLE + 1: CONNECTING + 2: WRONG_PASSWORD + 3: NO AP FOUND + 4: CONNECT FAIL + 5: GOT IP/CONNECTED + + ]===]) connection:send('
') diff --git a/teensy/comms.cpp b/teensy/comms.cpp new file mode 100644 index 0000000..67e7cdd --- /dev/null +++ b/teensy/comms.cpp @@ -0,0 +1,165 @@ +#include +#include "log.h" +#include "comms.h" +#include "lua.hpp" + +#define HWS Serial2 + +#define PACK_OVERHEAD 5 +#define BUFF_SIZE 128 + +uint8_t buff[BUFF_SIZE + 1]; // makes it easier to handle a trailing null + +int maybe_packet(uint8_t *p) { + uint8_t type = p[0]; + + if (type < 32) + return 1; + return 0; +} + +uint16_t packet_length(uint8_t *p) { + if (!maybe_packet(p)) { + return -1; + } + + uint16_t l = p[1] << 8 + p[2]; + return l; +} + +uint16_t crc16(uint8_t *p, int l) { + return 1; // TODO actually make this generate crcs +} + +// takes in a max size, how many bytes are received and how many should be +int read_pack(uint8_t *p, uint16_t max, uint16_t recv_len, uint16_t pack_len) { + if (pack_len == 0) // quick shortcut + return 0; + + if (recv_len < pack_len) { // if we haven't received everything, we should try + if (pack_len > max) { // packet is too large to fit in buffer, return -1 + return -1; // say we can't handle this with the buffer provided + } else { + uint16_t i = pack_len - recv_len; // off by one? + uint16_t q = 0; + while(i > 0) { + if (HWS.available()) { + *p++ = HWS.read(); + i--; + q++; + } else { + delayMicroseconds(1); // short delay + } + } + + return q; // we read some data + } + } + + return 0; // nothing to read +} + +int parse_packet(uint8_t *p, uint16_t recv_len) { + if (maybe_packet(p)) { + uint16_t pack_len = packet_length(p); + + // TODO CRC check on smaller packets + + switch(p[0]) { + case 1: + // otherwise we'll need to load it ourselves into the program buffer + if (recv_len == pack_len) { + p[PACK_OVERHEAD + pack_len + 1] = 0; + lua_start(p + PACK_OVERHEAD); // load it directly from our buffer + } else { + int t = read_pack(l_prog_buff, MAX_PRGMSIZE - 1, 0, pack_len); + + if (t < 0) { // program too big. + // all programs should have no values that are valid packet starts, so we'll just let the rest of the code skip them. might be a bug later TODO + HWS.write(0x03); + HWS.write(0x00); + HWS.write(38); + HWS.write(0); + HWS.write(0); + HWS.print("Program too big, reduce the total size"); + } else { + l_prog_buff[t+1] = 0; // null byte + lua_start(l_prog_buff); + } + } + break; + + case 5: // send the output log + HWS.write(0x06); // send packet header + HWS.write(LOG_RINGSIZE >> 8); + HWS.write(LOG_RINGSIZE & 0xFF); + HWS.write(0); + HWS.write(0); + + char *rb_p = log_curpos+1; + if (rb_p - log_ringbuffer >= LOG_RINGSIZE) + rb_p = log_ringbuffer; + + while(rb_p != log_curpos) { + if (*rb_p) + HWS.write(*rb_p); + rb_p++; + if (rb_p - log_ringbuffer >= LOG_RINGSIZE) + rb_p = log_ringbuffer; + } + + break; + + case 8: + // TODO recv bitmap + break; + + case 10: // KV data for lua + // TODO call lua func for this + break; + case 11: // set brightness. Do not honor currently. + // TODO + break; + + case 9: // req KV data + case 7: // we should never get this, we send request for bitmap + case 6: // we should never get this, we send output log + case 4: // we should never get this, we send PRGMACK + case 3: // we should never get this, we send error + case 2: // we should never get this, we send it + case 0: // null packet, do nothing + default: + return 0; + } + + return 1; + } else { + return 0; + } +} + +void setup_comms() { + HWS.begin(115200); +} + +void loop_comms() { + if (HWS.available() >= 5) { + uint c = 5; + + buff[0] = HWS.read(); + if (!maybe_packet(buff)) + return; // skip this byte, we'll check on the next loop + + uint16_t pack_len = packet_length(buff); + + int len = read_pack(buff + PACK_OVERHEAD, BUFF_SIZE - PACK_OVERHEAD, 0, pack_len); + + // we read the rest of the packet, tell parser + if (len > 0) { + c+ = len; + } + + parse_packet(buff, c); + } +} + diff --git a/teensy/comms.h b/teensy/comms.h new file mode 100644 index 0000000..51aed73 --- /dev/null +++ b/teensy/comms.h @@ -0,0 +1,11 @@ +#ifndef __LUALIGHTS_COMMS_H +#define __LUALIGHTS_COMMS_H + +#include +#include +#include + +void setup_comms(); +void loop_comms(); + +#endif \ No newline at end of file diff --git a/teensy/log.h b/teensy/log.h index 89dd0ee..61c0407 100644 --- a/teensy/log.h +++ b/teensy/log.h @@ -5,7 +5,7 @@ // usb_serial_class LogOut = Serial; #include "HardwareSerial.h" #define LogOut Serial3 -#define LOG_RINGSIZE 4096 +#define LOG_RINGSIZE 1024 extern char log_ringbuffer[LOG_RINGSIZE + 1]; // ring buffer for the output log extern char *log_curpos; diff --git a/teensy/lua-alloc.h b/teensy/lua-alloc.h index 6000d65..09b02d3 100644 --- a/teensy/lua-alloc.h +++ b/teensy/lua-alloc.h @@ -2,7 +2,7 @@ #define __LUALIGHTS_LUA_ALLOC_H // 24K seems to be an ok amount for now. Might need to expand it -#define LL_ARENA_SIZE 24576 +#define LL_ARENA_SIZE 32768 typedef size_t malloc_size_t; diff --git a/teensy/lua.hpp b/teensy/lua.hpp index 58d169a..00f0004 100644 --- a/teensy/lua.hpp +++ b/teensy/lua.hpp @@ -1,7 +1,7 @@ #ifndef __LUALIGHTS_LUA_H #define __LUALIGHTS_LUA_H -#define MAX_PRGMSIZE 8192 +#define MAX_PRGMSIZE 12288 extern char l_prog_buff[MAX_PRGMSIZE]; void l_init();