2 Firmata.h - Firmata library
3 Copyright (C) 2006-2008 Hans-Christoph Steiner. 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.
20 /* Version numbers for the protocol. The protocol is still changing, so these
21 * version numbers are important. This number can be queried so that host
22 * software can test whether it will be compatible with the currently
23 * installed firmware. */
24 #define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes
25 #define FIRMATA_MINOR_VERSION 1 // for backwards compatible changes
27 #define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages
29 // message command bytes (128-255/0x80-0xFF)
30 #define DIGITAL_MESSAGE 0x90 // send data for a digital pin
31 #define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM)
32 #define REPORT_ANALOG 0xC0 // enable analog input by pin #
33 #define REPORT_DIGITAL 0xD0 // enable digital input by port pair
35 #define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
37 #define REPORT_VERSION 0xF9 // report protocol version
38 #define SYSTEM_RESET 0xFF // reset from MIDI
40 #define START_SYSEX 0xF0 // start a MIDI Sysex message
41 #define END_SYSEX 0xF7 // end a MIDI Sysex message
43 // extended command set using sysex (0-127/0x00-0x7F)
44 /* 0x00-0x0F reserved for user-defined commands */
45 #define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
46 #define STRING_DATA 0x71 // a string message with 14-bits per char
47 #define SHIFT_DATA 0x75 // a bitstream to/from a shift register
48 #define I2C_REQUEST 0x76 // send an I2C read/write request
49 #define I2C_REPLY 0x77 // a reply to an I2C read request
50 #define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins
51 #define REPORT_FIRMWARE 0x79 // report name and version of the firmware
52 #define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop
53 #define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
54 #define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages
55 // these are DEPRECATED to make the naming more consistent
56 #define FIRMATA_STRING 0x71 // same as STRING_DATA
57 #define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
58 #define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY
59 #define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
62 //#define INPUT 0x00 // defined in wiring.h
63 //#define OUTPUT 0x01 // defined in wiring.h
64 #define ANALOG 0x02 // analog pin in analogInput mode
65 #define PWM 0x03 // digital pin in PWM output mode
66 #define SERVO 0x04 // digital pin in Servo output mode
67 #define SHIFT 0x05 // shiftIn/shiftOut mode
68 #define I2C 0x06 // pin included in I2C setup
71 // callback function types
72 typedef void (*callbackFunction
)(byte
, int);
73 typedef void (*systemResetCallbackFunction
)(void);
74 typedef void (*stringCallbackFunction
)(char*);
75 typedef void (*sysexCallbackFunction
)(byte command
, byte argc
, byte
*argv
);
79 // TODO make it a subclass of HardwareSerial
84 /* Arduino constructors */
87 /* querying functions */
88 void printVersion(void);
89 void blinkVersion(void);
90 void printFirmwareVersion(void);
91 // void setFirmwareVersion(byte major, byte minor); // see macro below
92 void setFirmwareNameAndVersion(const char *name
, byte major
, byte minor
);
93 /* serial receive handling */
95 void processInput(void);
96 /* serial send handling */
97 void sendAnalog(byte pin
, int value
);
98 void sendDigital(byte pin
, int value
);
99 void sendDigitalPort(byte portNumber
, int portData
);
100 void sendString(const char* string
);
101 void sendString(byte command
, const char* string
);
102 void sendSysex(byte command
, byte bytec
, byte
* bytev
);
103 // void print(); // TODO implement so it's compatible to Serial
104 // void println(); // TODO implement so it's compatible to Serial
105 /* attach & detach callback functions to messages */
106 void attach(byte command
, callbackFunction newFunction
);
107 void attach(byte command
, systemResetCallbackFunction newFunction
);
108 void attach(byte command
, stringCallbackFunction newFunction
);
109 void attach(byte command
, sysexCallbackFunction newFunction
);
110 void detach(byte command
);
111 // void flush(); // TODO implement flush, probably by subclassing
114 /* firmware name and version */
115 byte firmwareVersionCount
;
116 byte
*firmwareVersionVector
;
117 /* input message handling */
118 byte waitForData
; // this flag says the next serial input will be data
119 byte executeMultiByteCommand
; // execute this after getting multi-byte data
120 byte multiByteChannel
; // channel data for multiByteCommands
121 byte storedInputData
[MAX_DATA_BYTES
]; // multi-byte data
123 boolean parsingSysex
;
125 /* callback functions */
126 callbackFunction currentAnalogCallback
;
127 callbackFunction currentDigitalCallback
;
128 callbackFunction currentReportAnalogCallback
;
129 callbackFunction currentReportDigitalCallback
;
130 callbackFunction currentPinModeCallback
;
131 systemResetCallbackFunction currentSystemResetCallback
;
132 stringCallbackFunction currentStringCallback
;
133 sysexCallbackFunction currentSysexCallback
;
135 /* private methods ------------------------------ */
136 void processSysexMessage(void);
137 void systemReset(void);
138 void pin13strobe(int count
, int onInterval
, int offInterval
);
141 extern FirmataClass Firmata
;
143 /*==============================================================================
145 *============================================================================*/
147 /* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the
148 * firmware name. It needs to be a macro so that __FILE__ is included in the
149 * firmware source file rather than the library source file.
151 #define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y)
153 // total number of pins currently supported
154 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Arduino NG and Diecimila
155 #define TOTAL_ANALOG_PINS 8
156 #define TOTAL_DIGITAL_PINS 22 // 14 digital + 8 analog
157 #define TOTAL_PORTS 3 // total number of ports for the board
158 #define ANALOG_PORT 2 // port# of analog used as digital
159 #define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0
160 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
161 #elif defined(__AVR_ATmega8__) // old Arduinos
162 #define TOTAL_ANALOG_PINS 6
163 #define TOTAL_DIGITAL_PINS 20 // 14 digital + 6 analog
164 #define TOTAL_PORTS 3 // total number of ports for the board
165 #define ANALOG_PORT 2 // port# of analog used as digital
166 #define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0
167 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
168 #elif defined(__AVR_ATmega1280__)// Arduino Mega
169 #define TOTAL_ANALOG_PINS 16
170 #define TOTAL_DIGITAL_PINS 70 // 54 digital + 16 analog
171 #define TOTAL_PORTS 9 // total number of ports for the board
172 #define ANALOG_PORT 8 // port# of analog used as digital
173 #define FIRST_ANALOG_PIN 54 // pin# corresponding to analog 0
174 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
175 #elif defined(__AVR_ATmega128__)// Wiring
176 #define TOTAL_ANALOG_PINS 8
177 #define TOTAL_DIGITAL_PINS 51
178 #define TOTAL_PORTS 7 // total number of ports for the board
179 #define ANALOG_PORT 5 // port# of analog used as digital
180 #define FIRST_ANALOG_PIN 40 // pin# corresponding to analog 0
181 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
182 #elif defined(__AVR_AT90USB162__) // Teensy
183 #define TOTAL_ANALOG_PINS 0
184 #define TOTAL_DIGITAL_PINS 21 // 21 digital + no analog
185 #define TOTAL_PORTS 4 // total number of ports for the board
186 #define ANALOG_PORT 3 // port# of analog used as digital
187 #define FIRST_ANALOG_PIN 21 // pin# corresponding to analog 0
188 #define VERSION_BLINK_PIN 6 // digital pin to blink version on
189 #elif defined(__AVR_ATmega32U4__) // Teensy
190 #define TOTAL_ANALOG_PINS 12
191 #define TOTAL_DIGITAL_PINS 25 // 11 digital + 12 analog
192 #define TOTAL_PORTS 4 // total number of ports for the board
193 #define ANALOG_PORT 3 // port# of analog used as digital
194 #define FIRST_ANALOG_PIN 11 // pin# corresponding to analog 0
195 #define VERSION_BLINK_PIN 11 // digital pin to blink version on
196 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) // Teensy++
197 #define TOTAL_ANALOG_PINS 8
198 #define TOTAL_DIGITAL_PINS 46 // 38 digital + 8 analog
199 #define TOTAL_PORTS 6 // total number of ports for the board
200 #define ANALOG_PORT 5 // port# of analog used as digital
201 #define FIRST_ANALOG_PIN 38 // pin# corresponding to analog 0
202 #define VERSION_BLINK_PIN 6 // digital pin to blink version on
203 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) // Sanguino
204 #define TOTAL_ANALOG_PINS 8
205 #define TOTAL_DIGITAL_PINS 32 // 24 digital + 8 analog
206 #define TOTAL_PORTS 4 // total number of ports for the board
207 #define ANALOG_PORT 3 // port# of analog used as digital
208 #define FIRST_ANALOG_PIN 24 // pin# corresponding to analog 0
209 #define VERSION_BLINK_PIN 0 // digital pin to blink version on
210 #elif defined(__AVR_ATmega645__) // Illuminato
211 #define TOTAL_ANALOG_PINS 6
212 #define TOTAL_DIGITAL_PINS 42 // 36 digital + 6 analog
213 #define TOTAL_PORTS 6 // total number of ports for the board
214 #define ANALOG_PORT 4 // port# of analog used as digital
215 #define FIRST_ANALOG_PIN 36 // pin# corresponding to analog 0
216 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
217 #else // anything else
218 #define TOTAL_ANALOG_PINS 6
219 #define TOTAL_DIGITAL_PINS 14
220 #define TOTAL_PORTS 3 // total number of ports for the board
221 #define ANALOG_PORT 2 // port# of analog used as digital
222 #define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0
223 #define VERSION_BLINK_PIN 13 // digital pin to blink version on
228 #endif /* Firmata_h */