8e9064639386f338b4c4b4c41bec8701a0724d93
[phd-thesis.git] / top / lst / example.icl
1 module example
2
3 import StdEnv
4 import Data.Func
5 import iTasks
6 import mTask.Interpret
7 import mTask.Interpret.Device.TCP
8 import mTask.Interpret.Device.Serial
9
10 //Start w = doTasks autoHome w
11
12 arduino = {TTYSettings | zero & devicePath="/dev/ttyACM0"}/*\label{lst:example:spec1}*/
13 nodeMCU = {TCPSettings | host="192.168.0.1", port=8123, pingTimeout= ?None}/*\label{lst:example:spec2}*/
14
15 autoHome :: Task ()/*\label{lst:example:task1}*/
16 autoHome = withDevice arduino \dev1-> /*\label{lst:example:conn1}*/
17 withDevice nodeMCU \dev2-> /*\label{lst:example:conn2}*/
18 parallel [(Embedded, chooseTask dev1 dev2)] [] <<@ ArrangeWithTabs True/*\label{lst:example:par1}*/
19 >>* [OnAction (Action "Shutdown") (always (shutDown 0))]/*\label{lst:example:task2}\label{lst:example:par2}*/
20
21 chooseTask :: MTDevice MTDevice (SharedTaskList ()) -> Task ()/*\label{lst:example:ct1}*/
22 chooseTask dev1 dev2 stl = tune (Title "Run a task") $
23 enterChoice [] (zip2 [0..] (map fst tasks)) <<@ Hint "Choose a task"
24 >>? \(i, n)->enterChoice [] ["arduino", "node"]/*\label{lst:example:selectdev}*/
25 <<@ Hint "Which device?"
26 >>? \device->appendTask Embedded (mkTask n i device) stl
27 >-| chooseTask dev1 dev2 stl
28 where
29 mkTask n i device stl
30 # dev = if (device == "node") dev2 dev1
31 = ((snd (tasks !! i) $ dev)
32 >>* [OnAction ActionClose $ always $ return ()]) <<@ Title n/*\label{lst:example:ct2}*/
33
34 tasks :: [(String, MTDevice -> Task ())]/*\label{lst:example:tasks1}*/
35 tasks =
36 [ ("temp", \dev->
37 liftmTask (DHT (DHT_DHT (DigitalPin D6) DHT22) \dht->
38 {main=temperature dht}
39 ) dev
40 >&> \t->viewSharedInformation
41 [ViewAs \i->toString (fromMaybe 0.0 i) +++ "C"] t
42 <<@ Hint "Current Temperature" @! ())
43 , ("lightswitch", \dev-> /*\label{lst:example:ls1}*/
44 withShared False \sh->
45 liftmTask (lightswitch sh) dev
46 -|| updateSharedInformation [] sh <<@ Hint "Switch")/*\label{lst:example:ls2}*/
47 , ("factorial", \dev->
48 updateInformation [] 5 <<@ Hint "Factorial of what?"
49 >>? \i->liftmTask (factorial i) dev
50 >>- \r->viewInformation [] r <<@ Hint "Result" @! ())
51 ]
52
53 factorial :: Int -> Main (MTask v Int) | mtask v
54 factorial i =
55 fun \fac=(\i->If (i ==. lit 0) (lit 1) (i *. fac (i -. lit 1)))
56 In {main=rtrn (fac (lit i))}
57 blink :: Int -> Main (MTask v ()) | mtask v
58 blink d =
59 declarePin D13 PMOutput \d13->
60 fun \bl=(\st->
61 writeD d13 st
62 >>|. delay (lit d)
63 >>|. bl (Not st))
64 In {main=bl true}
65 lightswitch :: (Shared sds Bool) -> Main (MTask v ()) | liftsds, mtask v & RWShared sds & TC (sds () Bool Bool)
66 lightswitch sh =
67 declarePin D13 PMOutput \d13->
68 liftsds \ls=sh
69 In fun \f=(\st->
70 getSds ls
71 >>*. [IfValue ((!=.)st) (\v->writeD d13 v)]
72 >>|. f (Not st))
73 In {main=f true}
74
75 Start w = doTasks t w
76 t = withShared True \sh->
77 updateSharedInformation [] sh <<@ Hint "Light switch"