[224] | 1 | /*
|
---|
| 2 | SerialFirmata.h
|
---|
| 3 | Copyright (C) 2016 Jeff Hoefs. All rights reserved.
|
---|
| 4 |
|
---|
| 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.
|
---|
| 9 |
|
---|
| 10 | See file LICENSE.txt for further informations on licensing terms.
|
---|
| 11 |
|
---|
| 12 | This version of SerialFirmata.h differs from the ConfigurableFirmata
|
---|
| 13 | version in the following ways:
|
---|
| 14 |
|
---|
| 15 | - Defines FIRMATA_SERIAL_FEATURE (could add to Configurable version as well)
|
---|
| 16 | - Imports Firmata.h rather than ConfigurableFirmata.h
|
---|
| 17 |
|
---|
| 18 | Last updated by Jeff Hoefs: January 10th, 2016
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | #ifndef SerialFirmata_h
|
---|
| 22 | #define SerialFirmata_h
|
---|
| 23 |
|
---|
| 24 | #include <Firmata.h>
|
---|
| 25 | #include "FirmataFeature.h"
|
---|
| 26 | // SoftwareSerial is currently only supported for AVR-based boards and the Arduino 101
|
---|
| 27 | // The third condition checks if the IDE is in the 1.0.x series, if so, include SoftwareSerial
|
---|
| 28 | // since it should be available to all boards in that IDE.
|
---|
| 29 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ARC32) || (ARDUINO >= 100 && ARDUINO < 10500)
|
---|
| 30 | #include <SoftwareSerial.h>
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
| 33 | #define FIRMATA_SERIAL_FEATURE
|
---|
| 34 |
|
---|
| 35 | // Serial port Ids
|
---|
| 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
|
---|
| 41 |
|
---|
| 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
|
---|
| 47 |
|
---|
| 48 | #define SERIAL_PORT_ID_MASK 0x0F
|
---|
| 49 | #define MAX_SERIAL_PORTS 8
|
---|
| 50 | #define SERIAL_READ_ARR_LEN 12
|
---|
| 51 |
|
---|
| 52 | // map configuration query response resolution value to serial pin type
|
---|
| 53 | #define RES_RX1 0x02
|
---|
| 54 | #define RES_TX1 0x03
|
---|
| 55 | #define RES_RX2 0x04
|
---|
| 56 | #define RES_TX2 0x05
|
---|
| 57 | #define RES_RX3 0x06
|
---|
| 58 | #define RES_TX3 0x07
|
---|
| 59 |
|
---|
| 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
|
---|
| 68 |
|
---|
| 69 | // Serial read modes
|
---|
| 70 | #define SERIAL_READ_CONTINUOUSLY 0x00
|
---|
| 71 | #define SERIAL_STOP_READING 0x01
|
---|
| 72 | #define SERIAL_MODE_MASK 0xF0
|
---|
| 73 |
|
---|
| 74 | namespace {
|
---|
| 75 |
|
---|
| 76 | struct serial_pins {
|
---|
| 77 | uint8_t rx;
|
---|
| 78 | uint8_t tx;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | /*
|
---|
| 82 | * Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin.
|
---|
| 83 | */
|
---|
| 84 | inline uint8_t getSerialPinType(uint8_t pin) {
|
---|
| 85 | #if defined(PIN_SERIAL_RX)
|
---|
| 86 | // TODO when use of HW_SERIAL0 is enabled
|
---|
| 87 | #endif
|
---|
| 88 | #if defined(PIN_SERIAL1_RX)
|
---|
| 89 | if (pin == PIN_SERIAL1_RX) return RES_RX1;
|
---|
| 90 | if (pin == PIN_SERIAL1_TX) return RES_TX1;
|
---|
| 91 | #endif
|
---|
| 92 | #if defined(PIN_SERIAL2_RX)
|
---|
| 93 | if (pin == PIN_SERIAL2_RX) return RES_RX2;
|
---|
| 94 | if (pin == PIN_SERIAL2_TX) return RES_TX2;
|
---|
| 95 | #endif
|
---|
| 96 | #if defined(PIN_SERIAL3_RX)
|
---|
| 97 | if (pin == PIN_SERIAL3_RX) return RES_RX3;
|
---|
| 98 | if (pin == PIN_SERIAL3_TX) return RES_TX3;
|
---|
| 99 | #endif
|
---|
| 100 | return 0;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /*
|
---|
| 104 | * Get the RX and TX pins numbers for the specified HW serial port.
|
---|
| 105 | */
|
---|
| 106 | inline serial_pins getSerialPinNumbers(uint8_t portId) {
|
---|
| 107 | serial_pins pins;
|
---|
| 108 | switch (portId) {
|
---|
| 109 | #if defined(PIN_SERIAL_RX)
|
---|
| 110 | // case HW_SERIAL0:
|
---|
| 111 | // // TODO when use of HW_SERIAL0 is enabled
|
---|
| 112 | // break;
|
---|
| 113 | #endif
|
---|
| 114 | #if defined(PIN_SERIAL1_RX)
|
---|
| 115 | case HW_SERIAL1:
|
---|
| 116 | pins.rx = PIN_SERIAL1_RX;
|
---|
| 117 | pins.tx = PIN_SERIAL1_TX;
|
---|
| 118 | break;
|
---|
| 119 | #endif
|
---|
| 120 | #if defined(PIN_SERIAL2_RX)
|
---|
| 121 | case HW_SERIAL2:
|
---|
| 122 | pins.rx = PIN_SERIAL2_RX;
|
---|
| 123 | pins.tx = PIN_SERIAL2_TX;
|
---|
| 124 | break;
|
---|
| 125 | #endif
|
---|
| 126 | #if defined(PIN_SERIAL3_RX)
|
---|
| 127 | case HW_SERIAL3:
|
---|
| 128 | pins.rx = PIN_SERIAL3_RX;
|
---|
| 129 | pins.tx = PIN_SERIAL3_TX;
|
---|
| 130 | break;
|
---|
| 131 | #endif
|
---|
| 132 | default:
|
---|
| 133 | pins.rx = 0;
|
---|
| 134 | pins.tx = 0;
|
---|
| 135 | }
|
---|
| 136 | return pins;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | } // end namespace
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | class SerialFirmata: public FirmataFeature
|
---|
| 143 | {
|
---|
| 144 | public:
|
---|
| 145 | SerialFirmata();
|
---|
| 146 | boolean handlePinMode(byte pin, int mode);
|
---|
| 147 | void handleCapability(byte pin);
|
---|
| 148 | boolean handleSysex(byte command, byte argc, byte* argv);
|
---|
| 149 | void update();
|
---|
| 150 | void reset();
|
---|
| 151 | void checkSerial();
|
---|
| 152 |
|
---|
| 153 | private:
|
---|
| 154 | byte reportSerial[MAX_SERIAL_PORTS];
|
---|
| 155 | int serialBytesToRead[SERIAL_READ_ARR_LEN];
|
---|
| 156 | signed char serialIndex;
|
---|
| 157 |
|
---|
| 158 | Stream *swSerial0;
|
---|
| 159 | Stream *swSerial1;
|
---|
| 160 | Stream *swSerial2;
|
---|
| 161 | Stream *swSerial3;
|
---|
| 162 |
|
---|
| 163 | Stream* getPortFromId(byte portId);
|
---|
| 164 |
|
---|
| 165 | };
|
---|
| 166 |
|
---|
| 167 | #endif /* SerialFirmata_h */
|
---|