george comments
[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))
24 <<@ Hint "Choose a task"
25 >>? \(i, n)->enterChoice [] ["arduino", "node"]
26 <<@ Hint "Which device?"
27 >>? \device->appendTask Embedded (mkTask n i device) stl
28 >-| chooseTask dev1 dev2 stl
29 where
30 mkTask n i device stl
31 # dev = if (device == "node") dev2 dev1
32 = ((snd (tasks !! i) $ dev)
33 >>* [OnAction ActionClose $ always $ return ()]) <<@ Title n/*\label{lst:example:ct2}*/
34
35 tasks :: [(String, MTDevice -> Task ())]/*\label{lst:example:tasks1}*/
36 tasks =
37 [ ("temp", \dev->
38 liftmTask ( DHT (DHT_DHT (DigitalPin D6) DHT22) \dht->
39 {main=temperature dht}) dev
40 >&> \t->viewSharedInformation
41 [ViewAs \i->toString (fromMaybe 0.0 i) +++ "C"] t
42 <<@ Hint "Current Temperature"
43 @! ())
44 , ("lightswitch", \dev->
45 withShared False \sh->
46 liftmTask (lightswitch sh) dev
47 -|| updateSharedInformation [] sh <<@ Hint "Switch")
48 , ("factorial", \dev->
49 updateInformation [] 5 <<@ Hint "Factorial of what?"
50 >>? \i->liftmTask (factorial i) dev
51 >>- \r->viewInformation [] r <<@ Hint "Result"
52 @! ())
53 ]
54
55 factorial i = {main=rtrn (lit i)}
56 blink d = {main=writeD (lit d) (lit True)}
57 lightswitch sh = {main=rtrn (lit ())}