Berry `zigbee.find()` (#21889)

This commit is contained in:
s-hadinger 2024-08-02 15:43:31 +02:00 committed by GitHub
parent 8f74a226e7
commit 6b98a320b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 3 deletions

View File

@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
- Berry new type "addr" to ctypes mapping (#21883) - Berry new type "addr" to ctypes mapping (#21883)
- Berry `file.savecode()` (#21884) - Berry `file.savecode()` (#21884)
- Berry `solidify.nocompact()` and reduce size of Matter UI (#21885) - Berry `solidify.nocompact()` and reduce size of Matter UI (#21885)
- Berry `zigbee.find()`
### Breaking Changed ### Breaking Changed
- Berry `energy` module support for 8 phases and move to pseudo-arrays (#21887) - Berry `energy` module support for 8 phases and move to pseudo-arrays (#21887)

View File

@ -51,6 +51,7 @@ static int zd_member(bvm *vm) {
extern int zc_info(struct bvm *vm); extern int zc_info(struct bvm *vm);
extern int zc_item(struct bvm *vm); extern int zc_item(struct bvm *vm);
extern int zc_find(struct bvm *vm);
extern int32_t zc_size(void* d); BE_FUNC_CTYPE_DECLARE(zc_size, "i", "."); extern int32_t zc_size(void* d); BE_FUNC_CTYPE_DECLARE(zc_size, "i", ".");
extern int zc_iter(bvm *vm); extern int zc_iter(bvm *vm);
extern void zc_abort(void); BE_FUNC_CTYPE_DECLARE(zc_abort, "", "."); extern void zc_abort(void); BE_FUNC_CTYPE_DECLARE(zc_abort, "", ".");
@ -113,6 +114,7 @@ class be_class_zb_coord_ntv (scope: global, name: zb_coord_ntv, strings: weak) {
info, func(zc_info) info, func(zc_info)
item, func(zc_item) item, func(zc_item)
find, func(zc_find)
size, ctype_func(zc_size) size, ctype_func(zc_size)
iter, func(zc_iter) iter, func(zc_iter)

View File

@ -99,8 +99,8 @@ extern "C" {
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
int zc_item(struct bvm *vm); // implement item() and find()
int zc_item(struct bvm *vm) { int zc_item_or_find(struct bvm *vm, bbool raise_if_unknown) {
int32_t top = be_top(vm); // Get the number of arguments int32_t top = be_top(vm); // Get the number of arguments
if (zigbee.init_phase) { if (zigbee.init_phase) {
be_raise(vm, "internal_error", "zigbee not started"); be_raise(vm, "internal_error", "zigbee not started");
@ -109,7 +109,11 @@ extern "C" {
const Z_Device & device = be_isint(vm, 2) ? zigbee_devices.findShortAddr(be_toint(vm, 2)) const Z_Device & device = be_isint(vm, 2) ? zigbee_devices.findShortAddr(be_toint(vm, 2))
: zigbee_devices.parseDeviceFromName(be_tostring(vm, 2)); : zigbee_devices.parseDeviceFromName(be_tostring(vm, 2));
if (!device.valid()) { if (!device.valid()) {
be_raise(vm, "value_error", "unknown device"); if (raise_if_unknown) {
be_raise(vm, "index_error", "unknown device");
} else {
be_return_nil(vm);
}
} }
be_pushntvclass(vm, &be_class_zb_device); be_pushntvclass(vm, &be_class_zb_device);
@ -121,6 +125,20 @@ extern "C" {
be_raise(vm, kTypeError, nullptr); be_raise(vm, kTypeError, nullptr);
} }
// `zigbee.item(shortaddr:int | friendlyname:str) -> instance of zb_device`
// raise en exception if not found
int zc_item(struct bvm *vm);
int zc_item(struct bvm *vm) {
return zc_item_or_find(vm, true);
}
// `zigbee.find(shortaddr:int | friendlyname:str) -> instance of zb_device`
// return `nil` if not found
int zc_find(struct bvm *vm);
int zc_find(struct bvm *vm) {
return zc_item_or_find(vm, false);
}
int32_t zc_size(void*) { int32_t zc_size(void*) {
return zigbee_devices.devicesSize(); return zigbee_devices.devicesSize();
} }