CMUcam4 Arduino Interface Library  For Firmware Versions 1.00 - 1.03
Application Programmable Interface Online Documentation
 All Classes Files Functions Variables Macros Pages
CMUcom4.cpp
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 #include "CMUcom4.h"
21 
22 /*******************************************************************************
23 * Constructor Functions
24 *******************************************************************************/
25 
27 {
28  _port = CMUCOM4_SERIAL;
29 }
30 
32 {
33  _port = port;
34 }
35 
36 /*******************************************************************************
37 * Public Functions
38 *******************************************************************************/
39 
40 void CMUcom4::begin(unsigned long baud)
41 {
42 #if defined(__AVR_ATmega1280__) || \
43  defined(__AVR_ATmega2560__) || \
44  defined(__SAM3X8E__)
45  switch(_port)
46  {
47  case CMUCOM4_SERIAL1: Serial1.begin(baud); break;
48  case CMUCOM4_SERIAL2: Serial2.begin(baud); break;
49  case CMUCOM4_SERIAL3: Serial3.begin(baud); break;
50  default: Serial.begin(baud); break;
51  }
52 #else
53  Serial.begin(baud);
54 #endif
55  delayMilliseconds(CMUCOM4_BEGIN_DELAY);
56 }
57 
59 {
60 #if defined(__AVR_ATmega1280__) || \
61  defined(__AVR_ATmega2560__) || \
62  defined(__SAM3X8E__)
63  switch(_port)
64  {
65  case CMUCOM4_SERIAL1: Serial1.end(); break;
66  case CMUCOM4_SERIAL2: Serial2.end(); break;
67  case CMUCOM4_SERIAL3: Serial3.end(); break;
68  default: Serial.end(); break;
69  }
70 #else
71  Serial.end();
72 #endif
73  delayMilliseconds(CMUCOM4_END_DELAY);
74 }
75 
77 {
78 #if defined(__AVR_ATmega1280__) || \
79  defined(__AVR_ATmega2560__) || \
80  defined(__SAM3X8E__)
81  switch(_port)
82  {
83  case CMUCOM4_SERIAL1: return Serial1.read(); break;
84  case CMUCOM4_SERIAL2: return Serial2.read(); break;
85  case CMUCOM4_SERIAL3: return Serial3.read(); break;
86  default: return Serial.read(); break;
87  }
88 #else
89  return Serial.read();
90 #endif
91 }
92 
93 size_t CMUcom4::write(uint8_t c)
94 {
95 #if defined(__AVR_ATmega1280__) || \
96  defined(__AVR_ATmega2560__) || \
97  defined(__SAM3X8E__)
98  switch(_port)
99  {
100  case CMUCOM4_SERIAL1: return Serial1.write(c); break;
101  case CMUCOM4_SERIAL2: return Serial2.write(c); break;
102  case CMUCOM4_SERIAL3: return Serial3.write(c); break;
103  default: return Serial.write(c); break;
104  }
105 #else
106  return Serial.write(c);
107 #endif
108 }
109 
110 size_t CMUcom4::write(const char * str)
111 {
112 #if defined(__AVR_ATmega1280__) || \
113  defined(__AVR_ATmega2560__) || \
114  defined(__SAM3X8E__)
115  switch(_port)
116  {
117  case CMUCOM4_SERIAL1: return Serial1.write(str); break;
118  case CMUCOM4_SERIAL2: return Serial2.write(str); break;
119  case CMUCOM4_SERIAL3: return Serial3.write(str); break;
120  default: return Serial.write(str); break;
121  }
122 #else
123  return Serial.write(str);
124 #endif
125 }
126 
127 size_t CMUcom4::write(const uint8_t * buffer, size_t size)
128 {
129 #if defined(__AVR_ATmega1280__) || \
130  defined(__AVR_ATmega2560__) || \
131  defined(__SAM3X8E__)
132  switch(_port)
133  {
134  case CMUCOM4_SERIAL1: return Serial1.write(buffer, size); break;
135  case CMUCOM4_SERIAL2: return Serial2.write(buffer, size); break;
136  case CMUCOM4_SERIAL3: return Serial3.write(buffer, size); break;
137  default: return Serial.write(buffer, size); break;
138  }
139 #else
140  return Serial.write(buffer, size);
141 #endif
142 }
143 
145 {
146 #if defined(__AVR_ATmega1280__) || \
147  defined(__AVR_ATmega2560__) || \
148  defined(__SAM3X8E__)
149  switch(_port)
150  {
151  case CMUCOM4_SERIAL1: return Serial1.available(); break;
152  case CMUCOM4_SERIAL2: return Serial2.available(); break;
153  case CMUCOM4_SERIAL3: return Serial3.available(); break;
154  default: return Serial.available(); break;
155  }
156 #else
157  return Serial.available();
158 #endif
159 }
160 
162 {
163 #if defined(__AVR_ATmega1280__) || \
164  defined(__AVR_ATmega2560__) || \
165  defined(__SAM3X8E__)
166  switch(_port)
167  {
168  case CMUCOM4_SERIAL1: Serial1.flush(); break;
169  case CMUCOM4_SERIAL2: Serial2.flush(); break;
170  case CMUCOM4_SERIAL3: Serial3.flush(); break;
171  default: Serial.flush(); break;
172  }
173 #else
174  Serial.flush();
175 #endif
176 }
177 
179 {
180 #if defined(__AVR_ATmega1280__) || \
181  defined(__AVR_ATmega2560__) || \
182  defined(__SAM3X8E__)
183  switch(_port)
184  {
185  case CMUCOM4_SERIAL1: return Serial1.peek(); break;
186  case CMUCOM4_SERIAL2: return Serial2.peek(); break;
187  case CMUCOM4_SERIAL3: return Serial3.peek(); break;
188  default: return Serial.peek(); break;
189  }
190 #else
191  return Serial.peek();
192 #endif
193 }
194 
195 void CMUcom4::delayMilliseconds(unsigned long ms)
196 {
197  return delay(ms);
198 }
199 
200 unsigned long CMUcom4::milliseconds()
201 {
202  return millis();
203 }
204 
205 /***************************************************************************//**
206 * @file
207 * @par MIT License - TERMS OF USE:
208 * @n Permission is hereby granted, free of charge, to any person obtaining a
209 * copy of this software and associated documentation files (the "Software"), to
210 * deal in the Software without restriction, including without limitation the
211 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
212 * sell copies of the Software, and to permit persons to whom the Software is
213 * furnished to do so, subject to the following conditions:
214 * @n
215 * @n The above copyright notice and this permission notice shall be included in
216 * all copies or substantial portions of the Software.
217 * @n
218 * @n THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
219 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
220 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
221 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
222 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
223 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
224 * SOFTWARE.
225 *******************************************************************************/