Browse Source

Check relay and led id on 'public' methods

fastled
Xose Pérez 8 years ago
parent
commit
76e465cac2
2 changed files with 11 additions and 0 deletions
  1. +4
    -0
      code/src/led.ino
  2. +7
    -0
      code/src/relay.ino

+ 4
- 0
code/src/led.ino View File

@ -22,21 +22,25 @@ std::vector<led_t> _leds;
bool ledAuto; bool ledAuto;
bool ledStatus(unsigned char id) { bool ledStatus(unsigned char id) {
if (id <= _leds.size()) return false;
bool status = digitalRead(_leds[id].pin); bool status = digitalRead(_leds[id].pin);
return _leds[id].reverse ? !status : status; return _leds[id].reverse ? !status : status;
} }
bool ledStatus(unsigned char id, bool status) { bool ledStatus(unsigned char id, bool status) {
if (id <= _leds.size()) return false;
bool s = _leds[id].reverse ? !status : status; bool s = _leds[id].reverse ? !status : status;
digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status); digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
return status; return status;
} }
bool ledToggle(unsigned char id) { bool ledToggle(unsigned char id) {
if (id <= _leds.size()) return false;
return ledStatus(id, !ledStatus(id)); return ledStatus(id, !ledStatus(id));
} }
void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) { void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
if (id <= _leds.size()) return;
static unsigned long next = millis(); static unsigned long next = millis();
if (next < millis()) { if (next < millis()) {
next += (ledToggle(id) ? delayOn : delayOff); next += (ledToggle(id) ? delayOn : delayOff);


+ 7
- 0
code/src/relay.ino View File

@ -22,6 +22,7 @@ bool recursive = false;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void relayMQTT(unsigned char id) { void relayMQTT(unsigned char id) {
if (id >= _relays.size()) return;
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3]; char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str()); sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
@ -53,14 +54,18 @@ void relayWS() {
bool relayStatus(unsigned char id) { bool relayStatus(unsigned char id) {
#ifdef SONOFF_DUAL #ifdef SONOFF_DUAL
if (id >= 2) return false;
return ((dualRelayStatus & (1 << id)) > 0); return ((dualRelayStatus & (1 << id)) > 0);
#else #else
if (id >= _relays.size()) return false;
return (digitalRead(_relays[id]) == HIGH); return (digitalRead(_relays[id]) == HIGH);
#endif #endif
} }
bool relayStatus(unsigned char id, bool status, bool report) { bool relayStatus(unsigned char id, bool status, bool report) {
if (id >= _relays.size()) return false;
bool changed = false; bool changed = false;
if (relayStatus(id) != status) { if (relayStatus(id) != status) {
@ -96,6 +101,7 @@ bool relayStatus(unsigned char id, bool status, bool report) {
} }
bool relayStatus(unsigned char id, bool status) { bool relayStatus(unsigned char id, bool status) {
if (id >= _relays.size()) return false;
return relayStatus(id, status, true); return relayStatus(id, status, true);
} }
@ -159,6 +165,7 @@ void relayRetrieve() {
} }
void relayToggle(unsigned char id) { void relayToggle(unsigned char id) {
if (id >= _relays.size()) return;
relayStatus(id, !relayStatus(id)); relayStatus(id, !relayStatus(id));
} }


Loading…
Cancel
Save