2019-12-08 16:16:33 +01:00
|
|
|
#include <Arduino.h>
|
2019-11-10 19:50:40 +01:00
|
|
|
#include <Ethernet.h>
|
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
#include "src/WebSocketsServer.h"
|
|
|
|
#include "src/image.hpp"
|
2019-12-13 19:24:04 +01:00
|
|
|
#include "src/panel.hpp"
|
2019-12-08 16:16:33 +01:00
|
|
|
#include "src/ProtocolDL.hpp"
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
#define USE_SERIAL Serial
|
2019-12-06 23:58:05 +01:00
|
|
|
|
2019-11-10 19:50:40 +01:00
|
|
|
byte mac[] = { 0xBE, 0xB7, 0x5C, 0x30, 0xC3, 0x04 };
|
|
|
|
IPAddress ip(10, 23, 42, 24);
|
|
|
|
IPAddress router(10, 23, 42, 1);
|
|
|
|
IPAddress subnet(255, 255, 254, 0);
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
|
|
|
|
|
|
|
Image image;
|
2019-12-14 00:40:23 +01:00
|
|
|
Panel panel1(22, 24, 23, 0 * PANEL_WIDTH, 0 * PANEL_HEIGHT); //data, clock, load
|
|
|
|
Panel panel2(28, 29, 31, 1 * PANEL_WIDTH, 0 * PANEL_HEIGHT);
|
2019-12-08 16:16:33 +01:00
|
|
|
ProtocolDL protocol = ProtocolDL(image);
|
|
|
|
|
|
|
|
unsigned long last_activity = 0;
|
|
|
|
|
|
|
|
bool someOneIsConnected = false;
|
|
|
|
|
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
|
|
|
static bool in_header = true;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case WStype_DISCONNECTED:
|
|
|
|
USE_SERIAL.print("[");
|
|
|
|
USE_SERIAL.print(num);
|
|
|
|
USE_SERIAL.println("] Disconnected!");
|
|
|
|
someOneIsConnected = false;
|
|
|
|
break;
|
|
|
|
case WStype_CONNECTED:
|
|
|
|
{
|
|
|
|
//IPAddress ip = webSocket.remoteIP(num);
|
|
|
|
USE_SERIAL.print("[");
|
|
|
|
USE_SERIAL.print(num);
|
|
|
|
USE_SERIAL.print("] Connected ");
|
|
|
|
USE_SERIAL.print(" url: ");
|
|
|
|
//USE_SERIAL.println(payload);
|
|
|
|
|
|
|
|
// send message to client
|
|
|
|
webSocket.sendTXT(num, "Connected");
|
|
|
|
someOneIsConnected = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WStype_TEXT:
|
|
|
|
USE_SERIAL.print("[");
|
|
|
|
USE_SERIAL.print(num);
|
|
|
|
USE_SERIAL.print("] get Text: ");
|
|
|
|
//USE_SERIAL.println(payload);
|
|
|
|
|
|
|
|
// send message to client
|
|
|
|
// webSocket.sendTXT(num, "message here");
|
|
|
|
|
|
|
|
// send data to all connected clients
|
|
|
|
// webSocket.broadcastTXT("message here");
|
|
|
|
break;
|
|
|
|
case WStype_BIN:
|
|
|
|
USE_SERIAL.print("[");
|
|
|
|
USE_SERIAL.print(num);
|
|
|
|
USE_SERIAL.print("] get binary length: ");
|
|
|
|
USE_SERIAL.println(length);
|
|
|
|
|
|
|
|
for(uint16_t i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
protocol.newByte(payload[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(protocol.isComplete())
|
|
|
|
{
|
|
|
|
Serial.println("complete");
|
2019-12-13 23:10:20 +01:00
|
|
|
panel1.send_image(&image);
|
|
|
|
panel2.send_image(&image);
|
2019-12-08 16:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
}
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
void setup() {
|
|
|
|
// USE_SERIAL.begin(921600);
|
|
|
|
USE_SERIAL.begin(115200);
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
//Serial.setDebugOutput(true);
|
|
|
|
//USE_SERIAL.setDebugOutput(true);
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
Ethernet.init(10);
|
|
|
|
Ethernet.begin(mac, ip, router, router, subnet);
|
2019-11-10 19:50:40 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
USE_SERIAL.println();
|
|
|
|
USE_SERIAL.println();
|
|
|
|
USE_SERIAL.println();
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
for(uint8_t t = 4; t > 0; t--) {
|
|
|
|
USE_SERIAL.print("[SETUP] BOOT WAIT ");
|
|
|
|
USE_SERIAL.print(t);
|
|
|
|
USE_SERIAL.println("...");
|
|
|
|
USE_SERIAL.flush();
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
webSocket.begin();
|
|
|
|
webSocket.onEvent(webSocketEvent);
|
|
|
|
|
2019-12-13 23:10:20 +01:00
|
|
|
panel1.init();
|
|
|
|
panel2.init();
|
2019-12-06 23:58:05 +01:00
|
|
|
|
2019-12-13 19:24:04 +01:00
|
|
|
Serial.println("setup done");
|
2019-11-10 14:25:30 +01:00
|
|
|
}
|
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-11-10 19:50:40 +01:00
|
|
|
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00...
|
|
|
|
// Width Height Delay Pixel
|
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
void default_image(Image* p) {
|
2019-11-10 14:25:30 +01:00
|
|
|
static int offset = 0;
|
|
|
|
|
2019-12-06 23:58:05 +01:00
|
|
|
// reset image to maximum size
|
2019-12-08 16:16:33 +01:00
|
|
|
p->set_size(32767, 32767);
|
2019-12-06 23:58:05 +01:00
|
|
|
|
|
|
|
// toggle all pixels in tilted bars
|
2019-12-08 16:16:33 +01:00
|
|
|
int dim = max(p->getWidth(), p->getHeight());
|
2019-11-10 19:50:40 +01:00
|
|
|
for (int n = 0; n < dim; n++) {
|
2019-12-08 16:16:33 +01:00
|
|
|
int x = (n + offset) % p->getWidth();
|
|
|
|
int y = n % p->getHeight();
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
byte pixel = p->get_pixel(x, y);
|
|
|
|
p->set_pixel(x, y, !pixel);
|
2019-11-10 14:25:30 +01:00
|
|
|
}
|
|
|
|
offset++;
|
2019-11-10 19:50:40 +01:00
|
|
|
}
|
2019-11-10 14:25:30 +01:00
|
|
|
|
2019-11-10 19:50:40 +01:00
|
|
|
void loop() {
|
2019-12-08 16:16:33 +01:00
|
|
|
webSocket.loop();
|
2019-11-10 19:50:40 +01:00
|
|
|
|
2019-12-08 16:16:33 +01:00
|
|
|
if (someOneIsConnected == false) {
|
|
|
|
default_image(&image);
|
2019-12-13 23:10:20 +01:00
|
|
|
panel1.send_image(&image);
|
|
|
|
panel2.send_image(&image);
|
2019-11-10 19:50:40 +01:00
|
|
|
}
|
2019-11-10 14:25:30 +01:00
|
|
|
}
|