forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-stock-price.forth
154 lines (130 loc) · 3.51 KB
/
example-stock-price.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
NETCON load
SSD1306SPI load
FONT57 load
WIFI load
\ stock price display with servo control
\ see it in action: https://youtu.be/4ad7dZmnoH8
1024 constant: buffer-len
buffer-len buffer: buffer
variable: price
variable: change
variable: open
4 constant: SERVO \ d2
SERVO GPIO_OUT gpio-mode
\ servo control
: short 19250 750 ; immediate
: medium 18350 1650 ; immediate
: long 17200 2800 ; immediate
: pulse ( off-cycle-us on-cycle-us -- ) immediate
['], SERVO , ['], GPIO_HIGH , ['] gpio-write ,
['], ( on cycle ) , ['] us ,
['], SERVO , ['], GPIO_LOW , ['] gpio-write ,
['], ( off cycle ) , ['] us , ;
: down ( -- ) 30 0 do short pulse loop ;
: midway ( -- ) 30 0 do medium pulse loop ;
: up ( -- ) 30 0 do long pulse loop ;
: parse-code ( buffer -- code | throws:ECONVERT )
9 + 3 >number invert if
ECONVERT throw
then ;
exception: EHTTP
: read-code ( netconn -- http-code | throws:EHTTP )
buffer-len buffer netcon-readln
0 <= if EHTTP throw then
buffer "HTTP/" str-starts? if
buffer parse-code
else
EHTTP throw
then ;
: skip-headers ( netconn -- netconn )
begin
dup buffer-len buffer netcon-readln -1 <>
while
buffer strlen 0= if exit then
repeat
EHTTP throw ;
: read-resp ( netconn -- response-code )
dup read-code
swap skip-headers
buffer-len buffer netcon-readln
print: 'len=' . cr ;
: log ( response-code -- response-code ) dup print: 'HTTP:' . space buffer type cr ;
: consume ( netcon -- )
dup read-resp log
swap netcon-dispose
200 <> if EHTTP throw then ;
: connect ( -- netconn ) 8319 "zeroflag.dynu.net" TCP netcon-connect ;
: stock-fetch ( -- )
connect
dup "GET /stock/CLDR HTTP/1.0\r\n\r\n" netcon-write
consume ;
exception: ESTOCK
variable: idx
: reset ( -- ) 0 idx ! ;
: pos ( -- addr ) buffer idx @ + ;
: peek ( -- chr ) pos c@ ;
: next ( -- chr ) 1 idx +! idx @ buffer-len >= if ESTOCK throw then ;
: take ( chr -- ) begin dup peek <> while next repeat drop ;
: 0! ( -- ) 0 pos c! ;
: parse ( -- )
reset buffer price !
$, take 0!
next pos change !
$, take 0!
next pos open !
10 take 0! ;
: trend ( str -- )
c@ case
$+ of up endof
$- of down endof
drop midway
endcase ;
: open? ( -- bool ) open @ "1" =str ;
: center ( str -- ) DISPLAY_WIDTH swap str-width - 2 / font-size @ / text-left ! ;
: spacer ( -- ) draw-lf draw-cr 2 text-top +! ;
: stock-draw ( -- )
stock-fetch parse
price @ center price @ draw-str
spacer
change @ center change @ draw-str
change @ trend ;
: error-draw ( exception -- )
display-clear
0 text-left ! 0 text-top !
"Err: " draw-str
case
ENETCON of "NET" draw-str endof
EHTTP of "HTTP" draw-str endof
ESTOCK of "API" draw-str endof
"Other" draw-str
ex-type
endcase
display ;
: show ( -- )
display-clear
3 text-top !
0 text-left !
stock-draw
display ;
0 task: stock-task
0 init-variable: last-refresh
: expired? ( -- bool ) ms@ last-refresh @ - 60 1000 * > ;
: stock-start ( task -- )
activate
begin
last-refresh @ 0= expired? or if
ms@ last-refresh !
{ show } catch ?dup if error-draw then
then
pause
again ;
: main ( -- )
stack-show
font-medium
font5x7 font !
display-init
multi
stock-task stock-start ;
\ ' boot is: main
\ turnkey
main