forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-philips-hue-lightswitch.forth
53 lines (45 loc) · 1.22 KB
/
example-philips-hue-lightswitch.forth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
12 constant: BUTTON_BEDROOM \ D6 pin on nodemcu
14 constant: BUTTON_HALL \ D5 pin on nodemcu
\ setup gpio buttons
BUTTON_HALL GPIO_IN gpio-mode
BUTTON_HALL GPIO_INTTYPE_EDGE_NEG gpio-set-interrupt
BUTTON_BEDROOM GPIO_IN gpio-mode
BUTTON_BEDROOM GPIO_INTTYPE_EDGE_NEG gpio-set-interrupt
800 constant: DEBOUNCE_TIME \ 0.8 sec
0 init-variable: last-hall-event
0 init-variable: last-bedroom-event
Event buffer: event
: toggle-hall ( -- )
ms@ last-hall-event @ - DEBOUNCE_TIME > if
HALL toggle
ms@ last-hall-event !
then ;
: toggle-bedroom ( -- )
ms@ last-bedroom-event @ - DEBOUNCE_TIME > if
BEDROOM toggle
ms@ last-bedroom-event !
then ;
: switch-loop ( task -- )
activate
begin
event next-event
event .type @ EVT_GPIO = if
event .payload @
case
BUTTON_HALL of
toggle-hall
endof
BUTTON_BEDROOM of
toggle-bedroom
endof
drop
endcase
else
print: 'unknown event: ' event .type ? cr
then
again
deactivate ;
0 task: hue-task
: hue-start ( -- )
multi
hue-task switch-loop ;