epicardium.h 8.92 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
#ifndef _EPICARDIUM_H
#define _EPICARDIUM_H
#include <stdint.h>
4
5
#include <stddef.h>
#include <errno.h>
Rahix's avatar
Rahix committed
6

7
8
9
10
11
/* clang-format off */
#define API_INT_CTRL_C  1
#define API_INT_BHI160  2
#define API_INT_MAX     API_INT_BHI160

Rahix's avatar
Rahix committed
12
#ifndef API
Rahix's avatar
Rahix committed
13
#define API(id, def) def
Rahix's avatar
Rahix committed
14
15
#endif

Rahix's avatar
Rahix committed
16
17
18
#define API_UART_WRITE         0x1
#define API_UART_READ          0x2
#define API_LEDS_SET           0x3
Rahix's avatar
Rahix committed
19
#define API_VIBRA_SET          0x4
20
#define API_VIBRA_VIBRATE      0x5
21
#define API_STREAM_READ        0x6
22
23
24
#define API_INTERRUPT_ENABLE   0x7
#define API_INTERRUPT_DISABLE  0x8

Gerd's avatar
Gerd committed
25
26
27
28
29
30
31
32
#define API_DISP_OPEN          0x10
#define API_DISP_CLOSE         0x11
#define API_DISP_PRINT         0x12
#define API_DISP_CLEAR         0x13
#define API_DISP_UPDATE        0x14
#define API_DISP_LINE          0x15
#define API_DISP_RECT          0x16
#define API_DISP_CIRC          0x17
Rahix's avatar
Rahix committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* clang-format on */

/**
 * UART/Serial Interface
 * =====================
 */

/**
 * Write a string to all connected serial devices.  This includes:
 *
 * - Real UART, whose pins are mapped onto USB-C pins.  Accessible via the HW-debugger.
 * - A CDC-ACM device available via USB.
 * - Maybe, in the future, bluetooth serial?
 *
 * :param str:  String to write.  Does not necessarily have to be NULL-terminated.
 * :param length:  Amount of bytes to print.
 */
Rahix's avatar
Rahix committed
50
API(API_UART_WRITE, void epic_uart_write_str(const char *str, intptr_t length));
Rahix's avatar
Rahix committed
51

Rahix's avatar
Rahix committed
52
53
54
55
56
57
58
/**
 * Blocking read a single character from any connected serial device.
 * ``epic_uart_read_chr`` only returns
 * once one byte has been read.
 *
 * :return:  The byte.
 */
Rahix's avatar
Rahix committed
59
60
API(API_UART_READ, char epic_uart_read_chr(void));

Rahix's avatar
Rahix committed
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * LEDs
 * ====
 */

/**
 * Set one of card10's RGB LEDs to a certain color.
 *
 * :param led:  Which led to set.  0-10 are the leds on the top and 11-14 are the 4 "ambient" leds.
 * :param r:  Red component of the color.
 * :param g:  Green component of the color.
 * :param b:  Blue component of the color.
 */
74
75
API(API_LEDS_SET, void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b));

76
77
78
79
80
81
82
83
84
85
/**
 * Sensor Data Streams
 * ===================
 * A few of card10's sensors can do continuous measurements.  To allow
 * performant access to their data, the following function is made for generic
 * access to streams.
 */

/**
 * Read sensor data into a buffer.  ``epic_stream_read()`` will read as many
Rahix's avatar
Rahix committed
86
87
88
89
90
91
92
93
94
95
96
97
 * sensor samples into the provided buffer as possible and return the number of
 * samples written.  If no samples are available, ``epic_stream_read()`` will
 * return ``0`` immediately.
 *
 * ``epic_stream_read()`` expects the provided buffer to have a size which is a
 * multiple of the sample size for the given stream.  For the sample-format and
 * size, please consult the sensors documentation.
 *
 * Before reading the internal sensor sample queue, ``epic_stream_read()`` will
 * call a sensor specific *poll* function to allow the sensor driver to fetch
 * new samples from its hardware.  This should, however, never take a long
 * amount of time.
98
99
100
101
102
103
104
105
106
107
108
 *
 * :param int sd: Sensor Descriptor.  You get sensor descriptors as return
 *    values when activating the respective sensors.
 * :param void* buf: Buffer where sensor data should be read into.
 * :param size_t count: How many bytes to read at max.  Note that fewer bytes
 *    might be read.  In most cases, this should be ``sizeof(buf)``.
 * :return: Number of data packets read (**not** number of bytes) or a negative
 *    error value.  Possible errors:
 *
 *    - ``-ENODEV``: Sensor is not currently available.
 *    - ``-EBADF``: The given sensor descriptor is unknown.
Rahix's avatar
Rahix committed
109
 *    - ``-EINVAL``:  ``count`` is not a multiple of the sensor's sample size.
110
 *    - ``-EBUSY``: The descriptor table lock could not be acquired.
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 *
 * **Example**:
 *
 * .. code-block:: cpp
 *
 *    #include "epicardium.h"
 *
 *    struct foo_measurement sensor_data[16];
 *    int foo_sd, n;
 *
 *    foo_sd = epic_foo_sensor_enable(9001);
 *
 *    while (1) {
 *            n = epic_stream_read(
 *                    foo_sd,
 *                    &sensor_data,
 *                    sizeof(sensor_data)
 *            );
 *
 *            // Print out the measured sensor samples
 *            for (int i = 0; i < n; i++) {
 *                    printf("Measured: %?\n", sensor_data[i]);
 *            }
 *    }
 */
API(API_STREAM_READ, int epic_stream_read(int sd, void *buf, size_t count));

Rahix's avatar
Rahix committed
138
139
140
141
142
143
144
145
146
147
/**
 * Misc
 * ====
 */

/**
 * Turn vibration motor on or off
 *
 * :param status: 1 to turn on, 0 to turn off.
 */
Gerd's avatar
Gerd committed
148
149
API(API_VIBRA_SET, void epic_vibra_set(int status));

150
151
152
153
154
155
156
/**
 * Turn vibration motor on for a given time
 *
 * :param millis: number of milliseconds to run the vibration motor.
 */
API(API_VIBRA_VIBRATE, void epic_vibra_vibrate(int millis));

157
158
159
160
161
162
163
164
165
166
/**
 * API interrupt type
 */
typedef uint32_t api_int_id_t;

/**
 * Enable/unmask an API interrupt
 *
 * :param int_id: The interrupt to be enabled
 */
167
API(API_INTERRUPT_ENABLE, int epic_interrupt_enable(api_int_id_t int_id));
168
169
170
171
172
173

/**
 * Disable/mask an API interrupt
 *
 * :param int_id: The interrupt to be disabled
 */
174
API(API_INTERRUPT_DISABLE, int epic_interrupt_disable(api_int_id_t int_id));
Gerd's avatar
Gerd committed
175
176
177
178
179
/**
 * Display
 * =======
 */

Rahix's avatar
Rahix committed
180
/** Line-Style */
Rahix's avatar
Rahix committed
181
enum disp_linestyle {
Rahix's avatar
Rahix committed
182
  /** */
Gerd's avatar
Gerd committed
183
  LINESTYLE_FULL = 0,
Rahix's avatar
Rahix committed
184
  /** */
Gerd's avatar
Gerd committed
185
186
187
  LINESTYLE_DOTTED = 1
};

Rahix's avatar
Rahix committed
188
/** Fill-Style */
Rahix's avatar
Rahix committed
189
enum disp_fillstyle {
Rahix's avatar
Rahix committed
190
  /** */
Gerd's avatar
Gerd committed
191
  FILLSTYLE_EMPTY = 0,
Rahix's avatar
Rahix committed
192
  /** */
Gerd's avatar
Gerd committed
193
194
195
196
197
198
199
  FILLSTYLE_FILLED = 1
};

/**
 * Locks the display.
 *
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
200
201
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
202
203
204
205
206
207
208
 */
API(API_DISP_OPEN, int epic_disp_open());

/**
 * Unlocks the display again.
 *
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
209
210
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
 */
API(API_DISP_CLOSE, int epic_disp_close());

/**
 * Causes the changes that have been written to the framebuffer
 * to be shown on the display
 */
API(API_DISP_UPDATE, int epic_disp_update());

/**
 * Prints a string into the display framebuffer
 *
 * :param posx: x position to print to. 0 <= x <= 160
 * :param posy: y position to print to. 0 <= y <= 80
 * :param pString: string to print
 * :param fg: foreground color in rgb565
 * :param bg: background color in rgb565
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
229
230
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
 */
API(API_DISP_PRINT,
    int epic_disp_print(
	    uint16_t posx,
	    uint16_t posy,
	    const char *pString,
	    uint16_t fg,
	    uint16_t bg)
    );

/**
 * Fills the whole screen with one color
 *
 * :param color: fill color in rgb565
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
246
247
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 */
API(API_DISP_CLEAR, int epic_disp_clear(uint16_t color));

/**
 * Draws a line on the display
 *
 * :param xstart: x starting position; 0 <= x <= 160
 * :param ystart: y starting position; 0 <= y <= 80
 * :param xend: x ending position; 0 <= x <= 160
 * :param yend: y ending position; 0 <= y <= 80
 * :param color: line color in rgb565
 * :param linestyle: 0 for solid, 1 for dottet (almost no visual difference)
 * :param pixelsize: thickness of the line; 1 <= pixelsize <= 8
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
262
263
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
264
265
266
267
268
269
270
271
 */
API(API_DISP_LINE,
    int epic_disp_line(
	    uint16_t xstart,
	    uint16_t ystart,
	    uint16_t xend,
	    uint16_t yend,
	    uint16_t color,
Rahix's avatar
Rahix committed
272
	    enum disp_linestyle linestyle,
Gerd's avatar
Gerd committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
	    uint16_t pixelsize)
    );

/**
 * Draws a rectangle on the display
 *
 * :param xstart: x coordinate of top left corner; 0 <= x <= 160
 * :param ystart: y coordinate of top left corner; 0 <= y <= 80
 * :param xend: x coordinate of bottom right corner; 0 <= x <= 160
 * :param yend: y coordinate of bottom right corner; 0 <= y <= 80
 * :param color: line color in rgb565
 * :param fillstyle: 0 for empty, 1 for filled
 * :param pixelsize: thickness of the rectangle outline; 1 <= pixelsize <= 8
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
287
288
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
289
290
291
292
293
294
295
296
 */
API(API_DISP_RECT,
    int epic_disp_rect(
	    uint16_t xstart,
	    uint16_t ystart,
	    uint16_t xend,
	    uint16_t yend,
	    uint16_t color,
Rahix's avatar
Rahix committed
297
	    enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
298
299
300
301
302
303
304
305
306
307
308
309
310
	    uint16_t pixelsize)
    );

/**
 * Draws a circle on the display
 *
 * :param x: x coordinate of the center; 0 <= x <= 160
 * :param y: y coordinate of the center; 0 <= y <= 80
 * :param rad: radius of the circle
 * :param color: fill and outline color of the circle (rgb565)
 * :param fillstyle: 0 for empty, 1 for filled
 * :param pixelsize: thickness of the circle outline; 1 <= pixelsize <= 8
 * :return: ``0`` on success or a negative value in case of an error:
Rahix's avatar
Rahix committed
311
312
 *
 *    - ``-EBUSY``: Display was already locked from another task.
Gerd's avatar
Gerd committed
313
314
315
316
317
318
319
 */
API(API_DISP_CIRC,
    int epic_disp_circ(
	    uint16_t x,
	    uint16_t y,
	    uint16_t rad,
	    uint16_t color,
Rahix's avatar
Rahix committed
320
	    enum disp_fillstyle fillstyle,
Gerd's avatar
Gerd committed
321
322
	    uint16_t pixelsize)
    );
323

Rahix's avatar
Rahix committed
324
#endif /* _EPICARDIUM_H */