Merge pull request #13570 from s-hadinger/berry_wire_enabled

Berry avoid crash when I2C is not configured
This commit is contained in:
s-hadinger 2021-11-04 20:56:07 +01:00 committed by GitHub
commit 15990f2aa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1552 additions and 1511 deletions

View File

@ -438,15 +438,16 @@ be_local_closure(wire_scan, /* name */
0, /* has sup protos */ 0, /* has sup protos */
NULL, /* no sub protos */ NULL, /* no sub protos */
1, /* has constants */ 1, /* has constants */
( &(const bvalue[ 4]) { /* constants */ ( &(const bvalue[ 5]) { /* constants */
/* K0 */ be_nested_string("i2c_enabled", 218388101, 11), /* K0 */ be_nested_string("i2c_enabled", 218388101, 11),
/* K1 */ be_nested_string("wire1", -1082245877, 5), /* K1 */ be_nested_string("wire1", -1082245877, 5),
/* K2 */ be_nested_string("detect", 8884370, 6), /* K2 */ be_nested_string("enabled", 49525662, 7),
/* K3 */ be_nested_string("wire2", -1065468258, 5), /* K3 */ be_nested_string("detect", 8884370, 6),
/* K4 */ be_nested_string("wire2", -1065468258, 5),
}), }),
(be_nested_const_str("wire_scan", -1623691416, 9)), (be_nested_const_str("wire_scan", -1623691416, 9)),
(be_nested_const_str("Tasmota.be", 825809411, 10)), (be_nested_const_str("input", -103256197, 5)),
( &(const binstruction[25]) { /* code */ ( &(const binstruction[33]) { /* code */
0x4C0C0000, // 0000 LDNIL R3 0x4C0C0000, // 0000 LDNIL R3
0x200C0403, // 0001 NE R3 R2 R3 0x200C0403, // 0001 NE R3 R2 R3
0x780E0005, // 0002 JMPF R3 #0009 0x780E0005, // 0002 JMPF R3 #0009
@ -458,20 +459,28 @@ be_local_closure(wire_scan, /* name */
0x80040600, // 0008 RET 1 R3 0x80040600, // 0008 RET 1 R3
0x880C0101, // 0009 GETMBR R3 R0 K1 0x880C0101, // 0009 GETMBR R3 R0 K1
0x8C0C0702, // 000A GETMET R3 R3 K2 0x8C0C0702, // 000A GETMET R3 R3 K2
0x5C140200, // 000B MOVE R5 R1 0x7C0C0200, // 000B CALL R3 1
0x7C0C0400, // 000C CALL R3 2 0x780E0006, // 000C JMPF R3 #0014
0x780E0001, // 000D JMPF R3 #0010 0x880C0101, // 000D GETMBR R3 R0 K1
0x880C0101, // 000E GETMBR R3 R0 K1 0x8C0C0703, // 000E GETMET R3 R3 K3
0x80040600, // 000F RET 1 R3 0x5C140200, // 000F MOVE R5 R1
0x880C0103, // 0010 GETMBR R3 R0 K3 0x7C0C0400, // 0010 CALL R3 2
0x8C0C0702, // 0011 GETMET R3 R3 K2 0x780E0001, // 0011 JMPF R3 #0014
0x5C140200, // 0012 MOVE R5 R1 0x880C0101, // 0012 GETMBR R3 R0 K1
0x7C0C0400, // 0013 CALL R3 2 0x80040600, // 0013 RET 1 R3
0x780E0001, // 0014 JMPF R3 #0017 0x880C0104, // 0014 GETMBR R3 R0 K4
0x880C0103, // 0015 GETMBR R3 R0 K3 0x8C0C0702, // 0015 GETMET R3 R3 K2
0x80040600, // 0016 RET 1 R3 0x7C0C0200, // 0016 CALL R3 1
0x4C0C0000, // 0017 LDNIL R3 0x780E0006, // 0017 JMPF R3 #001F
0x80040600, // 0018 RET 1 R3 0x880C0104, // 0018 GETMBR R3 R0 K4
0x8C0C0703, // 0019 GETMET R3 R3 K3
0x5C140200, // 001A MOVE R5 R1
0x7C0C0400, // 001B CALL R3 2
0x780E0001, // 001C JMPF R3 #001F
0x880C0104, // 001D GETMBR R3 R0 K4
0x80040600, // 001E RET 1 R3
0x4C0C0000, // 001F LDNIL R3
0x80040600, // 0020 RET 1 R3
}) })
) )
); );

View File

@ -23,6 +23,7 @@ extern int b_wire_scan(bvm *vm);
extern int b_wire_validwrite(bvm *vm); extern int b_wire_validwrite(bvm *vm);
extern int b_wire_validread(bvm *vm); extern int b_wire_validread(bvm *vm);
extern int b_wire_detect(bvm *vm); extern int b_wire_detect(bvm *vm);
extern int b_wire_enabled(bvm *vm);
/******************************************************************** /********************************************************************
** Solidified function: write_bytes ** Solidified function: write_bytes
@ -140,6 +141,7 @@ class be_class_tasmota_wire (scope: global, name: Wire) {
write, func(b_wire_validwrite) write, func(b_wire_validwrite)
read, func(b_wire_validread) read, func(b_wire_validread)
detect, func(b_wire_detect) detect, func(b_wire_detect)
enabled, func(b_wire_enabled)
read_bytes, closure(read_bytes_closure) read_bytes, closure(read_bytes_closure)
write_bytes, closure(write_bytes_closure) write_bytes, closure(write_bytes_closure)

View File

@ -309,8 +309,8 @@ class Tasmota
def wire_scan(addr,idx) def wire_scan(addr,idx)
# skip if the I2C index is disabled # skip if the I2C index is disabled
if idx != nil && !self.i2c_enabled(idx) return nil end if idx != nil && !self.i2c_enabled(idx) return nil end
if self.wire1.detect(addr) return self.wire1 end if self.wire1.enabled() && self.wire1.detect(addr) return self.wire1 end
if self.wire2.detect(addr) return self.wire2 end if self.wire2.enabled() && self.wire2.detect(addr) return self.wire2 end
return nil return nil
end end

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,26 @@
#include "be_constobj.h" #include "be_constobj.h"
static be_define_const_map_slots(be_class_tasmota_wire_map) { static be_define_const_map_slots(be_class_tasmota_wire_map) {
{ be_const_key(_end_transmission, 10), be_const_func(b_wire_endtransmission) },
{ be_const_key(bus, -1), be_const_var(0) },
{ be_const_key(_read, 7), be_const_func(b_wire_read) },
{ be_const_key(read, -1), be_const_func(b_wire_validread) },
{ be_const_key(_request_from, -1), be_const_func(b_wire_requestfrom) },
{ be_const_key(init, -1), be_const_func(b_wire_init) }, { be_const_key(init, -1), be_const_func(b_wire_init) },
{ be_const_key(write_bytes, -1), be_const_closure(write_bytes_closure) },
{ be_const_key(_begin_transmission, -1), be_const_func(b_wire_begintransmission) },
{ be_const_key(scan, -1), be_const_func(b_wire_scan) },
{ be_const_key(_write, 1), be_const_func(b_wire_write) },
{ be_const_key(write, -1), be_const_func(b_wire_validwrite) },
{ be_const_key(_available, -1), be_const_func(b_wire_available) }, { be_const_key(_available, -1), be_const_func(b_wire_available) },
{ be_const_key(detect, -1), be_const_func(b_wire_detect) }, { be_const_key(bus, -1), be_const_var(0) },
{ be_const_key(read_bytes, -1), be_const_closure(read_bytes_closure) }, { be_const_key(read_bytes, -1), be_const_closure(read_bytes_closure) },
{ be_const_key(_request_from, -1), be_const_func(b_wire_requestfrom) },
{ be_const_key(detect, -1), be_const_func(b_wire_detect) },
{ be_const_key(scan, 2), be_const_func(b_wire_scan) },
{ be_const_key(enabled, -1), be_const_func(b_wire_enabled) },
{ be_const_key(_write, 13), be_const_func(b_wire_write) },
{ be_const_key(read, -1), be_const_func(b_wire_validread) },
{ be_const_key(_end_transmission, 8), be_const_func(b_wire_endtransmission) },
{ be_const_key(_begin_transmission, -1), be_const_func(b_wire_begintransmission) },
{ be_const_key(write_bytes, 7), be_const_closure(write_bytes_closure) },
{ be_const_key(_read, -1), be_const_func(b_wire_read) },
{ be_const_key(write, -1), be_const_func(b_wire_validwrite) },
}; };
static be_define_const_map( static be_define_const_map(
be_class_tasmota_wire_map, be_class_tasmota_wire_map,
14 15
); );
BE_EXPORT_VARIABLE be_define_const_class( BE_EXPORT_VARIABLE be_define_const_class(

View File

@ -26,18 +26,36 @@
#include <Wire.h> #include <Wire.h>
// read the `bus` attribute and return `Wire` or `Wire1` // read the `bus` attribute and return `Wire` or `Wire1`
// Can return nullptr reference if the bus is not initialized
TwoWire & getWire(bvm *vm); TwoWire & getWire(bvm *vm);
TwoWire & getWire(bvm *vm) { TwoWire & getWire(bvm *vm) {
be_getmember(vm, 1, "bus"); be_getmember(vm, 1, "bus");
int32_t bus = be_toint(vm, -1); // bus is 1 or 2 int32_t bus = be_toint(vm, -1); // bus is 1 or 2
be_pop(vm, 1); be_pop(vm, 1);
if (!TasmotaGlobal.i2c_enabled_2) { bus = 1; } if (1 == bus && TasmotaGlobal.i2c_enabled) {
if (2 != bus) {
return Wire; return Wire;
} else { } else if (2 == bus && TasmotaGlobal.i2c_enabled_2) {
return Wire1; return Wire1;
} else {
be_raise(vm, "configuration_error", "I2C bus not initiliazedd");
return *(TwoWire*)nullptr;
} }
} }
bool I2cEnabled(bvm *vm);
bool I2cEnabled(bvm *vm) {
be_getmember(vm, 1, "bus");
int32_t bus = be_toint(vm, -1); // bus is 1 or 2
be_pop(vm, 1);
if (1 == bus && TasmotaGlobal.i2c_enabled) {
return true;
} else if (2 == bus && TasmotaGlobal.i2c_enabled_2) {
return true;
} else {
return false;
}
}
int32_t getBus(bvm *vm); // 1 or 2 int32_t getBus(bvm *vm); // 1 or 2
int32_t getBus(bvm *vm) { int32_t getBus(bvm *vm) {
be_getmember(vm, 1, "bus"); be_getmember(vm, 1, "bus");
@ -243,6 +261,14 @@ extern "C" {
} }
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
// Berry: `enabled() -> bool` true if I2C bus is enabled
int32_t b_wire_enabled(struct bvm *vm);
int32_t b_wire_enabled(struct bvm *vm) {
bool en = I2cEnabled(vm);
be_pushbool(vm, en);
be_return(vm);
}
} }
#endif // USE_I2C #endif // USE_I2C