-
Notifications
You must be signed in to change notification settings - Fork 55
/
port_handler.h
104 lines (84 loc) · 2.9 KB
/
port_handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
* Copyright 2016 ROBOTIS CO., LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#ifndef DYNAMIXEL_PORT_HANDLER_HPP_
#define DYNAMIXEL_PORT_HANDLER_HPP_
#include <Arduino.h>
class DXLPortHandler
{
public:
DXLPortHandler();
virtual void begin() = 0;
virtual void end() = 0;
virtual int available(void) = 0;
virtual int read() = 0;
virtual size_t write(uint8_t) = 0;
virtual size_t write(uint8_t *buf, size_t len) = 0;
bool getOpenState();
void setOpenState(bool);
private:
bool open_state_;
};
namespace DYNAMIXEL{
class SerialPortHandler : public DXLPortHandler
{
public:
SerialPortHandler(HardwareSerial& port, const int dir_pin = -1);
virtual void begin() override;
virtual void end() override;
virtual int available(void) override;
virtual int read() override;
virtual size_t write(uint8_t) override;
virtual size_t write(uint8_t *buf, size_t len) override;
virtual void begin(unsigned long baud);
virtual unsigned long getBaud() const;
private:
HardwareSerial& port_;
const int dir_pin_;
unsigned long baud_;
unsigned int mbedTXdelayus;
};
#if defined(__OPENCR__)
#define USB_SERIAL_CLASS USBSerial
#elif defined(__MK20DX128__) || defined(__MK20DX256__)
#include <usb_serial.h> // Teensy 3.0 and 3.1
#define USB_SERIAL_CLASS usb_serial_class
#elif defined(_SAM3XA_)
#include <UARTClass.h> // Arduino Due
#define USB_SERIAL_CLASS UARTClass
#elif defined(ARDUINO_AVR_LEONARDO)
// Arduino Leonardo USB Serial Port
#define USB_SERIAL_CLASS Serial_
#elif defined(ARDUINO_CommXEL)
#include "USBSerial.h"
#define USB_SERIAL_CLASS USBSerial
#else
#define USB_SERIAL_CLASS HardwareSerial
#endif
class USBSerialPortHandler : public DXLPortHandler
{
public:
USBSerialPortHandler(USB_SERIAL_CLASS& port);
virtual void begin() override;
virtual void end() override;
virtual int available(void) override;
virtual int read() override;
virtual size_t write(uint8_t) override;
virtual size_t write(uint8_t *buf, size_t len) override;
private:
USB_SERIAL_CLASS& port_;
};
}//namespace DYNAMIXEL
#endif /* DYNAMIXEL_PORT_HANDLER_HPP_ */