Removed recorder, added websockets instead.

This commit is contained in:
Fabian Schlenz 2019-10-23 05:37:55 +02:00
parent 01c364795f
commit f76354a4d5
6 changed files with 61 additions and 99 deletions

View File

@ -43,9 +43,6 @@
#define FPS 50 #define FPS 50
#define SHOW_TEXT_DELAY 100 #define SHOW_TEXT_DELAY 100
#define RECORDER_ENABLE
#define RECORDER_PORT 2122
#define MONITOR_LOOP_TIMES false #define MONITOR_LOOP_TIMES false
#define MONITOR_LOOP_TIME_THRESHOLD 500 #define MONITOR_LOOP_TIME_THRESHOLD 500
#define MONITOR_LOOP_TIME_COUNT_MAX 10 #define MONITOR_LOOP_TIME_COUNT_MAX 10

View File

@ -1,22 +0,0 @@
#pragma once
#include "my_wifi.h"
#include "config.h"
#include <ESPAsyncTCP.h>
#include <WiFiUdp.h>
#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

4
include/websockets.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void websocket_setup();
void websocket_loop();

View File

@ -18,7 +18,7 @@ lib_deps =
https://github.com/fabianonline/FastLED.git https://github.com/fabianonline/FastLED.git
https://github.com/fabianonline/NTPClient.git https://github.com/fabianonline/NTPClient.git
ESP8266WebServer ESP8266WebServer
ESPAsyncTCP WebSockets
[env:ota] [env:ota]
upload_port = 10.10.2.80 upload_port = 10.10.2.80
@ -27,7 +27,7 @@ platform = espressif8266
board = esp07 board = esp07
framework = arduino framework = arduino
lib_deps = ${extra.lib_deps} lib_deps = ${extra.lib_deps}
lib_ldf_mode = chain+ lib_ldf_mode = deep
build_flags = -Wl,-Teagle.flash.2m512.ld build_flags = -Wl,-Teagle.flash.2m512.ld
[env:local] [env:local]
@ -36,5 +36,5 @@ platform = espressif8266
board = esp07 board = esp07
framework = arduino framework = arduino
lib_deps = ${extra.lib_deps} lib_deps = ${extra.lib_deps}
lib_ldf_mode = chain+ lib_ldf_mode = deep
build_flags = -Wl,-Teagle.flash.2m512.ld build_flags = -Wl,-Teagle.flash.2m512.ld

View File

@ -1,71 +0,0 @@
#include "recorder.h"
#include "my_fastled.h"
#include "functions.h"
#include "effects.h"
#include "Window.h"
#include <WiFiUdp.h>
#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; y<LED_HEIGHT; y++) for(uint8_t x=0; x<LED_WIDTH; x++) {
uint16_t index = XYsafe(x, y);
CRGB pixel = leds[index];
_buffer[2 + (y*LED_WIDTH + x)*3 + 0] = (pixel.r==255 ? 254 : pixel.r);
_buffer[2 + (y*LED_WIDTH + x)*3 + 1] = (pixel.g==255 ? 254 : pixel.g);
_buffer[2 + (y*LED_WIDTH + x)*3 + 2] = (pixel.b==255 ? 254 : pixel.b);
}
_buffer[_buffer_len - 1] = 255;
_udp.beginPacket("10.10.2.1", 13330);
_udp.write(_buffer, _buffer_len);
_udp.endPacket();
_msgid++;
//_client->write(_buffer, _buffer_len);
}
}
#endif

54
src/websockets.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#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; y<LED_HEIGHT; y++) for(uint8_t x=0; x<LED_WIDTH; x++) {
uint16_t index = XYsafe(x, y);
CRGB pixel = leds[index];
_buffer[3 + (y*LED_WIDTH + x)*3 + 0] = (pixel.r==255 ? 254 : pixel.r);
_buffer[3 + (y*LED_WIDTH + x)*3 + 1] = (pixel.g==255 ? 254 : pixel.g);
_buffer[3 + (y*LED_WIDTH + x)*3 + 2] = (pixel.b==255 ? 254 : pixel.b);
}
_buffer[_size - 1] = 255;
ws.broadcastBIN(_buffer, _size);
delete _buffer;
}
void websocket_loop() {
if (ws.connectedClients()>0) {
send_data();
}
ws.loop();
}