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

Commit

Permalink
Merge pull request #62 from SciLor/develop
Browse files Browse the repository at this point in the history
v0.10.0
  • Loading branch information
SciLor authored May 21, 2021
2 parents c403796 + 8d2733a commit 079ff62
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
4 changes: 2 additions & 2 deletions HyperionRGB/BaseHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
typeof (Y) y_ = (Y); \
(x_ < y_) ? x_ : y_; })*/

enum Mode { OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012 };
enum Mode { OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012, RAINBOW_V2, RAINBOW_FULL, MODE_NONE };
enum UdpProtocol { UDP_RAW, UDP_FILLER, UDP_FRAGMENT, UDP_TPM2 };

#endif

15 changes: 14 additions & 1 deletion HyperionRGB/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void Config::initConfig(void) {
_cfgStruct.ports.udpLed = 19446;
_cfgStruct.led.timeoutMs = 5000;
_cfgStruct.led.autoswitch = true;
_cfgStruct.led.count = 1;
_cfgStruct.led.color = CRGB(254, 254, 254);
_cfgStruct.misc.udpProtocol = UDP_RAW;
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 @@ -72,10 +75,14 @@ void Config::loadStaticConfig(void) {
_cfgStruct.led.idleMode = CONFIG_LED_STANDARD_MODE;
_cfgStruct.led.timeoutMs = CONFIG_LED_STANDARD_MODE_TIMEOUT_MS;
_cfgStruct.led.autoswitch = CONFIG_LED_HYPERION_AUTOSWITCH;
_cfgStruct.led.count = CONFIG_LED_COUNT;
_cfgStruct.led.color = CONFIG_LED_STATIC_COLOR;

_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 All @@ -96,10 +103,17 @@ void Config::logConfig(void) {
Log.debug(" idleMode=%i", _cfgStruct.led.idleMode);
Log.debug(" timeoutMs=%i", _cfgStruct.led.timeoutMs);
Log.debug(" autoswitch=%i", _cfgStruct.led.autoswitch);
Log.debug(" count=%i", _cfgStruct.led.count);
Log.debug(" static-color.r=%i", _cfgStruct.led.color.r);
Log.debug(" static-color.g=%i", _cfgStruct.led.color.g);
Log.debug(" static-color.b=%i", _cfgStruct.led.color.b);

Log.debug("+PORTS+");
Log.debug(" jsonServer=%i", _cfgStruct.ports.jsonServer);
Log.debug(" udpLed=%i", _cfgStruct.ports.udpLed);

Log.debug("+MISC+");
Log.debug(" udpProtocol=%i", _cfgStruct.misc.udpProtocol);

}

Expand All @@ -123,4 +137,3 @@ ConfigIP Config::ip2cfg(const byte ip[4]) {
cfgIp.d = ip[3];
return cfgIp;
}

11 changes: 8 additions & 3 deletions HyperionRGB/ConfigStatic.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
#define CONFIG_LED_COLOR_ORDER RGB
#define CONFIG_LED_COUNT 50

//OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012
#define CONFIG_LED_STANDARD_MODE FIRE2012
//OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012, RAINBOW_V2, RAINBOW_FULL
#define CONFIG_LED_STANDARD_MODE RAINBOW_V2
#define CONFIG_LED_HYPERION_AUTOSWITCH true
#define CONFIG_LED_STANDARD_MODE_TIMEOUT_MS 5000
#define CONFIG_LED_STATIC_COLOR CRGB(254, 254, 254)

/*------------------------------------------------*/
/*Main configuration*/
Expand All @@ -52,7 +53,11 @@
//const byte CONFIG_WIFI_SUBNET[] = {255, 255, 255, 0};
//const byte CONFIG_WIFI_DNS[] = {192, 168, 0, 1};

#define CONFIG_WIFI_HOSTNAME "ESP8266"
#define CONFIG_WIFI_HOSTNAME "HyperionRGB"

#define CONFIG_PORT_JSON_SERVER 19444
#define CONFIG_PORT_UDP_LED 19446

#define CONFIG_PROTOCOL_UDP UDP_RAW //UDP_RAW, UDP_FRAGMENT, UDP_TPM2 - Hyperion 0: RAW, 2: Fragmented, 3: TPM2 - Check the README

#define CONFIG_OTA_AP_PASSWORD "HyperionRGB"
14 changes: 12 additions & 2 deletions HyperionRGB/ConfigStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define CONFIG_START_ADDRESS 0
#define CONFIG_ACTIVE_VERSION 3

#include <FastLED.h>

typedef struct {
uint8_t a;
uint8_t b;
Expand All @@ -30,8 +32,9 @@ typedef struct {
uint8_t idleMode;
uint32_t timeoutMs;
boolean autoswitch;

char spacer[59];
uint16_t count;
CRGB color;
char spacer[54];
} ConfigLed;

typedef struct {
Expand All @@ -41,11 +44,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
66 changes: 60 additions & 6 deletions HyperionRGB/HyperionRGB.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "WrapperJsonServer.h"

#include "WrapperWebconfig.h"
#include <DNSServer.h>

#define LED LED_BUILTIN // LED in NodeMCU at pin GPIO16 (D0) or LED_BUILTIN @Lolin32.
int ledState = LOW;
Expand All @@ -32,13 +33,16 @@ WrapperJsonServer jsonServer;
WrapperWebconfig webServer;
#endif

Mode activeMode;
Mode activeMode = MODE_NONE;
boolean autoswitch;

ThreadController threadController = ThreadController();
Thread statusThread = Thread();
EnhancedThread animationThread = EnhancedThread();
EnhancedThread resetThread = EnhancedThread();
EnhancedThread apThread = EnhancedThread();

DNSServer dnsServer;

void statusInfo(void) {
if (ledState == LOW) {
Expand All @@ -58,6 +62,12 @@ void animationStep() {
case FIRE2012:
ledStrip.fire2012Step();
break;
case RAINBOW_V2:
ledStrip.rainbowV2Step();
break;
case RAINBOW_FULL:
ledStrip.rainbowFullStep();
break;
}
}

Expand All @@ -74,8 +84,11 @@ void changeMode(Mode newMode, int interval = 0) {
ledStrip.show();
break;
case STATIC_COLOR:
ledStrip.fillSolid(static_cast<CRGB>(Config::getConfig()->led.color));
break;
case RAINBOW:
case RAINBOW_V2:
case RAINBOW_FULL:
if (interval == 0)
interval = 500;
animationThread.setInterval(interval);
Expand Down Expand Up @@ -127,37 +140,55 @@ void resetMode(void) {
resetThread.enabled = false;
}

void resetApIdle(void) {
if (wifi.isAPConnected()) {
Log.info("AP is used, keeping it alive...");
apThread.reset();
} else {
Log.error("Restarting because nobody connected via ap...");
delay(1000);
ESP.restart();
}
}

void initConfig(void) {
#if defined(CONFIG_OVERWRITE_WEBCONFIG) && defined(CONFIG_ENABLE_WEBCONFIG)
Config::loadStaticConfig();
#endif

const char* ssid;
const char* password;
const char* hostname;
const byte* ip;
const byte* subnet;
const byte* dns;
uint16_t jsonServerPort;
uint16_t udpLedPort;
UdpProtocol udpProtocol;
uint16_t ledCount;

#ifdef CONFIG_ENABLE_WEBCONFIG
//TODO Fallback
ConfigStruct* cfg = Config::getConfig();

ssid = cfg->wifi.ssid;
password = cfg->wifi.password;
hostname = cfg->wifi.hostname;
ip = Config::cfg2ip(cfg->wifi.ip);
subnet = Config::cfg2ip(cfg->wifi.subnet);
dns = Config::cfg2ip(cfg->wifi.dns);
jsonServerPort = cfg->ports.jsonServer;
udpLedPort = cfg->ports.udpLed;
udpProtocol = static_cast<UdpProtocol>(cfg->misc.udpProtocol);
autoswitch = cfg->led.autoswitch;
ledCount = cfg->led.count;

Log.info("CFG=%s", "EEPROM config loaded");
Config::logConfig();
#else
ssid = CONFIG_WIFI_SSID;
password = CONFIG_WIFI_PASSWORD;
hostname = CONFIG_WIFI_HOSTNAME;
#ifdef CONFIG_WIFI_STATIC_IP
ip = CONFIG_WIFI_IP;
subnet = CONFIG_WIFI_SUBNET;
Expand All @@ -168,22 +199,28 @@ void initConfig(void) {
#endif
jsonServerPort = CONFIG_PORT_JSON_SERVER;
udpLedPort = CONFIG_PORT_UDP_LED;
udpProtocol = CONFIG_PROTOCOL_UDP;
autoswitch = CONFIG_LED_HYPERION_AUTOSWITCH;
ledCount = CONFIG_LED_COUNT

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

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

void handleEvents(void) {
ota.handle();
udpLed.handle();
jsonServer.handle();
#ifdef CONFIG_ENABLE_WEBCONFIG
webServer.handle();
if (wifi.isAP())
dnsServer.processNextRequest();
if (webServer.handle() && wifi.isAPConnected()) {
apThread.reset();
}
#endif

threadController.run();
Expand Down Expand Up @@ -212,11 +249,26 @@ void setup(void) {
resetThread.enabled = false;
threadController.add(&resetThread);

ledStrip.begin();
#ifdef CONFIG_ENABLE_WEBCONFIG
ledStrip.begin(Config::getConfig()->led.count);
#else
ledStrip.begin(CONFIG_LED_COUNT);
#endif
resetMode();
animationStep();

wifi.begin();
if (wifi.isAP()) {
apThread.onRun(resetApIdle);
apThread.setInterval(60*1000);
apThread.reset();
threadController.add(&apThread);

dnsServer.start(53, "*", IPAddress(192, 168, 4, 1)); //Port 53 - standard port

} else {
apThread.enabled = false;
}

#ifdef CONFIG_ENABLE_WEBCONFIG
webServer = WrapperWebconfig();
Expand Down Expand Up @@ -246,6 +298,8 @@ void loop(void) {
switch (activeMode) {
case RAINBOW:
case FIRE2012:
case RAINBOW_V2:
case RAINBOW_FULL:
animationThread.runIfNeeded();
break;
case STATIC_COLOR:
Expand Down
22 changes: 14 additions & 8 deletions HyperionRGB/WrapperJsonServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ void WrapperJsonServer::handleConnection(boolean newClient) {
void WrapperJsonServer::readData(void) {
String data = _tcpClient.readStringUntil('\n');
Log.debug("Received data: %s", data.c_str());
StaticJsonBuffer<TCP_BUFFER> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(data.c_str());
if (root.success()) {
String command = root["command"].asString();
StaticJsonDocument<TCP_BUFFER> doc;
deserializeJson(doc, data.c_str());
if (!doc.isNull()) {
JsonObject root = doc.as<JsonObject>();
String command = root["command"];
if (command.equals("serverinfo")) {
Log.info("serverinfo");
_tcpClient.print("{\"info\":{\"effects\":["
"{\"args\":{\"speed\":1.0},\"name\":\"Hyperion UDP\",\"script\":\"hyperion_udp\"},"
"{\"args\":{\"speed\":2.0},\"name\":\"Rainbow mood\",\"script\":\"rainbow\"},"
"{\"args\":{\"speed\":2.0},\"name\":\"Rainbow swirl\",\"script\":\"rainbow\"},"
"{\"args\":{\"speed\":2.0},\"name\":\"Rainbow swirl v2\",\"script\":\"rainbow_v2\"},"
"{\"args\":{\"speed\":2.0},\"name\":\"Rainbow full\",\"script\":\"rainbow_full\"},"
"{\"args\":{\"speed\":62.5},\"name\":\"Fire2012\",\"script\":\"fire2012\"}"
"],"
"\"activeLedColor\":[{\"RGB Value\":[");
Expand Down Expand Up @@ -82,7 +85,7 @@ void WrapperJsonServer::readData(void) {
clearCmd();
_tcpClient.println("{\"success\":true}");
} else if (command.equals("effect")) {
String effect = root["effect"]["name"].asString();
String effect = root["effect"]["name"];
double effectSpeed = root["effect"]["args"]["speed"];
int interval = 0;
if (effectSpeed > 0) {
Expand All @@ -91,8 +94,12 @@ void WrapperJsonServer::readData(void) {

if (effect.equals("Hyperion UDP")) {
effectChange(HYPERION_UDP);
} else if (effect.equals("Rainbow mood")) {
} else if (effect.equals("Rainbow swirl")) {
effectChange(RAINBOW, interval);
} else if (effect.equals("Rainbow swirl v2")) {
effectChange(RAINBOW_V2, interval);
} else if (effect.equals("Rainbow full")) {
effectChange(RAINBOW_FULL, interval);
} else if (effect.equals("Fire2012")) {
effectChange(FIRE2012, interval);
}
Expand Down Expand Up @@ -137,4 +144,3 @@ void WrapperJsonServer::effectChange(Mode effect, int interval/* = 0*/) {
effectChangePointer(effect, interval);
}
}

Loading

0 comments on commit 079ff62

Please sign in to comment.