\subsection{Framework}
Systems built with support for \gls{mTask} are often following the same design
pattern. First the devices are created --- with or without the interaction of
the user --- and they are then connected. When all devices are registered, the
\gls{mTask}-\glspl{Task} can be sent and \gls{iTasks}-\glspl{Task} can be
started to monitor the output. When everything is finished, the devices are
removed and the system is shut down.

\begin{lstlisting}[language=Clean,label={lst:framework},
	caption={\gls{mTask} framework for building applications}]
w :: Task ()
w =         makeDevice "dev1" (...) >>= connectDevice
	>>= \dev1->makeDevice "dev2" (...) >>= connectDevice
	>>= \dev2->...
	...
	>>* [OnAction (Action "Shutdown") $ always
		$   deleteDevice dev1 >>| deleteDevice dev2
		>>| ...
		>>| shutDown 0
	]
\end{lstlisting}

\subsection{Thermostat}
The thermostat is a classic example program for showing interactions between
peripherals. The following program shows a system containing two devices. The
first device --- the sensor -- contains a temperature sensor that measures the
room temperature. The second device --- the actor --- contains a heater,
connected to the digital pin \CI{D5}. Moreover, this device contains a led to
indicate whether the heater is on. The following code shows an implementation
for this. The code fully uses the framework. Note that a little bit of type
twiddling is required to fully us the result from the \gls{SDS}. This approach
is still type safe due to the type safety of \CI{Dynamic}s.

\begin{lstlisting}[caption={Thermostat example}]
thermos :: Task ()
thermos =           makeDevice "nodeM" nodeMCU >>= connectDevice
	>>= \nod->      makeDevice "stm32" stm32   >>= connectDevice
	>>= \stm->      sendTaskToDevice "sensing" sensing (nod, OnInterval 1000)
	>>= \(st, [t])->sendTaskToDevice "acting" acting (stm, OnInterval 1000)
			(\(BCValue s)->set (BCValue $ dynInt (dynamic s) > 0) (shareShare nod a))
	>>| treturn ()
where
	dynInt :: Dynamic -> Int
	dynInt (a :: Int) = a

	sensing = sds \x=0 In {main=
			x =. analogRead A0 :. pub x
		}
	acting = sds \cool=False In {main=
			IF cool (ledOn LED1) (ledOff LED1) :.
			digitalWrite D5 cool
		}
	nodeMCU = TCP
\end{lstlisting}

\subsection[Lifting mTasks to iTasks-Tasks]%
	{Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}-\glspl{Task}}
If the user does not want to know where and when a \gls{mTask} is actually
executed and is just interested in the results it can lift the \gls{mTask} to
an \gls{iTasks}-\gls{Task}. The function is called with a name, \gls{mTask},
device and interval specification and it will return a \gls{Task} that finishes
if and only if the \gls{mTask} has returned.

\begin{lstlisting}[caption={Lifting \gls{mTask}-\glspl{Task} to \gls{iTasks}}]
liftmTask :: String (Main (ByteCode () Stmt)) (MTaskDevice, MTaskInterval) -> Task [MTaskShare]
liftmTask wta mTask c=:(dev, _)= sendTaskToDevice wta mTask c
	>>= \(t, shs)->wait "Waiting for mTask to return" (taskRemoved t) (deviceShare dev)
	>>| viewInformation "Done!" [] ()
	>>| treturn shs
where
	taskRemoved t d = isNothing $ find (\t1->t1.ident==t.ident) d.deviceTasks
\end{lstlisting}

The factorial example can then be lifted to a real \gls{iTasks}-\gls{mTask}
with the following code:
\begin{lstlisting}[caption={Lifting the factorial \gls{Task} to \gls{iTasks}}]
factorial :: MTaskDevice -> Task BCValue
factorial dev = enterInformation "Factorial of ?" []
	>>= \fac->liftmTask "fact" (fact fac) (dev, OnInterval 100)
	@         fromJust o find (\x->x.humanName == "result")
	@         \s->s.MTaskShare.value
where
	fact i = sds \y=i
		In namedsds \x=(1 Named "result")
		In {main = IF (y <=. lit 1)
			( pub x :. retrn                  )
			( x =. x *. y :.  y =. y -. lit 1 )}
\end{lstlisting}

\subsection{Heartbeat \& Oxygen Saturation Sensor}
As an example, the addition of a new sensor will be demonstrated. The heartbeat
and oxygen saturation sensor is a \textsc{PCB} the size of a fingernail with a
red \gls{LED} and a light sensor on it. Moreover, it contains an \textsc{I2C}
chip to communicate. The company producing the chip provides the programmer
with example code for \gls{Arduino} and \textsc{mbed}. So suppose %TODO
Adding peripheral is supposedly simple.
