Blame view

RIOT/sys/arduino/include/serialport.hpp 8.9 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_arduino_api
   * @{
   *
   * @file
   * @brief       Definition of the Arduino 'Serial' interface
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   */
  
  #ifndef ARDUINO_SERIAL_H
  #define ARDUINO_SERIAL_H
  
  extern "C" {
  #include "ringbuffer.h"
  #include "periph/uart.h"
  }
  
  /**
   * @brief   Default RX buffer size - same as the original Arduino...
   */
  #define SERIAL_RX_BUFSIZE       (64)
  
  /**
   * @brief   Formatting options for Serial.print(int, format)
   */
  enum SerialFormat {
      BIN,            /**< format to binary representation */
      OCT,            /**< format to octal representation */
      DEC,            /**< format to decimal representation */
      HEX             /**< format to hex representation */
  };
  
  /**
   * @brief   Arduino Serial Interface
   */
  class SerialPort {
  
  private:
      uart_t dev;
      char rx_mem[SERIAL_RX_BUFSIZE];
      ringbuffer_t rx_buf;
  
  public:
      /**
       * @brief   Constructor maps the serial port to a RIOT UART device
       *
       * @param[in] dev       RIOT UART device
       */
      SerialPort(uart_t dev);
  
      /**
       * @brief   Get the number of bytes (characters) available for reading from
       *          the serial port
       *
       * This is data that's already arrived and stored in the serial receive
       * buffer (which holds 64 bytes). available() inherits from the Stream
       * utility class.
       *
       * Copied from https://www.arduino.cc/en/Serial/Available
       *
       * @return  The number of bytes available to read
       */
      int available(void);
  
      /**
       * @brief   Sets the data rate in bits per second (baud) for serial data
       *          transmission
       *
       * For communicating with the computer, use one of these rates: 300, 600,
       * 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You
       * can, however, specify other rates - for example, to communicate over
       * pins 0 and 1 with a component that requires a particular baud rate.
       *
       * Copied from https://www.arduino.cc/en/Serial/Begin
       *
       * @param[in] speed     speed in bits per second (baud)
       */
      void begin(int speed);
  
      /**
       * @brief   Disables serial communication, allowing the RX and TX pins to be
       *          used for general input and output
       *
       * To re-enable serial communication, call @ref begin()
       *
       * Copied from https://www.arduino.cc/en/Serial/End
       */
      void end(void);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * Prints data to the serial port as human-readable ASCII text. This command
       * can take many forms. Numbers are printed using an ASCII character for
       * each digit. Floats are similarly printed as ASCII digits, defaulting to
       * two decimal places. Bytes are sent as a single character. Characters and
       * strings are sent as is.
       *
       * Copied from https://www.arduino.cc/en/Serial/Print
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t print(int val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * @see print()
       *
       * @param[in] val       the value to print
       * @param[in] format    specifies the number base
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t print(int val, SerialFormat format);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t print(float val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * @see print()
       *
       * @param[in] val       the value to print
       * @param[in] format    number of decimal places
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t print(float val, int format);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * @see print()
       *
       * @param[in] val       the value to print
       *
      * @return  the number of bytes written, reading that number is optional
       */
      size_t print(char val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *
       * @see print()
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t print(const char *val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * This command takes the same forms as @ref print().
       *
       * Copied from https://www.arduino.cc/en/Serial/Println
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(int val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * @see println()
       *
       * @param[in] val       the value to print
       * @param[in] format    specifies the number base
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(int val, SerialFormat format);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * @see println()
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(float val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * @see println()
       *
       * @param[in] val       the value to print
       * @param[in] format    number of decimal places
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(float val, int format);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * @see println()
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(char val);
  
      /**
       * @brief   Prints data to the serial port as human-readable ASCII text
       *          followed by a carriage return character (ASCII 13, or "\r") and
       *          a newline character (ASCII 10, or "\n")
       *
       * @see println()
       *
       * @param[in] val       the value to print
       *
       * @return  the number of bytes written, reading that number is optional
       */
      size_t println(const char *val);
  
      /**
       * @brief   Reads incoming serial data
       *
       * Copied from https://www.arduino.cc/en/Serial/Read
       *
       * @return  the first byte of incoming serial data available
       * @return  -1 if no data is available
       */
      int read(void);
  
      /**
       * @brief   Writes binary data to the serial port
       *
       * This data is sent as a byte or series of bytes; to send the characters
       * representing the digits of a number use the @ref print() function instead.
       *
       * Copied from https://www.arduino.cc/en/Serial/Write
       *
       * @param[in] val       a value to send as a single byte
       *
       * @return  the number of bytes written, reading that number is optional
       */
      int write(int val);
  
      /**
       * @brief   Writes binary data to the serial port
       *
       * @see write()
       *
       * @param[in] str       a string to send as a series of bytes
       *
       * @return  the number of bytes written, reading that number is optional
       */
      int write(const char *str);
  
      /**
       * @brief   Writes binary data to the serial port
       * @details [long description]
       *
       * @param[in] buf       an array to send as a series of bytes
       * @param[in] len       the length of the buffer
       *
       * @return  the number of bytes written, reading that number is optional
       */
      int write(char *buf, int len);
  };
  
  #endif /* ARDUINO_SERIAL_H */
  /** @} */