Browse Source

Moving light code to its own module

fastled
Xose Pérez 7 years ago
parent
commit
9e75bdb45a
9 changed files with 273 additions and 169 deletions
  1. +48
    -48
      code/espurna/config/data.h
  2. +10
    -11
      code/espurna/config/general.h
  3. +12
    -2
      code/espurna/config/hardware.h
  4. BIN
      code/espurna/data/index.html.gz
  5. +3
    -0
      code/espurna/espurna.ino
  6. +188
    -0
      code/espurna/light.ino
  7. +5
    -86
      code/espurna/relay.ino
  8. +4
    -5
      code/espurna/web.ino
  9. +3
    -17
      code/html/custom.js

+ 48
- 48
code/espurna/config/data.h
File diff suppressed because it is too large
View File


+ 10
- 11
code/espurna/config/general.h View File

@ -59,11 +59,7 @@
#define RELAY_PROVIDER_RELAY 0 #define RELAY_PROVIDER_RELAY 0
#define RELAY_PROVIDER_DUAL 1 #define RELAY_PROVIDER_DUAL 1
#define RELAY_PROVIDER_MY9291 2
#ifndef RELAY_PROVIDER
#define RELAY_PROVIDER RELAY_PROVIDER_RELAY
#endif
#define RELAY_PROVIDER_LIGHT 2
// Pulse time in seconds // Pulse time in seconds
#define RELAY_PULSE_TIME 1 #define RELAY_PULSE_TIME 1
@ -192,18 +188,21 @@
#define I2C_SCL_FREQUENCY 1000 #define I2C_SCL_FREQUENCY 1000
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MY9291
// LIGHT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#define LIGHT_PROVIDER_NONE 0
#define LIGHT_PROVIDER_WS2812 1
#define LIGHT_PROVIDER_5050 2
#define LIGHT_PROVIDER_MY9192 3
#define LIGHT_DEFAULT_COLOR "#C0C0C0"
#define LIGHT_SAVE_DELAY 5
#define MY9291_DI_PIN 13 #define MY9291_DI_PIN 13
#define MY9291_DCKI_PIN 15 #define MY9291_DCKI_PIN 15
#define MY9291_COMMAND MY9291_COMMAND_DEFAULT #define MY9291_COMMAND MY9291_COMMAND_DEFAULT
#define MY9291_COLOR_RED 0
#define MY9291_COLOR_GREEN 0
#define MY9291_COLOR_BLUE 0
#define MY9291_COLOR_WHITE 192
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// DOMOTICZ // DOMOTICZ
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 12
- 2
code/espurna/config/hardware.h View File

@ -250,8 +250,8 @@
#define MANUFACTURER "AI THINKER" #define MANUFACTURER "AI THINKER"
#define DEVICE "AI LIGHT" #define DEVICE "AI LIGHT"
#undef RELAY_PROVIDER
#define RELAY_PROVIDER RELAY_PROVIDER_MY9291
#define RELAY_PROVIDER RELAY_PROVIDER_LIGHT
#define LIGHT_PROVIDER LIGHT_PROVIDER_MY9192
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Jan Goedeke Wifi Relay // Jan Goedeke Wifi Relay
@ -432,3 +432,13 @@
#define BUTTON_DEFAULT_HIGH 2 #define BUTTON_DEFAULT_HIGH 2
#define BUTTON_SET_PULLUP 4 #define BUTTON_SET_PULLUP 4
#endif #endif
// Relay providers
#ifndef RELAY_PROVIDER
#define RELAY_PROVIDER RELAY_PROVIDER_RELAY
#endif
// Light provider
#ifndef LIGHT_PROVIDER
#define LIGHT_PROVIDER LIGHT_PROVIDER_NONE
#endif

BIN
code/espurna/data/index.html.gz View File


+ 3
- 0
code/espurna/espurna.ino View File

@ -125,6 +125,9 @@ void setup() {
} }
webSetup(); webSetup();
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
lightSetup();
#endif
relaySetup(); relaySetup();
buttonSetup(); buttonSetup();
ledSetup(); ledSetup();


+ 188
- 0
code/espurna/light.ino View File

@ -0,0 +1,188 @@
/*
LIGHT MODULE
Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
#include <Ticker.h>
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
#include <my9291.h>
my9291 * _my9291;
Ticker colorTicker;
bool _mqttSkipColor = false;
#endif
// -----------------------------------------------------------------------------
// UTILS
// -----------------------------------------------------------------------------
void color_rgb2array(const char * rgb, unsigned int * array) {
char * p = (char *) rgb;
if (p[0] == '#') ++p;
unsigned long value = strtol(p, NULL, 16);
array[0] = (value >> 16) & 0xFF;
array[1] = (value >> 8) & 0xFF;
array[2] = (value) & 0xFF;
}
void color_array2rgb(unsigned int * array, char * rgb) {
unsigned long value = array[0];
value = (value << 8) + array[1];
value = (value << 8) + array[2];
sprintf(rgb, "#%06X", value);
}
// -----------------------------------------------------------------------------
// LIGHT MANAGEMENT
// -----------------------------------------------------------------------------
void lightColor(const char * rgb, bool save) {
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
unsigned int array[4] = {0};
color_rgb2array(rgb, array);
// If all set to the same value use white instead
if ((array[0] == array[1]) && (array[1] == array[2])) {
array[3] = array[0];
array[0] = array[1] = array[2] = 0;
}
// Set new color (if light is open it will automatically change)
_my9291->setColor((my9291_color_t) { array[0], array[1], array[2], array[3] });
#endif
// Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
if (save) {
colorTicker.once(LIGHT_SAVE_DELAY, lightColorSave);
}
// Report color to MQTT broker
_mqttSkipColor = true;
mqttSend(MQTT_COLOR_TOPIC, rgb);
// Report color to WS clients
char message[20];
sprintf(message, "{\"color\": \"%s\"}", rgb);
wsSend(message);
}
void lightColor(const char * rgb) {
lightColor(rgb, true);
}
String lightColor() {
String response;
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
my9291_color_t color = _my9291->getColor();
unsigned int array[3];
if (color.white > 0) {
array[0] = array[1] = array[2] = color.white;
} else {
array[0] = color.red;
array[1] = color.green;
array[2] = color.blue;
}
char rgb[8];
color_array2rgb(array, rgb);
response = String(rgb);
#endif
return response;
}
void lightState(bool state) {
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
_my9291->setState(state);
#endif
}
bool lightState() {
bool response = false;
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
response = _my9291->getState();
#endif
return response;
}
// -----------------------------------------------------------------------------
// PERSISTANCE
// -----------------------------------------------------------------------------
void lightColorSave() {
setSetting("color", lightColor());
saveSettings();
}
void lightColorRetrieve() {
lightColor(getSetting("color", LIGHT_DEFAULT_COLOR).c_str(), false);
}
// -----------------------------------------------------------------------------
// MQTT
// -----------------------------------------------------------------------------
void lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
if (type == MQTT_CONNECT_EVENT) {
char buffer[strlen(MQTT_COLOR_TOPIC) + mqttSetter.length() + 20];
sprintf(buffer, "%s%s", MQTT_COLOR_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
}
if (type == MQTT_MESSAGE_EVENT) {
// Match topic
char * t = mqttSubtopic((char *) topic);
int len = mqttSetter.length();
if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return;
if (strncmp(t, MQTT_COLOR_TOPIC, strlen(MQTT_COLOR_TOPIC)) == 0) {
if (_mqttSkipColor) {
_mqttSkipColor = false;
} else {
lightColor(payload, true);
}
return;
}
}
}
// -----------------------------------------------------------------------------
// SETUP
// -----------------------------------------------------------------------------
void lightSetup() {
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY9192
_my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
#endif
lightColorRetrieve();
mqttRegister(lightMQTTCallback);
}
#endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE

+ 5
- 86
code/espurna/relay.ino View File

@ -25,54 +25,10 @@ bool recursive = false;
unsigned char _dual_status = 0; unsigned char _dual_status = 0;
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
#include <my9291.h>
my9291 * _my9291;
Ticker colorTicker;
#endif
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// PROVIDER
// RELAY PROVIDERS
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
void setLightColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char white) {
// Set new color (if light is open it will automatically change)
_my9291->setColor((my9291_color_t) { red, green, blue, white });
// Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
colorTicker.once(5, saveLightColor);
}
String getLightColor() {
char buffer[16];
my9291_color_t color = _my9291->getColor();
sprintf(buffer, "%d,%d,%d,%d", color.red, color.green, color.blue, color.white);
return String(buffer);
}
void saveLightColor() {
my9291_color_t color = _my9291->getColor();
setSetting("colorRed", color.red);
setSetting("colorGreen", color.green);
setSetting("colorBlue", color.blue);
setSetting("colorWhite", color.white);
saveSettings();
}
void retrieveLightColor() {
unsigned int red = getSetting("colorRed", MY9291_COLOR_RED).toInt();
unsigned int green = getSetting("colorGreen", MY9291_COLOR_GREEN).toInt();
unsigned int blue = getSetting("colorBlue", MY9291_COLOR_BLUE).toInt();
unsigned int white = getSetting("colorWhite", MY9291_COLOR_WHITE).toInt();
_my9291->setColor((my9291_color_t) { red, green, blue, white });
}
#endif
void relayProviderStatus(unsigned char id, bool status) { void relayProviderStatus(unsigned char id, bool status) {
#if RELAY_PROVIDER == RELAY_PROVIDER_DUAL #if RELAY_PROVIDER == RELAY_PROVIDER_DUAL
@ -85,8 +41,8 @@ void relayProviderStatus(unsigned char id, bool status) {
Serial.flush(); Serial.flush();
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
_my9291->setState(status);
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
lightState(status);
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_RELAY #if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
@ -102,8 +58,8 @@ bool relayProviderStatus(unsigned char id) {
return ((_dual_status & (1 << id)) > 0); return ((_dual_status & (1 << id)) > 0);
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
return _my9291->getState();
#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
return lightState();
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_RELAY #if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
@ -444,11 +400,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str()); sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer); mqttSubscribe(buffer);
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
sprintf(buffer, "%s%s", MQTT_COLOR_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
#endif
} }
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
@ -458,33 +409,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
int len = mqttSetter.length(); int len = mqttSetter.length();
if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return; if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return;
// Color topic
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
if (strncmp(t, MQTT_COLOR_TOPIC, strlen(MQTT_COLOR_TOPIC)) == 0) {
unsigned char red, green, blue = 0;
char * p;
p = strtok((char *) payload, ",");
red = atoi(p);
p = strtok(NULL, ",");
if (p != NULL) {
green = atoi(p);
p = strtok(NULL, ",");
if (p != NULL) blue = atoi(p);
} else {
green = blue = red;
}
if ((red == green) && (green == blue)) {
setLightColor(0, 0, 0, red);
} else {
setLightColor(red, green, blue, 0);
}
return;
}
#endif
// Relay topic // Relay topic
if (strncmp(t, MQTT_RELAY_TOPIC, strlen(MQTT_RELAY_TOPIC)) == 0) { if (strncmp(t, MQTT_RELAY_TOPIC, strlen(MQTT_RELAY_TOPIC)) == 0) {
@ -555,11 +479,6 @@ void relaySetup() {
#endif #endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
_my9291 = new my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
retrieveLightColor();
#endif
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
for (unsigned int i=0; i < _relays.size(); i++) { for (unsigned int i=0; i < _relays.size(); i++) {
pinMode(_relays[i].pin, OUTPUT); pinMode(_relays[i].pin, OUTPUT);


+ 4
- 5
code/espurna/web.ino View File

@ -121,10 +121,9 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
if (action.equals("on")) relayStatus(relayID, true); if (action.equals("on")) relayStatus(relayID, true);
if (action.equals("off")) relayStatus(relayID, false); if (action.equals("off")) relayStatus(relayID, false);
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if (action.equals("color") && root.containsKey("data")) { if (action.equals("color") && root.containsKey("data")) {
JsonArray& data = root["data"];
setLightColor(data[0], data[1], data[2], data[3]);
lightColor(root["data"], true);
} }
#endif #endif
@ -389,9 +388,9 @@ void _wsStart(uint32_t client_id) {
relay.add(relayStatus(relayID)); relay.add(relayStatus(relayID));
} }
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
root["colorVisible"] = 1; root["colorVisible"] = 1;
root["color"] = getLightColor();
root["color"] = lightColor();
#endif #endif
root["relayMode"] = getSetting("relayMode", RELAY_MODE); root["relayMode"] = getSetting("relayMode", RELAY_MODE);


+ 3
- 17
code/html/custom.js View File

@ -32,20 +32,8 @@ function validateForm(form) {
} }
function doColor() { function doColor() {
var color = $(this).wheelColorPicker('getColor');
var data = [];
if ((color.r == color.g) && (color.g == color.b)) {
data.push(0);
data.push(0);
data.push(0);
data.push(parseInt(color.r * 255));
} else {
data.push(parseInt(color.r * 255));
data.push(parseInt(color.g * 255));
data.push(parseInt(color.b * 255));
data.push(0);
}
websock.send(JSON.stringify({'action': 'color', 'data' : data}));
var color = $(this).wheelColorPicker('getValue', 'css');
websock.send(JSON.stringify({'action': 'color', 'data' : color}));
} }
@ -293,9 +281,7 @@ function processData(data) {
} }
if (key == "color") { if (key == "color") {
var color = data[key].split(",");
if (color[3] > 0) color[0] = color[1] = color[2] = color[3];
$("input[name='color']").wheelColorPicker('setRgb', color[0] / 255, color[1] / 255, color[2] / 255, true);
$("input[name='color']").wheelColorPicker('setValue', data[key], true);
return; return;
} }


Loading…
Cancel
Save