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