forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-philips-hue-clap.forth
62 lines (51 loc) · 1.48 KB
/
example-philips-hue-clap.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
\ simple clap sound detector demo that toggles philips hue lights
4 constant: SOUND_PIN \ D2 leg
variable: last-sound
variable: started
variable: clap1
variable: clap2
variable: silence
200 constant: SILENCE_LOW
450 constant: SILENCE_HIGH
0 constant: CLAP_LOW
150 constant: CLAP_HIGH
defer: clap-detected
Event buffer: event
0 task: detector-task
: sound-event? ( event -- bool )
{ .type @ EVT_GPIO = }
{ .payload @ SOUND_PIN = } bi and ;
: recent-event? ( event -- bool )
ms@ swap .ms @ - 1000 < ;
: clap? ( -- bool )
print: 'clap1: ' clap1 ? cr
print: 'clap2: ' clap2 ? cr
print: 'silence: ' silence ? cr
SILENCE_LOW silence @ SILENCE_HIGH between?
CLAP_LOW clap2 @ CLAP_HIGH between?
CLAP_LOW clap1 @ CLAP_HIGH between?
and and ;
: event-loop ( task -- )
activate
begin
event next-event
event sound-event? event recent-event? and if
event .ms @ last-sound @ - silence !
silence @ 60 > if
last-sound @ started @ - clap2 !
clap? if clap-detected then
clap2 @ clap1 !
event .ms @ started !
then
event .ms @ last-sound !
then
again
deactivate ;
: lights-on ( -- ) BEDROOM toggle ;
: clap-detector-start ( -- )
multi
SOUND_PIN GPIO_IN gpio-mode
SOUND_PIN GPIO_INTTYPE_EDGE_NEG gpio-set-interrupt
['] clap-detected is: lights-on
detector-task event-loop ;
clap-detector-start