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 #16 from SciLor/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
SciLor committed Nov 19, 2016
2 parents cf22f0a + fe5fcdb commit 323662e
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 64 deletions.
2 changes: 1 addition & 1 deletion 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, AMBILIGHT, STATIC_COLOR, RAINBOW, FIRE2012 };
enum Mode { OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012 };

#endif

6 changes: 6 additions & 0 deletions HyperionRGB/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void Config::initConfig(void) {
strncpy(_cfgStruct.wifi.hostname, "ESP8266", 32);
_cfgStruct.ports.jsonServer = 19444;
_cfgStruct.ports.udpLed = 19446;
_cfgStruct.led.timeoutMs = 5000;
_cfgStruct.led.autoswitch = true;
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 @@ -68,6 +70,8 @@ void Config::loadStaticConfig(void) {
_cfgStruct.wifi.dns.d = 0;
#endif
_cfgStruct.led.idleMode = CONFIG_LED_STANDARD_MODE;
_cfgStruct.led.timeoutMs = CONFIG_LED_STANDARD_MODE_TIMEOUT_MS;
_cfgStruct.led.autoswitch = CONFIG_LED_HYPERION_AUTOSWITCH;

_cfgStruct.ports.jsonServer = CONFIG_PORT_JSON_SERVER;
_cfgStruct.ports.udpLed = CONFIG_PORT_UDP_LED;
Expand All @@ -90,6 +94,8 @@ void Config::logConfig(void) {

Log.debug("+LED+");
Log.debug(" idleMode=%i", _cfgStruct.led.idleMode);
Log.debug(" timeoutMs=%i", _cfgStruct.led.timeoutMs);
Log.debug(" autoswitch=%i", _cfgStruct.led.autoswitch);

Log.debug("+PORTS+");
Log.debug(" jsonServer=%i", _cfgStruct.ports.jsonServer);
Expand Down
9 changes: 8 additions & 1 deletion HyperionRGB/ConfigStatic.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@
#define CONFIG_LED_DATAPIN D1
#define CONFIG_LED_CLOCKPIN D2 //Comment out for clockless

//Pin order, see FastLED doc. NodeMCU should work with FASTLED_ESP8266_RAW_PIN_ORDER
#define FASTLED_ESP8266_RAW_PIN_ORDER
//#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
//#define FASTLED_ESP8266_D1_PIN_ORDER

#define CONFIG_LED_COLOR_ORDER RGB
#define CONFIG_LED_COUNT 50

//OFF, RAINBOW, FIRE2012
//OFF, HYPERION_UDP, STATIC_COLOR, RAINBOW, FIRE2012
#define CONFIG_LED_STANDARD_MODE FIRE2012
#define CONFIG_LED_HYPERION_AUTOSWITCH true
#define CONFIG_LED_STANDARD_MODE_TIMEOUT_MS 5000

/*------------------------------------------------*/
/*Main configuration*/
Expand Down
4 changes: 3 additions & 1 deletion HyperionRGB/ConfigStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ typedef struct {

typedef struct {
uint8_t idleMode;
uint32_t timeoutMs;
boolean autoswitch;

char spacer[64];
char spacer[59];
} ConfigLed;

typedef struct {
Expand Down
3 changes: 3 additions & 0 deletions HyperionRGB/EnhancedThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ void EnhancedThread::reset(void) {
void EnhancedThread::setRunOnce(bool runOnce) {
_runOnce = runOnce;
}
unsigned long EnhancedThread::getInterval(void) {
return interval;
}

1 change: 1 addition & 0 deletions HyperionRGB/EnhancedThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EnhancedThread: public Thread {
runIfNeeded(void),
reset(void),
setRunOnce(bool);
unsigned long getInterval(void);
private:
bool _runOnce;
};
Expand Down
51 changes: 35 additions & 16 deletions HyperionRGB/HyperionRGB.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ WrapperJsonServer jsonServer;
#endif

Mode activeMode;
boolean autoswitch;

ThreadController threadController = ThreadController();
Thread statusThread = Thread();
Expand Down Expand Up @@ -60,37 +61,47 @@ void animationStep() {
}
}

void changeMode(Mode newMode) {
void changeMode(Mode newMode, double interval = 1.0d) {
if (newMode != activeMode) {
Log.info("Mode changed to %i", newMode);
activeMode = newMode;

if (!autoswitch)
udpLed.stop();

switch (activeMode) {
case RAINBOW:
animationThread.setInterval(500);
break;
case FIRE2012:
animationThread.setInterval(16);
animationThread.setInterval(interval);
break;
case HYPERION_UDP:
if (!autoswitch)
udpLed.begin();
}
}
}

void updateLed(int id, byte r, byte g, byte b) {
Log.verbose("LED %i, r=%i, g=%i, b=%i", id + 1, r, g, b);
ledStrip.leds[id].setRGB(r, g, b);
if (activeMode == HYPERION_UDP) {
Log.verbose("LED %i, r=%i, g=%i, b=%i", id + 1, r, g, b);
ledStrip.leds[id].setRGB(r, g, b);
}
}
void refreshLeds(void) {
Log.debug("refresh LEDs");
ledStrip.show();
changeMode(AMBILIGHT);
resetThread.reset();
if (activeMode == HYPERION_UDP) {
Log.debug("refresh LEDs");
ledStrip.show();
if (autoswitch)
resetThread.reset();
} else if (autoswitch) {
changeMode(HYPERION_UDP);
Log.info("Autoswitch to HYPERION_UDP");
}
}

void ledColorWipe(byte r, byte g, byte b) {
Log.debug("LED color wipe: r=%i, g=%i, b=%i", r, g, b);
ledStrip.fillSolid(r, g, b);
changeMode(STATIC_COLOR);
ledStrip.fillSolid(r, g, b);
}
void resetMode(void) {
Log.info("Reset Mode");
Expand Down Expand Up @@ -126,6 +137,7 @@ void initConfig(void) {
dns = Config::cfg2ip(cfg->wifi.dns);
jsonServerPort = cfg->ports.jsonServer;
udpLedPort = cfg->ports.udpLed;
autoswitch = cfg->led.autoswitch;

Log.info("CFG=%s", "EEPROM config loaded");
Config::logConfig();
Expand All @@ -142,6 +154,7 @@ void initConfig(void) {
#endif
jsonServerPort = CONFIG_PORT_JSON_SERVER;
udpLedPort = CONFIG_PORT_UDP_LED;
autoswitch = CONFIG_LED_HYPERION_AUTOSWITCH;

Log.info("CFG=%s", "Static config loaded");
#endif
Expand Down Expand Up @@ -177,7 +190,11 @@ void setup(void) {
animationThread.setInterval(500);

resetThread.onRun(resetMode);
resetThread.setInterval(5000);
#ifdef CONFIG_ENABLE_WEBCONFIG
resetThread.setInterval(Config::getConfig()->led.timeoutMs);
#else
resetThread.setInterval(CONFIG_LED_STANDARD_MODE_TIMEOUT_MS);
#endif
resetThread.enabled = false;
threadController.add(&resetThread);

Expand All @@ -194,8 +211,10 @@ void setup(void) {
#else
ota.begin(CONFIG_WIFI_HOSTNAME);
#endif

udpLed.begin();

if (autoswitch || activeMode == HYPERION_UDP)
udpLed.begin();

udpLed.onUpdateLed(updateLed);
udpLed.onRefreshLeds(refreshLeds);

Expand All @@ -217,7 +236,7 @@ void loop(void) {
break;
case STATIC_COLOR:
break;
case AMBILIGHT:
case HYPERION_UDP:
break;
}
}
4 changes: 0 additions & 4 deletions HyperionRGB/WrapperFastLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

#include "BaseHeader.h"

#define FASTLED_ESP8266_RAW_PIN_ORDER
//#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
//#define FASTLED_ESP8266_D1_PIN_ORDER

#include <FastLED.h>

class WrapperFastLed {
Expand Down
29 changes: 19 additions & 10 deletions HyperionRGB/WrapperJsonServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,32 @@ void WrapperJsonServer::readData(void) {
if (command.equals("serverinfo")) {
Log.info("serverinfo");
_tcpClient.println("{\"info\":{\"effects\":["
"{\"args\":{\"speed\":1.0},\"name\":\"Rainbow mood\",\"script\":\"rainbow\"},"
"{\"args\":{\"speed\":1.0},\"name\":\"Fire2012\",\"script\":\"fire2012\"}"
"],\"hostname\":\"ESP8266\",\"priorities\":[],\"transform\":[{\"blacklevel\":[0.0,0.0,0.0],\"gamma\":[1.0,1.0,1.0],\"id\":\"default\",\"saturationGain\":1.0,\"threshold\":[0.0,0.0,0.0],\"valueGain\":1.0,\"whitelevel\":[1.0,1.0,1.0]}]},\"success\":true}");
"{\"args\":{\"speed\":1.0},\"name\":\"Hyperion UDP\",\"script\":\"hyperion_udp\"},"
"{\"args\":{\"speed\":2.0},\"name\":\"Rainbow mood\",\"script\":\"rainbow\"},"
"{\"args\":{\"speed\":62.5},\"name\":\"Fire2012\",\"script\":\"fire2012\"}"
"],"
"\"hostname\":\"ESP8266\","
"\"priorities\":[],\"transform\":[{\"blacklevel\":[0.0,0.0,0.0],\"gamma\":[1.0,1.0,1.0],\"id\":\"default\",\"saturationGain\":1.0,\"threshold\":[0.0,0.0,0.0],\"valueGain\":1.0,\"whitelevel\":[1.0,1.0,1.0]}]},"
"\"success\":true}");
} else if (command.equals("color")) {
int duration = root["duration"];
ledColorWipe(root["color"][0], root["color"][1], root["color"][2]);
_tcpClient.println("{\"success\":true}");
} else if (command.equals("clear") || command.equals("clearall")) {
clearCmd();
_tcpClient.println("{\"success\":true}");
} else if (command.equals("effect")) {
String effect = root["effect"]["name"].asString();
int speed = root["effect"]["speed"];
double speed = root["effect"]["speed"];
double interval = 1 / speed;
int duration = root["duration"];

if (effect.equals("Rainbow mood")) {
effectChange(RAINBOW);
if (effect.equals("Hyperion UDP")) {
effectChange(HYPERION_UDP);
} else if (effect.equals("Rainbow mood")) {
effectChange(RAINBOW, interval);
} else if (effect.equals("Fire2012")) {
effectChange(FIRE2012);
effectChange(FIRE2012, interval);
}
_tcpClient.println("{\"success\":true}");
} else {
Expand Down Expand Up @@ -94,12 +103,12 @@ void WrapperJsonServer::clearCmd(void) {
}
}

void WrapperJsonServer::onEffectChange(void(* function) (Mode)) {
void WrapperJsonServer::onEffectChange(void(* function) (Mode, double)) {
effectChangePointer = function;
}
void WrapperJsonServer::effectChange(Mode effect) {
void WrapperJsonServer::effectChange(Mode effect, double interval/* = 1.0d*/) {
if (effectChangePointer) {
effectChangePointer(effect);
effectChangePointer(effect, interval);
}
}

6 changes: 3 additions & 3 deletions HyperionRGB/WrapperJsonServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WrapperJsonServer {
void
onLedColorWipe(void(* function) (byte, byte, byte)),
onClearCmd(void(* function) (void)),
onEffectChange(void(* function) (Mode));
onEffectChange(void(* function) (Mode, double));
private:
void
handleConnection(boolean newClient),
Expand All @@ -33,8 +33,8 @@ class WrapperJsonServer {
clearCmd(void),
(* clearCmdPointer) (void);
void
effectChange(Mode effect),
(* effectChangePointer) (Mode);
effectChange(Mode effect, double interval = 1.0d),
(* effectChangePointer) (Mode, double);

WiFiServer _tcpServer;
WiFiClient _tcpClient;
Expand Down
25 changes: 18 additions & 7 deletions HyperionRGB/WrapperUdpLed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ WrapperUdpLed::WrapperUdpLed(uint16_t ledCount, uint16_t udpPort) {

_udpBuffer = new byte [_bufferSize+1];
_udpBuffer[_bufferSize] = 0;

_opened = false;
}

void WrapperUdpLed::begin(void) {
Log.info("Open port %i for UDP...", _udpPort);
if (_udp.begin(_udpPort)) {
Log.info("success");
} else {
Log.error("no success");
}
if (!_opened) {
Log.info("Open port %i for UDP...", _udpPort);
if (_udp.begin(_udpPort)) {
Log.info("success");
_opened = true;
} else {
Log.error("no success");
}
}
}
void WrapperUdpLed::stop(void) {
if (_opened) {
Log.info("Close port %i for UDP...", _udpPort);
_udp.stop();
_opened = false;
}
}

void WrapperUdpLed::handle(void) {
int bytes = _udp.parsePacket();
if (bytes > 0) {
Expand Down
2 changes: 2 additions & 0 deletions HyperionRGB/WrapperUdpLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WrapperUdpLed {

void
begin(void),
stop(void),
handle(void);

void
Expand All @@ -23,6 +24,7 @@ class WrapperUdpLed {
uint16_t _udpPort;
byte* _udpBuffer;
uint16_t _bufferSize;
boolean _opened;

void
updateLed(int id, byte r, byte g, byte b),
Expand Down
Loading

0 comments on commit 323662e

Please sign in to comment.