Skip to content
This repository was archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Prepare configuration for udp protocol selection
Browse files Browse the repository at this point in the history
Not checked if compileable
  • Loading branch information
SciLor committed Jun 9, 2018
1 parent 3d5a974 commit 28dabf7
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
3 changes: 3 additions & 0 deletions HyperionRGB/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void Config::initConfig(void) {
_cfgStruct.ports.udpLed = 19446;
_cfgStruct.led.timeoutMs = 5000;
_cfgStruct.led.autoswitch = true;
_cfgStruct.misc.udpProtocol = 0;
EEPROM.end();
saveConfig();
Log.info("Configuration at 0x%x with v%i (v%i expected), new configuration created", CONFIG_START_ADDRESS, version, CONFIG_ACTIVE_VERSION);
Expand Down Expand Up @@ -76,6 +77,8 @@ void Config::loadStaticConfig(void) {
_cfgStruct.ports.jsonServer = CONFIG_PORT_JSON_SERVER;
_cfgStruct.ports.udpLed = CONFIG_PORT_UDP_LED;

_cfgStruct.misc.udpProtocol = CONFIG_PROTOCOL_UDP;

saveConfig();
Log.info("CFG=%s", "loadStaticConfig END");
}
Expand Down
2 changes: 2 additions & 0 deletions HyperionRGB/ConfigStatic.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@

#define CONFIG_PORT_JSON_SERVER 19444
#define CONFIG_PORT_UDP_LED 19446

#define CONFIG_PROTOCOL_UDP 0
9 changes: 8 additions & 1 deletion HyperionRGB/ConfigStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct {
uint8_t idleMode;
uint32_t timeoutMs;
boolean autoswitch;

char spacer[59];
} ConfigLed;

Expand All @@ -41,11 +41,18 @@ typedef struct {
char spacer[32];
} ConfigPort;

typedef struct {
uint8_t udpProtocol;

char spacer[63];
} ConfigMisc;

typedef struct {
uint8_t version;
ConfigWifi wifi;
ConfigLed led;
ConfigPort ports;
ConfigMisc misc;
} ConfigStruct;

#endif
5 changes: 4 additions & 1 deletion HyperionRGB/HyperionRGB.ino
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void initConfig(void) {
const byte* dns;
uint16_t jsonServerPort;
uint16_t udpLedPort;
uint8_t udpProtocol;

#ifdef CONFIG_ENABLE_WEBCONFIG
//TODO Fallback
Expand All @@ -154,6 +155,7 @@ void initConfig(void) {
dns = Config::cfg2ip(cfg->wifi.dns);
jsonServerPort = cfg->ports.jsonServer;
udpLedPort = cfg->ports.udpLed;
udpProtocol = cfg->misc.udpProtocol;
autoswitch = cfg->led.autoswitch;

Log.info("CFG=%s", "EEPROM config loaded");
Expand All @@ -171,13 +173,14 @@ void initConfig(void) {
#endif
jsonServerPort = CONFIG_PORT_JSON_SERVER;
udpLedPort = CONFIG_PORT_UDP_LED;
udpProtocol = CONFIG_PROTOCOL_UDP;
autoswitch = CONFIG_LED_HYPERION_AUTOSWITCH;

Log.info("CFG=%s", "Static config loaded");
#endif

wifi = WrapperWiFi(ssid, password, ip, subnet, dns);
udpLed = WrapperUdpLed(CONFIG_LED_COUNT, udpLedPort);
udpLed = WrapperUdpLed(CONFIG_LED_COUNT, udpLedPort, udpProtocol);
jsonServer = WrapperJsonServer(CONFIG_LED_COUNT, jsonServerPort);
}

Expand Down
41 changes: 31 additions & 10 deletions HyperionRGB/WrapperUdpLed.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "WrapperUdpLed.h"

WrapperUdpLed::WrapperUdpLed(uint16_t ledCount, uint16_t udpPort) {
WrapperUdpLed::WrapperUdpLed(uint16_t ledCount, uint16_t udpPort, uint8_t udpProtocol) {
_udp = WiFiUDP();
_ledCount = ledCount;
_udpPort = udpPort;
_udpProtocol = udpProtocol;

_bufferSize = _ledCount * 3; //3 bytes per LED

Expand All @@ -15,7 +16,7 @@ WrapperUdpLed::WrapperUdpLed(uint16_t ledCount, uint16_t udpPort) {

void WrapperUdpLed::begin(void) {
if (!_opened) {
Log.info("Open port %i for UDP...", _udpPort);
Log.info("Open port %i for UDP with protocol %i...", _udpPort, _udpProtocol);
if (_udp.begin(_udpPort)) {
Log.info("success");
_opened = true;
Expand All @@ -35,15 +36,35 @@ void WrapperUdpLed::handle(void) {
int bytes = _udp.parsePacket();
if (bytes > 0) {
Log.debug("UDP-Packet received, length: %i", bytes);
if (bytes == _bufferSize) {
_udp.readBytes(_udpBuffer, _bufferSize);
Log.verbose("Contents: %s", _udpBuffer);
for (int i=0; i<_ledCount; i++) {
updateLed(i, _udpBuffer[i*3+0], _udpBuffer[i*3+1], _udpBuffer[i*3+2]);
//See @https://hyperion-project.org/wiki/UDP-Device
if (_udpProtocol == 0) {
//Protocol 0: Raw Format, 3 bytes per LED, Full LED set.
/// {0: R, 1: G, 2: B}
if (bytes == _bufferSize) {
_udp.readBytes(_udpBuffer, _bufferSize);
Log.verbose("Contents: %s", _udpBuffer);
for (int i=0; i<_ledCount; i++) {
updateLed(i, _udpBuffer[i*3+0], _udpBuffer[i*3+1], _udpBuffer[i*3+2]);
}
refreshLeds();
} else {
Log.debug("UDP-Packet size expected=%i, actual=%i", _bufferSize, bytes);
}
refreshLeds();
} else {
Log.debug("UDP-Packet size expected=%i, actual=%i", _bufferSize, bytes);
} else (_udpProtocol == 2) {
//Protocol 2:
/// 0: Update ID & 0xF
/// 1: Fragment ?!
/// 2: First LED ID, high byte / 3: First LED ID, low byte --> int16_t?
/// 4: {0: R, 1: G, 2: B}
} else (_udpProtocol == 3) {
//Protocol 3: TPM2.net
/// 0: 0x9C
/// 1: 0xDA
/// 2: Data length (LED count * 3), high byte, 3: Data length (LED count * 3), low byte --> int16_t?
/// 4: Fragment ID
/// 5: Fragment ID maximum
/// 6: {0: R, 1: G, 2: B}
/// 7: 0x36
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion HyperionRGB/WrapperUdpLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class WrapperUdpLed {
public:
WrapperUdpLed() {};
WrapperUdpLed(uint16_t ledCount, uint16_t udpPort);
WrapperUdpLed(uint16_t ledCount, uint16_t udpPort, uint8_t udpProtocol);

void
begin(void),
Expand All @@ -22,6 +22,7 @@ class WrapperUdpLed {
WiFiUDP _udp;
uint16_t _ledCount;
uint16_t _udpPort;
uint8_t _udpProtocol;
byte* _udpBuffer;
uint16_t _bufferSize;
boolean _opened;
Expand Down
15 changes: 15 additions & 0 deletions HyperionRGB/WrapperWebconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ void WrapperWebconfig::changeConfig(void) {
cfg->ports.udpLed = 19446;
} else if (argName == "led-idleMode") {
cfg->led.idleMode = getSelectedEntry<uint8_t>(argValue, _idleModes);
} else if (argName == "led-udpProtocol") {
cfg->misc.udpProtocol = getSelectedEntry<uint8_t>(argValue, _udpProtocols);
} else if (argName == "led-timeoutMs") {
if (argValue.equals("0")) {
cfg->led.timeoutMs = 0;
Expand Down Expand Up @@ -322,11 +324,15 @@ String WrapperWebconfig::config(void) {
groupContent = "";

_idleModes = new LinkedList<SelectEntryBase*>();
_udpProtocols = new LinkedList<SelectEntryBase*>();
getIdleModes(cfg->led.idleMode, _idleModes);
getUdpProtocols(cfg->misc.udpProtocol, _udpProtocols);
groupContent += selectTemplate("LED Idle Mode", "", "led-idleMode", _idleModes);
groupContent += checkboxTemplate("Autoswitch to Hyperion_UDP/Idle Mode", "Automatically switch to Hyperion_UDP when UDP Data arriving and switch back to idle mode after timeout", "led-autoswitch", cfg->led.autoswitch);
groupContent += textTemplate("Timeout Fallback in MS", "Switches back to Idle Mode after x milliseconds when no UDP data is arriving", "led-timeoutMs", escape(cfg->led.timeoutMs), "5000", 10);
groupContent += selectTemplate("LED UDP Protocol", "", "led-udpProtocol", _udpProtocols);
clearLinkedList(_idleModes);
clearLinkedList(_udpProtocols);

html += groupTemplate("LEDs", groupContent);

Expand Down Expand Up @@ -370,10 +376,13 @@ void WrapperWebconfig::initHelperVars(void) {
ConfigStruct *cfg = Config::getConfig();

_idleModes = new LinkedList<SelectEntryBase*>();
_udpProtocols = new LinkedList<SelectEntryBase*>();
getIdleModes(cfg->led.idleMode, _idleModes);
getIdleModes(cfg->misc.udpProtocol, _udpProtocols);
}
void WrapperWebconfig::clearHelperVars(void) {
clearLinkedList(_idleModes);
clearLinkedList(_udpProtocols);
}
void WrapperWebconfig::clearLinkedList(LinkedList<SelectEntryBase*>* target) {
Log.debug("Clearing LinkedList HEAP=%i", ESP.getFreeHeap());
Expand Down Expand Up @@ -409,3 +418,9 @@ void WrapperWebconfig::getIdleModes(uint8_t active, LinkedList<SelectEntryBase*>
target->add((SelectEntryBase*) new SelectEntry<uint8_t>("Rainbow", "Rainbow", active == RAINBOW, RAINBOW));
target->add((SelectEntryBase*) new SelectEntry<uint8_t>("Fire2012", "Fire2012", active == FIRE2012, FIRE2012));
}

void WrapperWebconfig::getUdpProtocols(uint8_t active, LinkedList<SelectEntryBase*>* target) {
target->add((SelectEntryBase*) new SelectEntry<uint8_t>("0", "Protocol 0 (Raw)", active == 0, 0));
target->add((SelectEntryBase*) new SelectEntry<uint8_t>("0", "Protocol 2 (Fragment)", active == 2, 2));
target->add((SelectEntryBase*) new SelectEntry<uint8_t>("0", "Protocol 3 (TPM2 Fragments)", active == 3, 3));
}
3 changes: 2 additions & 1 deletion HyperionRGB/WrapperWebconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class WrapperWebconfig {
T getSelectedEntry(String selectedEntryValue, LinkedList<SelectEntryBase*>* target);

LinkedList<SelectEntryBase*>* _idleModes;

LinkedList<SelectEntryBase*>* _udpProtocols;

#if defined(ESP8266)
ESP8266WebServer* _server = new ESP8266WebServer(80);
#elif defined(ESP32)
Expand Down

0 comments on commit 28dabf7

Please sign in to comment.