Skip to content

Instantly share code, notes, and snippets.

@groundwater
Created December 7, 2013 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groundwater/7850484 to your computer and use it in GitHub Desktop.
Save groundwater/7850484 to your computer and use it in GitHub Desktop.
function httpHandler(request, response) {
server.log(request.body);
local b = request.body
local l1
local l2
if (b.len() > 16){
l1 = b.slice(0, 16)
l2 = b.slice(16, b.len())
}else{
l1 = b.slice(0, b.len())
l2 = ""
}
device.send("msg",[l1, l2]);
response.send(200, "OK");
}
http.onrequest(httpHandler);
class Screen {
port = null;
lines = null;
positions = null;
constructor(_port) {
port = _port;
lines = ["booting...", ""];
positions = [0, 0];
}
function set0(line) {
lines[0] = line;
}
function set1(line) {
lines[1] = line;
}
function clear_screen() {
port.write(0xFE);
port.write(0x01);
}
function cursor_at_line0() {
port.write(0xFE);
port.write(128);
}
function cursor_at_line1() {
port.write(0xFE);
port.write(192);
}
function write_string(string) {
foreach(i, char in string) {
port.write(char);
}
}
function start() {
update_screen();
}
function update_screen() {
imp.wakeup(0.4, update_screen.bindenv(this));
cursor_at_line0();
display_message(0);
cursor_at_line1();
display_message(1);
}
function display_message(idx) {
local message = lines[idx];
local start = positions[idx];
local end = positions[idx] + 16;
if (end > message.len()) {
end = message.len();
}
local string = message.slice(start, end);
for (local i = string.len(); i < 16; i++) {
string = string + " ";
}
write_string(string);
if (message.len() > 16) {
positions[idx]++;
if (positions[idx] > message.len() - 1) {
positions[idx] = 0;
}
}
}
}
// Register with the server
imp.configure("Serial Display", [], []);
// Configure the UART port
local port0 = hardware.uart57
port0.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
// Boot!
server.log("booting!");
// Allocate and start a screen
screen <- Screen(port0);
screen.clear_screen();
screen.start();
led <- hardware.pin9;
led.configure(DIGITAL_OUT_OD);
blinky <- false;
state <- 0
function blink() {
if(!blinky) return;
led.write(state);
imp.wakeup(0.5, blink);
state = 1 - state;
}
function r(line) {
blinky = true;
blink();
screen.set0(line[0]); // Write the first line
screen.set1(line[1]);
};
agent.on("msg", r);
button <- hardware.pin1;
function buttonChanged() {
blinky = false;
led.write(1);
server.log("button!");
screen.set0("OKAY");
screen.set1("");
}
button.configure(DIGITAL_IN_PULLUP, buttonChanged);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment