|
| 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__) || defined(__MKL26Z64__) || defined(__IMXRT1062__) |
| 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 | +#elif (defined(__STM32F1__) and !(defined(USE_STM32_HW_SERIAL))) or defined(SPARK) |
| 58 | + // Stm32duino Maple mini USB Serial Port |
| 59 | + #define SERIAL_CLASS USBSerial |
| 60 | +#else |
| 61 | + #include <HardwareSerial.h> // Arduino AVR |
| 62 | + #define SERIAL_CLASS HardwareSerial |
| 63 | +#endif |
| 64 | + |
| 65 | +class ArduinoHardware { |
| 66 | + public: |
| 67 | + ArduinoHardware(SERIAL_CLASS* io , long baud= 57600){ |
| 68 | + iostream = io; |
| 69 | + baud_ = baud; |
| 70 | + } |
| 71 | + ArduinoHardware() |
| 72 | + { |
| 73 | +#if defined(USBCON) and !(defined(USE_USBCON)) |
| 74 | + /* Leonardo support */ |
| 75 | + iostream = &Serial1; |
| 76 | +#elif defined(USE_TEENSY_HW_SERIAL) or defined(USE_STM32_HW_SERIAL) |
| 77 | + iostream = &Serial1; |
| 78 | +#else |
| 79 | + iostream = &Serial; |
| 80 | +#endif |
| 81 | + baud_ = 57600; |
| 82 | + } |
| 83 | + ArduinoHardware(ArduinoHardware& h){ |
| 84 | + this->iostream = h.iostream; |
| 85 | + this->baud_ = h.baud_; |
| 86 | + } |
| 87 | + |
| 88 | + void setPort(SERIAL_CLASS* io){ |
| 89 | + this->iostream = io; |
| 90 | + } |
| 91 | + |
| 92 | + void setBaud(long baud){ |
| 93 | + this->baud_= baud; |
| 94 | + } |
| 95 | + |
| 96 | + int getBaud(){return baud_;} |
| 97 | + |
| 98 | + void init(){ |
| 99 | +#if defined(USE_USBCON) |
| 100 | + // Startup delay as a fail-safe to upload a new sketch |
| 101 | + delay(3000); |
| 102 | +#endif |
| 103 | + iostream->begin(baud_); |
| 104 | + } |
| 105 | + |
| 106 | + int read(){return iostream->read();}; |
| 107 | + void write(uint8_t* data, int length){ |
| 108 | + iostream->write(data, length); |
| 109 | + } |
| 110 | + |
| 111 | + unsigned long time(){return millis();} |
| 112 | + |
| 113 | + protected: |
| 114 | + SERIAL_CLASS* iostream; |
| 115 | + long baud_; |
| 116 | +}; |
| 117 | + |
| 118 | +#endif |
0 commit comments