ArduinoHardware.h
Go to the documentation of this file.
00001 /* 
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  *  * Neither the name of Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote prducts derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #ifndef ROS_ARDUINO_HARDWARE_H_
00036 #define ROS_ARDUINO_HARDWARE_H_
00037 
00038 #if ARDUINO>=100
00039   #include <Arduino.h>  // Arduino 1.0
00040 #else
00041   #include <WProgram.h>  // Arduino 0022
00042 #endif
00043 
00044 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)
00045   #if defined(USE_TEENSY_HW_SERIAL)
00046     #define SERIAL_CLASS HardwareSerial // Teensy HW Serial
00047   #else
00048     #include <usb_serial.h>  // Teensy 3.0 and 3.1
00049     #define SERIAL_CLASS usb_serial_class
00050   #endif
00051 #elif defined(_SAM3XA_)
00052   #include <UARTClass.h>  // Arduino Due
00053   #define SERIAL_CLASS UARTClass
00054 #elif defined(USE_USBCON)
00055   // Arduino Leonardo USB Serial Port
00056   #define SERIAL_CLASS Serial_
00057 #elif (defined(__STM32F1__) and !(defined(USE_STM32_HW_SERIAL))) or defined(SPARK) 
00058   // Stm32duino Maple mini USB Serial Port
00059   #define SERIAL_CLASS USBSerial
00060 #else 
00061   #include <HardwareSerial.h>  // Arduino AVR
00062   #define SERIAL_CLASS HardwareSerial
00063 #endif
00064 
00065 class ArduinoHardware {
00066   public:
00067     ArduinoHardware(SERIAL_CLASS* io , long baud= 57600){
00068       iostream = io;
00069       baud_ = baud;
00070     }
00071     ArduinoHardware()
00072     {
00073 #if defined(USBCON) and !(defined(USE_USBCON))
00074       /* Leonardo support */
00075       iostream = &Serial1;
00076 #elif defined(USE_TEENSY_HW_SERIAL) or defined(USE_STM32_HW_SERIAL)
00077       iostream = &Serial1;
00078 #else
00079       iostream = &Serial;
00080 #endif
00081       baud_ = 57600;
00082     }
00083     ArduinoHardware(ArduinoHardware& h){
00084       this->iostream = h.iostream;
00085       this->baud_ = h.baud_;
00086     }
00087   
00088     void setBaud(long baud){
00089       this->baud_= baud;
00090     }
00091   
00092     int getBaud(){return baud_;}
00093 
00094     void init(){
00095 #if defined(USE_USBCON)
00096       // Startup delay as a fail-safe to upload a new sketch
00097       delay(3000); 
00098 #endif
00099       iostream->begin(baud_);
00100     }
00101 
00102     int read(){return iostream->read();};
00103     void write(uint8_t* data, int length){
00104       for(int i=0; i<length; i++)
00105         iostream->write(data[i]);
00106     }
00107 
00108     unsigned long time(){return millis();}
00109 
00110   protected:
00111     SERIAL_CLASS* iostream;
00112     long baud_;
00113 };
00114 
00115 #endif


rosserial_arduino
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Sat Oct 7 2017 03:08:41