Skip to content

Commit

Permalink
Support to set on/off state per channel using switches (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Jan 26, 2018
1 parent cd54372 commit 789e834
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 11 additions & 1 deletion code/espurna/light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Ticker _light_transition_ticker;
typedef struct {
unsigned char pin;
bool reverse;
bool state;
unsigned char value; // target or nominal value
unsigned char shadow; // represented value
double current; // transition value
Expand Down Expand Up @@ -381,7 +382,7 @@ void _shadow() {
// Transitions
unsigned char target;
for (unsigned int i=0; i < _light_channel.size(); i++) {
if (_light_state) {
if (_light_state && _light_channel[i].state) {
target = _light_channel[i].value;
if ((_light_brightness < LIGHT_MAX_BRIGHTNESS) && _light_has_color && (i < 3)) {
target *= ((float) _light_brightness / LIGHT_MAX_BRIGHTNESS);
Expand Down Expand Up @@ -659,6 +660,14 @@ void lightSave() {
}
#endif

void lightState(unsigned char i, bool state) {
_light_channel[i].state = state;
}

bool lightState(unsigned char i) {
return _light_channel[i].state;
}

void lightState(bool state) {
_light_state = state;
}
Expand Down Expand Up @@ -1027,6 +1036,7 @@ void lightSetup() {
io_info[i][1] = getIOFunc(_light_channel[i].pin);
io_info[i][2] = _light_channel[i].pin;
pinMode(_light_channel[i].pin, OUTPUT);
_light_channel[i].state = true;
}
pwm_init(LIGHT_MAX_PWM, pwm_duty_init, PWM_CHANNEL_NUM_MAX, io_info);
pwm_start();
Expand Down
24 changes: 23 additions & 1 deletion code/espurna/relay.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,30 @@ void _relayProviderStatus(unsigned char id, bool status) {
#endif

#if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
lightState(status);

// If the number of relays matches the number of light channels
// assume each relay controls one channel.
// If the number of relays is the number of channels plus 1
// assume the first one controls all the channels and
// the rest one channel each.
// Otherwise every relay controls all channels.
// TODO: this won't work with a mixed of dummy and real relays
// but this option is not allowed atm (YANGNI)
if (_relays.size() == lightChannels()) {
lightState(id, status);
lightState(true);
} else if (_relays.size() == lightChannels() + 1) {
if (id == 0) {
lightState(status);
} else {
lightState(id-1, status);
}
} else {
lightState(status);
}

lightUpdate(true, true);

#endif

#if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
Expand Down

0 comments on commit 789e834

Please sign in to comment.