emits red light and measures the returning light intensity. The microcontroller
hosting the device has to keep track of four seconds of samples to determine
the heartbeat. In the \gls{mTask}-system, an abstraction is made. The current
-implementation runs on \textsc{mbed} supported devices.
-%TODO Adding peripheral is supposedly simple.
+implementation runs on \textsc{mbed} supported devices.
+
+\subsubsection{\gls{mTask} Classes}
+First, a class has to be devised to store the functionality of the sensor. The
+heartbeat sensor updates four values continuously, namely the heartbeat, the
+validity of the reading, the oxygen saturation and the validity of it. For
+every value a function is added to the new \CI{hb} class. Moreover, the
+introduced datatype housing the values should implement the \CI{mTaskType}
+classes. The definition is as follows:
+
+\begin{lstlisting}[caption={The \texttt{hb} class}]
+:: Heartbeat = HB Int
+:: SP02 = SP02 Int
+
+instance toByteCode Heartbeat, SP02
+instance fromByteCode Heartbeat, SP02
+derive class iTask Heartbeat, SP02
+
+class hb v where
+ getHb :: (v Heartbeat Expr)
+ validHb :: (v Bool Expr)
+ getSp02 :: (v SP02 Expr)
+ validSp02 :: (v Bool Expr)
+\end{lstlisting}
+
+\subsubsection{Bytecode Implementation}
+The class is available now, and the implementation can be created. The
+implementation is trivial since the functionality is limited to retrieving
+single values and no assignment is possible. The following code shows the
+implementation. Dedicated bytecode instructions have been added to support the
+functionality.
+
+\begin{lstlisting}[caption={The \texttt{hb} bytecode instance}]
+:: BC
+ = BCNop
+ | ...
+ | BCGetHB
+ | BCValidHB
+ | BCGetSP02
+ | BCValidSP02
+ | ...
+
+instance hb ByteCode where
+ getHb = tell` [BCGetHB]
+ validHb = tell` [BCValidHB]
+ getSp02 = tell` [BCGetSP02]
+ validSp02 = tell` [BCValidSP02]
+\end{lstlisting}
+
+\subsubsection{Device Interface}
+The bytecode instructions are added but still the functionality needs to be
+added to the device interface to be implemented by clients. The following
+addition to \CI{interface.h} and the interpreter shows the added instructions.
+
+\begin{lstlisting}[caption={Adding the device interface}]
+// interface.h
+...
+#if HAVEHB == 1
+uint16_t get_hb();
+bool valid_hb();
+uint16_t get_spo2();
+bool valid_spo2();
+#endif
+...
+
+// interpret.c
+ while(pc < plen){
+ switch(program[pc++]){
+ ...
+#if HAVEHB == 1
+ case BCGETHB:
+ stack[sp++] = get_hb();
+ break;
+ case BCVALIDHB:
+ stack[sp++] = valid_hb();
+ break;
+ case BCGETSP02:
+ stack[sp++] = get_spo2();
+ break;
+ case BCVALIDSP02:
+ stack[sp++] = valid_spo2();
+ break;
+#endif
+ ...
+\end{lstlisting}
+
+\subsubsection{Client Software}
+The device client software always executes the \CI{real\_setup} in which the
+client software can setup the connection and peripherals. In the case of the
+heartbeat peripheral it starts a thread running the calculations. The thread
+started in the setup will set the global heartbeat and oxygen level variables
+so that the interface functions for it can access it. The code is then as
+follows:
+
+\begin{lstlisting}[language=C,caption={}]
+Serial pc;
+Thread thread;
+
+void heartbeat_thread(void) {
+ // Constant heartbeat calculations
+}
+
+void real_setup(void) {
+ pc.baud(19200);
+ thread.start(heartbeat_thread);
+}
+\end{lstlisting}
+
+\subsubsection{Example Program}
+% todo