3 Copyright (C) 2016 Jeff Hoefs. All rights reserved.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 See file LICENSE.txt for further informations on licensing terms.
12 This version of SerialFirmata.h differs from the ConfigurableFirmata
13 version in the following ways:
15 - Defines FIRMATA_SERIAL_FEATURE (could add to Configurable version as well)
16 - Imports Firmata.h rather than ConfigurableFirmata.h
18 Last updated October 16th, 2016
21 #ifndef SerialFirmata_h
22 #define SerialFirmata_h
25 #include "FirmataFeature.h"
26 // SoftwareSerial is currently only supported for AVR-based boards and the Arduino 101.
27 // Limited to Arduino 1.6.6 or higher because Arduino builder cannot find SoftwareSerial
28 // prior to this release.
29 #if (ARDUINO > 10605) && (defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ARC32))
30 #include <SoftwareSerial.h>
33 #define FIRMATA_SERIAL_FEATURE
36 #define HW_SERIAL0 0x00
37 #define HW_SERIAL1 0x01
38 #define HW_SERIAL2 0x02
39 #define HW_SERIAL3 0x03
40 #define HW_SERIAL4 0x04
41 #define HW_SERIAL5 0x05
42 #define HW_SERIAL6 0x06
43 // extensible up to 0x07
45 #define SW_SERIAL0 0x08
46 #define SW_SERIAL1 0x09
47 #define SW_SERIAL2 0x0A
48 #define SW_SERIAL3 0x0B
49 // extensible up to 0x0F
51 #define SERIAL_PORT_ID_MASK 0x0F
52 #define MAX_SERIAL_PORTS 8
53 #define SERIAL_READ_ARR_LEN 12
55 // map configuration query response resolution value to serial pin type
69 // Serial command bytes
70 #define SERIAL_CONFIG 0x10
71 #define SERIAL_WRITE 0x20
72 #define SERIAL_READ 0x30
73 #define SERIAL_REPLY 0x40
74 #define SERIAL_CLOSE 0x50
75 #define SERIAL_FLUSH 0x60
76 #define SERIAL_LISTEN 0x70
79 #define SERIAL_READ_CONTINUOUSLY 0x00
80 #define SERIAL_STOP_READING 0x01
81 #define SERIAL_MODE_MASK 0xF0
91 * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin.
93 inline uint8_t getSerialPinType(uint8_t pin) {
94 #if defined(PIN_SERIAL_RX)
95 // TODO when use of HW_SERIAL0 is enabled
97 #if defined(PIN_SERIAL1_RX)
98 if (pin == PIN_SERIAL1_RX) return RES_RX1;
99 if (pin == PIN_SERIAL1_TX) return RES_TX1;
101 #if defined(PIN_SERIAL2_RX)
102 if (pin == PIN_SERIAL2_RX) return RES_RX2;
103 if (pin == PIN_SERIAL2_TX) return RES_TX2;
105 #if defined(PIN_SERIAL3_RX)
106 if (pin == PIN_SERIAL3_RX) return RES_RX3;
107 if (pin == PIN_SERIAL3_TX) return RES_TX3;
109 #if defined(PIN_SERIAL4_RX)
110 if (pin == PIN_SERIAL4_RX) return RES_RX4;
111 if (pin == PIN_SERIAL4_TX) return RES_TX4;
113 #if defined(PIN_SERIAL5_RX)
114 if (pin == PIN_SERIAL5_RX) return RES_RX5;
115 if (pin == PIN_SERIAL5_TX) return RES_TX5;
117 #if defined(PIN_SERIAL6_RX)
118 if (pin == PIN_SERIAL6_RX) return RES_RX6;
119 if (pin == PIN_SERIAL6_TX) return RES_TX6;
125 * Get the RX and TX pins numbers for the specified HW serial port.
127 inline serial_pins getSerialPinNumbers(uint8_t portId) {
130 #if defined(PIN_SERIAL_RX)
132 // // TODO when use of HW_SERIAL0 is enabled
135 #if defined(PIN_SERIAL1_RX)
137 pins.rx = PIN_SERIAL1_RX;
138 pins.tx = PIN_SERIAL1_TX;
141 #if defined(PIN_SERIAL2_RX)
143 pins.rx = PIN_SERIAL2_RX;
144 pins.tx = PIN_SERIAL2_TX;
147 #if defined(PIN_SERIAL3_RX)
149 pins.rx = PIN_SERIAL3_RX;
150 pins.tx = PIN_SERIAL3_TX;
153 #if defined(PIN_SERIAL4_RX)
155 pins.rx = PIN_SERIAL4_RX;
156 pins.tx = PIN_SERIAL4_TX;
159 #if defined(PIN_SERIAL5_RX)
161 pins.rx = PIN_SERIAL5_RX;
162 pins.tx = PIN_SERIAL5_TX;
165 #if defined(PIN_SERIAL6_RX)
167 pins.rx = PIN_SERIAL6_RX;
168 pins.tx = PIN_SERIAL6_TX;
181 class SerialFirmata: public FirmataFeature
185 boolean handlePinMode(byte pin, int mode);
186 void handleCapability(byte pin);
187 boolean handleSysex(byte command, byte argc, byte* argv);
193 byte reportSerial[MAX_SERIAL_PORTS];
194 int serialBytesToRead[SERIAL_READ_ARR_LEN];
195 signed char serialIndex;
197 #if defined(SoftwareSerial_h)
204 Stream* getPortFromId(byte portId);
208 #endif /* SerialFirmata_h */