mirror of https://github.com/arendst/Tasmota.git
Merge pull request #13131 from s-hadinger/berry_serial
Berry support for Serial
This commit is contained in:
commit
660e37d5fe
|
@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
|
|||
- ESP32 add GPIO 6/7/8/11 to template and remove GPIO 28-31 (remapping so backwards compatible)
|
||||
- Crash recorder ``Status 12`` for ESP32/ESP32S2/ESP32C3, supporting Esp-idf 3.3/4.4
|
||||
- Support for ESP32/ESP32S2 DAC gpio via Berry
|
||||
- Berry support for Serial
|
||||
|
||||
### Changed
|
||||
- M5 Stack Core2 uses UNIVERSAL_DISPLAY with enabled LVGL as default now
|
||||
|
|
|
@ -105,6 +105,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = {
|
|||
extern void be_load_tasmota_ntvlib(bvm *vm);
|
||||
extern void be_load_wirelib(bvm *vm);
|
||||
extern void be_load_onewirelib(bvm *vm);
|
||||
extern void be_load_serial_lib(bvm *vm);
|
||||
extern void be_load_Driver_class(bvm *vm);
|
||||
extern void be_load_Timer_class(bvm *vm);
|
||||
extern void be_load_driver_i2c_lib(bvm *vm);
|
||||
|
@ -148,6 +149,7 @@ BERRY_API void be_load_custom_libs(bvm *vm)
|
|||
be_load_tasmota_ntvlib(vm);
|
||||
be_load_Driver_class(vm);
|
||||
be_load_md5_lib(vm);
|
||||
be_load_serial_lib(vm);
|
||||
#ifdef USE_ALEXA_AVS
|
||||
be_load_crypto_lib(vm);
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/********************************************************************
|
||||
* Tasmota lib
|
||||
*
|
||||
* To use: `import wire`
|
||||
*
|
||||
* 2 wire communication - I2C
|
||||
*******************************************************************/
|
||||
#include "be_constobj.h"
|
||||
|
||||
#include "esp32-hal.h"
|
||||
|
||||
extern int b_serial_init(bvm *vm);
|
||||
extern int b_serial_deinit(bvm *vm);
|
||||
|
||||
extern int b_serial_write(bvm *vm);
|
||||
extern int b_serial_read(bvm *vm);
|
||||
extern int b_serial_available(bvm *vm);
|
||||
extern int b_serial_flush(bvm *vm);
|
||||
|
||||
#include "../generate/be_fixed_be_class_tasmota_serial.h"
|
||||
|
||||
void be_load_serial_lib(bvm *vm) {
|
||||
be_pushntvclass(vm, &be_class_tasmota_serial);
|
||||
be_setglobal(vm, "Serial");
|
||||
be_pop(vm, 1);
|
||||
}
|
||||
|
||||
/* @const_object_info_begin
|
||||
|
||||
class be_class_tasmota_serial (scope: global, name: Serial) {
|
||||
.p, var
|
||||
|
||||
SERIAL_5N1, int(SERIAL_5N1)
|
||||
SERIAL_6N1, int(SERIAL_6N1)
|
||||
SERIAL_7N1, int(SERIAL_7N1)
|
||||
SERIAL_8N1, int(SERIAL_8N1)
|
||||
SERIAL_5N2, int(SERIAL_5N2)
|
||||
SERIAL_6N2, int(SERIAL_6N2)
|
||||
SERIAL_7N2, int(SERIAL_7N2)
|
||||
SERIAL_8N2, int(SERIAL_8N2)
|
||||
SERIAL_5E1, int(SERIAL_5E1)
|
||||
SERIAL_6E1, int(SERIAL_6E1)
|
||||
SERIAL_7E1, int(SERIAL_7E1)
|
||||
SERIAL_8E1, int(SERIAL_8E1)
|
||||
SERIAL_5E2, int(SERIAL_5E2)
|
||||
SERIAL_6E2, int(SERIAL_6E2)
|
||||
SERIAL_7E2, int(SERIAL_7E2)
|
||||
SERIAL_8E2, int(SERIAL_8E2)
|
||||
SERIAL_5O1, int(SERIAL_5O1)
|
||||
SERIAL_6O1, int(SERIAL_6O1)
|
||||
SERIAL_7O1, int(SERIAL_7O1)
|
||||
SERIAL_8O1, int(SERIAL_8O1)
|
||||
SERIAL_5O2, int(SERIAL_5O2)
|
||||
SERIAL_6O2, int(SERIAL_6O2)
|
||||
SERIAL_7O2, int(SERIAL_7O2)
|
||||
SERIAL_8O2, int(SERIAL_8O2)
|
||||
|
||||
init, func(b_serial_init)
|
||||
deinit, func(b_serial_deinit)
|
||||
|
||||
write, func(b_serial_write)
|
||||
read, func(b_serial_read)
|
||||
available, func(b_serial_available)
|
||||
flush, func(b_serial_flush)
|
||||
}
|
||||
@const_object_info_end */
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,47 @@
|
|||
#include "be_constobj.h"
|
||||
|
||||
static be_define_const_map_slots(be_class_tasmota_serial_map) {
|
||||
{ be_const_key(SERIAL_5E2, 3), be_const_int(SERIAL_5E2) },
|
||||
{ be_const_key(SERIAL_8E1, -1), be_const_int(SERIAL_8E1) },
|
||||
{ be_const_key(SERIAL_8O1, 22), be_const_int(SERIAL_8O1) },
|
||||
{ be_const_key(SERIAL_5O1, -1), be_const_int(SERIAL_5O1) },
|
||||
{ be_const_key(read, 0), be_const_func(b_serial_read) },
|
||||
{ be_const_key(flush, -1), be_const_func(b_serial_flush) },
|
||||
{ be_const_key(SERIAL_7E2, -1), be_const_int(SERIAL_7E2) },
|
||||
{ be_const_key(SERIAL_5N2, -1), be_const_int(SERIAL_5N2) },
|
||||
{ be_const_key(SERIAL_6N2, -1), be_const_int(SERIAL_6N2) },
|
||||
{ be_const_key(SERIAL_6E2, -1), be_const_int(SERIAL_6E2) },
|
||||
{ be_const_key(available, -1), be_const_func(b_serial_available) },
|
||||
{ be_const_key(SERIAL_6N1, 12), be_const_int(SERIAL_6N1) },
|
||||
{ be_const_key(SERIAL_7O1, -1), be_const_int(SERIAL_7O1) },
|
||||
{ be_const_key(SERIAL_7N1, 14), be_const_int(SERIAL_7N1) },
|
||||
{ be_const_key(SERIAL_6O1, -1), be_const_int(SERIAL_6O1) },
|
||||
{ be_const_key(SERIAL_6E1, -1), be_const_int(SERIAL_6E1) },
|
||||
{ be_const_key(write, -1), be_const_func(b_serial_write) },
|
||||
{ be_const_key(SERIAL_8O2, -1), be_const_int(SERIAL_8O2) },
|
||||
{ be_const_key(SERIAL_5O2, 24), be_const_int(SERIAL_5O2) },
|
||||
{ be_const_key(SERIAL_5E1, 26), be_const_int(SERIAL_5E1) },
|
||||
{ be_const_key(deinit, -1), be_const_func(b_serial_deinit) },
|
||||
{ be_const_key(SERIAL_5N1, -1), be_const_int(SERIAL_5N1) },
|
||||
{ be_const_key(SERIAL_8N1, 1), be_const_int(SERIAL_8N1) },
|
||||
{ be_const_key(SERIAL_7E1, -1), be_const_int(SERIAL_7E1) },
|
||||
{ be_const_key(SERIAL_8N2, -1), be_const_int(SERIAL_8N2) },
|
||||
{ be_const_key(dot_p, 8), be_const_var(0) },
|
||||
{ be_const_key(SERIAL_8E2, -1), be_const_int(SERIAL_8E2) },
|
||||
{ be_const_key(SERIAL_7O2, -1), be_const_int(SERIAL_7O2) },
|
||||
{ be_const_key(SERIAL_7N2, -1), be_const_int(SERIAL_7N2) },
|
||||
{ be_const_key(init, -1), be_const_func(b_serial_init) },
|
||||
{ be_const_key(SERIAL_6O2, 9), be_const_int(SERIAL_6O2) },
|
||||
};
|
||||
|
||||
static be_define_const_map(
|
||||
be_class_tasmota_serial_map,
|
||||
31
|
||||
);
|
||||
|
||||
BE_EXPORT_VARIABLE be_define_const_class(
|
||||
be_class_tasmota_serial,
|
||||
1,
|
||||
NULL,
|
||||
Serial
|
||||
);
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
xdrv_52_3_berry_serial.ino - Berry scripting language, native fucnctions
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef USE_BERRY
|
||||
|
||||
#include <berry.h>
|
||||
#include <TasmotaSerial.h>
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Native functions mapped to Berry functions
|
||||
*
|
||||
* import wire
|
||||
*
|
||||
* wire.get_free_heap() -> int
|
||||
*
|
||||
\*********************************************************************************************/
|
||||
extern "C" {
|
||||
TasmotaSerial * b_serial_get(struct bvm *vm) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ow = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
be_pop(vm, 1);
|
||||
return ow;
|
||||
}
|
||||
|
||||
// Berry: `init(rx_gpio:int, tx_gpio:int, speed:int [, config:int])`
|
||||
int32_t b_serial_init(struct bvm *vm);
|
||||
int32_t b_serial_init(struct bvm *vm) {
|
||||
int32_t argc = be_top(vm); // Get the number of arguments
|
||||
if (argc >= 4 && be_isint(vm, 2) && be_isint(vm, 3) && be_isint(vm, 4)) {
|
||||
int32_t rx = be_toint(vm, 2);
|
||||
int32_t tx = be_toint(vm, 3);
|
||||
int32_t speed = be_toint(vm, 4);
|
||||
int32_t mode = SERIAL_8N1;
|
||||
if (argc >= 5 && be_isint(vm, 5)) {
|
||||
mode = be_toint(vm, 5);
|
||||
}
|
||||
TasmotaSerial * ser = new TasmotaSerial(rx, tx);
|
||||
bool ok = ser->begin(speed, mode);
|
||||
if (!ok) {
|
||||
delete ser;
|
||||
be_raise(vm, "internal_error", "Unable to start serial");
|
||||
}
|
||||
be_pushcomptr(vm, (void*) ser);
|
||||
be_setmember(vm, 1, ".p");
|
||||
be_return_nil(vm);
|
||||
}
|
||||
be_raise(vm, kTypeError, nullptr);
|
||||
}
|
||||
|
||||
// Berry: `deinit(void)`
|
||||
int32_t b_serial_deinit(struct bvm *vm);
|
||||
int32_t b_serial_deinit(struct bvm *vm) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
if (ser) {
|
||||
delete ser;
|
||||
// clear
|
||||
be_pushcomptr(vm, (void*) NULL);
|
||||
be_setmember(vm, 1, ".p");
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
// Berry: `available(void) -> int` (how many bytes are waiting in RX buffer)
|
||||
int32_t b_serial_available(struct bvm *vm);
|
||||
int32_t b_serial_available(struct bvm *vm) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
if (ser) {
|
||||
be_pushint(vm, ser->available());
|
||||
be_return(vm);
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
// Berry: `flush(void) -> nil` (wait for all bytes to be sent, and clear rx buffer)
|
||||
int32_t b_serial_flush(struct bvm *vm);
|
||||
int32_t b_serial_flush(struct bvm *vm) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
if (ser) {
|
||||
ser->flush();
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
// Berry: `read(void) -> bytes()` (read all bytes in RX buffer)
|
||||
int32_t b_serial_read(struct bvm *vm);
|
||||
int32_t b_serial_read(struct bvm *vm) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
if (ser) {
|
||||
int32_t len = ser->available();
|
||||
if (len < 0) { len = 0; }
|
||||
if (len > 0) {
|
||||
// read bytes on stack
|
||||
char * rx_buf = new char[len];
|
||||
len = ser->read(rx_buf, len);
|
||||
be_pushbytes(vm, rx_buf, len);
|
||||
delete[] rx_buf;
|
||||
be_return(vm);
|
||||
} else {
|
||||
be_pushbytes(vm, nullptr, 0);
|
||||
be_return(vm);
|
||||
}
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
// Berry: `write(int || bytes) -> nil` (write bytes, blocking until all sent)
|
||||
int32_t b_serial_write(struct bvm *vm);
|
||||
int32_t b_serial_write(struct bvm *vm) {
|
||||
int32_t argc = be_top(vm); // Get the number of arguments
|
||||
if (argc >= 2 && (be_isint(vm, 2) || be_isbytes(vm, 2))) {
|
||||
be_getmember(vm, 1, ".p");
|
||||
TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1);
|
||||
if (ser) {
|
||||
if (be_isint(vm, 2)) {
|
||||
int32_t val = be_toint(vm, 2);
|
||||
ser->write(val);
|
||||
be_pushint(vm, 1);
|
||||
} else {
|
||||
size_t len;
|
||||
const uint8_t * buf = (const uint8_t *) be_tobytes(vm, 2, &len);
|
||||
for (uint32_t i=0; i<len; i++) {
|
||||
ser->write(buf[i]);
|
||||
}
|
||||
be_pushint(vm, len);
|
||||
}
|
||||
be_return(vm);
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
be_raise(vm, kTypeError, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // USE_BERRY
|
Loading…
Reference in New Issue