\documentclass[../thesis.tex]{subfiles}

\input{subfilepreamble}


\begin{document}
\input{subfileprefix}
\ifSubfilesClassLoaded{\appendix\setcounter{chapter}{1}}{}
\chapter{Auxiliary mTask type classes}%
\label{chp:mtask_aux}
\lstset{basicstyle=\tt\footnotesize}
\section{Peripherals}\label{sec:aux_peripherals}
This section shows the peripherals not mentioned in \cref{chp:top4iot}.
All constructors use \gls{HOAS} to create a type safe sensor object from a connection specification that can be used to interact with the sensor.
The measurement tasks all yield unstable values contaning the measured value.
The auxiliary functions such as calibration yield stable values indicating the result.
Tasks suffixed with the backtick (\cleaninline{'}) indicate variants for which the timing interval can be specified (see \cref{chp:green_computing_mtask}).

\subsection{Air quality sensor}
The \gls{MTASK} language supports one type (\emph{CCS811} connected via \gls{I2C}) of air quality sensors that measures \gls{TVOC} (\unit{ppm}) and \gls{ECO2} ($\%$).
Besides the constructor and tasks for the measurements there is also a calibration task that can be used to calibrate the sensor from temperature and humidity readings to increase the accuracy.
The complete interface is shown in \cref{lst:mtask_aqs}.

\begin{lstClean}[label={lst:mtask_aqs},caption={Air quality sensor interface in \gls{MTASK}.}]
:: AirQualitySensor // abstract

class AirQualitySensor v where
	airqualitysensor :: I2CAddr ((v AirQualitySensor) -> Main (v a)) -> Main (v a)
	tvoc` :: (TimingInterval v) (v AirQualitySensor) -> MTask v Int
	tvoc :: (v AirQualitySensor) -> MTask v Int
	co2` :: (TimingInterval v) (v AirQualitySensor) -> MTask v Int
	co2 :: (v AirQualitySensor) -> MTask v Int
	setEnvironmentalData :: (v AirQualitySensor) (v Real) (v Real) -> MTask v ()
	setEnvFromDHT :: (v AirQualitySensor) (v DHT) -> MTask v ()
\end{lstClean}

\subsection{Gesture sensor}
The \gls{MTASK} language supports one type (\emph{PAJ7620} connected via \gls{I2C}) of gesture sensors.
The \emph{PAJ7620} contains an optical CMOS array that measures the reflection of the on-board \gls{IR} \gls{LED} to detect up to several different gestures.
The complete interface containing the constructor and the measurement task is shown in \cref{lst:mtask_gesture}.

\begin{lstClean}[label={lst:mtask_gesture},caption={Gesture sensor interface in \gls{MTASK}.}]
:: GestureSensor // abstract
:: Gesture = GNone | GRight | GLeft | GUp | GDown | GForward | GBackward
           | GClockwise | GCountClockwise

class GestureSensor v where
	gestureSensor :: I2CAddr ((v GestureSensor) -> Main (v a)) -> Main (v a)
	gesture` :: (TimingInterval v) (v GestureSensor) -> MTask v Gesture
	gesture :: (v GestureSensor) -> MTask v Gesture
\end{lstClean}

\subsection{Light intensity sensor}
The \gls{MTASK} language supports one type (\emph{BH1750} connected via \gls{I2C}) of light intensity sensors that measure the light intensity in \unit{lx}.
The complete interface containing the constructor and the measurement task is shown in \cref{lst:mtask_light}.

\begin{lstClean}[label={lst:mtask_light},caption={Light intensity sensor interface in \gls{MTASK}.}]
:: LightSensor // abstract

class LightSensor v where
	lightsensor :: I2CAddr ((v LightSensor) -> Main (v b)) -> Main (v b)
	light` :: (TimingInterval v) (v LightSensor) -> MTask v Real
	light :: (v LightSensor) -> MTask v Real
\end{lstClean}

\subsection{Motion detection sensor}
The \gls{MTASK} language supports motion sensing using a \gls{PIR} sensor through a type class that only contains macros.
\gls{PIR} sensors detect motion by the \gls{IR} reflection through a number of fresnel lenses and communicates through a digital \gls{GPIO} pin.
Therefore, a \gls{PIR} is nothing more than a \cleaninline{DPIN} according to \gls{MTASK} but for uniformity, a type class is available (see \cref{lst:mtask_pir}).

\begin{lstClean}[label={lst:mtask_pir},caption={\Gls{PIR} sensor interface in \gls{MTASK}.}]
:: PIR :== DPin

class PIR v | step, expr, pinMode v & dio DPin v where
	PIR :: DPin ((v PIR) -> Main (v b)) -> Main (v b) | expr, step, pinMode v

	motion` :: (TimingInterval v) (v PIR) -> MTask v Bool
	motion :: (v PIR) -> MTask v Bool | dio DPin v
\end{lstClean}

\subsection{Sound detection sensor}
The \gls{MTASK} language supports motion sensing using one type of sensor (\emph{SEN-12642}) that outputs either a gate value through a digital \gls{GPIO} pin, the envelope (amplitude) through an analog \gls{GPIO} pin and an audio output.
Only the sound level---i.e.\ the envelope---and the sound presence are available in \gls{MTASK} through a type class containing only macros.
Therefore, a sound detector is nothing more than a tuple of a \cleaninline{DPin} for the gate value and an \cleaninline{APin} for the envelope (see \cref{lst:mtask_sound}).

\begin{lstClean}[label={lst:mtask_sound},caption={Sound detection sensor interface in \gls{MTASK}.}]
:: SoundDetector :== (DPin, APin)

class SoundDetector v | tupl, expr, pinMode v & dio DPin v
where
	soundDetector :: DPin APin ((v SoundDetector) -> Main (v b)) -> Main (v b)

	soundPresence` :: (TimingInterval v) (v SoundDetector) -> MTask v Bool
	soundPresence :: (v SoundDetector) -> MTask v Bool | tupl v & dio DPin v

	soundLevel` :: (TimingInterval v) (v SoundDetector) -> MTask v Bool
	soundLevel :: (v SoundDetector) -> MTask v Bool | tupl, aio v
\end{lstClean}

\subsection{\IIC{} buttons}
The \gls{MTASK} language supports one type of \gls{I2C} buttons (the \gls{I2C} buttons from the \gls{WEMOS} d1 mini \gls{OLED} shield).
The buttons from this shield provide more information than just the status (see \cleaninline{ButtonStatus}).
The complete interface containing the constructor and the measurement tasks is shown in \cref{lst:mtask_i2cbutton}.

\begin{lstClean}[label={lst:mtask_i2cbutton},caption={\Gls{I2C} button interface in \gls{MTASK}.}]
:: I2CButton  // abstract
:: ButtonStatus = ButtonNone | ButtonPress | ButtonLong | ButtonDouble | ButtonHold

class i2cbutton v where
	i2cbutton :: I2CAddr ((v I2CButton) -> Main (v b)) -> Main (v b) | type b

	AButton` :: (TimingInterval v) (v I2CButton) -> MTask v ButtonStatus
	AButton :: (v I2CButton) -> MTask v ButtonStatus

	BButton` :: (TimingInterval v) (v I2CButton) -> MTask v ButtonStatus
	BButton :: (v I2CButton) -> MTask v ButtonStatus
\end{lstClean}

\subsection{LED matrix}
The \gls{MTASK} language supports one type of \gls{LED} matrix (the $8\times8$ \gls{LED} matrix shield for the \gls{WEMOS} d1 mini).
Instead of containing a \gls{TOP}-like interface, the \gls{ARDUINO} interface is directly translated to \gls{MTASK}.
As a result, every task immediately returns a stable value indicating the result.
The complete interface containing the constructor and the interaction tasks is shown in \cref{lst:mtask_ledmatrix}.

\begin{lstClean}[label={lst:mtask_ledmatrix},caption={\Gls{LED} matrix interface in \gls{MTASK}.}]
:: LEDMatrix // abstract
:: LEDMatrixInfo = { dataPin :: Pin, clockPin :: Pin }

class LEDMatrix v where
	ledmatrix :: LEDMatrixInfo ((v LEDMatrix) -> Main (v b)) -> Main (v b) | type b
	LMDot :: (v LEDMatrix) (v Int) (v Int) (v Bool) -> MTask v ()
	LMIntensity :: (v LEDMatrix) (v Int) -> MTask v ()
	LMClear :: (v LEDMatrix) -> MTask v ()
	LMDisplay :: (v LEDMatrix) -> MTask v ()
\end{lstClean}

\subsection{Connection types}\label{lst:connection_types}
\begin{lstClean}[caption={}]
:: TCPSettings =
	{ host        :: String
	, port        :: Int
	, pingTimeout :: ?Int
	}
:: MQTTSettings = 
	{ host     :: String
	, port     :: Int
	, mcuId    :: String
	, serverId :: String
	, auth     :: MQTTAuth
	}
:: TTYSettings = {
		devicePath :: String,
		baudrate   :: BaudRate,
		bytesize   :: ByteSize,
		parity     :: Parity,
		stop2bits  :: Bool,
		xonxoff    :: Bool,
		sleepTime  :: Int
	}
\end{lstClean}


\lstset{basicstyle=\tt}
\input{subfilepostamble}
\end{document}
