esp32-sk6812/animation_test/src/Fader.cpp
Thomas Kolb f28de024d8 Added animation test framework
This framework provides the same animation interface as the ESP
firmware, but sends commands to the ESP via UDP. Therefore animations
can be tested without re-flashing the firmware for every tiny tuning
change.
2019-12-24 17:32:15 +01:00

116 lines
2.6 KiB
C++

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <errno.h>
#include <string.h>
#include "Fader.h"
Fader::Fader(std::size_t nstrips, std::size_t nmodules_per_strip)
: m_strips(nstrips),
m_modulesPerStrip(nmodules_per_strip)
{
m_fd = socket(AF_INET, SOCK_DGRAM, 0);
}
void Fader::set_color(uint32_t strip, uint32_t module, const Fader::Color &color)
{
m_commands.emplace_back(Command{
SET_COLOUR,
static_cast<uint8_t>(strip),
static_cast<uint8_t>(module),
color
});
}
void Fader::add_color(uint32_t strip, uint32_t module, const Fader::Color &color)
{
m_commands.emplace_back(Command{
ADD_COLOUR,
static_cast<uint8_t>(strip),
static_cast<uint8_t>(module),
color
});
}
void Fader::fade_color(uint32_t strip, uint32_t module, const Fader::Color &color)
{
m_commands.emplace_back(Command{
FADE_COLOUR,
static_cast<uint8_t>(strip),
static_cast<uint8_t>(module),
color
});
}
void Fader::set_color(const Fader::Color &color)
{
for(std::size_t strip = 0; strip < m_strips; strip++) {
for(std::size_t module = 0; module < m_modulesPerStrip; module++) {
set_color(strip, module, color);
}
}
}
void Fader::add_color(const Fader::Color &color)
{
for(std::size_t strip = 0; strip < m_strips; strip++) {
for(std::size_t module = 0; module < m_modulesPerStrip; module++) {
add_color(strip, module, color);
}
}
}
void Fader::fade_color(const Fader::Color &color)
{
for(std::size_t strip = 0; strip < m_strips; strip++) {
for(std::size_t module = 0; module < m_modulesPerStrip; module++) {
fade_color(strip, module, color);
}
}
}
void Fader::set_fadestep(uint8_t newFadestep)
{
m_commands.emplace_back(Command{
SET_FADESTEP,
0,
0,
Color{newFadestep, 0, 0, 0}
});
}
void Fader::update(void)
{
// TODO: convert the m_commands vector to an UDP packet and send it
std::vector<uint8_t> packet;
for(auto &command : m_commands) {
packet.push_back(command.action);
packet.push_back(command.strip);
packet.push_back(command.module);
packet.push_back(command.color.r);
packet.push_back(command.color.g);
packet.push_back(command.color.b);
packet.push_back(command.color.w);
}
sockaddr_in target_addr;
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(2703);
target_addr.sin_addr.s_addr = inet_addr("10.42.7.145");
int ret = sendto(m_fd, packet.data(), packet.size(), 0, reinterpret_cast<const sockaddr*>(&target_addr), sizeof(target_addr));
if(ret == -1) {
std::cerr << "sendto failed: " << strerror(errno) << std::endl;
}
m_commands.clear();
}