years ago...

This commit is contained in:
Ryan Voots 2021-04-29 21:46:54 -04:00
parent f8088c7b94
commit 835a2e97cb
8 changed files with 255 additions and 3 deletions

View file

@ -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('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Reprogram</title></head>')
connection:send('<body>')
connection:send('<h1>Arguments</h1>')
local form = [===[
<form method="POST">
<textarea name="program"></textarea>
<input type="submit" value="Submit">
</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('<h2>Received the following values:</h2>')
connection:send("<ul>\n")
for name, value in pairs(rd) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
end
connection:send("</ul>\n")
else
connection:send("NOT IMPLEMENTED")
end
connection:send('</body></html>')
end

31
nodemcu/http/reprog.lua Normal file
View file

@ -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('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Reprogram</title></head>')
connection:send('<body>')
connection:send('<h1>Arguments</h1>')
local form = [===[
<form method="POST">
<textarea name="program"></textarea>
<input type="submit" value="Submit">
</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('<h2>Received the following values:</h2>')
connection:send("<ul>\n")
for name, value in pairs(rd) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
end
connection:send("</ul>\n")
else
connection:send("NOT IMPLEMENTED")
end
connection:send('</body></html>')
end

View file

@ -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('</pre><br />')

165
teensy/comms.cpp Normal file
View file

@ -0,0 +1,165 @@
#include <Arduino.h>
#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);
}
}

11
teensy/comms.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef __LUALIGHTS_COMMS_H
#define __LUALIGHTS_COMMS_H
#include <Adafruit_GFX.h>
#include <RGBmatrixPanel.h>
#include <Fonts/TomThumb.h>
void setup_comms();
void loop_comms();
#endif

View file

@ -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;

View file

@ -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;

View file

@ -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();