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 // extensible up to 0x07
42 #define SW_SERIAL0 0x08
43 #define SW_SERIAL1 0x09
44 #define SW_SERIAL2 0x0A
45 #define SW_SERIAL3 0x0B
46 // extensible up to 0x0F
48 #define SERIAL_PORT_ID_MASK 0x0F
49 #define MAX_SERIAL_PORTS 8
50 #define SERIAL_READ_ARR_LEN 12
52 // map configuration query response resolution value to serial pin type
60 // Serial command bytes
61 #define SERIAL_CONFIG 0x10
62 #define SERIAL_WRITE 0x20
63 #define SERIAL_READ 0x30
64 #define SERIAL_REPLY 0x40
65 #define SERIAL_CLOSE 0x50
66 #define SERIAL_FLUSH 0x60
67 #define SERIAL_LISTEN 0x70
70 #define SERIAL_READ_CONTINUOUSLY 0x00
71 #define SERIAL_STOP_READING 0x01
72 #define SERIAL_MODE_MASK 0xF0
82 * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin.
84 inline uint8_t getSerialPinType(uint8_t pin) {
85 #if defined(PIN_SERIAL_RX)
86 // TODO when use of HW_SERIAL0 is enabled
88 #if defined(PIN_SERIAL1_RX)
89 if (pin == PIN_SERIAL1_RX) return RES_RX1;
90 if (pin == PIN_SERIAL1_TX) return RES_TX1;
92 #if defined(PIN_SERIAL2_RX)
93 if (pin == PIN_SERIAL2_RX) return RES_RX2;
94 if (pin == PIN_SERIAL2_TX) return RES_TX2;
96 #if defined(PIN_SERIAL3_RX)
97 if (pin == PIN_SERIAL3_RX) return RES_RX3;
98 if (pin == PIN_SERIAL3_TX) return RES_TX3;
104 * Get the RX and TX pins numbers for the specified HW serial port.
106 inline serial_pins getSerialPinNumbers(uint8_t portId) {
109 #if defined(PIN_SERIAL_RX)
111 // // TODO when use of HW_SERIAL0 is enabled
114 #if defined(PIN_SERIAL1_RX)
116 pins.rx = PIN_SERIAL1_RX;
117 pins.tx = PIN_SERIAL1_TX;
120 #if defined(PIN_SERIAL2_RX)
122 pins.rx = PIN_SERIAL2_RX;
123 pins.tx = PIN_SERIAL2_TX;
126 #if defined(PIN_SERIAL3_RX)
128 pins.rx = PIN_SERIAL3_RX;
129 pins.tx = PIN_SERIAL3_TX;
142 class SerialFirmata: public FirmataFeature
146 boolean handlePinMode(byte pin, int mode);
147 void handleCapability(byte pin);
148 boolean handleSysex(byte command, byte argc, byte* argv);
154 byte reportSerial[MAX_SERIAL_PORTS];
155 int serialBytesToRead[SERIAL_READ_ARR_LEN];
156 signed char serialIndex;
158 #if defined(SoftwareSerial_h)
165 Stream* getPortFromId(byte portId);
169 #endif /* SerialFirmata_h */