Skip to content

Commit

Permalink
Berry add new type "addr" to ctypes mapping (#21883)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Aug 1, 2024
1 parent a3ed99e commit 836ce9e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 72 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ All notable changes to this project will be documented in this file.
- NeoPool command `NPSetOption<x>` to enabled/disable data validation/connection statistics (#21850)
- Analog GPIO ``ADC Input`` with ``AdcParam<x> 1,<start_range>,<end_range>,<margin>,1`` provide direct light control
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
- Berry add new type "addr" to ctypes mapping

### Breaking Changed

Expand Down
15 changes: 4 additions & 11 deletions lib/libesp32/berry_tasmota/include/be_ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ enum {
ctypes_ptr32 = 9,
ctypes_ptr64 = -9,

// address (no acces to the value)
ctypes_addr = 15,

// special
ctypes_bf = 0, //bif-field
};

Expand All @@ -52,17 +56,6 @@ typedef struct be_ctypes_structure_t {
const be_ctypes_structure_item_t * items;
} be_ctypes_structure_t;

typedef struct be_ctypes_class_t {
const char * name;
const be_ctypes_structure_t * definitions;
} be_ctypes_class_t;

typedef struct be_ctypes_classes_t {
uint16_t size;
const char **instance_mapping; /* array of instance class names for automatic instanciation of class */
const be_ctypes_class_t * classes;
} be_ctypes_classes_t;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
74 changes: 14 additions & 60 deletions lib/libesp32/berry_tasmota/src/be_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*******************************************************************/
#include "be_constobj.h"
#include <string.h>
#include "be_ctypes.h"

extern __attribute__((noreturn)) void be_raisef(bvm *vm, const char *except, const char *msg, ...);

Expand Down Expand Up @@ -34,65 +35,6 @@ int32_t bin_search_ctypes(const char * needle, const void * table, size_t elt_si
}
}

enum {
ctypes_i32 = 14,
ctypes_i24 = 13,
ctypes_i16 = 12,
ctypes_i8 = 11,
ctypes_u32 = 4,
ctypes_u24 = 3,
ctypes_u16 = 2,
ctypes_u8 = 1,

// big endian
ctypes_be_i32 = -14,
ctypes_be_i24 = -13,
ctypes_be_i16 = -12,
ctypes_be_i8 = -11,
ctypes_be_u32 = -4,
ctypes_be_u16 = -2,
ctypes_be_u8 = -1,

// floating point
ctypes_float = 5,
ctypes_double = 10,

// pointer
ctypes_ptr32 = 9,
ctypes_ptr64 = -9,

ctypes_bf = 0, //bif-field
};

typedef struct be_ctypes_structure_item_t {
const char * name;
uint16_t offset_bytes;
uint8_t offset_bits : 3;
uint8_t len_bits : 5;
int8_t type : 5;
uint8_t mapping : 3;
} be_ctypes_structure_item_t;

typedef struct be_ctypes_structure_t {
uint16_t size_bytes; /* size in bytes */
uint16_t size_elt; /* number of elements */
const char **instance_mapping; /* array of instance class names for automatic instanciation of class */
const be_ctypes_structure_item_t * items;
} be_ctypes_structure_t;

typedef struct be_ctypes_class_t {
const char * name;
const be_ctypes_structure_t * definitions;
} be_ctypes_class_t;

typedef struct be_ctypes_classes_t {
uint16_t size;
const char **instance_mapping; /* array of instance class names for automatic instanciation of class */
const be_ctypes_class_t * classes;
} be_ctypes_classes_t;

// const be_ctypes_class_t * g_ctypes_classes = NULL;

//
// Constructor for ctypes structure
//
Expand Down Expand Up @@ -232,6 +174,15 @@ int be_ctypes_member(bvm *vm) {
int32_t val = be_toint(vm, -1);
be_pop(vm, 1);
be_pushcomptr(vm, (void*) val);
} else if (ctypes_addr == member->type) {
be_getmember(vm, 1, "_buffer"); // self.get or self.geti
be_pushvalue(vm, 1); // push self
be_call(vm, 1);
be_pop(vm, 1);
uint8_t *addr = (uint8_t*) be_tocomptr(vm, -1);
be_pop(vm, 1);
addr += member->offset_bytes;
be_pushcomptr(vm, addr);
} else {
// general int support
int size = member->type; // eventually 1/2/4, positive if little endian, negative if big endian
Expand All @@ -253,7 +204,7 @@ int be_ctypes_member(bvm *vm) {
be_pop(vm, 3);
// int result at top of stack
}
// the int result is at top of the stack
// the int or comptr result is at top of the stack
// check if we need an instance mapping
if (member->mapping > 0 && definitions->instance_mapping) {
const char * mapping_name = definitions->instance_mapping[member->mapping - 1];
Expand Down Expand Up @@ -362,6 +313,9 @@ int be_ctypes_setmember(bvm *vm) {
be_call(vm, 4);
be_pop(vm, 5);
be_return_nil(vm);
} else if (ctypes_addr == member->type) {
be_raisef(vm, "attribute_error", "class '%s' cannot assign to attribute '%s'",
be_classname(vm, 1), be_tostring(vm, 2));
} else {
// general int support
int size = member->type; // eventually 1/2/4, positive if little endian, negative if big endian
Expand Down

0 comments on commit 836ce9e

Please sign in to comment.