Berry Leds fix and cleaning (#22640)

This commit is contained in:
s-hadinger 2024-12-12 20:55:29 +01:00 committed by GitHub
parent 1099babb16
commit 7834dbf6b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 30 additions and 39 deletions

View File

@ -76,9 +76,6 @@ extern "C" {
be_getmember(vm, 1, "_p"); be_getmember(vm, 1, "_p");
void * strip = (void*) be_tocomptr(vm, -1); void * strip = (void*) be_tocomptr(vm, -1);
be_pop(vm, 1); be_pop(vm, 1);
if (strip == nullptr) {
be_raise(vm, "internal_error", "tasmotaled object not initialized");
}
return strip; return strip;
} }
int32_t be_get_leds_type(bvm *vm) { int32_t be_get_leds_type(bvm *vm) {
@ -119,7 +116,6 @@ extern "C" {
// if GPIO is '-1' // if GPIO is '-1'
led_type = 0; led_type = 0;
Ws2812InitStrip(); // ensure the tasmotaled object is initialized, because Berry code runs before the driver is initialized Ws2812InitStrip(); // ensure the tasmotaled object is initialized, because Berry code runs before the driver is initialized
// strip = Ws2812GetStrip(); TODO
} else { } else {
if (led_type < 1) { led_type = 1; } if (led_type < 1) { led_type = 1; }
TasmotaLEDPusher * pusher = TasmotaLEDPusher::Create(hardware, gpio); TasmotaLEDPusher * pusher = TasmotaLEDPusher::Create(hardware, gpio);
@ -144,15 +140,13 @@ extern "C" {
// all other commands need a valid tasmotaled pointer // all other commands need a valid tasmotaled pointer
int32_t leds_type = be_get_leds_type(vm); int32_t leds_type = be_get_leds_type(vm);
TasmotaLED * strip = (TasmotaLED*) be_get_tasmotaled(vm); // raises an exception if pointer is invalid TasmotaLED * strip = (TasmotaLED*) be_get_tasmotaled(vm); // raises an exception if pointer is invalid
// detect native driver // detect native driver means strip == nullptr
bool native = (leds_type == 0);
be_pushnil(vm); // push a default `nil` return value be_pushnil(vm); // push a default `nil` return value
switch (cmd) { switch (cmd) {
case 1: // # 01 : begin void -> void case 1: // # 01 : begin void -> void
if (native) Ws2812Begin(); if (strip) strip->Begin();
else if (strip) strip->Begin(); else Ws2812Begin();
break; break;
case 2: // # 02 : show void -> void case 2: // # 02 : show void -> void
{ {
@ -176,43 +170,40 @@ extern "C" {
} }
uint32_t pixels_size; // number of bytes to push uint32_t pixels_size; // number of bytes to push
bool update_completed = false; bool update_completed = false;
if (native) { if (strip) {
strip->Show();
pixels_size = strip->PixelCount() * strip->PixelSize();
update_completed = strip->CanShow();
} else {
Ws2812Show(); Ws2812Show();
pixels_size = Ws2812PixelsSize(); pixels_size = Ws2812PixelsSize();
update_completed =Ws2812CanShow(); update_completed =Ws2812CanShow();
} }
else if (strip) {
strip->Show();
pixels_size = strip->PixelCount() * strip->PixelSize();
update_completed = strip->CanShow();
}
} }
break; break;
case 3: // # 03 : CanShow void -> bool case 3: // # 03 : CanShow void -> bool
if (native) be_pushbool(vm, Ws2812CanShow()); if (strip) be_pushbool(vm, strip->CanShow());
else if (strip) be_pushbool(vm, strip->CanShow()); else be_pushbool(vm, Ws2812CanShow());
break; break;
case 4: // # 04 : IsDirty void -> bool case 4: // # 04 : IsDirty void -> bool
if (native) be_pushbool(vm, Ws2812IsDirty()); if (strip) be_pushbool(vm, strip->IsDirty());
else if (strip) be_pushbool(vm, strip->IsDirty()); else be_pushbool(vm, Ws2812IsDirty());
break; break;
case 5: // # 05 : Dirty void -> void case 5: // # 05 : Dirty void -> void
if (native) Ws2812Dirty(); if (strip) strip->Dirty();
else if (strip) strip->Dirty(); else Ws2812Dirty();
break; break;
case 6: // # 06 : Pixels void -> bytes() (mapped to the buffer) case 6: // # 06 : Pixels void -> bytes() (mapped to the buffer)
{ if (strip) be_pushcomptr(vm, strip->Pixels());
if (native) be_pushcomptr(vm, Ws2812Pixels()); else be_pushcomptr(vm, Ws2812Pixels());
else if (strip) be_pushcomptr(vm, strip->Pixels());
}
break; break;
case 7: // # 07 : PixelSize void -> int case 7: // # 07 : PixelSize void -> int
if (native) be_pushint(vm, Ws2812PixelSize()); if (strip) be_pushint(vm, strip->PixelSize());
else if (strip) be_pushint(vm, strip->PixelSize()); else be_pushint(vm, Ws2812PixelSize());
break; break;
case 8: // # 08 : PixelCount void -> int case 8: // # 08 : PixelCount void -> int
if (native) be_pushint(vm, Ws2812PixelCount()); if (strip) be_pushint(vm, strip->PixelCount());
else if (strip) be_pushint(vm, strip->PixelCount()); else be_pushint(vm, Ws2812PixelCount());
break; break;
case 9: // # 09 : ClearTo (color:??) -> void case 9: // # 09 : ClearTo (color:??) -> void
{ {
@ -227,11 +218,11 @@ extern "C" {
if (from < 0) { from = 0; } if (from < 0) { from = 0; }
if (len < 0) { len = 0; } if (len < 0) { len = 0; }
if (native) Ws2812ClearTo(r, g, b, w, from, from + len - 1); if (strip) strip->ClearTo(rgbw, from, from + len - 1);
else if (strip) strip->ClearTo(rgbw, from, from + len - 1); else Ws2812ClearTo(r, g, b, w, from, from + len - 1);
} else { } else {
if (native) Ws2812ClearTo(r, g, b, w, -1, -1); if (strip) strip->ClearTo(rgbw);
else if (strip) strip->ClearTo(rgbw); else Ws2812ClearTo(r, g, b, w, -1, -1);
} }
} }
break; break;
@ -239,24 +230,24 @@ extern "C" {
{ {
int32_t idx = be_toint(vm, 3); int32_t idx = be_toint(vm, 3);
uint32_t wrgb = be_toint(vm, 4); uint32_t wrgb = be_toint(vm, 4);
if (native) { if (strip) {
strip->SetPixelColor(idx, wrgb);
} else {
uint8_t w = (wrgb >> 24) & 0xFF; uint8_t w = (wrgb >> 24) & 0xFF;
uint8_t r = (wrgb >> 16) & 0xFF; uint8_t r = (wrgb >> 16) & 0xFF;
uint8_t g = (wrgb >> 8) & 0xFF; uint8_t g = (wrgb >> 8) & 0xFF;
uint8_t b = (wrgb ) & 0xFF; uint8_t b = (wrgb ) & 0xFF;
Ws2812SetPixelColor(idx, r, g, b, w); Ws2812SetPixelColor(idx, r, g, b, w);
} else if (strip) {
strip->SetPixelColor(idx, wrgb);
} }
} }
break; break;
case 11: // # 11 : GetPixelColor (idx:int) -> color:int wrgb case 11: // # 11 : GetPixelColor (idx:int) -> color:int wrgb
{ {
int32_t idx = be_toint(vm, 3); int32_t idx = be_toint(vm, 3);
if (native) { if (strip) {
be_pushint(vm, Ws2812GetPixelColor(idx));
} else if (strip) {
be_pushint(vm, strip->GetPixelColor(idx)); be_pushint(vm, strip->GetPixelColor(idx));
} else {
be_pushint(vm, Ws2812GetPixelColor(idx));
} }
} }
break; break;