Berry move mapping from C++ to C (#19450)

This commit is contained in:
s-hadinger 2023-09-03 22:50:03 +02:00 committed by GitHub
parent fe0423c6cd
commit bb4d99114a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 496 additions and 471 deletions

View File

@ -12,7 +12,7 @@
extern int be_ntv_display_start(bvm *vm);
extern int be_ntv_display_dimmer(bvm *vm);
extern bool be_ntv_display_started(void);
extern bbool be_ntv_display_started(void);
BE_FUNC_CTYPE_DECLARE(be_ntv_display_started, "b", "")
extern void be_ntv_display_touch_update(int32_t touches, int32_t raw_x, int32_t raw_y, int32_t gesture);
BE_FUNC_CTYPE_DECLARE(be_ntv_display_touch_update, "", "iiii")

View File

@ -0,0 +1,115 @@
/********************************************************************
* Light_state class - abstract light state
*
* Handles all states and events for a virtual light.
* Can be eventually subclassed to handle a physical light.
*
*******************************************************************/
#ifdef USE_LIGHT
#include "be_constobj.h"
#include "be_mapping.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
extern void * ls_init(int32_t type); BE_FUNC_CTYPE_DECLARE(ls_init, "+_p", "i")
extern void ls_set_rgb(void* l, int32_t r, int32_t g, int32_t b); BE_FUNC_CTYPE_DECLARE(ls_set_rgb, "", ".iii")
extern void ls_set_huesat(void* l, int32_t hue, int32_t sat); BE_FUNC_CTYPE_DECLARE(ls_set_huesat, "", ".ii")
extern void ls_set_hue16sat(void* l, int32_t hue16, int32_t sat); BE_FUNC_CTYPE_DECLARE(ls_set_hue16sat, "", ".ii")
extern void ls_set_ct(void* l, int32_t ct); BE_FUNC_CTYPE_DECLARE(ls_set_ct, "", ".i")
extern void ls_set_bri(void* l, int32_t bri); BE_FUNC_CTYPE_DECLARE(ls_set_bri, "", ".i")
extern void ls_set_xy(void* l, float x, float y); BE_FUNC_CTYPE_DECLARE(ls_set_xy, "", ".ff")
extern int32_t ls_r(void* l); BE_VAR_CTYPE_DECLARE(ls_r, "i");
extern int32_t ls_g(void* l); BE_VAR_CTYPE_DECLARE(ls_g, "i");
extern int32_t ls_b(void* l); BE_VAR_CTYPE_DECLARE(ls_b, "i");
extern float ls_x(void* l); BE_VAR_CTYPE_DECLARE(ls_x, "f");
extern float ls_y(void* l); BE_VAR_CTYPE_DECLARE(ls_y, "f");
extern int32_t ls_hue(void* l); BE_VAR_CTYPE_DECLARE(ls_hue, "i");
extern int32_t ls_hue16(void* l); BE_VAR_CTYPE_DECLARE(ls_hue16, "i");
extern int32_t ls_sat(void* l); BE_VAR_CTYPE_DECLARE(ls_sat, "i");
extern int32_t ls_bri(void* l); BE_VAR_CTYPE_DECLARE(ls_bri, "i");
extern int32_t ls_ct(void* l); BE_VAR_CTYPE_DECLARE(ls_ct, "i");
extern int32_t ls_type(void* l); BE_VAR_CTYPE_DECLARE(ls_type, "i");
extern int32_t ls_mode_rgb(void* l); BE_VAR_CTYPE_DECLARE(ls_mode_rgb, "b");
extern int32_t ls_mode_ct(void* l); BE_VAR_CTYPE_DECLARE(ls_mode_ct, "b");
extern void ls_set_mode_rgb(void* l); BE_FUNC_CTYPE_DECLARE(ls_set_mode_rgb, "", ".");
extern void ls_set_mode_ct(void* l); BE_FUNC_CTYPE_DECLARE(ls_set_mode_ct, "", ".");
extern int32_t ls_get_power(void* l); BE_VAR_CTYPE_DECLARE(ls_get_power, "b");
extern void ls_set_power(void* l, int32_t pow); BE_FUNC_CTYPE_DECLARE(ls_set_power, "", ".b");
extern int32_t ls_reachable(void* p); BE_VAR_CTYPE_DECLARE(ls_reachable, "b");
extern void ls_set_reachable(void* l, int32_t pow); BE_FUNC_CTYPE_DECLARE(ls_set_reachable, "", ".b");
extern void ls_signal_change(void) {} BE_FUNC_CTYPE_DECLARE(ls_signal_change, "", ".");
extern int32_t ls_gamma8(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_gamma8, "i", "i")
extern int32_t ls_gamma10(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_gamma10, "i", "i")
extern int32_t ls_rev_gamma10(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_rev_gamma10, "i", "i")
// moved to constants array
const be_const_member_t light_state_members[] = {
{ ">b", be_ctype(ls_b) },
{ ">bri", be_ctype(ls_bri) },
{ ">ct", be_ctype(ls_ct) },
{ ">g", be_ctype(ls_g) },
{ ">hue", be_ctype(ls_hue) },
{ ">hue16", be_ctype(ls_hue16) },
{ ">mode_ct", be_ctype(ls_mode_ct) },
{ ">mode_rgb", be_ctype(ls_mode_rgb) },
{ ">power", be_ctype(ls_get_power) },
{ ">r", be_ctype(ls_r) },
{ ">reachable", be_ctype(ls_reachable) },
{ ">sat", be_ctype(ls_sat) },
{ ">type", be_ctype(ls_type) },
{ ">x", be_ctype(ls_x) },
{ ">y", be_ctype(ls_y) },
};
extern int light_state_get(bvm *vm);
static int light_state_member(bvm *vm) {
be_const_class_member_raise(vm, light_state_members, ARRAY_SIZE(light_state_members));
be_return(vm);
}
#include "be_fixed_be_class_light_state.h"
/* @const_object_info_begin
class be_class_light_state (scope: global, name: light_state) {
RELAY, int(0)
DIMMER, int(1)
CT, int(2)
RGB, int(3)
RGBW, int(4)
RGBCT, int(5)
_p, var
init, ctype_func(ls_init)
member, func(light_state_member)
set_rgb, ctype_func(ls_set_rgb)
set_huesat, ctype_func(ls_set_huesat)
set_hue16sat, ctype_func(ls_set_hue16sat)
set_xy, ctype_func(ls_set_xy)
set_ct, ctype_func(ls_set_ct)
set_bri, ctype_func(ls_set_bri)
set_mode_rgb, ctype_func(ls_set_mode_rgb)
set_mode_ct, ctype_func(ls_set_mode_ct)
set_power, ctype_func(ls_set_power)
set_reachable, ctype_func(ls_set_reachable)
get, func(light_state_get)
signal_change, ctype_func(ls_signal_change)
gamma8, static_ctype_func(ls_gamma8)
gamma10, static_ctype_func(ls_gamma10)
reverse_gamma10, static_ctype_func(ls_rev_gamma10)
}
@const_object_info_end */
#endif // USE_LIGHT

View File

@ -1,121 +0,0 @@
/********************************************************************
* Light_state class - abstract light state
*
* Handles all states and events for a virtual light.
* Can be eventually subclassed to handle a physical light.
*
*******************************************************************/
#ifdef USE_LIGHT
#include "be_constobj.h"
#include "be_mapping.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
extern void * ls_init(int32_t type); BE_FUNC_CTYPE_DECLARE(ls_init, "+_p", "i")
extern void ls_set_rgb(class LightStateClass* l, int32_t r, int32_t g, int32_t b); BE_FUNC_CTYPE_DECLARE(ls_set_rgb, "", ".iii")
extern void ls_set_huesat(class LightStateClass* l, int32_t hue, int32_t sat); BE_FUNC_CTYPE_DECLARE(ls_set_huesat, "", ".ii")
extern void ls_set_hue16sat(class LightStateClass* l, int32_t hue16, int32_t sat); BE_FUNC_CTYPE_DECLARE(ls_set_hue16sat, "", ".ii")
extern void ls_set_ct(class LightStateClass* l, int32_t ct); BE_FUNC_CTYPE_DECLARE(ls_set_ct, "", ".i")
extern void ls_set_bri(class LightStateClass* l, int32_t bri); BE_FUNC_CTYPE_DECLARE(ls_set_bri, "", ".i")
extern void ls_set_xy(class LightStateClass* l, float x, float y); BE_FUNC_CTYPE_DECLARE(ls_set_xy, "", ".ff")
extern int32_t ls_r(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_r, "i");
extern int32_t ls_g(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_g, "i");
extern int32_t ls_b(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_b, "i");
extern float ls_x(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_x, "f");
extern float ls_y(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_y, "f");
extern int32_t ls_hue(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_hue, "i");
extern int32_t ls_hue16(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_hue16, "i");
extern int32_t ls_sat(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_sat, "i");
extern int32_t ls_bri(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_bri, "i");
extern int32_t ls_ct(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_ct, "i");
extern int32_t ls_type(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_type, "i");
extern int32_t ls_mode_rgb(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_mode_rgb, "b");
extern int32_t ls_mode_ct(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_mode_ct, "b");
extern void ls_set_mode_rgb(class LightStateClass* l); BE_FUNC_CTYPE_DECLARE(ls_set_mode_rgb, "", ".");
extern void ls_set_mode_ct(class LightStateClass* l); BE_FUNC_CTYPE_DECLARE(ls_set_mode_ct, "", ".");
extern int32_t ls_get_power(class LightStateClass* l); BE_VAR_CTYPE_DECLARE(ls_get_power, "b");
extern void ls_set_power(class LightStateClass* l, int32_t pow); BE_FUNC_CTYPE_DECLARE(ls_set_power, "", ".b");
extern int32_t ls_reachable(class LightStateClass* p); BE_VAR_CTYPE_DECLARE(ls_reachable, "b");
extern void ls_set_reachable(class LightStateClass* l, int32_t pow); BE_FUNC_CTYPE_DECLARE(ls_set_reachable, "", ".b");
extern void ls_signal_change(void) {} BE_FUNC_CTYPE_DECLARE(ls_signal_change, "", ".");
extern int32_t ls_gamma8(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_gamma8, "i", "i")
extern int32_t ls_gamma10(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_gamma10, "i", "i")
extern int32_t ls_rev_gamma10(int32_t val); BE_FUNC_CTYPE_DECLARE(ls_rev_gamma10, "i", "i")
// moved to constants array
const be_const_member_t light_state_members[] = {
{ ">b", be_ctype(ls_b) },
{ ">bri", be_ctype(ls_bri) },
{ ">ct", be_ctype(ls_ct) },
{ ">g", be_ctype(ls_g) },
{ ">hue", be_ctype(ls_hue) },
{ ">hue16", be_ctype(ls_hue16) },
{ ">mode_ct", be_ctype(ls_mode_ct) },
{ ">mode_rgb", be_ctype(ls_mode_rgb) },
{ ">power", be_ctype(ls_get_power) },
{ ">r", be_ctype(ls_r) },
{ ">reachable", be_ctype(ls_reachable) },
{ ">sat", be_ctype(ls_sat) },
{ ">type", be_ctype(ls_type) },
{ ">x", be_ctype(ls_x) },
{ ">y", be_ctype(ls_y) },
};
extern "C" int light_state_get(bvm *vm);
static int light_state_member(bvm *vm) {
be_const_class_member_raise(vm, light_state_members, ARRAY_SIZE(light_state_members));
be_return(vm);
}
#include "be_fixed_be_class_light_state.h"
extern "C" void be_load_light_state_class(bvm *vm) {
be_pushntvclass(vm, &be_class_light_state);
be_setglobal(vm, "light_state");
be_pop(vm, 1);
}
/* @const_object_info_begin
class be_class_light_state (scope: global, name: light_state) {
RELAY, int(0)
DIMMER, int(1)
CT, int(2)
RGB, int(3)
RGBW, int(4)
RGBCT, int(5)
_p, var
init, ctype_func(ls_init)
member, func(light_state_member)
set_rgb, ctype_func(ls_set_rgb)
set_huesat, ctype_func(ls_set_huesat)
set_hue16sat, ctype_func(ls_set_hue16sat)
set_xy, ctype_func(ls_set_xy)
set_ct, ctype_func(ls_set_ct)
set_bri, ctype_func(ls_set_bri)
set_mode_rgb, ctype_func(ls_set_mode_rgb)
set_mode_ct, ctype_func(ls_set_mode_ct)
set_power, ctype_func(ls_set_power)
set_reachable, ctype_func(ls_set_reachable)
get, func(light_state_get)
signal_change, ctype_func(ls_signal_change)
gamma8, static_ctype_func(ls_gamma8)
gamma10, static_ctype_func(ls_gamma10)
reverse_gamma10, static_ctype_func(ls_rev_gamma10)
}
@const_object_info_end */
#endif // USE_LIGHT

View File

@ -10,11 +10,6 @@ extern char* tlr_get_log(uint32_t* idx, int32_t log_level); BE_FUNC_CTYPE_DECLAR
#include "be_fixed_be_class_tasmota_log_reader.h"
extern "C" void be_load_tasmota_log_reader_class(bvm *vm) {
be_pushntvclass(vm, &be_class_tasmota_log_reader);
be_setglobal(vm, "tasmota_log_reader");
be_pop(vm, 1);
}
/* @const_object_info_begin
class be_class_tasmota_log_reader (scope: global, name: tasmota_log_reader) {

View File

@ -0,0 +1,24 @@
/********************************************************************
* Berry module `unishox`
*
* To use: `import unishox`
*
* Allows to respond to HTTP request
*******************************************************************/
#ifdef USE_UNISHOX_COMPRESSION
#include "be_constobj.h"
extern int be_ntv_unishox_compress(bvm *vm);
extern int be_ntv_unishox_decompress(bvm *vm);
/* @const_object_info_begin
module unishox (scope: global) {
decompress, func(be_ntv_unishox_decompress)
compress, func(be_ntv_unishox_compress)
}
@const_object_info_end */
#include "be_fixed_unishox.h"
#endif // USE_UNISHOX_COMPRESSION

View File

@ -1,83 +0,0 @@
/********************************************************************
* Berry module `unishox`
*
* To use: `import unishox`
*
* Allows to respond to HTTP request
*******************************************************************/
#ifdef USE_UNISHOX_COMPRESSION
#include "be_constobj.h"
#include "be_mapping.h"
#include <string.h>
#include "unishox.h"
extern Unishox compressor;
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import unishox
*
*
\*********************************************************************************************/
static int be_ntv_unishox_compress(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 1 && be_isstring(vm, 1)) {
const char * s = be_tostring(vm, 1);
// do a dry-run to know the compressed size
int32_t compressed_size = compressor.unishox_compress(s, strlen(s), (char*) nullptr, 0);
if (compressed_size < 0) {
be_raise(vm, "internal_error", nullptr);
}
void * buf = be_pushbytes(vm, NULL, compressed_size);
if (compressed_size > 0) {
int32_t ret = compressor.unishox_compress(s, strlen(s), (char*) buf, compressed_size+5); // We expand by 4 the buffer size to avoid an error, but we are sure it will not overflow (see unishox implementation)
if (ret < 0 || ret != compressed_size) {
be_raisef(vm, "internal_error", "unishox size=%i ret=%i", compressed_size, ret);
}
}
be_return(vm);
}
be_raise(vm, "type_error", nullptr);
}
static int be_ntv_unishox_decompress(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 1 && be_isbytes(vm, 1)) {
size_t len;
const void * buf = be_tobytes(vm, 1, &len);
if (len == 0) {
be_pushstring(vm, "");
} else {
int32_t decomp_size = compressor.unishox_decompress((const char*)buf, len, (char*) nullptr, 0);
if (decomp_size < 0) {
be_raise(vm, "internal_error", nullptr);
}
if (decomp_size == 0) {
be_pushstring(vm, "");
} else {
void * buf_out = be_pushbuffer(vm, decomp_size);
int32_t ret = compressor.unishox_decompress((const char*)buf, len, (char*) buf_out, decomp_size);
if (ret < 0 || ret != decomp_size) {
be_raisef(vm, "internal_error", "unishox size=%i ret=%i", decomp_size, ret);
}
be_pushnstring(vm, (const char*) buf_out, decomp_size);
}
}
be_return(vm);
}
be_raise(vm, "type_error", nullptr);
}
/* @const_object_info_begin
module unishox (scope: global) {
decompress, func(be_ntv_unishox_decompress)
compress, func(be_ntv_unishox_compress)
}
@const_object_info_end */
#include "be_fixed_unishox.h"
#endif // USE_UNISHOX_COMPRESSION

View File

@ -28,75 +28,77 @@ Renderer *Init_uDisplay(const char *desc);
extern Renderer *renderer;
#endif // USE_UNIVERSAL_DISPLAY
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import display
* display.start(string) -> comptr or nil if failed
*
\*********************************************************************************************/
int be_ntv_display_start(struct bvm *vm) {
#ifdef USE_UNIVERSAL_DISPLAY
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isstring(vm, 1)) {
const char * desc = be_tostring(vm, 1);
// remove all objects on stack to avoid warnings in subsequent calls to Berry
be_pop(vm, argc);
Renderer * renderer = Init_uDisplay(desc);
if (renderer) {
be_pushcomptr(vm, renderer);
} else {
be_pushnil(vm);
extern "C" {
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import display
* display.start(string) -> comptr or nil if failed
*
\*********************************************************************************************/
int be_ntv_display_start(struct bvm *vm) {
#ifdef USE_UNIVERSAL_DISPLAY
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isstring(vm, 1)) {
const char * desc = be_tostring(vm, 1);
// remove all objects on stack to avoid warnings in subsequent calls to Berry
be_pop(vm, argc);
Renderer * renderer = Init_uDisplay(desc);
if (renderer) {
be_pushcomptr(vm, renderer);
} else {
be_pushnil(vm);
}
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
#else // USE_UNIVERSAL_DISPLAY
be_raise(vm, "internal_error", "universal display driver not present");
#endif // USE_UNIVERSAL_DISPLAY
}
// `display.dimmer([dim:int]) -> int` sets the dimmer of display, value 0..100. If `0` then turn off display. If no arg, read the current value.
int be_ntv_display_dimmer(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
int32_t dimmer;
if (argc >= 1) {
if (!be_isint(vm, 1)) { be_raise(vm, "type_error", "arg must be int"); }
dimmer = be_toint(vm, 1);
if ((dimmer < 0) || (dimmer > 100)) { be_raise(vm, "value_error", "value must be in range 0..100"); }
be_pop(vm, argc); // clear stack to avoid ripple errors in code called later
SetDisplayDimmer(dimmer);
ApplyDisplayDimmer();
}
be_pushint(vm, GetDisplayDimmer());
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
#else // USE_UNIVERSAL_DISPLAY
be_raise(vm, "internal_error", "universal display driver not present");
#endif // USE_UNIVERSAL_DISPLAY
}
// `display.dimmer([dim:int]) -> int` sets the dimmer of display, value 0..100. If `0` then turn off display. If no arg, read the current value.
int be_ntv_display_dimmer(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
int32_t dimmer;
if (argc >= 1) {
if (!be_isint(vm, 1)) { be_raise(vm, "type_error", "arg must be int"); }
dimmer = be_toint(vm, 1);
if ((dimmer < 0) || (dimmer > 100)) { be_raise(vm, "value_error", "value must be in range 0..100"); }
be_pop(vm, argc); // clear stack to avoid ripple errors in code called later
SetDisplayDimmer(dimmer);
ApplyDisplayDimmer();
void be_ntv_display_touch_update(int32_t touches, int32_t raw_x, int32_t raw_y, int32_t gesture) {
#if defined(USE_LVGL_TOUCHSCREEN) || defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_GT911) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS)
Touch_SetStatus(touches, raw_x, raw_y, gesture);
#endif
}
be_pushint(vm, GetDisplayDimmer());
be_return(vm);
}
void be_ntv_display_touch_update(int32_t touches, int32_t raw_x, int32_t raw_y, int32_t gesture) {
#if defined(USE_LVGL_TOUCHSCREEN) || defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_GT911) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS)
Touch_SetStatus(touches, raw_x, raw_y, gesture);
#endif
}
const char* be_ntv_display_driver_name(void) {
#ifdef USE_UNIVERSAL_DISPLAY
if (renderer) {
char* devname = renderer->devname();
if (devname) {
return devname;
const char* be_ntv_display_driver_name(void) {
#ifdef USE_UNIVERSAL_DISPLAY
if (renderer) {
char* devname = renderer->devname();
if (devname) {
return devname;
}
}
#endif
return "";
}
#endif
return "";
}
bool be_ntv_display_started(void) {
#ifdef USE_UNIVERSAL_DISPLAY
if (renderer) {
return true;
bbool be_ntv_display_started(void) {
#ifdef USE_UNIVERSAL_DISPLAY
if (renderer) {
return true;
}
#endif
return false;
}
#endif
return false;
}
#endif // USE_DISPLAY

View File

@ -24,201 +24,203 @@
#include "berry.h"
void* ls_init(int32_t type) {
if (type < 0 || type > LST_MAX) { return NULL; } // fail
LightStateClass * l = new LightStateClass();
l->setSubType(type);
if (type == 2) { l->setColorMode(LCM_CT); }
return (LightStateClass*)l;
}
int32_t ls_r(class LightStateClass* l) {
uint8_t r;
l->getActualRGBCW(&r, NULL, NULL, NULL, NULL);
return r;
}
int32_t ls_g(class LightStateClass* l) {
uint8_t g;
l->getActualRGBCW(NULL, &g, NULL, NULL, NULL);
return g;
}
int32_t ls_b(class LightStateClass* l) {
uint8_t b;
l->getActualRGBCW(NULL, NULL, &b, NULL, NULL);
return b;
}
float ls_x(class LightStateClass* l) {
float x;
l->getXY(&x, NULL);
return x;
}
float ls_y(class LightStateClass* l) {
float y;
l->getXY(NULL, &y);
return y;
}
int32_t ls_hue(class LightStateClass* l) {
uint16_t hue;
l->getHSB(&hue, NULL, NULL);
return hue;
}
int32_t ls_hue16(class LightStateClass* l) {
return l->getHue16();
}
int32_t ls_sat(class LightStateClass* l) {
uint8_t sat;
l->getHSB(NULL, &sat, NULL);
return sat;
}
int32_t ls_bri(class LightStateClass* l) {
return l->getBri();
}
int32_t ls_type(class LightStateClass* l) {
return l->getSubType();
}
void ls_set_rgb(class LightStateClass* l, int32_t r, int32_t g, int32_t b) {
l->setRGB(r, g, b, false);
}
void ls_set_huesat(class LightStateClass* l, int32_t hue, int32_t sat) {
l->setHS(hue, sat);
}
void ls_set_hue16sat(class LightStateClass* l, int32_t hue16, int32_t sat) {
l->setH16S(hue16, sat);
}
void ls_set_bri(class LightStateClass* l, int32_t bri) {
l->setBri(bri);
}
int32_t ls_get_power(class LightStateClass* l) {
return l->getPower();
}
void ls_set_xy(class LightStateClass* l, float x, float y) {
uint8_t rr, gg, bb;
XyToRgb(x, y, &rr, &gg, &bb);
l->setRGB(rr, gg, bb, false);
}
void ls_set_power(class LightStateClass* l, int32_t pow) {
l->setPower(pow);
}
int32_t ls_reachable(class LightStateClass* l) {
return l->getReachable();
}
void ls_set_reachable(class LightStateClass* l, int32_t reachable) {
l->setReachable(reachable);
}
void ls_set_mode_rgb(class LightStateClass* l) {
l->setColorMode(LCM_RGB);
}
void ls_set_mode_ct(class LightStateClass* l) {
l->setColorMode(LCM_CT);
}
int32_t ls_mode_rgb(class LightStateClass* l) {
return (l->getColorMode() & LCM_RGB) ? 1 : 0;
}
int32_t ls_mode_ct(class LightStateClass* l) {
return (l->getColorMode() & LCM_CT) ? 1 : 0;
}
void ls_set_ct(class LightStateClass* l, int32_t ct) {
l->setCT(ct);
l->setColorMode(LCM_CT);
}
int32_t ls_ct(class LightStateClass* l) {
return l->getCT();
}
// Gamma functions
int32_t ls_gamma8(int32_t val) {
return ledGamma(val);
}
int32_t ls_gamma10(int32_t val) {
return ledGamma10_10(val);
}
int32_t ls_rev_gamma10(int32_t val) {
return ledGammaReverse(val);
}
// get returning a complete map, similar to `light.get()`
extern "C" int light_state_get(bvm *vm) {
be_getmember(vm, 1, "_p");
LightStateClass * l = (LightStateClass *) be_tocomptr(vm, -1);
be_newobject(vm, "map");
uint32_t sub_type = l->getSubType();
char s_rgb[8] = {0}; // RGB raw levels string
uint8_t r, g, b, cw, ww;
uint16_t hue;
uint8_t sat;
uint8_t briRGB, briCT;
l->getActualRGBCW(&r, &g, &b, &cw, &ww);
l->getHSB(&hue, &sat, &briRGB);
briCT = l->getBriCT();
bool mode_rgb = l->getColorMode() & LCM_RGB;
bool mode_ct = !mode_rgb;
// type
be_map_insert_int(vm, "type", sub_type);
// power
be_map_insert_bool(vm, "power", l->getPower());
// bri
if (sub_type > 0) {
be_map_insert_int(vm, "bri", l->getBri());
extern "C" {
void* ls_init(int32_t type) {
if (type < 0 || type > LST_MAX) { return NULL; } // fail
LightStateClass * l = new LightStateClass();
l->setSubType(type);
if (type == 2) { l->setColorMode(LCM_CT); }
return (LightStateClass*)l;
}
// color_mode
if (sub_type >= 3) {
be_map_insert_bool(vm, "mode_rgb", ls_mode_rgb(l));
int32_t ls_r(class LightStateClass* l) {
uint8_t r;
l->getActualRGBCW(&r, NULL, NULL, NULL, NULL);
return r;
}
if (sub_type >= 4 || sub_type == 2) {
be_map_insert_bool(vm, "mode_ct", ls_mode_ct(l));
int32_t ls_g(class LightStateClass* l) {
uint8_t g;
l->getActualRGBCW(NULL, &g, NULL, NULL, NULL);
return g;
}
int32_t ls_b(class LightStateClass* l) {
uint8_t b;
l->getActualRGBCW(NULL, NULL, &b, NULL, NULL);
return b;
}
float ls_x(class LightStateClass* l) {
float x;
l->getXY(&x, NULL);
return x;
}
float ls_y(class LightStateClass* l) {
float y;
l->getXY(NULL, &y);
return y;
}
int32_t ls_hue(class LightStateClass* l) {
uint16_t hue;
l->getHSB(&hue, NULL, NULL);
return hue;
}
int32_t ls_hue16(class LightStateClass* l) {
return l->getHue16();
}
int32_t ls_sat(class LightStateClass* l) {
uint8_t sat;
l->getHSB(NULL, &sat, NULL);
return sat;
}
int32_t ls_bri(class LightStateClass* l) {
return l->getBri();
}
int32_t ls_type(class LightStateClass* l) {
return l->getSubType();
}
// RGB
if (sub_type >= 3 && mode_rgb) {
snprintf(s_rgb, sizeof(s_rgb), PSTR("%02X%02X%02X"), r, g, b);
be_map_insert_str(vm, "rgb", s_rgb);
be_map_insert_int(vm, "hue", hue);
be_map_insert_int(vm, "sat", sat);
void ls_set_rgb(class LightStateClass* l, int32_t r, int32_t g, int32_t b) {
l->setRGB(r, g, b, false);
}
void ls_set_huesat(class LightStateClass* l, int32_t hue, int32_t sat) {
l->setHS(hue, sat);
}
void ls_set_hue16sat(class LightStateClass* l, int32_t hue16, int32_t sat) {
l->setH16S(hue16, sat);
}
void ls_set_bri(class LightStateClass* l, int32_t bri) {
l->setBri(bri);
}
// CT when 2 white channels
if ((sub_type == 2 || sub_type == 5) && mode_ct) {
be_map_insert_int(vm, "ct", l->getCT());
int32_t ls_get_power(class LightStateClass* l) {
return l->getPower();
}
void ls_set_xy(class LightStateClass* l, float x, float y) {
uint8_t rr, gg, bb;
XyToRgb(x, y, &rr, &gg, &bb);
l->setRGB(rr, gg, bb, false);
}
void ls_set_power(class LightStateClass* l, int32_t pow) {
l->setPower(pow);
}
if (sub_type > 0) {
uint8_t channels[LST_MAX] = {0};
switch (sub_type) {
case 1:
channels[0] = briRGB;
break;
case 2:
channels[0] = cw;
channels[1] = ww;
break;
case 3:
case 4:
case 5:
channels[0] = r;
channels[1] = g;
channels[2] = b;
channels[3] = cw;
channels[4] = ww;
break;
default:
break;
int32_t ls_reachable(class LightStateClass* l) {
return l->getReachable();
}
void ls_set_reachable(class LightStateClass* l, int32_t reachable) {
l->setReachable(reachable);
}
void ls_set_mode_rgb(class LightStateClass* l) {
l->setColorMode(LCM_RGB);
}
void ls_set_mode_ct(class LightStateClass* l) {
l->setColorMode(LCM_CT);
}
int32_t ls_mode_rgb(class LightStateClass* l) {
return (l->getColorMode() & LCM_RGB) ? 1 : 0;
}
int32_t ls_mode_ct(class LightStateClass* l) {
return (l->getColorMode() & LCM_CT) ? 1 : 0;
}
void ls_set_ct(class LightStateClass* l, int32_t ct) {
l->setCT(ct);
l->setColorMode(LCM_CT);
}
int32_t ls_ct(class LightStateClass* l) {
return l->getCT();
}
// Gamma functions
int32_t ls_gamma8(int32_t val) {
return ledGamma(val);
}
int32_t ls_gamma10(int32_t val) {
return ledGamma10_10(val);
}
int32_t ls_rev_gamma10(int32_t val) {
return ledGammaReverse(val);
}
// get returning a complete map, similar to `light.get()`
extern "C" int light_state_get(bvm *vm) {
be_getmember(vm, 1, "_p");
LightStateClass * l = (LightStateClass *) be_tocomptr(vm, -1);
be_newobject(vm, "map");
uint32_t sub_type = l->getSubType();
char s_rgb[8] = {0}; // RGB raw levels string
uint8_t r, g, b, cw, ww;
uint16_t hue;
uint8_t sat;
uint8_t briRGB, briCT;
l->getActualRGBCW(&r, &g, &b, &cw, &ww);
l->getHSB(&hue, &sat, &briRGB);
briCT = l->getBriCT();
bool mode_rgb = l->getColorMode() & LCM_RGB;
bool mode_ct = !mode_rgb;
// type
be_map_insert_int(vm, "type", sub_type);
// power
be_map_insert_bool(vm, "power", l->getPower());
// bri
if (sub_type > 0) {
be_map_insert_int(vm, "bri", l->getBri());
}
be_map_insert_list_uint8(vm, "channels", channels, sub_type);
}
be_pop(vm, 1);
be_return(vm);
// color_mode
if (sub_type >= 3) {
be_map_insert_bool(vm, "mode_rgb", ls_mode_rgb(l));
}
if (sub_type >= 4 || sub_type == 2) {
be_map_insert_bool(vm, "mode_ct", ls_mode_ct(l));
}
// RGB
if (sub_type >= 3 && mode_rgb) {
snprintf(s_rgb, sizeof(s_rgb), PSTR("%02X%02X%02X"), r, g, b);
be_map_insert_str(vm, "rgb", s_rgb);
be_map_insert_int(vm, "hue", hue);
be_map_insert_int(vm, "sat", sat);
}
// CT when 2 white channels
if ((sub_type == 2 || sub_type == 5) && mode_ct) {
be_map_insert_int(vm, "ct", l->getCT());
}
if (sub_type > 0) {
uint8_t channels[LST_MAX] = {0};
switch (sub_type) {
case 1:
channels[0] = briRGB;
break;
case 2:
channels[0] = cw;
channels[1] = ww;
break;
case 3:
case 4:
case 5:
channels[0] = r;
channels[1] = g;
channels[2] = b;
channels[3] = cw;
channels[4] = ww;
break;
default:
break;
}
be_map_insert_list_uint8(vm, "channels", channels, sub_type);
}
be_pop(vm, 1);
be_return(vm);
}
}
#endif // USE_LIGHT

View File

@ -906,24 +906,25 @@ extern "C" {
* Tasmota Log Reader
*
\*********************************************************************************************/
uint32_t* tlr_init(void) {
uint32_t* idx = new uint32_t();
*idx = 0;
return idx;
}
char* tlr_get_log(uint32_t* idx, int32_t log_level) {
// bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* len_p) {
if (log_level < 0 || log_level > 4) { log_level = 2; } // default to LOG_LEVEL_INFO
char* line;
size_t len;
if (GetLog(log_level, idx, &line, &len) && len > 0) {
char* s = (char*) malloc(len+1);
memmove(s, line, len);
s[len] = 0;
return s; // caller will free()
} else {
return NULL;
extern "C" {
uint32_t* tlr_init(void) {
uint32_t* idx = new uint32_t();
*idx = 0;
return idx;
}
char* tlr_get_log(uint32_t* idx, int32_t log_level) {
// bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* len_p) {
if (log_level < 0 || log_level > 4) { log_level = 2; } // default to LOG_LEVEL_INFO
char* line;
size_t len;
if (GetLog(log_level, idx, &line, &len) && len > 0) {
char* s = (char*) malloc(len+1);
memmove(s, line, len);
s[len] = 0;
return s; // caller will free()
} else {
return NULL;
}
}
}

View File

@ -0,0 +1,90 @@
/*
xdrv_52_3_berry_unishox.ino - Berry scripting language, Unishox library
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
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 3 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, see <http://www.gnu.org/licenses/>.
*/
// Mappgin from internal light and a generic `light_state` Berry class
#ifdef USE_BERRY
#ifdef USE_UNISHOX_COMPRESSION
#include "be_mapping.h"
#include <string.h>
#include "unishox.h"
extern Unishox compressor;
extern "C" {
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import unishox
*
*
\*********************************************************************************************/
int be_ntv_unishox_compress(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 1 && be_isstring(vm, 1)) {
const char * s = be_tostring(vm, 1);
// do a dry-run to know the compressed size
int32_t compressed_size = compressor.unishox_compress(s, strlen(s), (char*) nullptr, 0);
if (compressed_size < 0) {
be_raise(vm, "internal_error", nullptr);
}
void * buf = be_pushbytes(vm, NULL, compressed_size);
if (compressed_size > 0) {
int32_t ret = compressor.unishox_compress(s, strlen(s), (char*) buf, compressed_size+5); // We expand by 4 the buffer size to avoid an error, but we are sure it will not overflow (see unishox implementation)
if (ret < 0 || ret != compressed_size) {
be_raisef(vm, "internal_error", "unishox size=%i ret=%i", compressed_size, ret);
}
}
be_return(vm);
}
be_raise(vm, "type_error", nullptr);
}
int be_ntv_unishox_decompress(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc == 1 && be_isbytes(vm, 1)) {
size_t len;
const void * buf = be_tobytes(vm, 1, &len);
if (len == 0) {
be_pushstring(vm, "");
} else {
int32_t decomp_size = compressor.unishox_decompress((const char*)buf, len, (char*) nullptr, 0);
if (decomp_size < 0) {
be_raise(vm, "internal_error", nullptr);
}
if (decomp_size == 0) {
be_pushstring(vm, "");
} else {
void * buf_out = be_pushbuffer(vm, decomp_size);
int32_t ret = compressor.unishox_decompress((const char*)buf, len, (char*) buf_out, decomp_size);
if (ret < 0 || ret != decomp_size) {
be_raisef(vm, "internal_error", "unishox size=%i ret=%i", decomp_size, ret);
}
be_pushnstring(vm, (const char*) buf_out, decomp_size);
}
}
be_return(vm);
}
be_raise(vm, "type_error", nullptr);
}
}
#endif // USE_UNISHOX_COMPRESSION
#endif // USE_BERRY