D:/DRISSI/arduino-0022/arduino-0022/libraries/Firmata/Firmata.h
00001 /*
00002   Firmata.h - Firmata library
00003   Copyright (C) 2006-2008 Hans-Christoph Steiner.  All rights reserved.
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   See file LICENSE.txt for further informations on licensing terms.
00011 */
00012 
00013 #ifndef Firmata_h
00014 #define Firmata_h
00015 
00016 #include <WProgram.h>
00017 #include <inttypes.h>
00018 
00019 
00020 /* Version numbers for the protocol.  The protocol is still changing, so these
00021  * version numbers are important.  This number can be queried so that host
00022  * software can test whether it will be compatible with the currently
00023  * installed firmware. */
00024 #define FIRMATA_MAJOR_VERSION   2 // for non-compatible changes
00025 #define FIRMATA_MINOR_VERSION   2 // for backwards compatible changes
00026 
00027 #define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages
00028 
00029 // message command bytes (128-255/0x80-0xFF)
00030 #define DIGITAL_MESSAGE         0x90 // send data for a digital pin
00031 #define ANALOG_MESSAGE          0xE0 // send data for an analog pin (or PWM)
00032 #define REPORT_ANALOG           0xC0 // enable analog input by pin #
00033 #define REPORT_DIGITAL          0xD0 // enable digital input by port pair
00034 //
00035 #define SET_PIN_MODE            0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
00036 //
00037 #define REPORT_VERSION          0xF9 // report protocol version
00038 #define SYSTEM_RESET            0xFF // reset from MIDI
00039 //
00040 #define START_SYSEX             0xF0 // start a MIDI Sysex message
00041 #define END_SYSEX               0xF7 // end a MIDI Sysex message
00042 
00043 // extended command set using sysex (0-127/0x00-0x7F)
00044 /* 0x00-0x0F reserved for user-defined commands */
00045 #define SERVO_CONFIG            0x70 // set max angle, minPulse, maxPulse, freq
00046 #define STRING_DATA             0x71 // a string message with 14-bits per char
00047 #define SHIFT_DATA              0x75 // a bitstream to/from a shift register
00048 #define I2C_REQUEST             0x76 // send an I2C read/write request
00049 #define I2C_REPLY               0x77 // a reply to an I2C read request
00050 #define I2C_CONFIG              0x78 // config I2C settings such as delay times and power pins
00051 #define EXTENDED_ANALOG         0x6F // analog write (PWM, Servo, etc) to any pin
00052 #define PIN_STATE_QUERY         0x6D // ask for a pin's current mode and value
00053 #define PIN_STATE_RESPONSE      0x6E // reply with pin's current mode and value
00054 #define CAPABILITY_QUERY        0x6B // ask for supported modes and resolution of all pins
00055 #define CAPABILITY_RESPONSE     0x6C // reply with supported modes and resolution
00056 #define ANALOG_MAPPING_QUERY    0x69 // ask for mapping of analog to pin numbers
00057 #define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
00058 #define REPORT_FIRMWARE         0x79 // report name and version of the firmware
00059 #define SAMPLING_INTERVAL       0x7A // set the poll rate of the main loop
00060 #define SYSEX_NON_REALTIME      0x7E // MIDI Reserved for non-realtime messages
00061 #define SYSEX_REALTIME          0x7F // MIDI Reserved for realtime messages
00062 // these are DEPRECATED to make the naming more consistent
00063 #define FIRMATA_STRING          0x71 // same as STRING_DATA
00064 #define SYSEX_I2C_REQUEST       0x76 // same as I2C_REQUEST
00065 #define SYSEX_I2C_REPLY         0x77 // same as I2C_REPLY
00066 #define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
00067 
00068 // pin modes
00069 //#define INPUT                 0x00 // defined in wiring.h
00070 //#define OUTPUT                0x01 // defined in wiring.h
00071 #define ANALOG                  0x02 // analog pin in analogInput mode
00072 #define PWM                     0x03 // digital pin in PWM output mode
00073 #define SERVO                   0x04 // digital pin in Servo output mode
00074 #define SHIFT                   0x05 // shiftIn/shiftOut mode
00075 #define I2C                     0x06 // pin included in I2C setup
00076 #define TOTAL_PIN_MODES         7
00077 
00078 extern "C" {
00079 // callback function types
00080     typedef void (*callbackFunction)(byte, int);
00081     typedef void (*systemResetCallbackFunction)(void);
00082     typedef void (*stringCallbackFunction)(char*);
00083     typedef void (*sysexCallbackFunction)(byte command, byte argc, byte*argv);
00084 }
00085 
00086 
00087 // TODO make it a subclass of a generic Serial/Stream base class
00088 class FirmataClass
00089 {
00090 public:
00091         FirmataClass();
00092 /* Arduino constructors */
00093     void begin();
00094     void begin(long);
00095 /* querying functions */
00096         void printVersion(void);
00097     void blinkVersion(void);
00098     void printFirmwareVersion(void);
00099   //void setFirmwareVersion(byte major, byte minor);  // see macro below
00100     void setFirmwareNameAndVersion(const char *name, byte major, byte minor);
00101 /* serial receive handling */
00102     int available(void);
00103     void processInput(void);
00104 /* serial send handling */
00105         void sendAnalog(byte pin, int value);
00106         void sendDigital(byte pin, int value); // TODO implement this
00107         void sendDigitalPort(byte portNumber, int portData);
00108     void sendString(const char* string);
00109     void sendString(byte command, const char* string);
00110         void sendSysex(byte command, byte bytec, byte* bytev);
00111 /* attach & detach callback functions to messages */
00112     void attach(byte command, callbackFunction newFunction);
00113     void attach(byte command, systemResetCallbackFunction newFunction);
00114     void attach(byte command, stringCallbackFunction newFunction);
00115     void attach(byte command, sysexCallbackFunction newFunction);
00116     void detach(byte command);
00117 
00118 private:
00119 /* firmware name and version */
00120     byte firmwareVersionCount;
00121     byte *firmwareVersionVector;
00122 /* input message handling */
00123     byte waitForData; // this flag says the next serial input will be data
00124     byte executeMultiByteCommand; // execute this after getting multi-byte data
00125     byte multiByteChannel; // channel data for multiByteCommands
00126     byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
00127 /* sysex */
00128     boolean parsingSysex;
00129     int sysexBytesRead;
00130 /* callback functions */
00131     callbackFunction currentAnalogCallback;
00132     callbackFunction currentDigitalCallback;
00133     callbackFunction currentReportAnalogCallback;
00134     callbackFunction currentReportDigitalCallback;
00135     callbackFunction currentPinModeCallback;
00136     systemResetCallbackFunction currentSystemResetCallback;
00137     stringCallbackFunction currentStringCallback;
00138     sysexCallbackFunction currentSysexCallback;
00139 
00140 /* private methods ------------------------------ */
00141     void processSysexMessage(void);
00142         void systemReset(void);
00143     void pin13strobe(int count, int onInterval, int offInterval);
00144 };
00145 
00146 extern FirmataClass Firmata;
00147 
00148 /*==============================================================================
00149  * MACROS
00150  *============================================================================*/
00151 
00152 /* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the
00153  * firmware name.  It needs to be a macro so that __FILE__ is included in the
00154  * firmware source file rather than the library source file.
00155  */
00156 #define setFirmwareVersion(x, y)   setFirmwareNameAndVersion(__FILE__, x, y)
00157 
00158 /* Hardware Abstraction Layer */
00159 #include "Boards.h"
00160 
00161 #endif /* Firmata_h */
00162