forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-philips-hue-pir.forth
67 lines (55 loc) · 1.63 KB
/
example-philips-hue-pir.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
\ Detects motion using a PIR sensor and turns Philips Hue lights on/off
\ I tested this with these mini IR PIR sensors
\ http://www.banggood.com/3Pcs-Mini-IR-Infrared-Pyroelectric-PIR-Body-Motion-Human-Sensor-Detector-Module-p-1020422.html
4 constant: PIR_PIN \ D2 leg
0 constant: MODE_MOTION
1 constant: MODE_NOMOTION
variable: mode
0 task: detector-task
Event buffer: event
defer: motion-detected
: detect-motion ( -- )
PIR_PIN GPIO_IN gpio-mode
PIR_PIN GPIO_INTTYPE_EDGE_POS gpio-set-interrupt
MODE_MOTION mode ! ;
: detect-nomotion ( -- )
PIR_PIN GPIO_IN gpio-mode
PIR_PIN GPIO_INTTYPE_EDGE_NEG gpio-set-interrupt
MODE_NOMOTION mode ! ;
: pir-event? ( event -- bool )
{ .type @ EVT_GPIO = }
{ .payload @ PIR_PIN = } bi and ;
: recent-event? ( event -- bool )
ms@ swap .ms @ - 800 < ;
: motion ( -- )
print: 'motion detected at ' event .ms ? cr
detect-nomotion
['] motion-detected catch ?dup if
ex-type cr
then ;
: nomotion ( -- )
print: 'motion stopped at ' event .ms ? cr
detect-motion ;
: event-loop ( task -- )
activate
begin
event next-event
event pir-event? event recent-event? and if
mode @
case
MODE_MOTION of motion endof
MODE_NOMOTION of nomotion endof
endcase
then
again
deactivate ;
: lights-on ( -- )
BEDROOM on? invert if BEDROOM on then ;
: lights-off ( -- )
BEDROOM on? if BEDROOM off then ;
: hue-motion-start ( -- )
multi
detect-motion
['] motion-detected is: lights-on
detector-task event-loop ;
hue-motion-start