Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry 'tasmota.int(v, min, max)' function #22721

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading