GEX thesis source code, full text, references

ch.pc_software.tex 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. \chapter{Client Software} \label{sec:clientsw}
  2. With the communication protocol clearly defined in \cref{sec:tinyframe,sec:units_overview}, and \cref{sec:wireless} for the wireless gateway, the implementation of client libraries is relatively straightforward. Two versions of the library have been developed as a proof-of-concept in languages C and Python.
  3. \section{General Library Structure}
  4. The structure of a GEX client library is in all cases similar:
  5. \begin{itemize}
  6. \item \textbf{USB or serial port access}
  7. This is the only platform-dependent part of the library. Unix-based systems provide a standardized POSIX API to configure the serial port. Raw access to \gls{USB} endpoints is possible using the libUSB C library. Access to the serial port or \gls{USB} from C on MS Windows has not been investigated, but should be possible using proprietary APIs.
  8. Accessing the serial port or \gls{USB} endpoints from Python is more straightforward thanks to the cross platform libraries \textit{PySerial} and \textit{PyUSB}.
  9. \item \textbf{TinyFrame implementation}
  10. The \textit{TinyFrame} protocol library can be used directly in desktop C applications, and it has been re-implemented in Python and other languages.
  11. \item \textbf{Higher-level GEX logic}
  12. The host side of the communication protocol described in \cref{sec:tinyframe} should be implemented as a part of the library. This includes the reading and writing of configuration files, unit list read-out, command payload building, and asynchronous event parsing.
  13. Additional utilities may be defined on top of this basic protocol support for the command API of different GEX units, as described in \cref{sec:units_overview}. Those unit-specific ``drivers'' are available in the provided Python library.
  14. \end{itemize}
  15. \section{Python Library}
  16. The Python GEX library implements both serial port access and raw access to \gls{USB} endpoints. Its development has been prioritized over the C library because of its potential to integrate with MATLAB, and because it promises to be the most convenient method to interact with GEX thanks to the ease-of-use that comes with the Python syntax. This library provides a high level \gls{API} above the individual unit types, removing the burden of building and parsing of the binary command payloads from the user.
  17. The library is composed of a \textit{transport} class, the core class \mono{gex.Client}, and unit classes (e.g., \mono{gex.I2C} or \mono{gex.SPI}).
  18. \noindent
  19. Three transport implementations have been developed:
  20. \begin{itemize}
  21. \item \mono{gex.TrxSerialSync} -- virtual serial port access with polling for a response
  22. \item \mono{gex.TrxSerialThread} -- virtual serial port access with a polling thread and semaphore-based notifications
  23. \item \mono{gex.TrxRawUSB} -- similar to \mono{gex.TrxSerialThread}, but using raw USB endpoint access
  24. \end{itemize}
  25. To use the wireless gateway, wrap either low-level transport in \mono{gex.DongleAdapter}. The adapter configures the gateway and converts the regular protocol to the right format.
  26. \begin{pythoncode}
  27. # Gateway connection example
  28. transport = gex.DongleAdapter(gex.TrxRawUSB(remote=True), slave=42)
  29. client = gex.Client(transport)
  30. # ...
  31. client.close()
  32. \end{pythoncode}
  33. \subsection{Example Python Script} \label{sec:ex_python_script}
  34. An example Python program displaying a test pattern on a \gls{LED} matrix using the \gls{I2C}-connected driver chip IS31FL3730 is presented in \cref{lst:py_api} as an illustration of the library usage. A photo of the produced \gls{LED} pattern can be seen in \cref{fig:pydemo}.
  35. \begin{listing}
  36. \begin{pythoncode}
  37. #!/bin/env python3
  38. # The I2C unit, called 'i2c', is configured to use PB6 and PB7
  39. import gex
  40. with gex.Client(gex.TrxRawUSB()) as client:
  41. bus = gex.I2C(client, 'i2c')
  42. addr = 0x61
  43. bus.write_reg(addr, 0x00, 0b00011000) # dual matrix
  44. bus.write_reg(addr, 0x0D, 0b00001110) # 34 mA
  45. bus.write_reg(addr, 0x19, 64) # set brightness
  46. # matrix 1
  47. bus.write_reg(addr, 0x01, [
  48. 0xAA, 0x55, 0xAA, 0x55,
  49. 0xAA, 0x55, 0xAA, 0x55
  50. ])
  51. # matrix 2
  52. bus.write_reg(addr, 0x0E, [
  53. 0xFF, 0x00, 0xFF, 0x00,
  54. 0xFF, 0x00, 0xFF, 0x00
  55. ])
  56. # update display
  57. bus.write_reg(addr, 0x0C, 0x01)
  58. \end{pythoncode}
  59. \caption{\label{lst:py_api} An example Python program using the GEX client library}
  60. \end{listing}
  61. \begin{figure}
  62. \centering
  63. \includegraphics[width=.7\textwidth] {img/phatmtx.jpg}
  64. \caption[GEX Zero with the Micro Dot pHAT add-on board]{\label{fig:pydemo}GEX Zero with the Micro Dot pHAT add-on board, showing a test pattern defined in a Python script}
  65. \end{figure}
  66. First, a client instance is created, receiving the transport as an argument. We use a \mono{with} block in the example to ensure the transport is safely closed before the program ends, even if that happens due to an exception; this is similar to the \mono{try-finally} pattern, but the de-initialization is done automatically. The client (and subsequently the transport) can be closed manually by calling its \mono{.close()} method. Inside the \mono{with} block, the script proceeds to create unit handles and use them to perform the desired task, in our case a communication with the \gls{LED} matrix driver over the \gls{I2C} bus.
  67. \section{MATLAB integration}
  68. The Python library can be accessed from MATLAB scripts thanks to MATLAB's two-way Python integration~\cite{matlabpy}. Controlling GEX from MATLAB may be useful when additional processing is required, e.g., with data from the \gls{ADC}; however, in many cases, an open-source alternative native to Python exists that could be used for the same purpose, such as the NumPy and SciPy libraries~\cite{numpyscipy}.
  69. The example in \cref{lst:matlab_api} (and \cref{fig:matlabpic}) demonstrates the use of MATLAB to calculate the frequency spectrum of an analog signal captured with GEX. The syntax needed to use the serial port transport (instead of raw access to USB endpoints) is shown in a comment.
  70. \begin{listing}
  71. \begin{matlabcode}
  72. % The ADC unit, called 'adc', is configured to use PA1 as Channel 0
  73. %trx = py.gex.TrxSerialThread(pyargs('port', '/dev/ttyUSB1', ...
  74. % 'baud', 115200));
  75. trx = py.gex.TrxRawUSB();
  76. client = py.gex.Client(trx);
  77. adc = py.gex.ADC(client, 'adc');
  78. L = 1000; Fs = 1000;
  79. adc.set_sample_rate(uint32(Fs)); % casting to unsigned integer
  80. data = adc.capture(uint32(L));
  81. data = double(py.array.array('f',data)); % numpy array to matlab format
  82. Y = fft(data);
  83. P2 = abs(Y/L);
  84. P1 = P2(1:L/2+1);
  85. P1(2:end-1) = 2*P1(2:end-1);
  86. f = Fs*(0:(L/2))/L;
  87. plot(f,P1)
  88. client.close()
  89. \end{matlabcode}
  90. \caption{\label{lst:matlab_api} Calling the Python GEX library from a MATLAB script}
  91. \end{listing}
  92. \begin{figure}
  93. \centering
  94. \includegraphics[width=\textwidth]{img/matlab-fft.png}
  95. \caption{A demonstration of the MATLAB/Python integration}
  96. \label{fig:matlabpic}
  97. \end{figure}
  98. \section{C Library}
  99. The C library is more simplistic than the Python one; it supports only the serial port transport (\gls{UART} or \gls{CDCACM}) and does not implement asynchronous polling or the unit support drivers. The implemented features---the transport, a basic protocol handler, and payload building and parsing utilities---are sufficient for most applications, though less convenient than the Python library.
  100. This low-level library is intended for applications where the performance of the Python implementation is insufficient, or where integration with existing C code is required. The full \gls{API} can be found in the library header files. A C version of the example Python script shown above, controlling an \gls{LED} matrix driver, is presented in \cref{lst:c_api_full}.
  101. The payloads in this example are specified as binary strings, for simplicity. Two better methods of payload construction are available: using C structs, or taking advantage of the Payload Builder utility (bundled with TinyFrame).
  102. \begin{listing}
  103. \begin{ccode}
  104. #include <signal.h>
  105. #include <assert.h>
  106. #include "gex.h"
  107. void main(void)
  108. {
  109. // Initialize GEX and the I2C unit handle
  110. GexClient *gex = GEX_Init("/dev/ttyACM0", 200);
  111. assert(NULL != gex);
  112. GexUnit *bus = GEX_GetUnit(gex, "i2c", "I2C");
  113. assert(NULL != bus);
  114. // Configure the matrix driver
  115. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x00\x18", 4);
  116. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x0d\x0e", 4);
  117. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x19\x40", 4);
  118. // Load data
  119. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x01"
  120. "\xAA\x55\xAA\x55\xAA\x55\xAA\x55", 11);
  121. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x0e"
  122. "\xFF\x00\xFF\x00\xFF\x00\xFF\x00", 11);
  123. // Update display
  124. GEX_Send(bus, 2, (uint8_t*) "\x61\x00\x0c\x01", 4);
  125. GEX_DeInit(gex);
  126. }
  127. \end{ccode}
  128. \caption{\label{lst:c_api_full} An example C program (GNU C99) controlling GEX using the low-level GEX library; this code has the same effect as the Python script shown in \cref{lst:py_api}, with payloads built following the command tables from \cref{sec:units_overview}.}
  129. \end{listing}
  130. \subsection{Structure-based Payload Construction}
  131. \begin{listing}
  132. \begin{ccode}
  133. struct i2c_write {
  134. uint16_t address;
  135. uint8_t reg;
  136. uint8_t value[8]; // largest needed payload size
  137. } __attribute__((packed));
  138. // 1-byte value
  139. GEX_Send(bus, 2, (void *) &(struct i2c_write) {
  140. .address = 0x61,
  141. .reg = 0x00,
  142. .value = {0x18},
  143. }, 3 + 1); // use only 1 byte of the value array
  144. // 8-byte value
  145. GEX_Send(bus, 2, (void *) &(struct i2c_write) {
  146. .address = 0x61,
  147. .reg = 0x01,
  148. .value = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55},
  149. }, 3 + 8);
  150. \end{ccode}
  151. \caption{\label{lst:c_api_struct} The variable-length struct approach to payload building}
  152. \end{listing}
  153. The structure-based method utilizes C structs to access individual fields in the payload. Simple payloads can be represented by a struct without problems, but payloads of a dynamic length pose a challenge; we can either define a new struct for each required length, or, when the variable-length array is located at the end of the payload, a struct with the largest needed payload size is defined and the real length is then specified when sending the message. The latter approach is illustrated in \cref{lst:c_api_struct}.
  154. \subsection{Using the Payload Builder Utility}
  155. \begin{listing}
  156. \begin{ccode}
  157. uint8_t buff[20];
  158. PayloadBuilder pb;
  159. pb = pb_init(&buff, 20, NULL);
  160. pb_u16(&pb, 0x61);
  161. pb_u8(&pb, 0x00);
  162. pb_u8(&pb, 0x18);
  163. GEX_Send(bus, 2, buff, pb_length(&pb));
  164. pb_rewind(&pb); // reset the builder for a new frame
  165. uint8_t screen[8] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
  166. pb_u16(&pb, 0x61);
  167. pb_u8(&pb, 0x01);
  168. pb_buf(&pb, &screen, 8);
  169. GEX_Send(bus, 2, buff, pb_length(&pb));
  170. \end{ccode}
  171. \caption{\label{lst:c_api_pb}Building and sending payloads using the PayloadBuilder utility}
  172. \end{listing}
  173. The Payload Builder utility offers a flexible solution to the construction of arbitrary binary payloads. It is used in the GEX firmware to construct messages and events, along with the binary settings storage content.
  174. An example of Payload Builder's usage is shown in \cref{lst:c_api_pb}. We give it a byte buffer and it then fills it with the payload values, taking care of buffer overflow, and advancing the write pointer by the right number of bytes. The third parameter of \mono{pb\_init()} is optional, a pointer to a function called when the buffer overflows; this callback can flush the buffer and rewind it, or report an error.
  175. Payload Builder is accompanied by Payload Parser, a tool doing the exact opposite. While it is not needed in our example, we will find this utility useful when processing command responses or events payloads. The full \gls{API} of those utilities can be found in their header files.