2021-02-28 19:50:37 +00:00
|
|
|
/********************************************************************
|
|
|
|
* Tasmota lib
|
|
|
|
*
|
|
|
|
* To use: `import wire`
|
|
|
|
*
|
|
|
|
* 2 wire communication - I2C
|
|
|
|
*******************************************************************/
|
2021-04-28 18:30:19 +01:00
|
|
|
#include "be_constobj.h"
|
2021-02-28 19:50:37 +00:00
|
|
|
|
2021-04-24 11:11:06 +01:00
|
|
|
#ifdef USE_I2C
|
|
|
|
|
2021-03-13 21:42:24 +00:00
|
|
|
extern int b_wire_init(bvm *vm);
|
|
|
|
|
2021-02-28 19:50:37 +00:00
|
|
|
extern int b_wire_begintransmission(bvm *vm);
|
|
|
|
extern int b_wire_endtransmission(bvm *vm);
|
|
|
|
extern int b_wire_requestfrom(bvm *vm);
|
|
|
|
extern int b_wire_available(bvm *vm);
|
|
|
|
extern int b_wire_write(bvm *vm);
|
|
|
|
extern int b_wire_read(bvm *vm);
|
|
|
|
|
2021-03-07 18:37:18 +00:00
|
|
|
extern int b_wire_scan(bvm *vm);
|
|
|
|
|
|
|
|
extern int b_wire_validwrite(bvm *vm);
|
2021-03-03 07:34:38 +00:00
|
|
|
extern int b_wire_validread(bvm *vm);
|
2021-03-20 17:44:35 +00:00
|
|
|
extern int b_wire_detect(bvm *vm);
|
2021-03-03 07:34:38 +00:00
|
|
|
|
2021-03-27 18:02:22 +00:00
|
|
|
/********************************************************************
|
|
|
|
"def read_bytes(addr,reg,size) "
|
|
|
|
"self._begin_transmission(addr) "
|
|
|
|
"self._write(reg) "
|
|
|
|
"self._end_transmission(false) "
|
|
|
|
"self._request_from(addr,size) "
|
|
|
|
"var ret=bytes(size) "
|
|
|
|
"while (self._available()) "
|
|
|
|
"ret..self._read() "
|
|
|
|
"end "
|
|
|
|
"return ret "
|
|
|
|
"end "
|
|
|
|
********************************************************************/
|
2021-06-27 14:16:22 +01:00
|
|
|
|
2021-03-27 18:02:22 +00:00
|
|
|
/********************************************************************
|
|
|
|
** Solidified function: read_bytes
|
|
|
|
********************************************************************/
|
2021-06-27 14:16:22 +01:00
|
|
|
be_local_closure(read_bytes, /* name */
|
|
|
|
be_nested_proto(
|
|
|
|
8, /* nstack */
|
|
|
|
4, /* argc */
|
|
|
|
0, /* has upvals */
|
|
|
|
NULL, /* no upvals */
|
|
|
|
0, /* has sup protos */
|
|
|
|
NULL, /* no sub protos */
|
|
|
|
1, /* has constants */
|
|
|
|
( &(const bvalue[ 6]) { /* upvals */
|
|
|
|
{ { .s=be_nested_const_str("_begin_transmission", -1515506120, 19) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_write", -2079504471, 6) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_end_transmission", -1057486896, 17) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_request_from", -329818692, 13) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_available", 1306196581, 10) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_read", 346717030, 5) }, BE_STRING},
|
|
|
|
}),
|
|
|
|
(be_nested_const_str("read_bytes", -718234123, 10)),
|
|
|
|
(be_nested_const_str("string", 398550328, 6)),
|
|
|
|
( &(const binstruction[24]) { /* code */
|
|
|
|
0x8C100100, // 0000 GETMET R4 R0 R256
|
|
|
|
0x5C180200, // 0001 MOVE R6 R1
|
|
|
|
0x7C100400, // 0002 CALL R4 2
|
|
|
|
0x8C100101, // 0003 GETMET R4 R0 R257
|
|
|
|
0x5C180400, // 0004 MOVE R6 R2
|
|
|
|
0x7C100400, // 0005 CALL R4 2
|
|
|
|
0x8C100102, // 0006 GETMET R4 R0 R258
|
|
|
|
0x50180000, // 0007 LDBOOL R6 0 0
|
|
|
|
0x7C100400, // 0008 CALL R4 2
|
|
|
|
0x8C100103, // 0009 GETMET R4 R0 R259
|
|
|
|
0x5C180200, // 000A MOVE R6 R1
|
|
|
|
0x5C1C0600, // 000B MOVE R7 R3
|
|
|
|
0x7C100600, // 000C CALL R4 3
|
|
|
|
0x60100002, // 000D GETGBL R4 G2
|
|
|
|
0x5C140600, // 000E MOVE R5 R3
|
|
|
|
0x7C100200, // 000F CALL R4 1
|
|
|
|
0x8C140104, // 0010 GETMET R5 R0 R260
|
|
|
|
0x7C140200, // 0011 CALL R5 1
|
|
|
|
0x78160003, // 0012 JMPF R5 #0017
|
|
|
|
0x8C140105, // 0013 GETMET R5 R0 R261
|
|
|
|
0x7C140200, // 0014 CALL R5 1
|
|
|
|
0x40140805, // 0015 CONNECT R5 R4 R5
|
|
|
|
0x7001FFF8, // 0016 JMP #0010
|
|
|
|
0x80040800, // 0017 RET 1 R4
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-03-27 18:02:22 +00:00
|
|
|
/*******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
"def write_bytes(addr,reg,b) "
|
|
|
|
"self._begin_transmission(addr) "
|
|
|
|
"self._write(reg) "
|
|
|
|
"self._write(b) "
|
|
|
|
"self._end_transmission() "
|
|
|
|
"end "
|
|
|
|
********************************************************************/
|
2021-06-27 14:16:22 +01:00
|
|
|
|
2021-03-27 18:02:22 +00:00
|
|
|
/********************************************************************
|
|
|
|
** Solidified function: write_bytes
|
|
|
|
********************************************************************/
|
2021-06-27 14:16:22 +01:00
|
|
|
be_local_closure(write_bytes, /* name */
|
|
|
|
be_nested_proto(
|
|
|
|
7, /* nstack */
|
|
|
|
4, /* argc */
|
|
|
|
0, /* has upvals */
|
|
|
|
NULL, /* no upvals */
|
|
|
|
0, /* has sup protos */
|
|
|
|
NULL, /* no sub protos */
|
|
|
|
1, /* has constants */
|
|
|
|
( &(const bvalue[ 3]) { /* upvals */
|
|
|
|
{ { .s=be_nested_const_str("_begin_transmission", -1515506120, 19) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_write", -2079504471, 6) }, BE_STRING},
|
|
|
|
{ { .s=be_nested_const_str("_end_transmission", -1057486896, 17) }, BE_STRING},
|
|
|
|
}),
|
|
|
|
(be_nested_const_str("write_bytes", 1227543792, 11)),
|
|
|
|
(be_nested_const_str("string", 398550328, 6)),
|
|
|
|
( &(const binstruction[12]) { /* code */
|
|
|
|
0x8C100100, // 0000 GETMET R4 R0 R256
|
|
|
|
0x5C180200, // 0001 MOVE R6 R1
|
|
|
|
0x7C100400, // 0002 CALL R4 2
|
|
|
|
0x8C100101, // 0003 GETMET R4 R0 R257
|
|
|
|
0x5C180400, // 0004 MOVE R6 R2
|
|
|
|
0x7C100400, // 0005 CALL R4 2
|
|
|
|
0x8C100101, // 0006 GETMET R4 R0 R257
|
|
|
|
0x5C180600, // 0007 MOVE R6 R3
|
|
|
|
0x7C100400, // 0008 CALL R4 2
|
|
|
|
0x8C100102, // 0009 GETMET R4 R0 R258
|
|
|
|
0x7C100200, // 000A CALL R4 1
|
|
|
|
0x80000000, // 000B RET 0 R0
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-03-27 18:02:22 +00:00
|
|
|
/*******************************************************************/
|
|
|
|
|
2021-04-28 18:30:19 +01:00
|
|
|
#if BE_USE_PRECOMPILED_OBJECT
|
|
|
|
#include "../generate/be_fixed_be_class_tasmota_wire.h"
|
|
|
|
#endif
|
2021-03-27 18:02:22 +00:00
|
|
|
|
2021-04-28 18:30:19 +01:00
|
|
|
void be_load_wirelib(bvm *vm) {
|
|
|
|
#if !BE_USE_PRECOMPILED_OBJECT
|
2021-03-13 21:42:24 +00:00
|
|
|
static const bnfuncinfo members[] = {
|
2021-03-28 18:07:48 +01:00
|
|
|
{ "bus", NULL }, // bus number
|
2021-03-13 21:42:24 +00:00
|
|
|
{ "init", b_wire_init },
|
2021-03-20 17:44:35 +00:00
|
|
|
{ "_begin_transmission", b_wire_begintransmission },
|
|
|
|
{ "_end_transmission", b_wire_endtransmission },
|
|
|
|
{ "_request_from", b_wire_requestfrom },
|
2021-03-13 21:42:24 +00:00
|
|
|
{ "_available", b_wire_available },
|
|
|
|
{ "_write", b_wire_write },
|
|
|
|
{ "_read", b_wire_read },
|
|
|
|
{ "scan", b_wire_scan },
|
|
|
|
{ "write", b_wire_validwrite },
|
|
|
|
{ "read", b_wire_validread },
|
2021-03-20 17:44:35 +00:00
|
|
|
{ "detect", b_wire_detect },
|
2021-03-27 18:02:22 +00:00
|
|
|
|
|
|
|
{ NULL, (bntvfunc) BE_CLOSURE }, /* mark section for berry closures */
|
|
|
|
{ "read_bytes", (bntvfunc) &read_bytes_closure },
|
|
|
|
{ "write_bytes", (bntvfunc) &write_bytes_closure },
|
2021-03-13 21:42:24 +00:00
|
|
|
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2021-03-27 18:02:22 +00:00
|
|
|
be_regclass(vm, "Wire", members);
|
2021-02-28 19:50:37 +00:00
|
|
|
#else
|
2021-04-28 18:30:19 +01:00
|
|
|
be_pushntvclass(vm, &be_class_tasmota_wire);
|
|
|
|
be_setglobal(vm, "Wire");
|
|
|
|
be_pop(vm, 1);
|
|
|
|
#endif
|
|
|
|
}
|
2021-02-28 19:50:37 +00:00
|
|
|
/* @const_object_info_begin
|
2021-04-28 18:30:19 +01:00
|
|
|
|
|
|
|
class be_class_tasmota_wire (scope: global, name: Wire) {
|
|
|
|
bus, var
|
|
|
|
|
|
|
|
init, func(b_wire_init)
|
|
|
|
_begin_transmission, func(b_wire_begintransmission)
|
|
|
|
_end_transmission, func(b_wire_endtransmission)
|
|
|
|
_request_from, func(b_wire_requestfrom)
|
|
|
|
_available, func(b_wire_available)
|
|
|
|
_write, func(b_wire_write)
|
|
|
|
_read, func(b_wire_read)
|
|
|
|
scan, func(b_wire_scan)
|
|
|
|
write, func(b_wire_validwrite)
|
|
|
|
read, func(b_wire_validread)
|
|
|
|
detect, func(b_wire_detect)
|
|
|
|
|
|
|
|
read_bytes, closure(read_bytes_closure)
|
|
|
|
write_bytes, closure(write_bytes_closure)
|
2021-02-28 19:50:37 +00:00
|
|
|
}
|
|
|
|
@const_object_info_end */
|
2021-04-24 11:11:06 +01:00
|
|
|
|
|
|
|
#endif // USE_I2C
|