CMUcam4 Arduino Interface Library  For Firmware Versions 1.00 - 1.03
Application Programmable Interface Online Documentation
 All Classes Files Functions Variables Macros Pages
CMUcom4.h
Go to the documentation of this file.
1 /***************************************************************************//**
2 * @file
3 * Portable serial and timer wrapper library.
4 *
5 * @version @n 1.1
6 * @date @n 2/7/2013
7 *
8 * @authors @n Kwabena W. Agyeman & Christopher J. Leaf
9 * @copyright @n (c) 2013 Kwabena W. Agyeman & Christopher J. Leaf
10 * @n All rights reserved - Please see the end of the file for the terms of use
11 *
12 * @par Update History:
13 * @n v0.1 - Beta code - 3/20/2012
14 * @n v0.9 - Original release - 4/18/2012
15 * @n v1.0 - Documented and updated release - 8/3/2012
16 * @n v1.1 - Added support for the Arduino Due, fixed the send frame command,
17  and fixed a number of compile time warnings - 2/7/2013.
18 *******************************************************************************/
19 
20 #ifndef _CMUCOM4_H_
21 #define _CMUCOM4_H_
22 
23 /**@cond CMUCOM4_PRIVATE*******************************************************/
24 
25 // Handle Arduino Library renaming.
26 #if defined(ARDUINO) && (ARDUINO >= 100)
27 #include "Arduino.h"
28 #else
29 #include "WProgram.h"
30 #endif
31 
32 // Try to save RAM for non-Mega boards.
33 #if defined(__AVR_ATmega1280__) || \
34  defined(__AVR_ATmega2560__) || \
35  defined(__SAM3X8E__)
36 #define CMUCOM4_INPUT_BUFFER_SIZE 256 ///< Responce input buffer size.
37 #define CMUCOM4_OUTPUT_BUFFER_SIZE 256 ///< Command output buffer size.
38 #else
39 #define CMUCOM4_INPUT_BUFFER_SIZE 160 ///< Responce input buffer size.
40 #define CMUCOM4_OUTPUT_BUFFER_SIZE 96 ///< Command output buffer size.
41 #endif
42 
43 /***************************************************************************//**
44 * This function macro expands whatever argument name that was passed to this
45 * function macro into a string. @par For example:
46 * <tt>@#define ARDUINO 100</tt> @n
47 * <tt>%CMUCOM4_N_TO_S(ARDUINO)</tt> exapands to @c "ARDUINO"
48 *******************************************************************************/
49 #define CMUCOM4_N_TO_S(x) #x
50 
51 /***************************************************************************//**
52 * This function macro expands whatever argument value that was passed to this
53 * function macro into a string. @par For example:
54 * <tt>@#define ARDUINO 100</tt> @n
55 * <tt>%CMUCOM4_V_TO_S(ARDUINO)</tt> exapands to @c "100"
56 *******************************************************************************/
57 #define CMUCOM4_V_TO_S(x) CMUCOM4_N_TO_S(x)
58 
59 /***************************************************************************//**
60 * Default firmware startup baud rate number.
61 *******************************************************************************/
62 #define CMUCOM4_SLOW_BAUD_RATE 19200
63 
64 /***************************************************************************//**
65 * Default firmware startup baud rate string.
66 *******************************************************************************/
67 #define CMUCOM4_SLOW_BR_STRING CMUCOM4_V_TO_S(CMUCOM4_SLOW_BAUD_RATE)
68 
69 /***************************************************************************//**
70 * Version 1.01 firmware and below maximum baud rate number.
71 *******************************************************************************/
72 #define CMUCOM4_MEDIUM_BAUD_RATE 115200
73 
74 /***************************************************************************//**
75 * Version 1.01 firmware and below maximum baud rate string.
76 *******************************************************************************/
77 #define CMUCOM4_MEDIUM_BR_STRING CMUCOM4_V_TO_S(CMUCOM4_MEDIUM_BAUD_RATE)
78 
79 /***************************************************************************//**
80 * Version 1.02 firmware and above maximum baud rate number.
81 *******************************************************************************/
82 #define CMUCOM4_FAST_BAUD_RATE 250000
83 
84 /***************************************************************************//**
85 * Version 1.02 firmware and above maximum baud rate string.
86 *******************************************************************************/
87 #define CMUCOM4_FAST_BR_STRING CMUCOM4_V_TO_S(CMUCOM4_FAST_BAUD_RATE)
88 
89 /***************************************************************************//**
90 * Default firmware startup stop bits number.
91 *******************************************************************************/
92 #define CMUCOM4_SLOW_STOP_BITS 0
93 
94 /***************************************************************************//**
95 * Default firmware startup stop bits string.
96 *******************************************************************************/
97 #define CMUCOM4_SLOW_SB_STRING CMUCOM4_V_TO_S(CMUCOM4_SLOW_STOP_BITS)
98 
99 /***************************************************************************//**
100 * Version 1.01 firmware and below necessary stop bits number.
101 *******************************************************************************/
102 #define CMUCOM4_MEDIUM_STOP_BITS 0
103 
104 /***************************************************************************//**
105 * Version 1.01 firmware and below necessary stop bits string.
106 *******************************************************************************/
107 #define CMUCOM4_MEDIUM_SB_STRING CMUCOM4_V_TO_S(CMUCOM4_MEDIUM_STOP_BITS)
108 
109 /***************************************************************************//**
110 * Version 1.02 firmware and above necessary stop bits number.
111 *******************************************************************************/
112 #define CMUCOM4_FAST_STOP_BITS 0
113 
114 /***************************************************************************//**
115 * Version 1.02 firmware and above necessary stop bits string.
116 *******************************************************************************/
117 #define CMUCOM4_FAST_SB_STRING CMUCOM4_V_TO_S(CMUCOM4_FAST_STOP_BITS)
118 
119 /***************************************************************************//**
120 * Serial CMUcom4::begin() post delay in milliseconds.
121 *******************************************************************************/
122 #define CMUCOM4_BEGIN_DELAY 1
123 
124 /***************************************************************************//**
125 * Serial CMUcom4::end() post delay in milliseconds.
126 *******************************************************************************/
127 #define CMUCOM4_END_DELAY 1
128 
129 /**@endcond********************************************************************/
130 
131 /***************************************************************************//**
132 * This is a convenient macro for specifying the Serial port when initializing a
133 * CMUcam4 or CMUcom4 object.
134 *******************************************************************************/
135 #define CMUCOM4_SERIAL 0
136 
137 /***************************************************************************//**
138 * This is a convenient macro for specifying the Serial1 port on an Arduino Mega
139 * when initializing a CMUcam4 or CMUcom4 object.
140 *******************************************************************************/
141 #define CMUCOM4_SERIAL1 1
142 
143 /***************************************************************************//**
144 * This is a convenient macro for specifying the Serial2 port on an Arduino Mega
145 * when initializing a CMUcam4 or CMUcom4 object.
146 *******************************************************************************/
147 #define CMUCOM4_SERIAL2 2
148 
149 /***************************************************************************//**
150 * This is a convenient macro for specifying the Serial3 port on an Arduino Mega
151 * when initializing a CMUcam4 or CMUcom4 object.
152 *******************************************************************************/
153 #define CMUCOM4_SERIAL3 3
154 
155 /***************************************************************************//**
156 * This is a hardware abstraction layer for the %CMUcam4 class. The %CMUcom4
157 * class targets the Ardunio prototyping platform by default.
158 *******************************************************************************/
159 class CMUcom4
160 {
161 
162 public:
163 
164 /***************************************************************************//**
165 * Initialize the %CMUcom4 object to use the default Serial port.
166 *******************************************************************************/
167 CMUcom4();
168 
169 /***************************************************************************//**
170 * Initialize the %CMUcom4 object to use the @c port Serial port.
171 * @param [in] port The port.
172 * @see CMUCOM4_SERIAL
173 * @see CMUCOM4_SERIAL1
174 * @see CMUCOM4_SERIAL2
175 * @see CMUCOM4_SERIAL3
176 *******************************************************************************/
177 CMUcom4(int port);
178 
179 /***************************************************************************//**
180 * Arduino Serial.begin() wrapper.
181 * @param [in] baud In bits per second.
182 * @see http://arduino.cc/en/Serial/Begin
183 *******************************************************************************/
184 void begin(unsigned long baud);
185 
186 /***************************************************************************//**
187 * Arduino Serial.end() wrapper.
188 * @see http://arduino.cc/en/Serial/End
189 *******************************************************************************/
190 void end();
191 
192 /***************************************************************************//**
193 * Arduino Serial.read() wrapper.
194 * @return The first byte of incoming serial data.
195 * @see http://arduino.cc/en/Serial/Read
196 *******************************************************************************/
197 int read();
198 
199 /***************************************************************************//**
200 * Arduino Serial.write() wrapper.
201 * @param [in] buffer An array to send as a series of bytes.
202 * @param [in] size The size of the buffer.
203 * @return The number of bytes written.
204 * @see http://arduino.cc/en/Serial/Write
205 *******************************************************************************/
206 size_t write(const uint8_t * buffer, size_t size);
207 
208 /***************************************************************************//**
209 * Arduino Serial.write() wrapper.
210 * @param [in] str A string to send as a series of bytes.
211 * @return The number of bytes written.
212 * @see http://arduino.cc/en/Serial/Write
213 *******************************************************************************/
214 size_t write(const char * str);
215 
216 /***************************************************************************//**
217 * Arduino Serial.write() wrapper.
218 * @param [in] c A character to send as a single byte.
219 * @return The number of bytes written.
220 * @see http://arduino.cc/en/Serial/Write
221 *******************************************************************************/
222 size_t write(uint8_t c);
223 
224 /***************************************************************************//**
225 * Arduino Serial.available() wrapper.
226 * @return The number of bytes available to be read.
227 * @see http://arduino.cc/en/Serial/Available
228 *******************************************************************************/
229 int available();
230 
231 /***************************************************************************//**
232 * Arduino Serial.flush() wrapper.
233 * @see http://arduino.cc/en/Serial/Flush
234 *******************************************************************************/
235 void flush();
236 
237 /***************************************************************************//**
238 * Arduino Serial.peek() wrapper.
239 * @return The first byte of incoming serial data available.
240 * @see http://arduino.cc/en/Serial/Peek
241 *******************************************************************************/
242 int peek();
243 
244 /***************************************************************************//**
245 * Arduino delay() wrapper.
246 * @param [in] ms The number of milliseconds to pause for.
247 * @see http://arduino.cc/en/Reference/Delay
248 *******************************************************************************/
249 void delayMilliseconds(unsigned long ms);
250 
251 /***************************************************************************//**
252 * Arduino millis() wrapper.
253 * @return Number of milliseconds since the program started.
254 * @see http://arduino.cc/en/Reference/Millis
255 *******************************************************************************/
256 unsigned long milliseconds();
257 
258 private:
259 
260 /***************************************************************************//**
261 * Selected serial port storage.
262 * @see CMUCOM4_SERIAL1
263 * @see CMUCOM4_SERIAL2
264 * @see CMUCOM4_SERIAL3
265 *******************************************************************************/
266 int _port;
267 };
268 
269 #endif
270 
271 /***************************************************************************//**
272 * @file
273 * @par MIT License - TERMS OF USE:
274 * @n Permission is hereby granted, free of charge, to any person obtaining a
275 * copy of this software and associated documentation files (the "Software"), to
276 * deal in the Software without restriction, including without limitation the
277 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
278 * sell copies of the Software, and to permit persons to whom the Software is
279 * furnished to do so, subject to the following conditions:
280 * @n
281 * @n The above copyright notice and this permission notice shall be included in
282 * all copies or substantial portions of the Software.
283 * @n
284 * @n THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
285 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
286 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
287 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
288 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
289 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
290 * SOFTWARE.
291 *******************************************************************************/