Supla Device jak wywołać metodę z klasy migania diody?

[email protected]
Posts: 1584
Joined: Mon Feb 06, 2023 8:56 am
Has thanked: 18 times
Been thanked: 26 times

Post

@klew czy StatusLed obsługuje Leda Rgb? wydaje mi się, że nie i dopiero taka implementacja działa:

Jeśli nie to zrobię PR :)

Code: Select all

/*
   Copyright (C) AC SOFTWARE SP. Z O.O

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "blinking_led.h"

#include <supla/auto_lock.h>
#include <supla/io.h>
#include <supla/log_wrapper.h>
#include <supla/mutex.h>
#include <supla/time.h>

#ifdef ARDUINO_ARCH_ESP32
#include <Arduino.h>
#endif

using Supla::Control::BlinkingLed;

BlinkingLed::BlinkingLed(Supla::Io::Base *io, uint8_t outPin, bool invert)
    : BlinkingLed(outPin, invert) {
  this->io = io;
}

BlinkingLed::BlinkingLed(uint8_t outPin, bool invert)
    : outPin(outPin), invert(invert) {
  mutex = Supla::Mutex::Create();
}

void BlinkingLed::onInit() {
  Supla::AutoLock autoLock(mutex);
  if (state == NOT_INITIALIZED) {
    turnOff();
  }
  // Skip pinMode for RGB LEDs - they have special handling
  if (!isRgbLed()) {
    Supla::Io::pinMode(outPin, OUTPUT, io);
  }
  lastUpdate = 0;
  updatePin();
}

void BlinkingLed::onTimer() {
  Supla::AutoLock autoLock(mutex);
  updatePin();
}

void BlinkingLed::setInvertedLogic(bool invertedLogic) {
  Supla::AutoLock autoLock(mutex);
  invert = invertedLogic;
  updatePin();
}

void BlinkingLed::setAlwaysOffSequence() {
  setCustomSequence(0, 1000);
}

void BlinkingLed::setAlwaysOnSequence() {
  setCustomSequence(1000, 0);
}

void BlinkingLed::turnOn() {
  lastUpdate = millis();
  state = ON;
  
#ifdef ARDUINO_ARCH_ESP32
  if (isRgbLed()) {
    // For RGB LEDs, write white color when ON
    uint8_t brightness = invert ? 0 : 255;
    rgbLedWrite(outPin, brightness, brightness, brightness);
  } else {
    Supla::Io::digitalWrite(outPin, invert ? 0 : 1, io);
  }
#else
  Supla::Io::digitalWrite(outPin, invert ? 0 : 1, io);
#endif
  
  auto copy = copyStateTo;
  if (copy) {
    copy->turnOn();
  }
}

void BlinkingLed::turnOff() {
  lastUpdate = millis();
  state = OFF;
  
#ifdef ARDUINO_ARCH_ESP32
  if (isRgbLed()) {
    // For RGB LEDs, write zero brightness when OFF
    uint8_t brightness = invert ? 255 : 0;
    rgbLedWrite(outPin, brightness, brightness, brightness);
  } else {
    Supla::Io::digitalWrite(outPin, invert ? 1 : 0, io);
  }
#else
  Supla::Io::digitalWrite(outPin, invert ? 1 : 0, io);
#endif
  
  auto copy = copyStateTo;
  if (copy) {
    copy->turnOff();
  }
}

void BlinkingLed::updatePin() {
  if (!enabled) {
    return;
  }
  if (onDuration == 0 || offDuration == 0) {
    if ((state == ON || state == NOT_INITIALIZED) && onDuration == 0) {
      turnOff();
    }
    if ((state == OFF || state == NOT_INITIALIZED) && offDuration == 0) {
      turnOn();
    }
    return;
  }

  if (state == ON && millis() - lastUpdate > onDuration) {
    turnOff();
  } else if (state == OFF) {
    if (onLimit > 0 && pauseDuration > 0 && onLimitCounter == 0 &&
        millis() - lastUpdate < pauseDuration && lastUpdate != 0) {
      return;
    }
    if (millis() - lastUpdate > offDuration || lastUpdate == 0) {
      if (onLimit > 0 && pauseDuration > 0 && onLimitCounter == 0) {
        onLimitCounter = onLimit;
        if (repeatLimit > 0) {
          if (repeatLimit == 1) {
            onDuration = 0;
            offDuration = 1000;
            onLimit = 0;
            return;
          }
          repeatLimit--;
        }
      }
      turnOn();
      if (onLimitCounter > 0) {
        onLimitCounter--;
      }
    }
  }
}

void BlinkingLed::setCustomSequence(uint32_t onDurationMs,
                                    uint32_t offDurationMs,
                                    uint32_t pauseDurrationMs,
                                    uint8_t onLimit,
                                    uint8_t repeatLimit,
                                    bool startWithOff) {
  Supla::AutoLock autoLock(mutex);
  enable();
  onDuration = onDurationMs;
  offDuration = offDurationMs;
  pauseDuration = pauseDurrationMs;
  this->onLimit = onLimit;
  onLimitCounter = onLimit;
  this->repeatLimit = repeatLimit;
  lastUpdate = 0;
  autoLock.unlock();
  if (startWithOff && offDuration > 0) {
    turnOff();
  } else if (onDuration > 0) {
    turnOn();
  } else {
    turnOff();
  }
  updatePin();
}

void BlinkingLed::setCopyStateTo(BlinkingLed *led) {
  copyStateTo = led;

  if (copyStateTo != nullptr) {
    copyStateTo->disable();
    updatePin();
  }
}

void BlinkingLed::disable() {
  enabled = false;
}

void BlinkingLed::enable() {
  enabled = true;
}

bool BlinkingLed::isRgbLed() {
#ifdef ARDUINO_ARCH_ESP32
  // ESP32-S3 has RGB LED on GPIO48
  #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C3
    return true;
  #endif
#else
  return false;
#endif
}
User avatar
klew
Posts: 13906
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

[email protected] wrote: Tue Jan 13, 2026 6:21 am @klew czy StatusLed obsługuje Leda Rgb? wydaje mi się, że nie i dopiero taka implementacja działa:

Jeśli nie to zrobię PR :)

Code: Select all

/*
   Copyright (C) AC SOFTWARE SP. Z O.O

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "blinking_led.h"

#include <supla/auto_lock.h>
#include <supla/io.h>
#include <supla/log_wrapper.h>
#include <supla/mutex.h>
#include <supla/time.h>

#ifdef ARDUINO_ARCH_ESP32
#include <Arduino.h>
#endif

using Supla::Control::BlinkingLed;

BlinkingLed::BlinkingLed(Supla::Io::Base *io, uint8_t outPin, bool invert)
    : BlinkingLed(outPin, invert) {
  this->io = io;
}

BlinkingLed::BlinkingLed(uint8_t outPin, bool invert)
    : outPin(outPin), invert(invert) {
  mutex = Supla::Mutex::Create();
}

void BlinkingLed::onInit() {
  Supla::AutoLock autoLock(mutex);
  if (state == NOT_INITIALIZED) {
    turnOff();
  }
  // Skip pinMode for RGB LEDs - they have special handling
  if (!isRgbLed()) {
    Supla::Io::pinMode(outPin, OUTPUT, io);
  }
  lastUpdate = 0;
  updatePin();
}

void BlinkingLed::onTimer() {
  Supla::AutoLock autoLock(mutex);
  updatePin();
}

void BlinkingLed::setInvertedLogic(bool invertedLogic) {
  Supla::AutoLock autoLock(mutex);
  invert = invertedLogic;
  updatePin();
}

void BlinkingLed::setAlwaysOffSequence() {
  setCustomSequence(0, 1000);
}

void BlinkingLed::setAlwaysOnSequence() {
  setCustomSequence(1000, 0);
}

void BlinkingLed::turnOn() {
  lastUpdate = millis();
  state = ON;
  
#ifdef ARDUINO_ARCH_ESP32
  if (isRgbLed()) {
    // For RGB LEDs, write white color when ON
    uint8_t brightness = invert ? 0 : 255;
    rgbLedWrite(outPin, brightness, brightness, brightness);
  } else {
    Supla::Io::digitalWrite(outPin, invert ? 0 : 1, io);
  }
#else
  Supla::Io::digitalWrite(outPin, invert ? 0 : 1, io);
#endif
  
  auto copy = copyStateTo;
  if (copy) {
    copy->turnOn();
  }
}

void BlinkingLed::turnOff() {
  lastUpdate = millis();
  state = OFF;
  
#ifdef ARDUINO_ARCH_ESP32
  if (isRgbLed()) {
    // For RGB LEDs, write zero brightness when OFF
    uint8_t brightness = invert ? 255 : 0;
    rgbLedWrite(outPin, brightness, brightness, brightness);
  } else {
    Supla::Io::digitalWrite(outPin, invert ? 1 : 0, io);
  }
#else
  Supla::Io::digitalWrite(outPin, invert ? 1 : 0, io);
#endif
  
  auto copy = copyStateTo;
  if (copy) {
    copy->turnOff();
  }
}

void BlinkingLed::updatePin() {
  if (!enabled) {
    return;
  }
  if (onDuration == 0 || offDuration == 0) {
    if ((state == ON || state == NOT_INITIALIZED) && onDuration == 0) {
      turnOff();
    }
    if ((state == OFF || state == NOT_INITIALIZED) && offDuration == 0) {
      turnOn();
    }
    return;
  }

  if (state == ON && millis() - lastUpdate > onDuration) {
    turnOff();
  } else if (state == OFF) {
    if (onLimit > 0 && pauseDuration > 0 && onLimitCounter == 0 &&
        millis() - lastUpdate < pauseDuration && lastUpdate != 0) {
      return;
    }
    if (millis() - lastUpdate > offDuration || lastUpdate == 0) {
      if (onLimit > 0 && pauseDuration > 0 && onLimitCounter == 0) {
        onLimitCounter = onLimit;
        if (repeatLimit > 0) {
          if (repeatLimit == 1) {
            onDuration = 0;
            offDuration = 1000;
            onLimit = 0;
            return;
          }
          repeatLimit--;
        }
      }
      turnOn();
      if (onLimitCounter > 0) {
        onLimitCounter--;
      }
    }
  }
}

void BlinkingLed::setCustomSequence(uint32_t onDurationMs,
                                    uint32_t offDurationMs,
                                    uint32_t pauseDurrationMs,
                                    uint8_t onLimit,
                                    uint8_t repeatLimit,
                                    bool startWithOff) {
  Supla::AutoLock autoLock(mutex);
  enable();
  onDuration = onDurationMs;
  offDuration = offDurationMs;
  pauseDuration = pauseDurrationMs;
  this->onLimit = onLimit;
  onLimitCounter = onLimit;
  this->repeatLimit = repeatLimit;
  lastUpdate = 0;
  autoLock.unlock();
  if (startWithOff && offDuration > 0) {
    turnOff();
  } else if (onDuration > 0) {
    turnOn();
  } else {
    turnOff();
  }
  updatePin();
}

void BlinkingLed::setCopyStateTo(BlinkingLed *led) {
  copyStateTo = led;

  if (copyStateTo != nullptr) {
    copyStateTo->disable();
    updatePin();
  }
}

void BlinkingLed::disable() {
  enabled = false;
}

void BlinkingLed::enable() {
  enabled = true;
}

bool BlinkingLed::isRgbLed() {
#ifdef ARDUINO_ARCH_ESP32
  // ESP32-S3 has RGB LED on GPIO48
  #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C3
    return true;
  #endif
#else
  return false;
#endif
}
To czy wspiera czy nie, zależy od tego jakiej diody używasz.
Te klasy pracują tylko on/off.
Nie dodawaj tego kodu w ten sposób.
Dodaj własną implementację Supła::Io i podaj ją do konstruktora status LED. Wtedy Io robi za HAL
Najlepsze suple dla Twojego domu :mrgreen:
[email protected]
Posts: 1584
Joined: Mon Feb 06, 2023 8:56 am
Has thanked: 18 times
Been thanked: 26 times

Post

klew wrote: Tue Jan 13, 2026 8:03 am
To czy wspiera czy nie, zależy od tego jakiej diody używasz.
Te klasy pracują tylko on/off.
Nie dodawaj tego kodu w ten sposób.
Dodaj własną implementację Supła::Io i podaj ją do konstruktora status LED. Wtedy Io robi za HAL
No jak to jakiej, tej na płytce 😤
Czyli mam rozumieć że nie tylko inaczej to zaimplementować?
User avatar
lukfud
Posts: 2480
Joined: Thu Nov 23, 2017 11:33 pm
Location: Warszawa
Has thanked: 13 times
Been thanked: 11 times

Post

[email protected] wrote: Tue Jan 13, 2026 8:11 am No jak to jakiej, tej na płytce 😤
Czyli mam rozumieć że nie tylko inaczej to zaimplementować?
Mój przykład:

Code: Select all

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel oPixels(5, -1, NEO_GRB + NEO_KHZ800);

constexpr uint8_t STATUS_PIXEL = 1;

Code: Select all

namespace Supla {
class ExtLed : public Supla::Io::Base {
 public:
  ExtLed(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0)
    : Supla::Io::Base(false), red_(red), green_(green), blue_(blue) {}

  void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {}

  void customDigitalWrite(
                         int channelNumber, uint8_t pin, uint8_t val) override {
    if (val == HIGH) {
      oPixels.setPixelColor(pin, oPixels.Color(red_, green_, blue_));
      oPixels.show();
    } else {
      oPixels.setPixelColor(pin, oPixels.Color(0, 0, 0));
      oPixels.show();
    }
  }

  int customDigitalRead(int channelNumber, uint8_t pin) override {
    return 0;
  }

  unsigned int customPulseIn(int channelNumber,
                   uint8_t pin, uint8_t value, uint64_t timeoutMicro) override {
    return 0;
  }

  void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}

  int customAnalogRead(int channelNumber, uint8_t pin) override {
    return 0;
  }

 protected:
  uint8_t red_ = 0;
  uint8_t green_ = 0;
  uint8_t blue_ = 0;
};
};

Code: Select all

auto extCfgLed = new Supla::ExtLed(0, 0, 255);
new Supla::Device::StatusLed(extCfgLed, STATUS_PIXEL);
https://www.facebook.com/groups/supladiy
https://www.facebook.com/groups/suplazamel
https://www.facebook.com/groups/suplaauraton
https://smartnerzeczy.pl
[email protected]
Posts: 1584
Joined: Mon Feb 06, 2023 8:56 am
Has thanked: 18 times
Been thanked: 26 times

Post

lukfud wrote: Tue Jan 13, 2026 8:21 am
[email protected] wrote: Tue Jan 13, 2026 8:11 am No jak to jakiej, tej na płytce 😤
Czyli mam rozumieć że nie tylko inaczej to zaimplementować?
Mój przykład:

Code: Select all

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel oPixels(5, -1, NEO_GRB + NEO_KHZ800);

constexpr uint8_t STATUS_PIXEL = 1;

Code: Select all

namespace Supla {
class ExtLed : public Supla::Io::Base {
 public:
  ExtLed(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0)
    : Supla::Io::Base(false), red_(red), green_(green), blue_(blue) {}

  void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {}

  void customDigitalWrite(
                         int channelNumber, uint8_t pin, uint8_t val) override {
    if (val == HIGH) {
      oPixels.setPixelColor(pin, oPixels.Color(red_, green_, blue_));
      oPixels.show();
    } else {
      oPixels.setPixelColor(pin, oPixels.Color(0, 0, 0));
      oPixels.show();
    }
  }

  int customDigitalRead(int channelNumber, uint8_t pin) override {
    return 0;
  }

  unsigned int customPulseIn(int channelNumber,
                   uint8_t pin, uint8_t value, uint64_t timeoutMicro) override {
    return 0;
  }

  void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {}

  int customAnalogRead(int channelNumber, uint8_t pin) override {
    return 0;
  }

 protected:
  uint8_t red_ = 0;
  uint8_t green_ = 0;
  uint8_t blue_ = 0;
};
};

Code: Select all

auto extCfgLed = new Supla::ExtLed(0, 0, 255);
new Supla::Device::StatusLed(extCfgLed, STATUS_PIXEL);
Czad! Dzięki za podpowiedź
User avatar
Robert Błaszczak
Posts: 5222
Joined: Sat Dec 22, 2018 8:55 pm
Location: Zielona Góra
Has thanked: 37 times
Been thanked: 27 times

Post

Od jakiegoś czasu boardy ESP32 obsługują natywnie adresowalne LED RGB. Nie ma potrzeby stosowania dodatkowych bibliotek (np. Adafruit_NeoPixel.h).
Pozdrawiam
Robert Błaszczak


Moja prywatna strona: www.blaszczak.pl

Return to “Arduino IDE”