forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-pir.forth
38 lines (31 loc) · 1.14 KB
/
example-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
NETCON load
EVENT load
GPIO load
\ Detects motion using a PIR sensor and notifies a server via TCP
\ 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 ( D2 leg ) constant: PIN
Event buffer: event
defer: listener
variable: last-time
: pir? ( evt -- bool ) { .type @ EVT_GPIO = } { .payload @ PIN = } bi and ;
: recent? ( evt -- bool ) ms@ swap .ms @ - 800 < ;
: time-since-last ( -- ms ) ms@ last-time @ - ;
: handle? ( -- bool ) event pir? event recent? time-since-last 5000 > and and ;
: start-detector ( -- )
PIN GPIO_IN gpio-mode
PIN GPIO_INTTYPE_EDGE_POS gpio-set-interrupt
begin
event next-event handle? if
ms@ last-time !
['] listener catch ?dup if ex-type cr then
then
again ;
"K" constant: ID
variable: server
: connect ( -- ) 8030 "192.168.0.10" TCP netcon-connect server ! ;
: dispose ( -- ) server @ netcon-dispose ;
: send ( -- ) server @ ID 1 netcon-write-buf ;
: notify-server ( -- ) connect ['] send catch dispose throw ;
' listener is: notify-server
start-detector