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,6 +28,7 @@ Renderer *Init_uDisplay(const char *desc);
extern Renderer *renderer;
#endif // USE_UNIVERSAL_DISPLAY
extern "C" {
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
@ -90,7 +91,7 @@ const char* be_ntv_display_driver_name(void) {
return "";
}
bool be_ntv_display_started(void) {
bbool be_ntv_display_started(void) {
#ifdef USE_UNIVERSAL_DISPLAY
if (renderer) {
return true;
@ -98,6 +99,7 @@ bool be_ntv_display_started(void) {
#endif
return false;
}
}
#endif // USE_DISPLAY
#endif // USE_BERRY

View File

@ -24,6 +24,7 @@
#include "berry.h"
extern "C" {
void* ls_init(int32_t type) {
if (type < 0 || type > LST_MAX) { return NULL; } // fail
LightStateClass * l = new LightStateClass();
@ -220,6 +221,7 @@ extern "C" int light_state_get(bvm *vm) {
be_pop(vm, 1);
be_return(vm);
}
}
#endif // USE_LIGHT
#endif // USE_BERRY

View File

@ -906,7 +906,7 @@ extern "C" {
* Tasmota Log Reader
*
\*********************************************************************************************/
extern "C" {
uint32_t* tlr_init(void) {
uint32_t* idx = new uint32_t();
*idx = 0;
@ -926,6 +926,7 @@ char* tlr_get_log(uint32_t* idx, int32_t log_level) {
return NULL;
}
}
}
/*********************************************************************************************\
* Logging functions

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