add publishing of sds's
[mTask.git] / mTaskCode.icl
1 implementation module mTaskCode
2
3 import iTasks
4 import gdynamic, gCons, GenEq, StdMisc, StdArray
5 import mTask
6
7 instance toCode MTask where toCode (MTask x) = "Task " + toCode x
8
9 argType :: (((Code a p)->Code b q)->In ((Code a p)->Code b q) (Code c s)) -> a | type a
10 argType f = undef
11
12 instance argTypes (Code a p) | showType a where argTypes f = showType
13 instance argTypes (Code a p, Code b q) | showType a & showType b where argTypes f = (showType, showType)
14 instance argTypes (Code a p, Code b q, Code c r) | showType a & showType b & showType c where argTypes f = (showType, showType, showType)
15
16 resType :: ((t->Code b Expr)->In (t->Code b q) (Main (Code c s))) -> Code b q | showType b
17 resType f = showType
18
19 var2Type :: (Code t p) -> Code t p | showType t
20 var2Type x = showType
21
22 resType2 :: ((t->Code b Expr)->In (t->Code b q) (Main (Code c s))) -> SV b | showType2 b
23 resType2 f = showType2
24
25 instance toCode (SV t) where toCode (SV s) = s
26
27 instance arith Code where
28 lit a = embed (c a)
29 (+.) x y = codeOp2 x " + " y
30 (-.) x y = codeOp2 x " - " y
31 (*.) x y = codeOp2 x " * " y
32 (/.) x y = codeOp2 x " / " y
33 instance boolExpr Code where
34 (&.) x y = codeOp2 x " && " y
35 (|.) x y = codeOp2 x " || " y
36 Not x = embed (brac (c "! " +.+ brac x))
37 (==.) x y = codeOp2 x " == " y
38 (!=.) x y = codeOp2 x " != " y
39 (<.) x y = codeOp2 x " < " y
40 (<=.) x y = codeOp2 x " <= " y
41 (>.) x y = codeOp2 x " > " y
42 (>=.) x y = codeOp2 x " >= " y
43 instance If Code Stmt Stmt Stmt where If c t e = IfStmt c t e
44 instance If Code e Stmt Stmt where If c t e = IfStmt c t e
45 instance If Code Stmt e Stmt where If c t e = IfStmt c t e
46 instance If Code x y Expr where If c t e = IfExpr c t e
47 IfExpr b t e = embed (brac (b +.+ indent +.+ nl +.+ c " ? " +.+ t +.+ nl +.+ c " : " +.+ e +.+ unindent))
48 IfStmt b t e =
49 getMode \mode.
50 let
51 v = varName t
52 newMode =
53 case mode of
54 Return s = Return s
55 Assign v = Assign v
56 _ = if (v == "") NoReturn (Assign v)
57 in
58 setMode SubExp +.+
59 c "if " +.+ brac b +.+ c " {" +.+
60 indent +.+ nl +.+ setMode newMode +.+ t +.+ unindent +.+ nl +.+ c "} else {" +.+
61 indent +.+ nl +.+ setMode newMode +.+ e +.+ unindent +.+ nl +.+ c "}"
62 instance IF Code where
63 IF b t e = IfStmt b t e
64 (?) b t =
65 getMode \mode.
66 c "if " +.+ setMode SubExp +.+ brac b +.+ c " {" +.+
67 indent +.+ nl +.+ setMode mode +.+ t +.+ c ";" +.+ unindent +.+ nl +.+ c "}"
68
69 instance sds Code where
70 sds f = // defCode f
71 {main = fresh \n.
72 let name = "sds"+toCode n
73 (v In body) = f (C (var name))
74 in setCode Var +.+ c (type2string v + " " + name + " = " + toCode v +
75 ";\n") +.+ setCode Setup +.+ unMain body}
76 con f = defCode f
77 pub _ = undef
78
79 defCode :: ((Code t p) -> In t (Main (Code u q))) -> Main (Code u r) | type t
80 defCode f =
81 {main = fresh \n.
82 let name = "sds"+toCode n
83 (v In body) = f (C (var name))
84 in setCode Var +.+ c (type2string v + " " + name + " = " + toCode v +
85 ";\n") +.+ setCode Setup +.+ unMain body}
86
87 var :: String (ReadWrite (Code v q)) CODE -> CODE
88 var sds Rd s = unC (embed (c sds)) Rd s
89 var sds (Wrt v) s = unC (embed (c ("(" + sds + " = ") +.+ v +.+ c ")")) Rd s
90
91 instance assign Code where
92 (=.) (C v) e = embed (setMode SubExp +.+ C \rw c.v (Wrt (toExpr e)) c)
93 instance seq Code where
94 (>>=.) x f =
95 getMode \mode. fresh \n. let v = "b" + toCode n in
96 addBinds v +.+ var2Type x +.+ c (v + ";") +.+ nl +.+
97 setMode (Assign v) +.+ x +.+ nl +.+ setMode mode +.+ f (embed (c v))
98 (:.) x y = getMode \mode. setMode NoReturn +.+ embed x +.+ nl +.+ setMode mode +.+ y
99 instance step` Code where
100 (>>*.) x f =
101 getMode \mode. fresh \n.
102 let v = "s" + toCode n in
103 c "while(true) {" +.+ indent +.+ nl +.+
104 var2Type x +.+ c (v + ";") +.+ nl +.+
105 setMode (Assign v) +.+ x +.+ nl +.+
106 setMode mode +.+ codeSteps (f (c v)) +.+
107 unindent +.+ nl +.+ c "}"
108 codeSteps :: [Step Code t] -> Code u p
109 codeSteps [] = C \rw c.c
110 codeSteps [Cond b e:x] =
111 getMode \mode. setMode SubExp +.+
112 c "if (" +.+ b +.+ c ") {" +.+ indent +.+ nl +.+
113 setMode mode +.+ e +.+
114 optBreak mode +.+ unindent +.+ nl +.+ c "}" +.+ nl +.+ setMode mode +.+ codeSteps x
115 codeSteps [Ever e:x] =
116 getMode \mode. e +.+ optBreak mode
117
118 optBreak :: Mode -> Code u p
119 optBreak mode =
120 case mode of
121 Return s = C \rw c.c
122 _ = nl +.+ c "break;"
123
124 instance setDelay Code where
125 setDelay d t = embed (c "setDelay" +.+ brac (t +.+ c ", " +.+ d))
126 instance mtask Code a | taskImp2 a & types a where
127 task f =
128 {main = freshMTask \n.
129 let (app, a) = taskImp2 n types
130 (b In main) = f (\d a.app (long d) a)
131 in codeMTaskBody (loopCode n (b a)) (unMain main)}
132 instance mtasks Code a b | taskImp2 a & types a & taskImp2 b & types b where
133 tasks f =
134 {main =
135 freshMTask \t1.
136 freshMTask \t2.
137 let (app1, a1) = taskImp2 t1 types
138 (app2, a2) = taskImp2 t2 types
139 ((b1, b2) In main) = f ((\d a.app1 (long d) a),(\d a.app2 (long d) a))
140 in codeMTaskBody (loopCode t2 (b2 a2)) (codeMTaskBody (loopCode t1 (b1 a1)) (unMain main))}
141
142 loopCode :: Int (Code a b) -> Code c d
143 loopCode n b =
144 nl +.+ c "case " +.+ c n +.+ c ": {" +.+ indent +.+ nl +.+
145 setMode NoReturn +.+ b +.+ nl +.+ c "break;" +.+
146 unindent +.+ nl +.+ c "} "
147
148 instance taskImp2 () where
149 taskImp2 n () = (app, ())
150 where app d a = setBinds [] +.+ embed (c "newTask(" +.+ c n +.+ c ", " +.+ d +.+ c ")")
151 instance taskImp2 (Code t p) where
152 taskImp2 n type1 = (app, ta)
153 where
154 n0 = "t0p->a[0]"
155 ta = c n0 +.+ type1
156 app d a =
157 setBinds [n0] +.+ embed (c "newTask(" +.+
158 c n +.+ c ", " +.+
159 d +.+ c ", " +.+
160 a +.+ c ")")
161 instance taskImp2 (Code a p, Code b q) where
162 taskImp2 n (type1, type2) = (app, (ta1, ta2)) where
163 n0 = "t0p->a[0]"
164 n1 = "t0p->a[1]"
165 ta1 = c n0 +.+ type1
166 ta2 = c n1 +.+ type2
167 app d (a1, a2) =
168 setBinds [n0,n1] +.+ embed (c "newTask(" +.+ c n +.+ c ", " +.+ long d +.+
169 c ", " +.+ a1 +.+ c ", " +.+ a2 +.+ c ")")
170 instance taskImp2 (Code a p, Code b q, Code c r) where
171 taskImp2 n (type1, type2, type3) = (app, (ta1, ta2, ta3))
172 where
173 n0 = "t0p->a[0]"
174 n1 = "t0p->a[1]"
175 n2 = "t0p->a[2]"
176 ta1 = c n0 +.+ type1
177 ta2 = c n1 +.+ type2
178 ta3 = c n2 +.+ type3
179 app d (a1, a2, a3) =
180 setBinds [n0,n1,n2] +.+ embed (c "newTask(" +.+
181 c n +.+ c ", " +.+
182 d +.+ c ", " +.+
183 a1 +.+ c ", " +.+
184 a2 +.+ c ", " +.+
185 a3 +.+ c ", 0)")
186 instance taskImp2 (Code a p, Code b q, Code c r, Code d s) where
187 taskImp2 n (type1, type2, type3, type4) = (app, (ta1, ta2, ta3, ta4))
188 where
189 n0 = "t0p->a[0]"
190 n1 = "t0p->a[1]"
191 n2 = "t0p->a[2]"
192 n3 = "t0p->a[3]"
193 ta1 = c n0 +.+ type1
194 ta2 = c n1 +.+ type2
195 ta3 = c n2 +.+ type3
196 ta4 = c n3 +.+ type4
197 app d (a1, a2, a3, a4) =
198 setBinds [n0,n1,n2,n3] +.+ embed (c "newTask(" +.+
199 c n +.+ c ", " +.+
200 d +.+ c ", " +.+
201 a1 +.+ c ", " +.+
202 a2 +.+ c ", " +.+
203 a3 +.+ c ", " +.+
204 a4 +.+ c ")")
205
206 class taskImp a :: Int a -> (Int a->Code MTask Expr, a)
207 instance taskImp () where
208 taskImp n () = (app, ())
209 where app i a = embed (c "newTask(" +.+ c n +.+ c ", " +.+ c i +.+ c ")")
210 instance taskImp (Code t p) where
211 taskImp n type1 = (app, ta)
212 where
213 ta = c "t0p->a[0]" +.+ type1
214 app i a =
215 embed (c "newTask(" +.+
216 c n +.+ c ", " +.+
217 c i +.+ c ", " +.+
218 a +.+ c ")")
219 instance taskImp (Code a p, Code b q) where
220 taskImp n (type1, type2) = (app, (ta1, ta2)) where
221 ta1 = c "t0p->a[0]" +.+ type1
222 ta2 = c "t0p->a[1]" +.+ type2
223 app i (a1, a2) =
224 embed (c "newTask(" +.+ c n +.+ c ", " +.+ c i +.+
225 c ", " +.+ a1 +.+ c ", " +.+ a2 +.+ c ")")
226 instance taskImp (Code a p, Code b q, Code c r) where
227 taskImp n (type1, type2, type3) = (app, (ta1, ta2, ta3))
228 where
229 ta1 = c "t0p->a[0]" +.+ type1
230 ta2 = c "t0p->a[1]" +.+ type2
231 ta3 = c "t0p->a[2]" +.+ type3
232 app i (a1, a2, a3) =
233 embed (c "newTask(" +.+
234 c n +.+ c ", " +.+
235 c i +.+ c ", " +.+
236 a1 +.+ c ", " +.+
237 a2 +.+ c ", " +.+
238 a3 +.+ c ")")
239 instance taskImp (Code a p, Code b q, Code c r, Code d s) where
240 taskImp n (type1, type2, type3, type4) = (app, (ta1, ta2, ta3, ta4))
241 where
242 ta1 = c "t0p->a[0]" +.+ type1
243 ta2 = c "t0p->a[1]" +.+ type2
244 ta3 = c "t0p->a[2]" +.+ type3
245 ta4 = c "t0p->a[3]" +.+ type4
246 app i (a1, a2, a3, a4) =
247 embed (c "newTask(" +.+
248 c n +.+ c ", " +.+
249 c i +.+ c ", " +.+
250 a1 +.+ c ", " +.+
251 a2 +.+ c ", " +.+
252 a3 +.+ c ", " +.+
253 a4 +.+ c ")")
254
255 tasksMain :: Int Int ((a->Code MTask Expr,b->Code MTask Expr) -> In (a->Code c d,b->Code e f) (Main (Code g h))) -> Main (Code i j) | taskImp a & types a & taskImp b & types b
256 tasksMain i j f =
257 { main =
258 freshMTask \n. freshMTask \m.
259 let
260 (app1, a1) = taskImp n types
261 (app2, a2) = taskImp m types
262 ((b1, b2) In {main = e}) = f (app1 i, app2 j)
263 in
264 codeMTaskBody (loopCode n (b1 a1) +.+ setMode NoReturn +.+ loopCode m (b2 a2)) e
265 }
266 class types a :: a
267 instance types () where types = ()
268 instance types (Code a p) | typeSelector a & isExpr p
269 where types = typeSelector
270 instance types (Code a p, Code b q) | typeSelector a & isExpr p & typeSelector b & isExpr q
271 where types = (typeSelector, typeSelector)
272 instance types (Code a p, Code b q, Code c r) | typeSelector a & isExpr p & typeSelector b & isExpr q & typeSelector c & isExpr r
273 where types = (typeSelector, typeSelector, typeSelector)
274 instance types (Code a p, Code b q, Code c r, Code d s) | typeSelector a & isExpr p & typeSelector b & isExpr q & typeSelector c & isExpr r & typeSelector d & isExpr s
275 where types = (typeSelector, typeSelector, typeSelector, typeSelector)
276
277 codeMTaskBody :: (Code v w) (Code c d) -> Code e f
278 codeMTaskBody loop e =
279 getMode \mode.
280 setMode NoReturn +.+
281 setCode Loop +.+ loop +.+
282 setMode mode +.+ setCode Setup +.+ embed e
283 instance fun Code () where
284 fun f =
285 {main = getMode \mode. fresh \n.
286 let fname = c ("f" + toCode n)
287 (g In {main=e}) = f (\x.embed (fname +.+ c " ()"))
288 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " () " +.+
289 funBody (setMode (Return (toCode (resType2 f))) +.+ setBinds [] +.+ g ()) +.+ setCode Setup +.+ setMode mode +.+ e
290 }
291 instance fun Code (Code t p) | type, showType t & isExpr p where
292 fun f =
293 {main =
294 getMode \mode. fresh \n.
295 let fname = c ("f" + toCode n)
296 aname = "a" + toCode n
297 (g In {main=e}) = f (\x.embed (fname +.+ c " " +.+ brac x))
298 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " " +.+
299 brac (argTypes f +.+ c (" " + aname)) +.+
300 funBody (setMode (Return (toCode (resType2 f))) +.+ setBinds [aname] +.+ g (embed (c aname))) +.+ setCode Setup +.+ setMode mode +.+ e
301 }
302 instance fun Code (Code a p, Code b q) | showType a & showType b where
303 fun f =
304 {main =
305 getMode \mode. fresh \n.
306 let fname = c ("f" + toCode n + " ")
307 aname = "a" + toCode n //+ " "
308 bname = "b" + toCode n //+ " "
309 (atype, btype) = argTypes f
310 (g In main)
311 = f (\(x,y).embed (fname +.+ codeOp2 x ", " y))
312 in setCode Fun +.+ nl +.+ resType f +.+ fname +.+
313 codeOp2 (atype +.+ c aname) ", " (btype +.+ c bname) +.+
314 funBody (setMode (Return (toCode (resType2 f))) +.+
315 setBinds [aname,bname] +.+ g (embed (c aname), embed (c bname))) +.+
316 setCode Setup +.+ setMode mode +.+ unMain main
317 }
318 instance fun Code (Code a p, Code b q, Code c r) | showType a & showType b & showType c where
319 fun f =
320 {main =
321 getMode \mode. fresh \n.
322 let fname = c ("f" + toCode n)
323 aname = "a" + toCode n
324 bname = "b" + toCode n
325 cname = "c" + toCode n
326 (atype,btype,ctype) = argTypes f
327 (g In {main=e}) = f (\(x,y,z).embed (fname +.+ c " " +.+ brac (x +.+ c ", " +.+ y +.+ c ", " +.+ z)))
328 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " " +.+
329 brac (atype +.+ c (" " + aname + ", ") +.+ btype +.+ c (" " + bname + ", ") +.+ ctype +.+ c (" " + cname)) +.+
330 funBody (setMode (Return (toCode (resType2 f))) +.+
331 setBinds [aname,bname,cname] +.+ g (embed (c aname), embed (c bname), embed (c cname))) +.+ setCode Setup +.+ setMode mode +.+ e
332 }
333 instance output Code where
334 output x = embed (c "Serial.println(" +.+ x +.+ c ")")
335 instance pinMode Code where
336 pinmode p m = embed (c ("pinMode(" + toCode p + ", " + consName{|*|} m + ")"))
337 instance digitalIO Code where
338 digitalRead p = embed (c ("digitalRead(" + toCode p + ")"))
339 digitalWrite p b = embed (c ("digitalWrite(" + toCode p + ", ") +.+ b +.+ c ")")
340 instance dIO Code where
341 dIO p = C (ioc p) where
342 ioc :: p (ReadWrite (Code t q)) CODE -> CODE | pin, readPinD p
343 ioc p Rd s = f Rd s where (C f) = embed (c ("digitalRead(" + toCode p + ")"))
344 ioc p (Wrt v) s = f Rd s where (C f) = embed (c ("pWrite(" + toCode p + ", ") +.+ v +.+ c ")")
345 instance aIO Code where
346 aIO p = C (ioc p) where
347 ioc :: p (ReadWrite (Code t q)) CODE -> CODE | pin p
348 ioc p Rd s = unC (embed (c ("analogRead(" + toCode p + ")"))) Rd s
349 ioc p (Wrt v) s = unC (embed (c ("pWrite(" + toCode p + ", ") +.+ v +.+ c ")")) Rd s
350 instance analogIO Code where
351 analogRead p = embed (c ("analogRead(" + toCode p + ")"))
352 analogWrite p b = embed (c ("analogWrite(" + toCode p + ", ") +.+ b +.+ c ")")
353 instance noOp Code where noOp = C \rw c.c
354
355 :: Code a p = C ((ReadWrite (Code a Expr)) CODE -> CODE)
356 unC :: (Code a p) -> ((ReadWrite (Code a Expr)) CODE -> CODE)
357 unC (C f) = f
358
359 :: Def = Var | Fun | Setup | Loop
360 :: Mode = /*MainMode |*/ NoReturn | Return String | SubExp | Assign String
361
362 setMode :: Mode -> Code a p
363 setMode m = C \rw c.{CODE|c & mode` = m}
364
365 getMode :: (Mode -> Code a p) -> Code a p
366 getMode f = C \rw c.unC (f c.mode`) rw c
367
368 embed :: (Code a p) -> Code a p
369 embed e =
370 getMode \m. case m of
371 NoReturn = setMode SubExp +.+ e +.+ c ";"
372 Return "void" = setMode SubExp +.+ e +.+ c ";"
373 Return t = c "return " +.+ setMode SubExp +.+ e +.+ c ";"
374 Assign s = c (s + " = ") +.+ setMode SubExp +.+ e +.+ c ";"
375 SubExp = e
376 _ = abort "\n\nembed: unknown mode.\n"
377
378 (+.+) infixl 5 :: (Code a p) (Code b q) -> Code c r
379 (+.+) (C f) (C g) = C \rw c.g Rd (f Rd c)
380
381 fresh :: (Int -> (Code a p)) -> (Code a p)
382 fresh f = C \rw c.unC (f c.fresh) rw {CODE | c & fresh = c.fresh + 1}
383
384 freshMTask :: (Int -> (Code a p)) -> (Code a p)
385 freshMTask f = C \rw c.unC (f c.freshMTask) rw {CODE | c & freshMTask = c.freshMTask + 1}
386
387 setCode :: Def -> (Code a p)
388 setCode d = C \rw c.{c & def = d}
389
390 getCode :: (Def -> Code a p) -> (Code a p)
391 getCode f = C \rw c.unC (f c.def) rw c
392
393 brac :: (Code a p) -> Code b q
394 brac e = c "(" +.+ e +.+ c ")"
395
396 funBody :: (Code a p) -> Code b q
397 funBody e = c "{" +.+ indent +.+ nl +.+ e +.+ unindent +.+ nl +.+ c "}" +.+ nl
398
399 codeOp2 :: (Code a p) String (Code b q) -> Code c r
400 codeOp2 x n y = embed (brac (x +.+ c n +.+ y))
401
402 include :: String -> Code a b
403 include lib = C \rw c.{c & includes = [lib:c.includes]}
404
405 argList :: [a] -> String | toCode a
406 argList [a] = toCode a
407 argList [a:x] = toCode a + "," + argList x
408 argList [] = ""
409
410 c :: a -> Code b p | toCode a
411 c a = C \rw c.case c.def of
412 Fun = {c & funs = [toCode a: c.funs]}
413 Var = {c & vars = [toCode a: c.vars]}
414 Setup = {c & setup = [toCode a: c.setup]}
415 Loop = {c & loop = [toCode a: c.loop]}
416
417 indent :: Code a p
418 indent =
419 C \rw c.case c.def of
420 Fun = {c & ifuns = inc c.ifuns}
421 Var = {c & ivars = inc c.ivars}
422 Setup = {c & isetup = inc c.isetup}
423 Loop = {c & iloop = inc c.iloop}
424
425 unindent :: Code a p
426 unindent =
427 C \rw c.case c.def of
428 Fun = {c & ifuns = dec c.ifuns}
429 Var = {c & ivars = dec c.ivars}
430 Setup = {c & isetup = dec c.isetup}
431 Loop = {c & iloop = dec c.iloop}
432 where
433 dec n | n > 1
434 = n - 1
435 = 0
436
437 nl :: Code a p
438 nl =
439 C \rw c.case c.def of
440 Fun = {c & funs = [str c.ifuns: c.funs]}
441 Var = {c & vars = [str c.ivars: c.vars]}
442 Setup = {c & setup = [str c.isetup: c.setup]}
443 Loop = {c & loop = [str c.iloop: c.loop]}
444 where
445 str n = toString ['\n':repeatn (tabSize * n) ' ']
446
447 setBinds :: [String] -> Code a p
448 setBinds list = C \rw c.{c & binds = list}
449
450 addBinds :: String -> Code a p
451 addBinds name = C \rw c.{c & binds = [name:c.binds]}
452
453 getBinds :: ([String] -> Code a p) -> (Code a p)
454 getBinds f = C \rw c.unC (f c.binds) rw c
455
456 // ----- driver ----- //
457
458 compile :: (Main (Code a p)) -> [String]
459 compile {main=(C f)} =
460 ["/*\n"
461 ," Generated code for Arduino\n"
462 ," Pieter Koopman, pieter@cs.ru.nl\n"
463 ,"*/\n"
464 ,"\n"
465 ,"#define MAX_ARGS 4\n"
466 ,"#define MAX_TASKS 20\n"
467 ,"#define MAX_TASK_NO MAX_TASKS - 1\n"
468 ,"#define NEXT_TASK(n) ((n) == MAX_TASK_NO ? 0 : (n) + 1)\n"
469 ,"\n"
470 ,"typedef union Arg {\n"
471 ," int i;\n"
472 ," bool b;\n"
473 ," char c;\n"
474 // ," float f;\n" // requires 4 bytes
475 ," word w;\n"
476 ,"} ARG;\n"
477 ,"\n"
478 ,"typedef struct Task {\n"
479 ," byte id;\n"
480 ," long wait;\n"
481 ," ARG a[MAX_ARGS];\n"
482 ,"} TASK;\n"
483 ,"\n"
484 ] ++
485 foldr (\lib c.["#include <":lib:".h>\n":c]) [] (mkset c.includes) ++
486 ["\n// --- variables ---\n"
487 ,"TASK tasks[MAX_TASKS];\n"
488 ,"byte t0 = 0, tc = 0, tn = 0;\n"
489 ,"long delta;\n"
490 ,"\n"
491 ,"int vInt;\n"
492 ,"bool vBool;\n"
493 ,"char vChar;\n"
494 ,"float vFloat;\n"
495 ,"unsigned long time = 0;\n"
496 :reverse c.vars
497 ] ++
498 ["\n// --- functions ---\n"
499 ,"byte newTask(byte id, long wait, word a0 = 0, word a1 = 0, word a2 = 0, word a3 = 0) {\n"
500 ," TASK *tnp = &tasks[tn];\n"
501 ," tnp->id = id;\n"
502 ," tnp->wait = wait;\n"
503 ," tnp->a[0].w = a0;\n"
504 ," tnp->a[1].w = a1;\n"
505 ," tnp->a[2].w = a2;\n"
506 ," tnp->a[3].w = a3;\n"
507 ," byte r = tn;\n"
508 ," tn = NEXT_TASK(tn);\n"
509 ," return r;\n"
510 ,"}\n"
511 ,"\n"
512 ,"byte setDelay(byte t, long d) {\n"
513 ," tasks[t].wait = d;\n"
514 ," return t;\n"
515 ,"}\n"
516 ,"boolean pressed(int b) {\n"
517 ," pinMode(A0, INPUT);\n"
518 ," int a0 = analogRead(A0);\n"
519 ," switch (b) {\n"
520 ," case 0: return a0 < ",toString RightBound,"; // right\n"
521 ," case 1: return ",toString RightBound," < a0 && a0 < ",toString UpBound,"; // up\n"
522 ," case 2: return ",toString UpBound," < a0 && a0 < ",toString DownBound,";// down\n"
523 ," case 3: return ",toString DownBound," < a0 && a0 < ",toString LeftBound,";//left\n"
524 ," case 4: return ",toString LeftBound," < a0 && a0 < ",toString SelectBound,";//select\n"
525 ," default: return ",toString SelectBound," < a0; //no button\n"
526 ," }\n"
527 ,"}\n"
528 ,"boolean pWrite (int pin, boolean b) {\n"
529 ," pinMode(pin, OUTPUT);\n"
530 ," digitalWrite(pin, b);\n"
531 ," return b;\n"
532 ,"}\n"
533 ,"int pWrite (int pin, int i) {\n"
534 ," pinMode(pin, OUTPUT);\n"
535 ," analogWrite(pin, i);\n"
536 ," return i;\n"
537 ,"}\n"
538 :reverse c.funs
539 ] ++
540 ["\n// --- setup --- \n"
541 ,"void setup () {\n"
542 ," Serial.begin(9600);\n"
543 ," "
544 :reverse c.setup
545 ] ++
546 ["\n}\n"
547 ,"\n// --- loop --- \n"
548 ,"void loop () {\n"
549 ," if (t0 != tn) {\n"
550 ," if (t0 == tc) {\n"
551 ," unsigned long time2 = millis();\n"
552 ," delta = time2 - time;\n"
553 ," time = time2;\n"
554 ," tc = tn;\n"
555 ," };\n"
556 ," TASK* t0p = &tasks[t0];\n"
557 ," t0p->wait -= delta;\n"
558 ," if (t0p->wait > 0L) {\n"
559 ," newTask(t0p->id, t0p->wait, t0p->a[0].w, t0p->a[1].w, t0p->a[2].w, t0p->a[3].w);\n"
560 ," } else {\n"
561 ," switch (t0p->id) {"
562 :reverse c.loop
563 ] ++
564 ["\n"
565 ," default:\n"
566 ," Serial.println(\"stopped\");\n"
567 ," t0 = tn; // no known task: force termination of tasks\n"
568 ," return;\n"
569 ," };\n"
570 ," }\n"
571 ," t0 = NEXT_TASK(t0);\n"
572 ," }\n"
573 ,"}\n"
574 ]
575 where c = f Rd newCode
576
577 mkset :: [a] -> [a] | Eq a
578 mkset [a:x] = [a:mkset (filter ((<>) a) x)]
579 mkset [] = []
580
581 newCode :: CODE
582 newCode =
583 { fresh = 0
584 , freshMTask = 0
585 , funs = []
586 , ifuns = 0
587 , vars = []
588 , ivars = 0
589 , setup = []
590 , isetup = 1
591 , loop = []
592 , iloop = 4
593 , includes = []
594 , def = Setup
595 , mode` = NoReturn
596 , binds = []
597 }
598
599
600 //Tools
601 instance toCode () where toCode _ = ""
602
603 class toCode a :: a -> String
604
605 instance toCode Long where toCode (L i) = toCode i + "L"
606
607 instance toCode Bool where toCode b = if b "true" "false"
608 instance toCode Int where toCode a = toString a
609 instance toCode Real where toCode a = toString a
610 instance toCode Char where
611 toCode '\0' = "'\\0'"
612 toCode '\n' = "'\\n'"
613 toCode '\\' = "\\"
614 toCode a = "'" + toString a + "'"
615 instance toCode String where toCode s = s
616 instance toCode DigitalPin where toCode x = s%(1, size s - 1) where s = consName{|*|} x
617 instance toCode AnalogPin where toCode x = consName{|*|} x
618
619 instance toCode Pin where
620 toCode (Digital p) = toCode p
621 toCode (Analog p) = toCode p
622