[136] | 1 | /*
|
---|
| 2 | Firmata.h - Firmata library
|
---|
| 3 | Copyright (C) 2006-2008 Hans-Christoph Steiner. 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 |
|
---|
| 13 | #ifndef Firmata_h
|
---|
| 14 | #define Firmata_h
|
---|
| 15 |
|
---|
| 16 | #include "Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */
|
---|
| 17 |
|
---|
| 18 | /* Version numbers for the protocol. The protocol is still changing, so these
|
---|
| 19 | * version numbers are important. This number can be queried so that host
|
---|
| 20 | * software can test whether it will be compatible with the currently
|
---|
| 21 | * installed firmware. */
|
---|
| 22 | #define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes
|
---|
| 23 | #define FIRMATA_MINOR_VERSION 3 // for backwards compatible changes
|
---|
| 24 | #define FIRMATA_BUGFIX_VERSION 6 // for bugfix releases
|
---|
| 25 |
|
---|
| 26 | #define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages
|
---|
| 27 |
|
---|
| 28 | // message command bytes (128-255/0x80-0xFF)
|
---|
| 29 | #define DIGITAL_MESSAGE 0x90 // send data for a digital pin
|
---|
| 30 | #define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM)
|
---|
| 31 | #define REPORT_ANALOG 0xC0 // enable analog input by pin #
|
---|
| 32 | #define REPORT_DIGITAL 0xD0 // enable digital input by port pair
|
---|
| 33 | //
|
---|
| 34 | #define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
|
---|
| 35 | //
|
---|
| 36 | #define REPORT_VERSION 0xF9 // report protocol version
|
---|
| 37 | #define SYSTEM_RESET 0xFF // reset from MIDI
|
---|
| 38 | //
|
---|
| 39 | #define START_SYSEX 0xF0 // start a MIDI Sysex message
|
---|
| 40 | #define END_SYSEX 0xF7 // end a MIDI Sysex message
|
---|
| 41 |
|
---|
| 42 | // extended command set using sysex (0-127/0x00-0x7F)
|
---|
| 43 | /* 0x00-0x0F reserved for user-defined commands */
|
---|
| 44 | #define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
|
---|
| 45 | #define STRING_DATA 0x71 // a string message with 14-bits per char
|
---|
| 46 | #define SHIFT_DATA 0x75 // a bitstream to/from a shift register
|
---|
| 47 | #define I2C_REQUEST 0x76 // send an I2C read/write request
|
---|
| 48 | #define I2C_REPLY 0x77 // a reply to an I2C read request
|
---|
| 49 | #define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins
|
---|
| 50 | #define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin
|
---|
| 51 | #define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value
|
---|
| 52 | #define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value
|
---|
| 53 | #define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins
|
---|
| 54 | #define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution
|
---|
| 55 | #define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers
|
---|
| 56 | #define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
|
---|
| 57 | #define REPORT_FIRMWARE 0x79 // report name and version of the firmware
|
---|
| 58 | #define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop
|
---|
| 59 | #define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
|
---|
| 60 | #define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages
|
---|
| 61 | // these are DEPRECATED to make the naming more consistent
|
---|
| 62 | #define FIRMATA_STRING 0x71 // same as STRING_DATA
|
---|
| 63 | #define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
|
---|
| 64 | #define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY
|
---|
| 65 | #define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
|
---|
| 66 |
|
---|
| 67 | // pin modes
|
---|
| 68 | //#define INPUT 0x00 // defined in wiring.h
|
---|
| 69 | //#define OUTPUT 0x01 // defined in wiring.h
|
---|
| 70 | #define ANALOG 0x02 // analog pin in analogInput mode
|
---|
| 71 | #define PWM 0x03 // digital pin in PWM output mode
|
---|
| 72 | #define SERVO 0x04 // digital pin in Servo output mode
|
---|
| 73 | #define SHIFT 0x05 // shiftIn/shiftOut mode
|
---|
| 74 | #define I2C 0x06 // pin included in I2C setup
|
---|
| 75 | #define TOTAL_PIN_MODES 7
|
---|
| 76 |
|
---|
| 77 | extern "C" {
|
---|
| 78 | // callback function types
|
---|
| 79 | typedef void (*callbackFunction)(byte, int);
|
---|
| 80 | typedef void (*systemResetCallbackFunction)(void);
|
---|
| 81 | typedef void (*stringCallbackFunction)(char*);
|
---|
| 82 | typedef void (*sysexCallbackFunction)(byte command, byte argc, byte*argv);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | // TODO make it a subclass of a generic Serial/Stream base class
|
---|
| 87 | class FirmataClass
|
---|
| 88 | {
|
---|
| 89 | public:
|
---|
| 90 | FirmataClass();
|
---|
| 91 | /* Arduino constructors */
|
---|
| 92 | void begin();
|
---|
| 93 | void begin(long);
|
---|
| 94 | void begin(Stream &s);
|
---|
| 95 | /* querying functions */
|
---|
| 96 | void printVersion(void);
|
---|
| 97 | void blinkVersion(void);
|
---|
| 98 | void printFirmwareVersion(void);
|
---|
| 99 | //void setFirmwareVersion(byte major, byte minor); // see macro below
|
---|
| 100 | void setFirmwareNameAndVersion(const char *name, byte major, byte minor);
|
---|
| 101 | //void unsetFirmwareVersion(); // only used for unit test
|
---|
| 102 | /* serial receive handling */
|
---|
| 103 | int available(void);
|
---|
| 104 | void processInput(void);
|
---|
| 105 | /* serial send handling */
|
---|
| 106 | void sendAnalog(byte pin, int value);
|
---|
| 107 | void sendDigital(byte pin, int value); // TODO implement this
|
---|
| 108 | void sendDigitalPort(byte portNumber, int portData);
|
---|
| 109 | void sendString(const char* string);
|
---|
| 110 | void sendString(byte command, const char* string);
|
---|
| 111 | void sendSysex(byte command, byte bytec, byte* bytev);
|
---|
| 112 | void write(byte c);
|
---|
| 113 | /* attach & detach callback functions to messages */
|
---|
| 114 | void attach(byte command, callbackFunction newFunction);
|
---|
| 115 | void attach(byte command, systemResetCallbackFunction newFunction);
|
---|
| 116 | void attach(byte command, stringCallbackFunction newFunction);
|
---|
| 117 | void attach(byte command, sysexCallbackFunction newFunction);
|
---|
| 118 | void detach(byte command);
|
---|
| 119 |
|
---|
| 120 | private:
|
---|
| 121 | Stream *FirmataSerial;
|
---|
| 122 | /* firmware name and version */
|
---|
| 123 | byte firmwareVersionCount;
|
---|
| 124 | byte *firmwareVersionVector;
|
---|
| 125 | /* input message handling */
|
---|
| 126 | byte waitForData; // this flag says the next serial input will be data
|
---|
| 127 | byte executeMultiByteCommand; // execute this after getting multi-byte data
|
---|
| 128 | byte multiByteChannel; // channel data for multiByteCommands
|
---|
| 129 | byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
|
---|
| 130 | /* sysex */
|
---|
| 131 | boolean parsingSysex;
|
---|
| 132 | int sysexBytesRead;
|
---|
| 133 | /* callback functions */
|
---|
| 134 | callbackFunction currentAnalogCallback;
|
---|
| 135 | callbackFunction currentDigitalCallback;
|
---|
| 136 | callbackFunction currentReportAnalogCallback;
|
---|
| 137 | callbackFunction currentReportDigitalCallback;
|
---|
| 138 | callbackFunction currentPinModeCallback;
|
---|
| 139 | systemResetCallbackFunction currentSystemResetCallback;
|
---|
| 140 | stringCallbackFunction currentStringCallback;
|
---|
| 141 | sysexCallbackFunction currentSysexCallback;
|
---|
| 142 |
|
---|
| 143 | /* private methods ------------------------------ */
|
---|
| 144 | void processSysexMessage(void);
|
---|
| 145 | void systemReset(void);
|
---|
| 146 | void strobeBlinkPin(int count, int onInterval, int offInterval);
|
---|
| 147 | void sendValueAsTwo7bitBytes(int value);
|
---|
| 148 | void startSysex(void);
|
---|
| 149 | void endSysex(void);
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | extern FirmataClass Firmata;
|
---|
| 153 |
|
---|
| 154 | /*==============================================================================
|
---|
| 155 | * MACROS
|
---|
| 156 | *============================================================================*/
|
---|
| 157 |
|
---|
| 158 | /* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the
|
---|
| 159 | * firmware name. It needs to be a macro so that __FILE__ is included in the
|
---|
| 160 | * firmware source file rather than the library source file.
|
---|
| 161 | */
|
---|
| 162 | #define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y)
|
---|
| 163 |
|
---|
| 164 | #endif /* Firmata_h */
|
---|
| 165 |
|
---|