Berry preliminary support for OneWire

This commit is contained in:
Stephan Hadinger 2021-08-23 20:32:32 +02:00
parent bda28e9981
commit ce8958c89b
6 changed files with 1931 additions and 1590 deletions

View File

@ -104,6 +104,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = {
#ifdef ESP32 #ifdef ESP32
extern void be_load_tasmota_ntvlib(bvm *vm); extern void be_load_tasmota_ntvlib(bvm *vm);
extern void be_load_wirelib(bvm *vm); extern void be_load_wirelib(bvm *vm);
extern void be_load_onewirelib(bvm *vm);
extern void be_load_Driver_class(bvm *vm); extern void be_load_Driver_class(bvm *vm);
extern void be_load_Timer_class(bvm *vm); extern void be_load_Timer_class(bvm *vm);
extern void be_load_driver_i2c_lib(bvm *vm); extern void be_load_driver_i2c_lib(bvm *vm);
@ -147,6 +148,9 @@ BERRY_API void be_load_custom_libs(bvm *vm)
be_load_wirelib(vm); be_load_wirelib(vm);
be_load_driver_i2c_lib(vm); be_load_driver_i2c_lib(vm);
#endif // USE_I2C #endif // USE_I2C
#if defined(USE_ONEWIRE) || defined(USE_DS18x20)
be_load_onewirelib(vm);
#endif
#ifdef USE_I2S_AUDIO_BERRY #ifdef USE_I2S_AUDIO_BERRY
be_load_driver_audio_lib(vm); be_load_driver_audio_lib(vm);
#endif #endif

View File

@ -0,0 +1,57 @@
/********************************************************************
* Tasmota lib
*
* To use: `import wire`
*
* 2 wire communication - I2C
*******************************************************************/
#include "be_constobj.h"
#if defined(USE_ONEWIRE) || defined(USE_DS18x20)
extern int b_onewire_init(bvm *vm);
extern int b_onewire_deinit(bvm *vm);
extern int b_onewire_reset(bvm *vm);
extern int b_onewire_select(bvm *vm);
extern int b_onewire_skip(bvm *vm);
extern int b_onewire_depower(bvm *vm);
extern int b_onewire_write(bvm *vm);
extern int b_onewire_read(bvm *vm);
extern int b_onewire_reset_search(bvm *vm);
extern int b_onewire_target_search(bvm *vm);
extern int b_onewire_search(bvm *vm);
#include "../generate/be_fixed_be_class_tasmota_onewire.h"
void be_load_onewirelib(bvm *vm) {
be_pushntvclass(vm, &be_class_tasmota_onewire);
be_setglobal(vm, "OneWire");
be_pop(vm, 1);
}
/* @const_object_info_begin
class be_class_tasmota_onewire (scope: global, name: OneWire) {
.p, var
init, func(b_onewire_init)
deinit, func(b_onewire_deinit)
reset, func(b_onewire_reset)
select, func(b_onewire_select)
skip, func(b_onewire_skip)
depower, func(b_onewire_depower)
write, func(b_onewire_write)
read, func(b_onewire_read)
reset_search, func(b_onewire_reset_search)
target_search, func(b_onewire_target_search)
search, func(b_onewire_search)
}
@const_object_info_end */
#endif // defined(USE_ONEWIRE) || defined(USE_DS18x20)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,28 @@
#include "be_constobj.h"
static be_define_const_map_slots(be_class_tasmota_onewire_map) {
{ be_const_key(reset, -1), be_const_func(b_onewire_reset) },
{ be_const_key(search, 10), be_const_func(b_onewire_search) },
{ be_const_key(depower, -1), be_const_func(b_onewire_depower) },
{ be_const_key(init, 2), be_const_func(b_onewire_init) },
{ be_const_key(deinit, -1), be_const_func(b_onewire_deinit) },
{ be_const_key(reset_search, -1), be_const_func(b_onewire_reset_search) },
{ be_const_key(skip, -1), be_const_func(b_onewire_skip) },
{ be_const_key(dot_p, -1), be_const_var(0) },
{ be_const_key(write, -1), be_const_func(b_onewire_write) },
{ be_const_key(read, -1), be_const_func(b_onewire_read) },
{ be_const_key(select, 11), be_const_func(b_onewire_select) },
{ be_const_key(target_search, -1), be_const_func(b_onewire_target_search) },
};
static be_define_const_map(
be_class_tasmota_onewire_map,
12
);
BE_EXPORT_VARIABLE be_define_const_class(
be_class_tasmota_onewire,
1,
NULL,
OneWire
);

View File

@ -0,0 +1,232 @@
/*
xdrv_52_3_berry_native.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
#if defined(USE_ONEWIRE) || defined(USE_DS18x20)
#include <berry.h>
#include <OneWire.h>
// // read the `bus` attribute and return `Wire` or `Wire1`
// TwoWire & getWire(bvm *vm);
// TwoWire & getWire(bvm *vm) {
// be_getmember(vm, 1, "bus");
// int32_t bus = be_toint(vm, -1); // bus is 1 or 2
// be_pop(vm, 1);
// if (2 != bus) {
// return Wire;
// } else {
// return Wire1;
// }
// }
// int32_t getBus(bvm *vm); // 1 or 2
// int32_t getBus(bvm *vm) {
// be_getmember(vm, 1, "bus");
// int32_t bus = be_toint(vm, -1);
// be_pop(vm, 1);
// return bus;
// }
/*********************************************************************************************\
* Native functions mapped to Berry functions
*
* import wire
*
* wire.get_free_heap() -> int
*
\*********************************************************************************************/
extern "C" {
OneWire * b_onewire_get(struct bvm *vm) {
be_getmember(vm, 1, ".p");
OneWire * ow = (OneWire *) be_tocomptr(vm, -1);
be_pop(vm, 1);
return ow;
}
// Berry: `init(gpio:int)`
int32_t b_onewire_init(struct bvm *vm);
int32_t b_onewire_init(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc > 1 && be_isint(vm, 2)) {
int gpio = be_toint(vm, 2);
OneWire * ow = new OneWire(gpio);
be_pushcomptr(vm, (void*) ow);
be_setmember(vm, 1, ".p");
be_return_nil(vm);
}
be_raise(vm, kTypeError, "gpio number expected");
}
// Berry: `deinit(void)`
int32_t b_onewire_deinit(struct bvm *vm);
int32_t b_onewire_deinit(struct bvm *vm) {
be_getmember(vm, 1, ".p");
OneWire * ow = (OneWire *) be_tocomptr(vm, -1);
if (ow) {
delete ow;
// clear
be_pushcomptr(vm, (void*) NULL);
be_setmember(vm, 1, ".p");
}
be_return_nil(vm);
}
// Berry: `reset(void) -> bool`
// returns true if a device is present
int32_t b_onewire_reset(struct bvm *vm);
int32_t b_onewire_reset(struct bvm *vm) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
uint8_t resp = ow->reset();
be_pushbool(vm, resp != 0);
be_return(vm);
}
be_return_nil(vm);
}
// Berry: `skip(void) -> nil`
int32_t b_onewire_skip(struct bvm *vm);
int32_t b_onewire_skip(struct bvm *vm) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
ow->skip();
}
be_return_nil(vm);
}
// Berry: `depower(void) -> nil`
int32_t b_onewire_depower(struct bvm *vm);
int32_t b_onewire_depower(struct bvm *vm) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
ow->depower();
}
be_return_nil(vm);
}
// Berry: `select(bytes) -> nil`
// returns true if a device is present
int32_t b_onewire_select(struct bvm *vm);
int32_t b_onewire_select(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc > 1 && be_isbytes(vm, 2)) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
uint8_t rom[8] = {0};
size_t len;
const void * buf = be_tobytes(vm, 2, &len);
if (len > 8) { len = 8; } /* 8 bytes max */
memmove(rom, buf, len);
ow->select(rom);
}
be_return_nil(vm);
}
be_raise(vm, kTypeError, NULL);
}
// Berry: `write(byte:int | bytes [, power:bool = false]) -> nil`
int32_t b_onewire_write(struct bvm *vm);
int32_t b_onewire_write(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
bool power = false;
if (argc > 1 && (be_isint(vm, 2) || be_isbytes(vm, 2))) {
if (argc > 2 && (be_isbool(vm, 3) || be_isint(vm,3))) {
power = be_tobool(vm, 3);
}
uint8_t val = 0;
if (be_isint(vm, 2)) {
val = be_toint(vm, 2);
} else { /* bytes */
size_t len = 0;
const uint8_t * buf = (const uint8_t *) be_tobytes(vm, 2, &len);
if (len > 0) {
val = buf[0];
} else {
be_return_nil(vm); /* abort */
}
}
OneWire * ow = b_onewire_get(vm);
if (ow) {
ow->write(val, power);
}
be_return_nil(vm);
}
be_raise(vm, kTypeError, NULL);
}
// Berry: `read(void) -> bytes()`
int32_t b_onewire_read(struct bvm *vm);
int32_t b_onewire_read(struct bvm *vm) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
uint8_t val = ow->read();
be_pushbytes(vm, &val, 1);
be_return(vm);
}
be_return_nil(vm);
}
// Berry: `reset_search(void) -> nil`
int32_t b_onewire_reset_search(struct bvm *vm);
int32_t b_onewire_reset_search(struct bvm *vm) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
ow->reset_search();
}
be_return_nil(vm);
}
// Berry: `target_search(family_code:int) -> nil`
int32_t b_onewire_target_search(struct bvm *vm);
int32_t b_onewire_target_search(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc > 1 && be_isint(vm, 2)) {
OneWire * ow = b_onewire_get(vm);
if (ow) {
ow->target_search(be_toint(vm, 2));
}
be_return_nil(vm);
}
be_raise(vm, kTypeError, NULL);
}
// Berry: `search(void) -> bytes() or nil`
int32_t b_onewire_search(struct bvm *vm);
int32_t b_onewire_search(struct bvm *vm) {
uint8_t rom[8] = {0};
OneWire * ow = b_onewire_get(vm);
if (ow) {
uint8_t res = ow->search(rom);
if (res) {
be_pushbytes(vm, rom, sizeof(rom));
be_return(vm);
} else {
be_return_nil(vm);
}
}
be_return_nil(vm);
}
}
#endif // USE_I2C
#endif // USE_BERRY