refactoors
[mTask.git] / mTaskExamples.icl
1 module mTaskExamples
2
3 import iTasks
4 import GenEq, StdMisc, StdArray
5 import mTask
6
7 Start =
8 [["//mTaskTFP16_3 \n"]
9 ,["// --- p0 \n"]
10 ,compile p0
11 ,["// --- p1 \n"]
12 ,compile p1
13 ,["// --- p2 \n"]
14 ,compile p2
15 ,["// --- p3 \n"]
16 ,compile p3
17 ,["// --- p4 \n"]
18 ,compile p4
19 ,["// --- p5 \n"]
20 ,compile p5
21 ,["// --- p6 \n"]
22 ,compile p6
23 ,["// --- p7 \n"]
24 ,compile p7
25 ,["// --- p8 \n"]
26 ,compile p8
27 ,["// --- p9 \n"]
28 ,compile p9
29 ,["// --- p10 \n"]
30 ,compile p10
31 ,["// --- p11 \n"]
32 ,compile p11
33 ,["// --- p12 \n"]
34 ,compile p12
35 ,["// --- fac \n"]
36 ,compile fac
37 ,["// --- blink \n"]
38 ,compile blink
39 ,["// --- heatingDemo \n"]
40 ,compile heatingDemo
41 ,["// --- hpinDemo \n"]
42 ,compile pinDemo
43 ,["// --- blink2 \n"]
44 ,compile blink2
45 ,["// --- blink3 \n"]
46 ,compile blink3
47 ,["// --- blinks \n"]
48 ,compile blinks
49 ,["// --- lcdCount \n"]
50 ,compile lcdCount
51
52 ,["// --- heating \n"]
53 ,compile heating
54 ]
55
56 lcdHello = LCD 16 2 [] \lcd = {main = print lcd (lit "Hello world")}
57
58 lcdCount =
59 LCD 16 2 [] \lcd =
60 task \t = (\c.
61 If (pressed upButton) (
62 setCursor lcd Zero Zero :.
63 print lcd c :.
64 t (sec 1) (c +. One)
65 ) (t (msec 10) c)) In
66 {main = t (sec 0) Zero}
67
68 printD0 = {main = serialPrint (Not d0)}
69
70 print36 = sds \x = 6 In {main = x =. x *. x :. serialPrint x}
71
72 pinDemo =
73 {main = a1 =. a0 =. lit 1 +. a0 :. a0 =. Not a0}
74
75 fac = fun \fac = (\n. If (n <. One) One (n *. fac (n -. One)))
76 In {main = fac (lit 6)}
77 One = lit 1
78 Zero = lit 0
79
80 blink =
81 task \t = (\s. setLED s :. t (If s (sec 1) (sec 3)) (Not s)) In {main = t (sec 0) (lit True)}
82 blink2 =
83 task \t = (\(). d13 =. Not d13 :. t (sec 1) ()) In {main = t (sec 0) ()}
84 blink3 =
85 task \t = (\s. d13 =. s :. t (If s (msec 100) (sec 1)) (Not s)) In {main = t (sec 0) (lit False)}
86 blinks =
87 task \t = (\b. d13 =. b :. t (sec 1) b) In {main = t (msec 0) true :. t (msec 100) false}
88
89 setLED b = d13 =. b
90 sec n = long (lit (n * 1000))
91 msec n = long (lit n)
92
93 qt = task \plus = (\(x,y).x +. y) In {main = plus (sec 0) (lit 3, lit 4)}
94 qs = fun \plus = (\(x,y).x +. y) In {main = plus (lit 3, lit 4)}
95
96 q1 =
97 tasks \(switch, heat) =
98 (\s1 = digitalWrite D2 s1:. heat (sec 60) s1
99 ,\s2 = analogRead A3 >>*. \v.
100 [Cond (v >. upper) (switch (sec 0) off)
101 ,Cond (v <. lower) (switch (sec 0) on)
102 ,Ever (heat (sec 1) s2)
103 ])
104 In {main = heat (sec 0) off}
105 where
106 upper = lit 876
107 lower = lit 123
108
109 serialReadInt = serialParseInt >>=. \i. serialRead >>*. \c. [Cond (c ==. lit '\n') i]
110
111 heating =
112 sds \goal = 500 In
113 fun \switch = (\s. d13 =. s) In
114 task \control = (\isOn.
115 a0 <. goal >>*. \mustOn.
116 [Cond (Not isOn &. mustOn) (switch on :. control minOnTime on)
117 ,Cond (isOn &. Not mustOn) (switch off:. control minOffTime off)
118 ,Ever (control (msec 100) isOn)
119 ]) In
120 task \change = (\().
121 serialAvailable ? (serialReadInt >>=. \g.goal =. g) :.
122 change (sec 1) ()) In
123 {main = switch off :. control (sec 0) off :. change (sec 1) ()}
124 where
125 minOnTime = sec 2
126 minOffTime = sec 1
127
128 heating2 =
129 sds \goal = 500 In
130 // fun \switch = setLED In
131 fun \switch = (\b. setLED b :. serialPrintln b) In
132 task \control = (\isOn.
133 a0 <. goal >>*. \mustOn.
134 [Cond (Not isOn &. mustOn) (switch on :. control minOnTime on)
135 ,Cond (isOn &. Not mustOn) (switch off:. control minOffTime off)
136 ,Ever (control (msec 100) isOn)
137 ]) In
138 task \change = (\().
139 serialAvailable ? (serialReadInt >>=. \i. serialPrintln (goal =. i)) :.
140 change (sec 1) ()) In
141 {main = switch off :. control (sec 0) off :. change (sec 1) ()}
142 where
143 minOnTime = sec 2
144 minOffTime = sec 1
145
146 thermoTask =
147 sds \goal = 500 In
148 fun \switch = (\on. d13 =. bool on :. a0 =. on) In
149 task \control =
150 (\isOn. a0 <. goal >>*. \mustOn.
151 [Cond (mustOn &. Not isOn) (switch mustOn :. control minOnTime mustOn)
152 ,Cond (Not mustOn &. isOn) (switch mustOn :. control minOffTime mustOn)
153 ,Ever (control (msec 100) isOn)
154 ]) In
155 {main = switch off :. control (sec 0) off}
156 where
157 minOnTime = sec 1 // 60
158 minOffTime = sec 2 //10
159 off = lit False
160
161 heatingDemo =
162 sds \heat = False In
163 sds \temp = 500 In
164 LCD 16 2 [] \lcd.
165 task \tempChange = (\().
166 lit 0 <. temp &. Not heat ? temp =. temp -. One:.
167 temp <. lit 1000 &. heat ? temp =. temp +. One :.
168 setCursor lcd (lit 5) Zero :.
169 print lcd (lit "temp ") :.
170 print lcd temp :.
171 print lcd (lit " ") :.
172 tempChange (msec 789) ()) In
173 fun \switch = (\s.
174 heat =. s :.
175 setCursor lcd Zero Zero :.
176 If s
177 (print lcd (lit "On "))
178 (print lcd (lit "Off"))) In
179 fun \measure = (\().
180 analogRead A0 >>=. \a0.
181 setCursor lcd Zero One :.
182 print lcd a0 :.
183 print lcd (lit " ") :.
184 a0) In
185 task \control = (\isOn.
186 measure () >>=. \val.temp <. val
187 >>*. \mustOn.
188 [Cond (Not isOn &. mustOn) (switch on :. control minOnTime on)
189 ,Cond (isOn &. Not mustOn) (switch off:. control minOffTime off)
190 ,Ever (control (msec 100) isOn)
191 ]
192 ) In
193 {main = switch off :. control (msec 10) off :. tempChange (sec 0) ()}
194 where
195 limit = lit 512
196 minOnTime = sec 3
197 minOffTime = sec 2
198 true = lit True
199 on = true
200 false = lit False
201 off = false
202
203 count =
204 LCD 16 2 [] \lcd.
205 task \count = (\n.
206 setCursor lcd Zero Zero :.
207 print lcd n :.
208 count (sec 1) (n +. One)) In
209 {main = count (sec 0) Zero}
210
211 p0 :: (Main (Code Int Expr))
212 p0 = sds \x = 6 In {main = x =. x *. lit 7}
213 p1 = {main = lit 2 +. lit 4 >>=. \x. (x +. lit 1) *. x}
214 p2 =
215 fun \f. (\x. lit 6 *. x)
216 In {main = lit 3 +. lit 4 >>=. \x. f x}
217 p3 =
218 fun \f. (\x. lit 6 *. x)
219 In {main = lit 3 +. lit 4 >>=. f} // higher order, somewhat remarkable that this works
220 p4 =
221 fun \f. (\x. lit 6 *. x)
222 In {main = lit 3 +. lit 4 >>=. \x. f x >>=. serialPrint}
223 p5 = {main = lit 7 >>*. \x. [Cond (x <. lit 36) (x *. x),Ever (lit 42)]}
224 p6 = sds \y = 1 In {main = lit 7 >>*. \x. [Cond (x <. lit 36) (y =. x *. x),Ever (y =. x)]}
225 p7 = sds \y = 1 In {main = y +. lit 1 >>*. \x. [Cond (x <. lit 36) ((y =. x *. x) >>*. \z.[Cond (z ==. x) y, Ever y]),Ever (y =. x)]}
226 p8 = sds \y = 1 In {main = pressed upButton >>*. \x. [Cond x (y =. y +. y),Ever (y =. lit 36)]}
227 //p8 = sds \y = 1 In {main = pressed upButton >>*. \x. [Cond x ((y =. lit 42) >>*. \z.[Cond (z ==. y) y, Ever y]),Ever (y =. lit 36)]}
228 //p9 = {main = If (pressed upButton) (lit 1) (lit 7)} // Overloading error [mTaskTFP16.icl,61,p9]: "isExpr" no instance available of type Stmt
229 p9 = {main = pressed upButton >>=. \b.If b (lit 1) (lit 7)}
230 p10 =
231 sds \y = 1 In
232 {main =
233 (pressed upButton >>*. \x.
234 [Cond x (y =. y +. y :.
235 x)
236 ,Ever (y =. lit 36 :.
237 lit False)
238 ])
239 >>=. \z. z &. z}
240 p11 =
241 sds \y = 1 In
242 {main =
243 y =. lit 2 :.
244 (pressed upButton >>=. \b.
245 If b
246 (y =. lit 3 :.
247 y +. lit 1)
248 (lit 42))
249 >>*. \x.
250 [Cond (x <. lit 36)
251 ((y =. x *. x) >>*. \z.
252 [Cond (z ==. x) (serialPrint y)
253 ,Ever (serialPrint (lit 0))
254 ])
255 ,Ever (y =. x)
256 ]
257 }
258 p12 =
259 task \t = (\(). pressed upButton >>*. \b.[Cond b (serialPrintln (lit 7)),Ever (t (lit 250) ():. lit 0)]) In
260 {main = t (lit 0) ()}