diff --git a/include/config.sample.h b/include/config.sample.h index 7fcf5c3..ed837c9 100644 --- a/include/config.sample.h +++ b/include/config.sample.h @@ -43,9 +43,6 @@ #define FPS 50 #define SHOW_TEXT_DELAY 100 -#define RECORDER_ENABLE -#define RECORDER_PORT 2122 - #define MONITOR_LOOP_TIMES false #define MONITOR_LOOP_TIME_THRESHOLD 500 #define MONITOR_LOOP_TIME_COUNT_MAX 10 diff --git a/include/recorder.h b/include/recorder.h deleted file mode 100644 index d9f6d3d..0000000 --- a/include/recorder.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include "my_wifi.h" -#include "config.h" -#include -#include - -#ifdef RECORDER_ENABLE -class Recorder { -private: - WiFiUDP _udp; - AsyncServer* _server; - AsyncClient* _client = NULL; - uint16_t _client_port = 0; - size_t _buffer_len; - char* _buffer; - uint16_t _msgid; - boolean _skip_next_frame = false; -public: - Recorder(); - void loop(); -}; -#endif diff --git a/include/websockets.h b/include/websockets.h new file mode 100644 index 0000000..0a971ea --- /dev/null +++ b/include/websockets.h @@ -0,0 +1,4 @@ +#pragma once + +void websocket_setup(); +void websocket_loop(); diff --git a/platformio.ini b/platformio.ini index ed69f8b..1142f2e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,7 +18,7 @@ lib_deps = https://github.com/fabianonline/FastLED.git https://github.com/fabianonline/NTPClient.git ESP8266WebServer - ESPAsyncTCP + WebSockets [env:ota] upload_port = 10.10.2.80 @@ -27,7 +27,7 @@ platform = espressif8266 board = esp07 framework = arduino lib_deps = ${extra.lib_deps} -lib_ldf_mode = chain+ +lib_ldf_mode = deep build_flags = -Wl,-Teagle.flash.2m512.ld [env:local] @@ -36,5 +36,5 @@ platform = espressif8266 board = esp07 framework = arduino lib_deps = ${extra.lib_deps} -lib_ldf_mode = chain+ +lib_ldf_mode = deep build_flags = -Wl,-Teagle.flash.2m512.ld diff --git a/src/recorder.cpp b/src/recorder.cpp deleted file mode 100644 index 88661f5..0000000 --- a/src/recorder.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "recorder.h" -#include "my_fastled.h" -#include "functions.h" -#include "effects.h" -#include "Window.h" -#include - -#ifdef RECORDER_ENABLE - -Recorder::Recorder() { - _buffer_len = LED_WIDTH * LED_HEIGHT * 3 + 3; - _buffer = (char*)malloc(_buffer_len); - _server = new AsyncServer(RECORDER_PORT); - _server->onClient([&](void* a, AsyncClient* c) { - LOGln("Recorder * New client: %s. Waiting for port number...", c->remoteIP().toString().c_str()); - if (_client) { - LOGln("Recorder * Killing old client."); - _client->close(); - _client_port = 0; - delete _client; - } - _client = c; - _msgid = 0; - char dim[3] = {LED_WIDTH, LED_HEIGHT, 255}; - _client->write(dim, 3); - _client->onDisconnect([&](void* a, AsyncClient* client) { - LOGln("Recorder * Client disconnected"); - _client_port = 0; - }, NULL); - _client->onData([&](void* a, AsyncClient* client, void* data, size_t len) { - if (*(char*)data == 'P') { - LOGln("Found."); - if (len >= 3) { - uint8_t* d = (uint8_t*)data; - _client_port = d[1]<<8 | d[2]; - LOGln("Recorder * Sending data to port %d", _client_port); - } - } else if (*(char*)data == 'E') { - String effect = String((char*)data+1); - LOGln("Recorder * Setting effect %s", effect.c_str()); - Window::getFullWindow()->clear(); - change_current_effect(effect); - } - }, NULL); - }, _server); - _server->begin(); - LOGln("Recorder * Listening on port %d", RECORDER_PORT); -} - -void Recorder::loop() { - if (_client && _client_port) { - _skip_next_frame = !_skip_next_frame; - if (_skip_next_frame == false) return; - _buffer[0] = _msgid >> 8; - _buffer[1] = _msgid & 0xFF; - for (uint8_t y=0; ywrite(_buffer, _buffer_len); - } -} -#endif diff --git a/src/websockets.cpp b/src/websockets.cpp new file mode 100644 index 0000000..90a18f4 --- /dev/null +++ b/src/websockets.cpp @@ -0,0 +1,54 @@ +#include +#include +#include "config.h" +#include "websockets.h" +#include "my_fastled.h" +#include "functions.h" +#include "effects.h" + +WebSocketsServer ws = WebSocketsServer(81); + +void ws_event(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { + if (type == WStype_CONNECTED) { + LOGln("Websockets * Client connected."); + } else if (type == WStype_DISCONNECTED) { + LOGln("Websockets * Client disconnected."); + } else if (type == WStype_TEXT) { + String msg = String((char*)payload); + LOGln("Websockets * Received: %s", msg.c_str()); + + if (msg.startsWith("E:")) { + change_current_effect(msg.substring(2)); + } + } +} + +void websocket_setup() { + ws.begin(); + ws.onEvent(ws_event); +} + +void send_data() { + uint16_t _size = LED_WIDTH * LED_HEIGHT * 3 + 4; + uint8_t* _buffer = new uint8_t[_size]; + _buffer[0] = LED_WIDTH; + _buffer[1] = LED_HEIGHT; + _buffer[2] = 255; + for (uint8_t y=0; y0) { + send_data(); + } + ws.loop(); +}