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