Skip to content

Commit

Permalink
Berry energy module support for 8 phases and move to pseudo-arrays (#…
Browse files Browse the repository at this point in the history
…21887)

* Berry `energy` module support for 8 phases and move to pseudo-arrays

* add size()
  • Loading branch information
s-hadinger authored Aug 1, 2024
1 parent 40ee4cc commit 010ca34
Show file tree
Hide file tree
Showing 5 changed files with 744 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
- Berry add `solidify.nocompact()` and reduce size of Matter UI

### Breaking Changed
- Berry `energy` module support for 8 phases and move to pseudo-arrays

### Changed
- Berry consolidated constants for solidified classes reduces Flash size (#2185)
Expand Down
10 changes: 10 additions & 0 deletions lib/libesp32/berry_tasmota/src/be_energylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#ifdef USE_ENERGY_SENSOR

// unless we get it from an include
#define ENERGY_MAX_PHASES 8 // Support max eight phases/channels

extern struct ENERGY Energy;
extern int module_energy_update_total(bvm *vm);

Expand All @@ -21,10 +24,17 @@ module energy (scope: global) {
init, closure(module_energy_init_closure)
_ptr, comptr(&Energy)
_deref, closure(module_energy__deref_closure)
_phases, int(ENERGY_MAX_PHASES)
_phases_float, class(be_class_energy_phases_float)
_phases_int32, class(be_class_energy_phases_int32)
_phases_uint8, class(be_class_energy_phases_uint8)
_phases_uint16, class(be_class_energy_phases_uint16)
read, closure(module_energy_read_closure)
member, closure(module_energy_member_closure)
setmember, closure(module_energy_setmember_closure)
tomap, closure(module_energy_tomap_closure)
update_total, func(module_energy_update_total)
}
Expand Down
120 changes: 119 additions & 1 deletion lib/libesp32/berry_tasmota/src/embedded/energy.be
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,117 @@
_energy = nil # avoid compilation error
energy_struct = nil

#@ solidify:bytes_array
# implement an array of simple types
class bytes_array : bytes
var item_type # item_type number for each element
var sz # size (number of items in the array)
var item_size # size in bytes of each element (inferred from item_type)

def init(ptr, item_type, sz)
self.item_type = item_type
self.sz = sz
var item_size # size in bytes of each element
if (item_type >= -4) && (item_type <= 4) && (item_type != 0)
item_size = (item_type > 0) ? item_type : - item_type
elif (item_type == 5)
item_size = 4
else
raise "value error", "unsupported item_type number"
end
self.item_size = item_size
super(self).init(ptr, self.item_size * self.sz)
end

def item(idx)
if (idx < 0) idx += self.sz end
if (idx < 0) || (idx >= self.sz)
raise "index_error", "list index out of range"
end
if (self.item_type == 5)
return self.getfloat(idx * self.item_size)
else
return self.get(idx * self.item_size, self.item_type)
end
end

def setitem(idx, v)
if (idx < 0) idx += self.sz end
if (idx < 0) || (idx >= self.sz)
raise "index_error", "list assignment index out of range"
end
if (self.item_type == 5)
return self.setfloat(idx * self.item_size, v)
else
return self.set(idx * self.item_size, self.item_type, v)
end
end

def size()
return self.sz
end

def tostring()
var ret = '['
var idx = 0
while (idx < self.sz)
if idx > 0
ret += ','
end
#
if (self.item_type == 5)
ret += str(self.getfloat(idx * self.item_size))
else
ret += str(self.get(idx * self.item_size, self.item_type))
end

idx += 1
end
ret += ']'
return ret
end
end

#@ solidify:energy_phases_float
class energy_phases_float : bytes_array
def init(ptr)
import energy
super(self).init(ptr, 5 #-type float-#, energy._phases)
end
end


#@ solidify:energy_phases_int32
class energy_phases_int32 : bytes_array
def init(ptr)
import energy
super(self).init(ptr, 4 #-type int32-#, energy._phases)
end
end

#@ solidify:energy_phases_uint8
class energy_phases_uint8 : bytes_array
def init(ptr)
import energy
super(self).init(ptr, 1 #-type int32-#, energy._phases)
end
end

#@ solidify:energy_phases_uint16
class energy_phases_uint16 : bytes_array
def init(ptr)
import energy
super(self).init(ptr, 2 #-type uint16-#, energy._phases)
end
end

energy = module("energy")
energy._ptr = nil
energy._ptr = nil # will be replaced by C code
energy._phases = 8 # will be replaced by C code
energy._phases_float = energy_phases_float
energy._phases_int32 = energy_phases_int32
energy._phases_uint8 = energy_phases_uint8
energy._phases_uint16 = energy_phases_uint16

def init(m)
global._energy = nil
Expand Down Expand Up @@ -46,8 +155,17 @@ def setmember(k, v)
end
energy.setmember = setmember

def tomap()
energy._deref()
if global._energy
return _energy.tomap()
end
end
energy.tomap = tomap

#@ solidify:energy.init
#@ solidify:energy._deref
#@ solidify:energy.read
#@ solidify:energy.member
#@ solidify:energy.setmember
#@ solidify:energy.tomap
Loading

0 comments on commit 010ca34

Please sign in to comment.