pitrix/src/recorder.cpp

68 lines
2.0 KiB
C++

#include "recorder.h"
#include "my_fastled.h"
#include "functions.h"
#include "effects.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());
change_current_effect(effect);
}
}, NULL);
}, _server);
_server->begin();
LOGln("Recorder * Listening on port %d", RECORDER_PORT);
}
void Recorder::loop() {
if (_client && _client_port) {
_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