add glue chapter
[msc-thesis1617.git] / mtask.io.tex
1 Values can be assigned to all expressions that have an \CI{Upd} role. Examples
2 of such expressions are \glspl{SDS} and \gls{GPIO} pins. Moreover, class
3 extensions can be created for specific peripherals such as built-in
4 \glspl{LED}. The classes facilitating this are shown in
5 Listing~\ref{lst:sdsio}. In this way the assignment is the same for every
6 assignable entity.
7
8 \begin{lstlisting}[%
9 label={lst:sdsio},caption={Input/Output classes}]
10 :: DigitalPin = D0 | D1 | D2 | D3 | D4 | D5 |D6 | D7 | D8 | D9 | D10 | D11 | D12 | D13
11 :: AnalogPin = A0 | A1 | A2 | A3 | A4 | A5
12 :: UserLED = LED1 | LED2 | LED3
13
14 class dIO v where dIO :: DigitalPin -> v Bool Upd
15 class aIO v where aIO :: AnalogPin -> v Int Upd
16 class analogRead v where
17 analogRead :: AnalogPin -> v Int Expr
18 analogWrite :: AnalogPin (v Int p) -> v Int Expr
19 class digitalRead v where
20 digitalRead :: DigitalPin -> v Bin Expr
21 digitalWrite :: DigitalPin (v Bool p) -> v Int Expr
22
23 :: UserLED = LED1 | LED2 | LED3
24 class userLed v where
25 ledOn :: (v UserLED q) -> (v () Stmt)
26 ledOff :: (v UserLED q) -> (v () Stmt)
27
28 class assign v where
29 (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | ...
30 \end{lstlisting}
31
32 One way of storing data in \gls{mTask}-\glspl{Task} is using \glspl{SDS}.
33 \glspl{SDS} serve as variables in \gls{mTask} and maintain their value across
34 executions. \glspl{SDS} can be used by multiple \glspl{Task} and can be used
35 to share data. The classes associated with \glspl{SDS} are listed in
36 Listing~\ref{lst:sdsclass}. The \CI{Main} type is introduced to box an
37 \gls{mTask} and make it recognizable by the type system by separating programs
38 and decorations such as \glspl{SDS}.
39
40 \begin{lstlisting}[%
41 label={lst:sdsclass},caption={\glspl{SDS} in \gls{mTask}}]
42 :: In a b = In infix 0 a b
43 :: Main a = {main :: a}
44
45 class sds v where
46 sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
47 \end{lstlisting}
48
49 In the \emph{Arduino} ecosystem, shields are available to plug into the
50 microcontroller and add functionality. These shields range from Bluetooth,
51 WiFi, Ethernet, LoRa, LCD screens and much more. Often the functionality
52 available in these shields is housed in a \gls{C++} class. This functionality
53 is ported using little work to \gls{mTask} by just creating a corresponding
54 class with the same functions. As an example, Listing~\ref{lst:lcd} shows parts
55 of the \gls{LCD} class as an \gls{mTask} class functions and as
56 Listing~\ref{lst:lcdc} shown the corresponding \emph{Arduino} class functions.
57
58 \begin{lstlisting}[label={lst:lcd},%
59 caption={Adding the \gls{LCD} to the \gls{mTask} language}]
60 :: LCD = ...
61
62 class lcd v where
63 begin :: (v LCD Expr) (v Int p) (v Int q) -> v () Expr
64 ...
65 scrollLeft :: (v LCD Expr) -> v () Expr
66 scrollRight :: (v LCD Expr) -> v () Expr
67 ...
68 \end{lstlisting}
69
70 \begin{lstlisting}[language=C++,label={lst:lcdc},%
71 caption={Functions from the \gls{Arduino} \gls{LCD} library}]
72 class LiquidCrystal {
73 public:
74 void begin(uint8_t cols, uint8_t rows);
75 ...
76 void scrollDisplayLeft();
77 void scrollDisplayRight();
78 ...
79 }
80 \end{lstlisting}