Skip to content

Commit

Permalink
Berry 'tasmota.int(v, min, max)' function (#22721)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Dec 27, 2024
1 parent 4d3a780 commit b729886
Show file tree
Hide file tree
Showing 4 changed files with 1,719 additions and 1,614 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- HASPmota support for `tabview` (#22707)
- Berry bit-shift operators to `int64` (#22709)
- Berry add unicode encoding to string parsing (#22713)
- Berry `tasmota.int(v, min, max)` function

### Breaking Changed

Expand Down
2 changes: 2 additions & 0 deletions lib/libesp32/berry_tasmota/src/be_tasmota_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class be_class_tasmota (scope: global, name: Tasmota) {
web_send_decimal, func(l_webSendDecimal)
webcolor, static_func(l_webcolor)
int, static_closure(class_Tasmota_int_closure)
get_power, func(l_getpower)
set_power, func(l_setpower)
get_switch, func(l_getswitch) // deprecated
Expand Down
41 changes: 40 additions & 1 deletion lib/libesp32/berry_tasmota/src/embedded/tasmota_class.be
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#- Do not use it -#

class Trigger end # for compilation
class Rule_Matche end # for compilation

tasmota = nil
#@ solidify:Tasmota
Expand Down Expand Up @@ -717,6 +716,46 @@ class Tasmota
return ret
end

# tasmota.int(v, min, max)
# ensures that v is int, and always between min and max
# if min>max returns min
# if v==nil returns min
static def int(v, min, max)
if (min == nil || max == nil) return int(v) end
min = int(min)
max = int(max)
if (min > max) return min end
if (v == nil) return min end
v = int(v) # v is int (not nil)
if (v < min) return min end
if (v > max) return max end
return v
end

#-
# Unit tests
# behave like normal int
assert(tasmota.int(4) == 4)
assert(tasmota.int(nil) == nil)
assert(tasmota.int(-3) == -3)
assert(tasmota.int(4.5) == 4)
assert(tasmota.int(true) == 1)
assert(tasmota.int(false) == 0)
# normal behavior
assert(tasmota.int(4, 0, 10) == 4)
assert(tasmota.int(0, 0, 10) == 0)
assert(tasmota.int(10, 0, 10) == 10)
assert(tasmota.int(10, 0, 0) == 0)
assert(tasmota.int(10, 10, 10) == 10)
assert(tasmota.int(-4, 0, 10) == 0)
assert(tasmota.int(nil, 0, 10) == 0)
# abnormal
assert(tasmota.int(4, 10, -10) == 10)
-#

# set_light and get_light deprecetaion
def get_light(l)
print('tasmota.get_light() is deprecated, use light.get()')
Expand Down
Loading

0 comments on commit b729886

Please sign in to comment.