Browse Source

Change light module to use soft config

v2
Xose Pérez 6 years ago
parent
commit
9aafa58fcd
6 changed files with 92 additions and 278 deletions
  1. +3
    -0
      code/espurna/config/general.h
  2. +27
    -230
      code/espurna/config/hardware.h
  3. +0
    -2
      code/espurna/config/prototypes.h
  4. +3
    -3
      code/espurna/hardware.ino
  5. +12
    -5
      code/espurna/ir.ino
  6. +47
    -38
      code/espurna/light.ino

+ 3
- 0
code/espurna/config/general.h View File

@ -775,6 +775,9 @@
#define LIGHT_SAVE_DELAY 5 // Persist color after 5 seconds to avoid wearing out
#endif
#ifndef LIGHT_MAX_CHANNELS
#define LIGHT_MAX_CHANNELS 6
#endif
#ifndef LIGHT_MAX_PWM


+ 27
- 230
code/espurna/config/hardware.h View File

@ -194,6 +194,32 @@ enum boards {
// -----------------------------------------------------------------------------
#elif \
defined(ARILUX_AL_LC01) || \
defined(ARILUX_AL_LC02) || \
defined(ARILUX_AL_LC06) || \
defined(ARILUX_AL_LC11) || \
defined(AUTHOMETION_LYT8266) || \
defined(GENERIC_AG_L4) || \
defined(HUACANXING_H801) || \
defined(HUACANXING_H802) || \
defined(INTERMITTECH_QUINLED) || \
defined(ITEAD_BNSZ01) || \
defined(ITEAD_SONOFF_LED)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// -----------------------------------------------------------------------------
#elif \
defined(AITHINKER_AI_LIGHT) || \
defined(ARILUX_E27) || \
defined(ITEAD_SONOFF_B1)
#define ESPURNA_IMAGE ESPURNA_MY92XX
// -----------------------------------------------------------------------------
#elif \
defined(GENERIC_ESP01S_DHT11_V10) || \
defined(GENERIC_ESP01S_DS18B20_V10) || \
@ -283,69 +309,10 @@ enum boards {
// -----------------------------------------------------------------------------
#elif defined(ITEAD_BNSZ01)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 1
#define LIGHT_CH1_PIN 12
#define LIGHT_CH1_INVERSE 0
#elif defined(ITEAD_SONOFF_B1)
#define ESPURNA_IMAGE ESPURNA_MY92XX
// Info
#define LIGHT_CHANNELS 5
#define MY92XX_MODEL MY92XX_MODEL_MY9231
#define MY92XX_CHIPS 2
#define MY92XX_DI_PIN 12
#define MY92XX_DCKI_PIN 14
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#define MY92XX_MAPPING 4, 3, 5, 0, 1
#define LIGHT_WHITE_FACTOR (0.1) // White LEDs are way more bright in the B1
#elif defined(ITEAD_SONOFF_LED)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 2
#define LIGHT_CH1_PIN 12 // Cold white
#define LIGHT_CH2_PIN 14 // Warm white
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#elif defined(AITHINKER_AI_LIGHT)
#define ESPURNA_IMAGE ESPURNA_MY92XX
// Info
#define LIGHT_CHANNELS 4
#define MY92XX_MODEL MY92XX_MODEL_MY9291
#define MY92XX_CHIPS 1
#define MY92XX_DI_PIN 13
#define MY92XX_DCKI_PIN 15
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#define MY92XX_MAPPING 0, 1, 2, 3
#elif defined(MAGICHOME_LED_CONTROLLER)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define DUMMY_RELAY_COUNT 1
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 14 // RED
#define LIGHT_CH2_PIN 5 // GREEN
#define LIGHT_CH3_PIN 12 // BLUE
#define LIGHT_CH4_PIN 13 // WHITE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
// IR
#define IR_SUPPORT 1
#define IR_RECEIVER_PIN 4
@ -355,188 +322,17 @@ enum boards {
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 5 // RED
#define LIGHT_CH2_PIN 12 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH4_PIN 15 // WHITE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
// IR
#define IR_SUPPORT 1
#define IR_RECEIVER_PIN 4
#define IR_BUTTON_SET 1
#elif defined(HUACANXING_H801)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define DEBUG_PORT Serial1
#define SERIAL_RX_ENABLED 1
#define LIGHT_CHANNELS 5
#define LIGHT_CH1_PIN 15 // RED
#define LIGHT_CH2_PIN 13 // GREEN
#define LIGHT_CH3_PIN 12 // BLUE
#define LIGHT_CH4_PIN 14 // WHITE1
#define LIGHT_CH5_PIN 4 // WHITE2
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#define LIGHT_CH5_INVERSE 0
#elif defined(HUACANXING_H802)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define DEBUG_PORT Serial1
#define SERIAL_RX_ENABLED 1
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 12 // RED
#define LIGHT_CH2_PIN 14 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH4_PIN 15 // WHITE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#elif defined(INTERMITTECH_QUINLED)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 2
#define LIGHT_CH1_PIN 0
#define LIGHT_CH2_PIN 2
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#elif defined(ARILUX_AL_LC01)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 3
#define LIGHT_CH1_PIN 5 // RED
#define LIGHT_CH2_PIN 12 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#elif defined(ARILUX_AL_LC02)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 12 // RED
#define LIGHT_CH2_PIN 5 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH4_PIN 15 // WHITE1
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#elif defined(ARILUX_AL_LC06)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 5
#define LIGHT_CH1_PIN 14 // RED
#define LIGHT_CH2_PIN 12 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH4_PIN 15 // WHITE1
#define LIGHT_CH5_PIN 5 // WHITE2
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#define LIGHT_CH5_INVERSE 0
#elif defined(ARILUX_AL_LC11)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 5
#define LIGHT_CH1_PIN 5 // RED
#define LIGHT_CH2_PIN 4 // GREEN
#define LIGHT_CH3_PIN 14 // BLUE
#define LIGHT_CH4_PIN 13 // WHITE1
#define LIGHT_CH5_PIN 12 // WHITE1
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#define LIGHT_CH5_INVERSE 0
#elif defined(ARILUX_E27)
#define ESPURNA_IMAGE ESPURNA_MY92XX
// Info
#define LIGHT_CHANNELS 4
#define MY92XX_MODEL MY92XX_MODEL_MY9291
#define MY92XX_CHIPS 1
#define MY92XX_DI_PIN 13
#define MY92XX_DCKI_PIN 15
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#define MY92XX_MAPPING 0, 1, 2, 3
#elif defined(AUTHOMETION_LYT8266)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 13 // RED
#define LIGHT_CH2_PIN 12 // GREEN
#define LIGHT_CH3_PIN 14 // BLUE
#define LIGHT_CH4_PIN 2 // WHITE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0
#define LIGHT_ENABLE_PIN 15
#elif defined(GIZWITS_WITTY_CLOUD)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 3
#define LIGHT_CH1_PIN 15 // RED
#define LIGHT_CH2_PIN 12 // GREEN
#define LIGHT_CH3_PIN 13 // BLUE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define ANALOG_SUPPORT 1 // TODO: specific or generic?
#elif defined(GENERIC_AG_L4)
#define ESPURNA_IMAGE ESPURNA_DIMMER
// Info
#define LIGHT_CHANNELS 3
#define LIGHT_CH1_PIN 14 // RED
#define LIGHT_CH2_PIN 13 // GREEN
#define LIGHT_CH3_PIN 12 // BLUE
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
// -----------------------------------------------------------------------------
// TEST boards (do not use!!)
// -----------------------------------------------------------------------------
@ -743,6 +539,7 @@ enum boards {
#define RELAY_PROVIDER RELAY_PROVIDER_LIGHT
#define LIGHT_PROVIDER LIGHT_PROVIDER_MY92XX
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#elif ESPURNA_IMAGE == ESPURNA_EMON


+ 0
- 2
code/espurna/config/prototypes.h View File

@ -129,8 +129,6 @@ void settingsRegisterKeyCheck(setting_key_check_callback_f callback);
char * ltrim(char * s);
void nice_delay(unsigned long ms);
#define ARRAYINIT(type, name, ...) type name[] = {__VA_ARGS__};
// -----------------------------------------------------------------------------
// WebServer
// -----------------------------------------------------------------------------


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

@ -375,7 +375,7 @@ void _hardwareLoad() {
setSetting("litProvider", LIGHT_PROVIDER_MY92XX);
setSetting("myModel", MY92XX_MODEL_MY9291); // 4 channels per chip
setSetting("myModel", 0); // 4 channels per chip
setSetting("myChips", 1);
setSetting("myDIGPIO", 13);
setSetting("myDCKIGPIO", 15);
@ -578,7 +578,7 @@ void _hardwareLoad() {
setSetting("litChFactor", 4, 0.1); // White LEDs are way more bright in the B1
setSetting("litChFactor", 5, 0.1); // White LEDs are way more bright in the B1
setSetting("myModel", MY92XX_MODEL_MY9231); // 3 channels per chip
setSetting("myModel", 1); // 3 channels per chip
setSetting("myChips", 2);
setSetting("myDIGPIO", 12);
setSetting("myDCKIGPIO", 14);
@ -945,7 +945,7 @@ void _hardwareLoad() {
setSetting("litProvider", LIGHT_PROVIDER_MY92XX);
setSetting("myModel", MY92XX_MODEL_MY9291); // 4 channels per chip
setSetting("myModel", 0); // 4 channels per chip
setSetting("myChips", 1);
setSetting("myDIGPIO", 13);
setSetting("myDCKIGPIO", 15);


+ 12
- 5
code/espurna/ir.ino View File

@ -14,6 +14,7 @@ Module key prefix: ir
#include <IRremoteESP8266.h>
#include <IRrecv.h>
bool _ir_enabled = true;
IRrecv * _ir_recv;
decode_results _ir_results;
unsigned long _ir_last_toggle = 0;
@ -113,15 +114,21 @@ bool _irKeyCheck(const char * key) {
void irSetup() {
_ir_recv = new IRrecv(IR_RECEIVER_PIN);
_ir_recv->enableIRIn();
_ir_enabled = (getSetting("irEnabled", 1).toInt() == 1);
if (_ir_enabled) {
_ir_recv = new IRrecv(getSetting("irGPIO", IR_RECEIVER_PIN).toInt());
_ir_recv->enableIRIn();
// Register loop
espurnaRegisterLoop(irLoop);
}
// Key Check
settingsRegisterKeyCheck(_irKeyCheck);
// Register loop
espurnaRegisterLoop(irLoop);
}
void irLoop() {


+ 47
- 38
code/espurna/light.ino View File

@ -19,7 +19,7 @@ extern "C" {
}
#if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
#define PWM_CHANNEL_NUM_MAX LIGHT_CHANNELS
#define PWM_CHANNEL_NUM_MAX LIGHT_MAX_CHANNELS
extern "C" {
#include "libs/pwm.h"
}
@ -32,12 +32,14 @@ Ticker _light_transition_ticker;
typedef struct {
unsigned char pin;
bool reverse;
bool inverse;
bool state;
unsigned char inputValue; // value that has been inputted
unsigned char value; // normalized value including brightness
unsigned char shadow; // represented value
double current; // transition value
double factor; // correction factor
unsigned char mapping; // channel mapping for MY90XX
} channel_t;
std::vector<channel_t> _light_channel;
@ -55,7 +57,6 @@ unsigned int _light_mireds = round((LIGHT_COLDWHITE_MIRED+LIGHT_WARMWHITE_MIRED)
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
#include <my92xx.h>
my92xx * _my92xx;
ARRAYINIT(unsigned char, _light_channel_map, MY92XX_MAPPING);
#endif
// Gamma Correction lookup table (8 bit)
@ -131,12 +132,12 @@ void _generateBrightness() {
double factor = (max_out > 0) ? (double) (max_in / max_out) : 0;
for (unsigned char i=0; i < channelSize; i++) {
_light_channel[i].value = round((double) _light_channel[i].value * factor * brightness);
_light_channel[i].value = round((double) _light_channel[i].value * factor * _light_channel[i].factor * brightness);
}
// Scale white channel to match brightness
for (unsigned char i=3; i < channelSize; i++) {
_light_channel[i].value = constrain(_light_channel[i].value * LIGHT_WHITE_FACTOR, 0, LIGHT_MAX_BRIGHTNESS);
_light_channel[i].value = constrain(_light_channel[i].value * _light_channel[i].factor, 0, LIGHT_MAX_BRIGHTNESS);
}
// For the rest of channels, don't apply brightness, it is already in the inputValue
@ -394,18 +395,18 @@ void _toCSV(char * buffer, size_t len, bool applyBrightness) {
// PROVIDER
// -----------------------------------------------------------------------------
unsigned int _toPWM(unsigned long value, bool gamma, bool reverse) {
unsigned int _toPWM(unsigned long value, bool gamma, bool inverse) {
value = constrain(value, 0, LIGHT_MAX_VALUE);
if (gamma) value = _light_gamma_table[value];
if (LIGHT_MAX_VALUE != LIGHT_LIMIT_PWM) value = map(value, 0, LIGHT_MAX_VALUE, 0, LIGHT_LIMIT_PWM);
if (reverse) value = LIGHT_LIMIT_PWM - value;
if (inverse) value = LIGHT_LIMIT_PWM - value;
return value;
}
// Returns a PWM value for the given channel ID
unsigned int _toPWM(unsigned char id) {
bool useGamma = _light_use_gamma && _light_has_color && (id < 3);
return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].reverse);
return _toPWM(_light_channel[id].shadow, useGamma, _light_channel[id].inverse);
}
void _shadow() {
@ -440,7 +441,7 @@ void _lightProviderUpdate() {
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
for (unsigned char i=0; i<_light_channel.size(); i++) {
_my92xx->setChannel(_light_channel_map[i], _toPWM(i));
_my92xx->setChannel(_light_channel[i].mapping, _toPWM(i));
}
_my92xx->setState(true);
_my92xx->update();
@ -1007,6 +1008,11 @@ void _lightConfigure() {
_light_use_transitions = getSetting("litTrans", LIGHT_USE_TRANSITIONS).toInt() == 1;
_light_transition_time = getSetting("litTime", LIGHT_TRANSITION_TIME).toInt();
for (unsigned char i=0; i<_light_channel.size(); i++) {
channel_t channel = _light_channel[i];
channel.factor = getSetting("litChFactor", i, 1).toFloat();
}
}
bool _lightKeyCheck(const char * key) {
@ -1033,41 +1039,44 @@ void lightSetup() {
_lightBackwards();
#ifdef LIGHT_ENABLE_PIN
pinMode(LIGHT_ENABLE_PIN, OUTPUT);
digitalWrite(LIGHT_ENABLE_PIN, HIGH);
#endif
{
unsigned char gpio = getSetting("litEnableGPIO", GPIO_NONE).toInt();
if (GPIO_NONE != gpio) {
pinMode(gpio, OUTPUT);
digitalWrite(gpio, getSetting("litEnableLogic", GPIO_LOGIC_DIRECT).toInt() == GPIO_LOGIC_DIRECT ? HIGH : LOW);
}
}
#if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX
_my92xx = new my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND);
for (unsigned char i=0; i<LIGHT_CHANNELS; i++) {
_light_channel.push_back((channel_t) {0, false, true, 0, 0, 0});
{
_my92xx = new my92xx(
(my92xx_model_t) getSetting("myModel", MY92XX_MODEL_MY9291).toInt(),
getSetting("myChips", 1).toInt(),
getSetting("myDIGPIO", 0).toInt(),
getSetting("myDCKIGPIO", 0).toInt(),
MY92XX_COMMAND
);
const char * map = getSetting("myMapping", "").c_str();
unsigned char channels = strlen(map);
for (unsigned char i=0; i<channels; i++) {
_light_channel.push_back((channel_t) {0, false, true, 0, 0, 0, 0, 1, map[i] - '0'});
}
}
#endif
#if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
#ifdef LIGHT_CH1_PIN
_light_channel.push_back((channel_t) {LIGHT_CH1_PIN, LIGHT_CH1_INVERSE, true, 0, 0, 0});
#endif
#ifdef LIGHT_CH2_PIN
_light_channel.push_back((channel_t) {LIGHT_CH2_PIN, LIGHT_CH2_INVERSE, true, 0, 0, 0});
#endif
#ifdef LIGHT_CH3_PIN
_light_channel.push_back((channel_t) {LIGHT_CH3_PIN, LIGHT_CH3_INVERSE, true, 0, 0, 0});
#endif
#ifdef LIGHT_CH4_PIN
_light_channel.push_back((channel_t) {LIGHT_CH4_PIN, LIGHT_CH4_INVERSE, true, 0, 0, 0});
#endif
#ifdef LIGHT_CH5_PIN
_light_channel.push_back((channel_t) {LIGHT_CH5_PIN, LIGHT_CH5_INVERSE, true, 0, 0, 0});
#endif
{
unsigned char gpio;
unsigned char index = 0;
while ((gpio = getSetting("litChGPIO", index, GPIO_NONE).toInt()) != GPIO_NONE) {
bool inverse = getSetting("litChLogic", index, GPIO_LOGIC_DIRECT).toInt() == GPIO_LOGIC_INVERSE;
_light_channel.push_back((channel_t) {gpio, inverse, true, 0, 0, 0, 0, 1});
++index;
}
uint32 pwm_duty_init[PWM_CHANNEL_NUM_MAX];
uint32 io_info[PWM_CHANNEL_NUM_MAX][3];
@ -1081,7 +1090,7 @@ void lightSetup() {
pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
pwm_start();
}
#endif
DEBUG_MSG_P(PSTR("[LIGHT] LIGHT_PROVIDER = %d\n"), LIGHT_PROVIDER);


Loading…
Cancel
Save