-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.2.1_Tablelamp_gpios.fs
65 lines (55 loc) · 1.34 KB
/
02.2.1_Tablelamp_gpios.fs
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
#! /usr/bin/env gforth
\ Forget and reload definitions if this file is re-included.
[ifdef] -Tablelamp
-Tablelamp
[endif]
marker -Tablelamp
include ./gpios.fs
11 constant ledPin
12 constant buttonPin
50 constant captureTime
0 constant LOW
1 constant HIGH
variable ledState
variable buttonState
variable lastbuttonState
variable lastChangeTime
variable reading
LOW ledState !
HIGH buttonState !
HIGH lastbuttonState !
ledPin output-pin
buttonPin input-pin
HIGH buttonPin pin-resmode
: ledOn ( -- ) ledPin pinset ;
: ledOff ( -- ) ledPin pinclr ;
: buttonRead ( -- n ) buttonPin pin@ ;
: ledSwitch ( -- ) ledState @ 0= IF 1 ledState ! ELSE 0 ledState ! THEN ;
: millis ( -- n )cputime drop drop drop ;
: Tablelamp ( -- ) ." Program is starting... " CR
begin
buttonRead reading !
reading @ lastbuttonState @ <> IF
millis lastChangeTime !
THEN
millis lastChangeTime @ - captureTime > IF
reading @ buttonState @ <> IF
reading @ buttonState !
buttonState @ LOW = IF
." Button is pressed!" CR
ledSwitch
ledState @ if
." turn on LED ... " CR
ELSE
." turn off LED ... " CR
THEN
THEN
ELSE
\ ." Button is released!" CR
THEN
THEN
ledState @ 0= if ledPin pinclr ELSE ledPin pinset THEN
reading @ lastbuttonState !
key? until
ledOff
;