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 ledStatus(unsigned char id) {
if (id <= _leds.size()) return false;
bool status = digitalRead(_leds[id].pin);
return _leds[id].reverse ? !status : status;
}
bool ledStatus(unsigned char id, bool status) {
if (id <= _leds.size()) return false;
bool s = _leds[id].reverse ? !status : status;
digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
return status;
}
bool ledToggle(unsigned char id) {
if (id <= _leds.size()) return false;
return ledStatus(id, !ledStatus(id));
}
void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
if (id <= _leds.size()) return;
static unsigned long next = millis();
if (next < millis()) {
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) {
if (id >= _relays.size()) return;
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
@ -53,14 +54,18 @@ void relayWS() {
bool relayStatus(unsigned char id) {
#ifdef SONOFF_DUAL
if (id >= 2) return false;
return ((dualRelayStatus & (1 << id)) > 0);
#else
if (id >= _relays.size()) return false;
return (digitalRead(_relays[id]) == HIGH);
#endif
}
bool relayStatus(unsigned char id, bool status, bool report) {
if (id >= _relays.size()) return false;
bool changed = false;
if (relayStatus(id) != status) {
@ -96,6 +101,7 @@ bool relayStatus(unsigned char id, bool status, bool report) {
}
bool relayStatus(unsigned char id, bool status) {
if (id >= _relays.size()) return false;
return relayStatus(id, status, true);
}
@ -159,6 +165,7 @@ void relayRetrieve() {
}
void relayToggle(unsigned char id) {
if (id >= _relays.size()) return;
relayStatus(id, !relayStatus(id));
}


Loading…
Cancel
Save