kitt_platform
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
ArduinoHardware.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef ROS_ARDUINO_HARDWARE_H_
36 #define ROS_ARDUINO_HARDWARE_H_
37 
38 #if ARDUINO>=100
39  #include <Arduino.h> // Arduino 1.0
40 #else
41  #include <WProgram.h> // Arduino 0022
42 #endif
43 
44 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
45  #if defined(USE_TEENSY_HW_SERIAL)
46  #define SERIAL_CLASS HardwareSerial // Teensy HW Serial
47  #else
48  #include <usb_serial.h> // Teensy 3.0 and 3.1
49  #define SERIAL_CLASS usb_serial_class
50  #endif
51 #elif defined(_SAM3XA_)
52  #include <UARTClass.h> // Arduino Due
53  #define SERIAL_CLASS UARTClass
54 #elif defined(USE_USBCON)
55  // Arduino Leonardo USB Serial Port
56  #define SERIAL_CLASS Serial_
57 #else
58  #include <HardwareSerial.h> // Arduino AVR
59  #define SERIAL_CLASS HardwareSerial
60 #endif
61 
63  public:
64  ArduinoHardware(SERIAL_CLASS* io , long baud= 57600){
65  iostream = io;
66  baud_ = baud;
67  }
69  {
70 #if defined(USBCON) and !(defined(USE_USBCON))
71  /* Leonardo support */
72  iostream = &Serial1;
73 #else
74  iostream = &Serial;
75 #endif
76  baud_ = 57600;
77  }
79  this->iostream = iostream;
80  this->baud_ = h.baud_;
81  }
82 
83  void setBaud(long baud){
84  this->baud_= baud;
85  }
86 
87  int getBaud(){return baud_;}
88 
89  void init(){
90 #if defined(USE_USBCON)
91  // Startup delay as a fail-safe to upload a new sketch
92  delay(3000);
93 #endif
94  iostream->begin(baud_);
95  }
96 
97  int read(){return iostream->read();};
98  void write(uint8_t* data, int length){
99  for(int i=0; i<length; i++)
100  iostream->write(data[i]);
101  }
102 
103  unsigned long time(){return millis();}
104 
105  protected:
107  long baud_;
108 };
109 
110 #endif
#define SERIAL_CLASS
Definition: ArduinoHardware.h:59
Definition: ArduinoHardware.h:62
void init()
Definition: ArduinoHardware.h:89
ArduinoHardware()
Definition: ArduinoHardware.h:68
ArduinoHardware(ArduinoHardware &h)
Definition: ArduinoHardware.h:78
SERIAL_CLASS * iostream
Definition: ArduinoHardware.h:106
int getBaud()
Definition: ArduinoHardware.h:87
void write(uint8_t *data, int length)
Definition: ArduinoHardware.h:98
void setBaud(long baud)
Definition: ArduinoHardware.h:83
int read()
Definition: ArduinoHardware.h:97
long baud_
Definition: ArduinoHardware.h:107
ArduinoHardware(SERIAL_CLASS *io, long baud=57600)
Definition: ArduinoHardware.h:64
unsigned long time()
Definition: ArduinoHardware.h:103