source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/CP_Firmata.h @ 3

Last change on this file since 3 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 10.1 KB
Line 
1/*
2  Firmata.h - Firmata library v2.5.3 - 2016-06-18
3  Copyright (c) 2006-2008 Hans-Christoph Steiner.  All rights reserved.
4  Copyright (C) 2009-2015 Jeff Hoefs.  All rights reserved.
5
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10
11  See file LICENSE.txt for further informations on licensing terms.
12*/
13
14#ifndef Firmata_h
15#define Firmata_h
16
17#include "CP_Boards.h"  /* Hardware Abstraction Layer + Wiring/Arduino */
18
19/* Version numbers for the protocol.  The protocol is still changing, so these
20 * version numbers are important.
21 * Query using the REPORT_VERSION message.
22 */
23#define FIRMATA_PROTOCOL_MAJOR_VERSION  2 // for non-compatible changes
24#define FIRMATA_PROTOCOL_MINOR_VERSION  5 // for backwards compatible changes
25#define FIRMATA_PROTOCOL_BUGFIX_VERSION 1 // for bugfix releases
26
27/* Version numbers for the Firmata library.
28 * The firmware version will not always equal the protocol version going forward.
29 * Query using the REPORT_FIRMWARE message.
30 */
31#define FIRMATA_FIRMWARE_MAJOR_VERSION  2
32#define FIRMATA_FIRMWARE_MINOR_VERSION  5
33#define FIRMATA_FIRMWARE_BUGFIX_VERSION 3
34
35/* DEPRECATED as of Firmata v2.5.1. As of 2.5.1 there are separate version numbers for
36 * the protocol version and the firmware version.
37 */
38#define FIRMATA_MAJOR_VERSION           2 // same as FIRMATA_PROTOCOL_MAJOR_VERSION
39#define FIRMATA_MINOR_VERSION           5 // same as FIRMATA_PROTOCOL_MINOR_VERSION
40#define FIRMATA_BUGFIX_VERSION          1 // same as FIRMATA_PROTOCOL_BUGFIX_VERSION
41
42#define MAX_DATA_BYTES                  64 // max number of data bytes in incoming messages
43
44// Arduino 101 also defines SET_PIN_MODE as a macro in scss_registers.h
45#ifdef SET_PIN_MODE
46#undef SET_PIN_MODE
47#endif
48
49// message command bytes (128-255/0x80-0xFF)
50#define DIGITAL_MESSAGE         0x90 // send data for a digital port (collection of 8 pins)
51#define ANALOG_MESSAGE          0xE0 // send data for an analog pin (or PWM)
52#define REPORT_ANALOG           0xC0 // enable analog input by pin #
53#define REPORT_DIGITAL          0xD0 // enable digital input by port pair
54//
55#define SET_PIN_MODE            0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
56#define SET_DIGITAL_PIN_VALUE   0xF5 // set value of an individual digital pin
57//
58#define REPORT_VERSION          0xF9 // report protocol version
59#define SYSTEM_RESET            0xFF // reset from MIDI
60//
61#define START_SYSEX             0xF0 // start a MIDI Sysex message
62#define END_SYSEX               0xF7 // end a MIDI Sysex message
63
64// extended command set using sysex (0-127/0x00-0x7F)
65/* 0x00-0x0F reserved for user-defined commands */
66#define SERIAL_MESSAGE          0x60 // communicate with serial devices, including other boards
67#define ENCODER_DATA            0x61 // reply with encoders current positions
68#define SERVO_CONFIG            0x70 // set max angle, minPulse, maxPulse, freq
69#define STRING_DATA             0x71 // a string message with 14-bits per char
70#define STEPPER_DATA            0x72 // control a stepper motor
71#define ONEWIRE_DATA            0x73 // send an OneWire read/write/reset/select/skip/search request
72#define SHIFT_DATA              0x75 // a bitstream to/from a shift register
73#define I2C_REQUEST             0x76 // send an I2C read/write request
74#define I2C_REPLY               0x77 // a reply to an I2C read request
75#define I2C_CONFIG              0x78 // config I2C settings such as delay times and power pins
76#define EXTENDED_ANALOG         0x6F // analog write (PWM, Servo, etc) to any pin
77#define PIN_STATE_QUERY         0x6D // ask for a pin's current mode and value
78#define PIN_STATE_RESPONSE      0x6E // reply with pin's current mode and value
79#define CAPABILITY_QUERY        0x6B // ask for supported modes and resolution of all pins
80#define CAPABILITY_RESPONSE     0x6C // reply with supported modes and resolution
81#define ANALOG_MAPPING_QUERY    0x69 // ask for mapping of analog to pin numbers
82#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
83#define REPORT_FIRMWARE         0x79 // report name and version of the firmware
84#define SAMPLING_INTERVAL       0x7A // set the poll rate of the main loop
85#define SCHEDULER_DATA          0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
86#define SYSEX_NON_REALTIME      0x7E // MIDI Reserved for non-realtime messages
87#define SYSEX_REALTIME          0x7F // MIDI Reserved for realtime messages
88// these are DEPRECATED to make the naming more consistent
89#define FIRMATA_STRING          0x71 // same as STRING_DATA
90#define SYSEX_I2C_REQUEST       0x76 // same as I2C_REQUEST
91#define SYSEX_I2C_REPLY         0x77 // same as I2C_REPLY
92#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
93
94// pin modes
95//#define INPUT                 0x00 // defined in Arduino.h
96//#define OUTPUT                0x01 // defined in Arduino.h
97#define PIN_MODE_ANALOG         0x02 // analog pin in analogInput mode
98#define PIN_MODE_PWM            0x03 // digital pin in PWM output mode
99#define PIN_MODE_SERVO          0x04 // digital pin in Servo output mode
100#define PIN_MODE_SHIFT          0x05 // shiftIn/shiftOut mode
101#define PIN_MODE_I2C            0x06 // pin included in I2C setup
102#define PIN_MODE_ONEWIRE        0x07 // pin configured for 1-wire
103#define PIN_MODE_STEPPER        0x08 // pin configured for stepper motor
104#define PIN_MODE_ENCODER        0x09 // pin configured for rotary encoders
105#define PIN_MODE_SERIAL         0x0A // pin configured for serial communication
106#define PIN_MODE_PULLUP         0x0B // enable internal pull-up resistor for pin
107#define PIN_MODE_IGNORE         0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
108#define TOTAL_PIN_MODES         13
109// DEPRECATED as of Firmata v2.5
110#define ANALOG                  0x02 // same as PIN_MODE_ANALOG
111#define PWM                     0x03 // same as PIN_MODE_PWM
112#define SERVO                   0x04 // same as PIN_MODE_SERVO
113#define SHIFT                   0x05 // same as PIN_MODE_SHIFT
114#define I2C                     0x06 // same as PIN_MODE_I2C
115#define ONEWIRE                 0x07 // same as PIN_MODE_ONEWIRE
116#define STEPPER                 0x08 // same as PIN_MODE_STEPPER
117#define ENCODER                 0x09 // same as PIN_MODE_ENCODER
118#define IGNORE                  0x7F // same as PIN_MODE_IGNORE
119
120extern "C" {
121  // callback function types
122  typedef void (*callbackFunction)(byte, int);
123  typedef void (*systemResetCallbackFunction)(void);
124  typedef void (*stringCallbackFunction)(char *);
125  typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv);
126}
127
128// TODO make it a subclass of a generic Serial/Stream base class
129class FirmataClass
130{
131  public:
132    FirmataClass();
133    /* Arduino constructors */
134    void begin();
135    void begin(long);
136    void begin(Stream &s);
137    /* querying functions */
138    void printVersion(void);
139    void blinkVersion(void);
140    void printFirmwareVersion(void);
141    //void setFirmwareVersion(byte major, byte minor);  // see macro below
142    void setFirmwareNameAndVersion(const char *name, byte major, byte minor);
143    void disableBlinkVersion();
144    /* serial receive handling */
145    int available(void);
146    void processInput(void);
147    void parse(unsigned char value);
148    boolean isParsingMessage(void);
149    /* serial send handling */
150    void sendAnalog(byte pin, int value);
151    void sendDigital(byte pin, int value); // TODO implement this
152    void sendDigitalPort(byte portNumber, int portData);
153    void sendString(const char *string);
154    void sendString(byte command, const char *string);
155    void sendSysex(byte command, byte bytec, byte *bytev);
156    void write(byte c);
157    /* attach & detach callback functions to messages */
158    void attach(byte command, callbackFunction newFunction);
159    void attach(byte command, systemResetCallbackFunction newFunction);
160    void attach(byte command, stringCallbackFunction newFunction);
161    void attach(byte command, sysexCallbackFunction newFunction);
162    void detach(byte command);
163
164    /* access pin state and config */
165    byte getPinMode(byte pin);
166    void setPinMode(byte pin, byte config);
167    /* access pin state */
168    int getPinState(byte pin);
169    void setPinState(byte pin, int state);
170
171    /* utility methods */
172    void sendValueAsTwo7bitBytes(int value);
173    void startSysex(void);
174    void endSysex(void);
175
176  private:
177    Stream *FirmataStream;
178    /* firmware name and version */
179    byte firmwareVersionCount;
180    byte *firmwareVersionVector;
181    /* input message handling */
182    byte waitForData; // this flag says the next serial input will be data
183    byte executeMultiByteCommand; // execute this after getting multi-byte data
184    byte multiByteChannel; // channel data for multiByteCommands
185    byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
186    /* sysex */
187    boolean parsingSysex;
188    int sysexBytesRead;
189    /* pin configuration */
190    byte pinConfig[TOTAL_PINS];
191    int pinState[TOTAL_PINS];
192
193    /* callback functions */
194    callbackFunction currentAnalogCallback;
195    callbackFunction currentDigitalCallback;
196    callbackFunction currentReportAnalogCallback;
197    callbackFunction currentReportDigitalCallback;
198    callbackFunction currentPinModeCallback;
199    callbackFunction currentPinValueCallback;
200    systemResetCallbackFunction currentSystemResetCallback;
201    stringCallbackFunction currentStringCallback;
202    sysexCallbackFunction currentSysexCallback;
203
204    boolean blinkVersionDisabled;
205
206    /* private methods ------------------------------ */
207    void processSysexMessage(void);
208    void systemReset(void);
209    void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval);
210};
211
212extern FirmataClass Firmata;
213
214/*==============================================================================
215 * MACROS
216 *============================================================================*/
217
218/* shortcut for setFirmwareNameAndVersion() that uses __FILE__ to set the
219 * firmware name.  It needs to be a macro so that __FILE__ is included in the
220 * firmware source file rather than the library source file.
221 */
222#define setFirmwareVersion(x, y)   setFirmwareNameAndVersion(__FILE__, x, y)
223
224#endif /* Firmata_h */
Note: See TracBrowser for help on using the repository browser.