Skip to content

Commit

Permalink
v1.2.7
Browse files Browse the repository at this point in the history
- Added smart LEDs strip/matrix examples
- Added *Events.Disable* module property
  • Loading branch information
genemars committed Apr 1, 2024
1 parent 0c42549 commit 33407a4
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 34 deletions.
50 changes: 43 additions & 7 deletions examples/color-light/color-light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,27 @@ using namespace Service::API::devices;

HomeGenie* homeGenie;

Adafruit_NeoPixel pixels(1, CONFIG_StatusLedNeoPixelPin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel statusLED(1, CONFIG_StatusLedNeoPixelPin, NEO_GRB + NEO_KHZ800);

#ifdef LED_ARRAY_COUNT
int num = LED_ARRAY_COUNT; // 90 = 3mt // 30 LEDs per meter (3 mt. strip)
int pin = LED_ARRAY_PIN;
Adafruit_NeoPixel pixels(num, pin, NEO_RGB + NEO_KHZ800);
#endif

bool changed = false;

void statusLedCallback(bool isLedOn) {
if (isLedOn) {
pixels.setPixelColor(0, Adafruit_NeoPixel::Color(50, 50, 0));
statusLED.setPixelColor(0, Adafruit_NeoPixel::Color(50, 50, 0));
} else {
pixels.setPixelColor(0, Adafruit_NeoPixel::Color(0, 0, 0));
statusLED.setPixelColor(0, Adafruit_NeoPixel::Color(0, 0, 0));
}
pixels.show();
statusLED.show();
}

void setup() {
pixels.begin();
statusLED.begin();

homeGenie = HomeGenie::getInstance();

Expand All @@ -61,11 +69,28 @@ void setup() {

auto colorLight = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, "C1", "Demo Light");
colorLight->onSetColor([](float r, float g, float b) {
pixels.setPixelColor(0, r, g, b);
pixels.show();
statusLED.setPixelColor(0, g, r, b);
#ifdef LED_ARRAY_COUNT
for (int i = 0; i < num; i++) {
pixels.setPixelColor(i, r, g, b);
}
#endif
changed = true;
});
homeGenie->addAPIHandler(colorLight);

#ifdef LED_ARRAY_COUNT
for (int i = 0; i < num; i++) {
auto address = String("L") + String(i + 1);
auto cl = new ColorLight(IO::IOEventDomains::HomeAutomation_HomeGenie, address.c_str(), "Demo Light");
cl->onSetColor([i](float r, float g, float b) {
pixels.setPixelColor(i, r, g, b);
changed = true;
});
homeGenie->addAPIHandler(cl);
}
#endif

// TODO: implement color/status recall on start
// TODO: implement color/status recall on start
// TODO: implement color/status recall on start
Expand All @@ -75,7 +100,18 @@ void setup() {
homeGenie->begin();
}

unsigned long ts = 0;

void loop()
{
homeGenie->loop();

if (changed) { //&& millis()-ts > 50) { // force 20fps max
changed = false;
statusLED.show();
#ifdef LED_ARRAY_COUNT
pixels.show();
#endif
ts = millis();
}
}
45 changes: 45 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,51 @@ board_build.partitions = min_spiffs.csv
lib_deps = ${env.lib_deps}
https://github.com/adafruit/[email protected]

[env:color-light-c3-25]
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = -Os -I examples -I src -D ESP32_C3 -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=25 -D LED_ARRAY_PIN=8 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
board_build.flash_size = 4MB
board_build.partitions = min_spiffs.csv
lib_deps = ${env.lib_deps}
https://github.com/adafruit/[email protected]

[env:color-light-c3-64]
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = -Os -I examples -I src -D ESP32_C3 -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=64 -D LED_ARRAY_PIN=5 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
board_build.flash_size = 4MB
board_build.partitions = min_spiffs.csv
lib_deps = ${env.lib_deps}
https://github.com/adafruit/[email protected]

[env:color-light-c3-90]
platform = [email protected]
board = esp32-c3-devkitc-02
#board = esp32-c3-devkitm-1
; change microcontroller
#board_build.mcu = esp32c3
; change MCU frequency
#board_build.f_cpu = 160000000L
build_flags = -Os -I examples -I src -D ESP32_C3 -D CONFIG_StatusLedPin=-1 -D LED_ARRAY_COUNT=90 -D LED_ARRAY_PIN=5 -D ARDUINO_USB_MODE=1 -D ARDUINO_USB_CDC_ON_BOOT=1
build_src_filter = +<src> -<src/main.cpp> +<examples/color-light>
board_build.flash_size = 4MB
board_build.partitions = min_spiffs.csv
lib_deps = ${env.lib_deps}
https://github.com/adafruit/[email protected]


[env:shutter]
platform = [email protected]
Expand Down
10 changes: 5 additions & 5 deletions src/io/IOEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ namespace IO {
virtual void sendEvent(const char *domain, const char *address, const uint8_t *eventPath, void *eventData, IOEventDataType dataType) {
if (eventReceiver != nullptr) {
eventReceiver->onIOEvent(this, domain, address, eventPath, eventData, dataType);
lastEventMs = millis();
}
};
virtual void sendEvent(const uint8_t *eventPath, void *eventData, IOEventDataType dataType) {
if (eventReceiver != nullptr && module != nullptr) {
eventReceiver->onIOEvent(this, module->domain.c_str(), module->address.c_str(), eventPath, eventData, dataType);
lastEventMs = millis();
auto eventsDisable = module->getProperty("Events.Disable");
if (eventsDisable == nullptr || eventsDisable->value == nullptr || eventsDisable->value != "1") {
eventReceiver->onIOEvent(this, module->domain.c_str(), module->address.c_str(), eventPath, eventData, dataType);
}
}
};

protected:
IIOEventReceiver *eventReceiver = nullptr;
unsigned long lastEventMs = 0;
const Module* module = nullptr;
Module* module = nullptr;
};

}
Expand Down
3 changes: 2 additions & 1 deletion src/net/NetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ namespace Net {
Logger::verbose("%s loop() >> BEGIN", NETMANAGER_LOG_PREFIX);

if (ESP_WIFI_STATUS == WL_CONNECTED) {
webSocket->loop();
for (int i = 0; i < 5; i++) // higher priority
webSocket->loop();
}

Logger::verbose("%s loop() << END", NETMANAGER_LOG_PREFIX);
Expand Down
14 changes: 13 additions & 1 deletion src/service/EventRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,19 @@ namespace Service {

void EventRouter::signalEvent(QueuedMessage m) {
if (WiFi.isConnected()) {
eventsQueue.add(m);
bool updated = false;
for (int i = 0; i < eventsQueue.size(); i++) {
auto qm = eventsQueue.get(i);
if (qm.domain == m.domain && qm.sender == m.sender && qm.event == m.event && qm.type == m.type) {
qm.data = m.data;
qm.value = m.value;
updated = true;
break;
}
}
if (!updated) {
eventsQueue.add(m);
}
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/service/api/devices/ColorLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ namespace Service { namespace API { namespace devices {

color.setColor(o[0], o[1], o[2], o[3]*1000);


// Event Stream Message Enqueue (for MQTT/SSE/WebSocket propagation)
auto eventValue = command->getOption(0);
auto msg = QueuedMessage(m, IOEventPaths::Status_ColorHsb, eventValue, nullptr, IOEventDataType::Undefined);
m->setProperty(IOEventPaths::Status_ColorHsb, eventValue, nullptr, IOEventDataType::Undefined);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
// level prop
auto levelValue = String(o[2]); // TODO: use sprintf %.6f
auto msg2 = QueuedMessage(m, IOEventPaths::Status_Level, levelValue, nullptr, IOEventDataType::Undefined);
m->setProperty(IOEventPaths::Status_Level, levelValue, nullptr, IOEventDataType::Undefined);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg2);
auto eventsDisable = module->getProperty("Events.Disable");
if (eventsDisable == nullptr || eventsDisable->value == nullptr || eventsDisable->value != "1") {
// color
auto eventValue = command->getOption(0);
auto msg = QueuedMessage(m, IOEventPaths::Status_ColorHsb, eventValue, nullptr, IOEventDataType::Undefined);
m->setProperty(IOEventPaths::Status_ColorHsb, eventValue, nullptr, IOEventDataType::Undefined);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
// level
auto levelValue = String(o[2]); // TODO: use sprintf %.6f
auto msg2 = QueuedMessage(m, IOEventPaths::Status_Level, levelValue, nullptr, IOEventDataType::Undefined);
m->setProperty(IOEventPaths::Status_Level, levelValue, nullptr, IOEventDataType::Undefined);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg2);
}

if (o[2] > 0) {
Switch::status = SWITCH_STATUS_ON;
Expand Down
13 changes: 8 additions & 5 deletions src/service/api/devices/Dimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ namespace Service { namespace API { namespace devices {
level.setLevel(l, transition);

// Event Stream Message Enqueue (for MQTT/SSE/WebSocket propagation)
auto eventPath = IOEventPaths::Status_Level;
auto eventValue = String(l);
auto msg = QueuedMessage(m, eventPath, eventValue, &l, IOEventDataType::Float);
m->setProperty(eventPath, eventValue, &l, IOEventDataType::Float);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
auto eventsDisable = module->getProperty("Events.Disable");
if (eventsDisable == nullptr || eventsDisable->value == nullptr || eventsDisable->value != "1") {
auto eventPath = IOEventPaths::Status_Level;
auto eventValue = String(l);
auto msg = QueuedMessage(m, eventPath, eventValue, &l, IOEventDataType::Float);
m->setProperty(eventPath, eventValue, &l, IOEventDataType::Float);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
}

if (l > 0) {
Switch::status = SWITCH_STATUS_ON;
Expand Down
13 changes: 8 additions & 5 deletions src/service/api/devices/Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ namespace Service { namespace API { namespace devices {
}

// Event Stream Message Enqueue (for MQTT/SSE/WebSocket propagation)
float l = status == SWITCH_STATUS_ON ? onLevel : 0;
auto eventValue = String(l);
auto msg = QueuedMessage(m, eventPath, eventValue, &l, IOEventDataType::Float);
m->setProperty(eventPath, eventValue, &l, IOEventDataType::Float);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
auto eventsDisable = module->getProperty("Events.Disable");
if (eventsDisable == nullptr || eventsDisable->value == nullptr || eventsDisable->value != "1") {
float l = status == SWITCH_STATUS_ON ? onLevel : 0;
auto eventValue = String(l);
auto msg = QueuedMessage(m, eventPath, eventValue, &l, IOEventDataType::Float);
m->setProperty(eventPath, eventValue, &l, IOEventDataType::Float);
HomeGenie::getInstance()->getEventRouter().signalEvent(msg);
}

responseCallback->writeAll(R"({ "ResponseText": "OK" })");

Expand Down

0 comments on commit 33407a4

Please sign in to comment.