root / arduino-1.0 / libraries / Firmata / Firmata.h @ 58d82c77
History | View | Annotate | Download (7.08 KB)
1 | 58d82c77 | Tom Mullins | /*
|
---|---|---|---|
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 1 // 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(Stream &s); |
||
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 | /* serial receive handling */
|
||
102 | int available(void); |
||
103 | void processInput(void); |
||
104 | /* serial send handling */
|
||
105 | void sendAnalog(byte pin, int value); |
||
106 | void sendDigital(byte pin, int value); // TODO implement this |
||
107 | void sendDigitalPort(byte portNumber, int portData); |
||
108 | void sendString(const char* string); |
||
109 | void sendString(byte command, const char* string); |
||
110 | void sendSysex(byte command, byte bytec, byte* bytev);
|
||
111 | /* attach & detach callback functions to messages */
|
||
112 | void attach(byte command, callbackFunction newFunction);
|
||
113 | void attach(byte command, systemResetCallbackFunction newFunction);
|
||
114 | void attach(byte command, stringCallbackFunction newFunction);
|
||
115 | void attach(byte command, sysexCallbackFunction newFunction);
|
||
116 | void detach(byte command);
|
||
117 | |||
118 | private:
|
||
119 | Stream &FirmataSerial; |
||
120 | /* firmware name and version */
|
||
121 | byte firmwareVersionCount; |
||
122 | byte *firmwareVersionVector; |
||
123 | /* input message handling */
|
||
124 | byte waitForData; // this flag says the next serial input will be data
|
||
125 | byte executeMultiByteCommand; // execute this after getting multi-byte data
|
||
126 | byte multiByteChannel; // channel data for multiByteCommands
|
||
127 | byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
|
||
128 | /* sysex */
|
||
129 | boolean parsingSysex; |
||
130 | int sysexBytesRead;
|
||
131 | /* callback functions */
|
||
132 | callbackFunction currentAnalogCallback; |
||
133 | callbackFunction currentDigitalCallback; |
||
134 | callbackFunction currentReportAnalogCallback; |
||
135 | callbackFunction currentReportDigitalCallback; |
||
136 | callbackFunction currentPinModeCallback; |
||
137 | systemResetCallbackFunction currentSystemResetCallback; |
||
138 | stringCallbackFunction currentStringCallback; |
||
139 | sysexCallbackFunction currentSysexCallback; |
||
140 | |||
141 | /* private methods ------------------------------ */
|
||
142 | void processSysexMessage(void); |
||
143 | void systemReset(void); |
||
144 | void pin13strobe(int count, int onInterval, int offInterval); |
||
145 | void sendValueAsTwo7bitBytes(int value); |
||
146 | void startSysex(void); |
||
147 | void endSysex(void); |
||
148 | }; |
||
149 | |||
150 | extern FirmataClass Firmata;
|
||
151 | |||
152 | /*==============================================================================
|
||
153 | * MACROS
|
||
154 | *============================================================================*/
|
||
155 | |||
156 | /* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the
|
||
157 | * firmware name. It needs to be a macro so that __FILE__ is included in the
|
||
158 | * firmware source file rather than the library source file.
|
||
159 | */
|
||
160 | #define setFirmwareVersion(x, y) setFirmwareNameAndVersion(__FILE__, x, y)
|
||
161 | |||
162 | #endif /* Firmata_h */ |