no more before some sleep
[msc-thesis1617.git] / arch.example.tex
1 \subsection{Framework}
2 Systems built with support for \gls{mTask} are often following the same design
3 pattern. First the devices are created --- with or without the interaction of
4 the user --- and they are then connected. When all devices are registered, the
5 \gls{mTask}-\glspl{Task} can be sent and \gls{iTasks}-\glspl{Task} can be
6 started to monitor the output. When everything is finished, the devices are
7 removed and the system is shut down.
8
9 \begin{lstlisting}[language=Clean,label={lst:framework},
10 caption={\gls{mTask} framework for building applications}]
11 w :: Task ()
12 w = makeDevice "dev1" (...) >>= connectDevice
13 >>= \dev1->makeDevice "dev2" (...) >>= connectDevice
14 >>= \dev2->...
15 ...
16 >>* [OnAction (Action "Shutdown") $ always
17 $ deleteDevice dev1 >>| deleteDevice dev2
18 >>| ...
19 >>| shutDown 0
20 ]
21 \end{lstlisting}
22
23 \subsection{Thermostat}
24 The thermostat is a classic example program for showing interactions between
25 peripherals. The following program shows a system containing two devices. The
26 first device --- the sensor --- contains a temperature sensor that measures the
27 room temperature. The second device --- the actor --- contains a heater,
28 connected to the digital pin \CI{D5}. Moreover, this device contains a led to
29 indicate whether the heater is on. The following code shows an implementation
30 for this. The code fully uses the framework. Note that a little bit of type
31 twiddling is required to fully us the result from the \gls{SDS}. This approach
32 is still type safe due to the type safety of \CI{Dynamic}s.
33
34 \begin{lstlisting}[caption={Thermostat example}]
35 thermos :: Task ()
36 thermos = makeDevice "nodeM" nodeMCU >>= connectDevice
37 >>= \nod-> makeDevice "stm32" stm32 >>= connectDevice
38 >>= \stm-> sendTaskToDevice "sensing" sensing (nod, OnInterval 1000)
39 >>= \(st, [t])->sendTaskToDevice "acting" acting (stm, OnInterval 1000)
40 (\(BCValue s)->set (BCValue $ dynInt (dynamic s) > 0) (shareShare nod a))
41 >>| treturn ()
42 where
43 dynInt :: Dynamic -> Int
44 dynInt (a :: Int) = a
45
46 sensing = sds \x=0 In {main=
47 x =. analogRead A0 :. pub x
48 }
49 acting = sds \cool=False In {main=
50 IF cool (ledOn LED1) (ledOff LED1) :.
51 digitalWrite D5 cool
52 }
53 nodeMCU = TCP
54 \end{lstlisting}
55
56 \subsection[Lifting mTasks to iTasks-Tasks]%
57 {Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}-\glspl{Task}}
58 If the user does not want to know where and when a \gls{mTask} is actually
59 executed and is just interested in the results it can lift the \gls{mTask} to
60 an \gls{iTasks}-\gls{Task}. The function is called with a name, \gls{mTask},
61 device and interval specification and it will return a \gls{Task} that finishes
62 if and only if the \gls{mTask} has returned.
63
64 \begin{lstlisting}[caption={Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}}]
65 liftmTask :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task [MTaskShare]
66 liftmTask wta mTask c=:(dev, _)= sendTaskToDevice wta mTask c
67 >>= \(t, shs)->wait "Waiting for mTask to return" (taskRemoved t) (deviceShare dev)
68 >>| viewInformation "Done!" [] ()
69 >>| treturn shs
70 where
71 taskRemoved t d = isNothing $ find (\t1->t1.ident==t.ident) d.deviceTasks
72 \end{lstlisting}
73
74 The factorial function example from Chapter~\ref{chp:mtaskcont} can then be
75 lifted to a real \gls{iTasks}-\gls{mTask} with the following code:
76 \begin{lstlisting}[caption={Lifting the factorial \gls{Task} to \gls{iTasks}}]
77 factorial :: MTaskDevice -> Task BCValue
78 factorial dev = enterInformation "Factorial of ?" []
79 >>= \fac->liftmTask "fact" (fact fac) (dev, OnInterval 100)
80 @ fromJust o find (\x->x.humanName == "result")
81 @ \s->s.MTaskShare.value
82 where
83 fact i = sds \y=i
84 In namedsds \x=(1 Named "result")
85 In {main = IF (y <=. lit 1)
86 ( pub x :. retrn )
87 ( x =. x *. y :. y =. y -. lit 1 )}
88 \end{lstlisting}
89
90 \subsection{Heartbeat \& Oxygen Saturation Sensor}
91 As an example, the addition of a new sensor will be demonstrated. The heartbeat
92 and oxygen saturation sensor add-on is a \textsc{PCB} the size of a fingernail
93 with a red \gls{LED} and a light sensor on it. Moreover, it contains an
94 \textsc{I2C} chip to communicate. The company producing the chip provides the
95 programmer with example code for \gls{Arduino} and \textsc{mbed}. The sensor
96 emits red light and measures the returning light intensity. The microcontroller
97 hosting the device has to keep track of four seconds of samples to determine
98 the heartbeat. In the \gls{mTask}-system, an abstraction is made. The current
99 implementation runs on \textsc{mbed} supported devices.
100 %TODO Adding peripheral is supposedly simple.