[224] | 1 | /*
|
---|
| 2 | Firmata.cpp - Firmata library v2.5.2 - 2016-2-15
|
---|
| 3 | Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
|
---|
| 4 | Copyright (C) 2009-2016 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 | //******************************************************************************
|
---|
| 15 | //* Includes
|
---|
| 16 | //******************************************************************************
|
---|
| 17 |
|
---|
| 18 | #include "Firmata.h"
|
---|
| 19 | #include "HardwareSerial.h"
|
---|
| 20 |
|
---|
| 21 | extern "C" {
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | //******************************************************************************
|
---|
| 27 | //* Support Functions
|
---|
| 28 | //******************************************************************************
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Split a 16-bit byte into two 7-bit values and write each value.
|
---|
| 32 | * @param value The 16-bit value to be split and written separately.
|
---|
| 33 | */
|
---|
| 34 | void FirmataClass::sendValueAsTwo7bitBytes(int value)
|
---|
| 35 | {
|
---|
| 36 | FirmataStream->write(value & 0x7F); // LSB
|
---|
| 37 | FirmataStream->write(value >> 7 & 0x7F); // MSB
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * A helper method to write the beginning of a Sysex message transmission.
|
---|
| 42 | */
|
---|
| 43 | void FirmataClass::startSysex(void)
|
---|
| 44 | {
|
---|
| 45 | FirmataStream->write(START_SYSEX);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * A helper method to write the end of a Sysex message transmission.
|
---|
| 50 | */
|
---|
| 51 | void FirmataClass::endSysex(void)
|
---|
| 52 | {
|
---|
| 53 | FirmataStream->write(END_SYSEX);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | //******************************************************************************
|
---|
| 57 | //* Constructors
|
---|
| 58 | //******************************************************************************
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * The Firmata class.
|
---|
| 62 | * An instance named "Firmata" is created automatically for the user.
|
---|
| 63 | */
|
---|
| 64 | FirmataClass::FirmataClass()
|
---|
| 65 | {
|
---|
| 66 | firmwareVersionCount = 0;
|
---|
| 67 | firmwareVersionVector = 0;
|
---|
| 68 | systemReset();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | //******************************************************************************
|
---|
| 72 | //* Public Methods
|
---|
| 73 | //******************************************************************************
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Initialize the default Serial transport at the default baud of 57600.
|
---|
| 77 | */
|
---|
| 78 | void FirmataClass::begin(void)
|
---|
| 79 | {
|
---|
| 80 | begin(57600);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Initialize the default Serial transport and override the default baud.
|
---|
| 85 | * Sends the protocol version to the host application followed by the firmware version and name.
|
---|
| 86 | * blinkVersion is also called. To skip the call to blinkVersion, call Firmata.disableBlinkVersion()
|
---|
| 87 | * before calling Firmata.begin(baud).
|
---|
| 88 | * @param speed The baud to use. 57600 baud is the default value.
|
---|
| 89 | */
|
---|
| 90 | void FirmataClass::begin(long speed)
|
---|
| 91 | {
|
---|
| 92 | Serial.begin(speed);
|
---|
| 93 | FirmataStream = &Serial;
|
---|
| 94 | blinkVersion();
|
---|
| 95 | printVersion(); // send the protocol version
|
---|
| 96 | printFirmwareVersion(); // send the firmware name and version
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Reassign the Firmata stream transport.
|
---|
| 101 | * @param s A reference to the Stream transport object. This can be any type of
|
---|
| 102 | * transport that implements the Stream interface. Some examples include Ethernet, WiFi
|
---|
| 103 | * and other UARTs on the board (Serial1, Serial2, etc).
|
---|
| 104 | */
|
---|
| 105 | void FirmataClass::begin(Stream &s)
|
---|
| 106 | {
|
---|
| 107 | FirmataStream = &s;
|
---|
| 108 | // do not call blinkVersion() here because some hardware such as the
|
---|
| 109 | // Ethernet shield use pin 13
|
---|
| 110 | printVersion();
|
---|
| 111 | printFirmwareVersion();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Send the Firmata protocol version to the Firmata host application.
|
---|
| 116 | */
|
---|
| 117 | void FirmataClass::printVersion(void)
|
---|
| 118 | {
|
---|
| 119 | FirmataStream->write(REPORT_VERSION);
|
---|
| 120 | FirmataStream->write(FIRMATA_PROTOCOL_MAJOR_VERSION);
|
---|
| 121 | FirmataStream->write(FIRMATA_PROTOCOL_MINOR_VERSION);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * Blink the Firmata protocol version to the onboard LEDs (if the board has an onboard LED).
|
---|
| 126 | * If VERSION_BLINK_PIN is not defined in Boards.h for a particular board, then this method
|
---|
| 127 | * does nothing.
|
---|
| 128 | * The first series of flashes indicates the firmware major version (2 flashes = 2).
|
---|
| 129 | * The second series of flashes indicates the firmware minor version (5 flashes = 5).
|
---|
| 130 | */
|
---|
| 131 | void FirmataClass::blinkVersion(void)
|
---|
| 132 | {
|
---|
| 133 | #if defined(VERSION_BLINK_PIN)
|
---|
| 134 | if (blinkVersionDisabled) return;
|
---|
| 135 | // flash the pin with the protocol version
|
---|
| 136 | pinMode(VERSION_BLINK_PIN, OUTPUT);
|
---|
| 137 | strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MAJOR_VERSION, 40, 210);
|
---|
| 138 | delay(250);
|
---|
| 139 | strobeBlinkPin(VERSION_BLINK_PIN, FIRMATA_FIRMWARE_MINOR_VERSION, 40, 210);
|
---|
| 140 | delay(125);
|
---|
| 141 | #endif
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Provides a means to disable the version blink sequence on the onboard LED, trimming startup
|
---|
| 146 | * time by a couple of seconds.
|
---|
| 147 | * Call this before Firmata.begin(). It only applies when using the default Serial transport.
|
---|
| 148 | */
|
---|
| 149 | void FirmataClass::disableBlinkVersion()
|
---|
| 150 | {
|
---|
| 151 | blinkVersionDisabled = true;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Sends the firmware name and version to the Firmata host application. The major and minor version
|
---|
| 156 | * numbers are the first 2 bytes in the message. The following bytes are the characters of the
|
---|
| 157 | * firmware name.
|
---|
| 158 | */
|
---|
| 159 | void FirmataClass::printFirmwareVersion(void)
|
---|
| 160 | {
|
---|
| 161 | byte i;
|
---|
| 162 |
|
---|
| 163 | if (firmwareVersionCount) { // make sure that the name has been set before reporting
|
---|
| 164 | startSysex();
|
---|
| 165 | FirmataStream->write(REPORT_FIRMWARE);
|
---|
| 166 | FirmataStream->write(firmwareVersionVector[0]); // major version number
|
---|
| 167 | FirmataStream->write(firmwareVersionVector[1]); // minor version number
|
---|
| 168 | for (i = 2; i < firmwareVersionCount; ++i) {
|
---|
| 169 | sendValueAsTwo7bitBytes(firmwareVersionVector[i]);
|
---|
| 170 | }
|
---|
| 171 | endSysex();
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Sets the name and version of the firmware. This is not the same version as the Firmata protocol
|
---|
| 177 | * (although at times the firmware version and protocol version may be the same number).
|
---|
| 178 | * @param name A pointer to the name char array
|
---|
| 179 | * @param major The major version number
|
---|
| 180 | * @param minor The minor version number
|
---|
| 181 | */
|
---|
| 182 | void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte minor)
|
---|
| 183 | {
|
---|
| 184 | const char *firmwareName;
|
---|
| 185 | const char *extension;
|
---|
| 186 |
|
---|
| 187 | // parse out ".cpp" and "applet/" that comes from using __FILE__
|
---|
| 188 | extension = strstr(name, ".cpp");
|
---|
| 189 | firmwareName = strrchr(name, '/');
|
---|
| 190 |
|
---|
| 191 | if (!firmwareName) {
|
---|
| 192 | // windows
|
---|
| 193 | firmwareName = strrchr(name, '\\');
|
---|
| 194 | }
|
---|
| 195 | if (!firmwareName) {
|
---|
| 196 | // user passed firmware name
|
---|
| 197 | firmwareName = name;
|
---|
| 198 | } else {
|
---|
| 199 | firmwareName ++;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | if (!extension) {
|
---|
| 203 | firmwareVersionCount = strlen(firmwareName) + 2;
|
---|
| 204 | } else {
|
---|
| 205 | firmwareVersionCount = extension - firmwareName + 2;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // in case anyone calls setFirmwareNameAndVersion more than once
|
---|
| 209 | free(firmwareVersionVector);
|
---|
| 210 |
|
---|
| 211 | firmwareVersionVector = (byte *) malloc(firmwareVersionCount + 1);
|
---|
| 212 | firmwareVersionVector[firmwareVersionCount] = 0;
|
---|
| 213 | firmwareVersionVector[0] = major;
|
---|
| 214 | firmwareVersionVector[1] = minor;
|
---|
| 215 | strncpy((char *)firmwareVersionVector + 2, firmwareName, firmwareVersionCount - 2);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | //------------------------------------------------------------------------------
|
---|
| 219 | // Serial Receive Handling
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * A wrapper for Stream::available()
|
---|
| 223 | * @return The number of bytes remaining in the input stream buffer.
|
---|
| 224 | */
|
---|
| 225 | int FirmataClass::available(void)
|
---|
| 226 | {
|
---|
| 227 | return FirmataStream->available();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /**
|
---|
| 231 | * Process incoming sysex messages. Handles REPORT_FIRMWARE and STRING_DATA internally.
|
---|
| 232 | * Calls callback function for STRING_DATA and all other sysex messages.
|
---|
| 233 | * @private
|
---|
| 234 | */
|
---|
| 235 | void FirmataClass::processSysexMessage(void)
|
---|
| 236 | {
|
---|
| 237 | switch (storedInputData[0]) { //first byte in buffer is command
|
---|
| 238 | case REPORT_FIRMWARE:
|
---|
| 239 | printFirmwareVersion();
|
---|
| 240 | break;
|
---|
| 241 | case STRING_DATA:
|
---|
| 242 | if (currentStringCallback) {
|
---|
| 243 | byte bufferLength = (sysexBytesRead - 1) / 2;
|
---|
| 244 | byte i = 1;
|
---|
| 245 | byte j = 0;
|
---|
| 246 | while (j < bufferLength) {
|
---|
| 247 | // The string length will only be at most half the size of the
|
---|
| 248 | // stored input buffer so we can decode the string within the buffer.
|
---|
| 249 | storedInputData[j] = storedInputData[i];
|
---|
| 250 | i++;
|
---|
| 251 | storedInputData[j] += (storedInputData[i] << 7);
|
---|
| 252 | i++;
|
---|
| 253 | j++;
|
---|
| 254 | }
|
---|
| 255 | // Make sure string is null terminated. This may be the case for data
|
---|
| 256 | // coming from client libraries in languages that don't null terminate
|
---|
| 257 | // strings.
|
---|
| 258 | if (storedInputData[j - 1] != '\0') {
|
---|
| 259 | storedInputData[j] = '\0';
|
---|
| 260 | }
|
---|
| 261 | (*currentStringCallback)((char *)&storedInputData[0]);
|
---|
| 262 | }
|
---|
| 263 | break;
|
---|
| 264 | default:
|
---|
| 265 | if (currentSysexCallback)
|
---|
| 266 | (*currentSysexCallback)(storedInputData[0], sysexBytesRead - 1, storedInputData + 1);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /**
|
---|
| 271 | * Read a single int from the input stream. If the value is not = -1, pass it on to parse(byte)
|
---|
| 272 | */
|
---|
| 273 | void FirmataClass::processInput(void)
|
---|
| 274 | {
|
---|
| 275 | int inputData = FirmataStream->read(); // this is 'int' to handle -1 when no data
|
---|
| 276 | if (inputData != -1) {
|
---|
| 277 | parse(inputData);
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /**
|
---|
| 282 | * Parse data from the input stream.
|
---|
| 283 | * @param inputData A single byte to be added to the parser.
|
---|
| 284 | */
|
---|
| 285 | void FirmataClass::parse(byte inputData)
|
---|
| 286 | {
|
---|
| 287 | int command;
|
---|
| 288 | if (parsingSysex) {
|
---|
| 289 | if (inputData == END_SYSEX) {
|
---|
| 290 | //stop sysex byte
|
---|
| 291 | parsingSysex = false;
|
---|
| 292 | //fire off handler function
|
---|
| 293 | processSysexMessage();
|
---|
| 294 | } else {
|
---|
| 295 | //normal data byte - add to buffer
|
---|
| 296 | storedInputData[sysexBytesRead] = inputData;
|
---|
| 297 | sysexBytesRead++;
|
---|
| 298 |
|
---|
| 299 | }
|
---|
| 300 | } else if ( (waitForData > 0) && (inputData < 128) ) {
|
---|
| 301 | waitForData--;
|
---|
| 302 | storedInputData[waitForData] = inputData;
|
---|
| 303 | if ( (waitForData == 0) && executeMultiByteCommand ) { // got the whole message
|
---|
| 304 | switch (executeMultiByteCommand) {
|
---|
| 305 | case ANALOG_MESSAGE:
|
---|
| 306 | if (currentAnalogCallback) {
|
---|
| 307 | (*currentAnalogCallback)(multiByteChannel,
|
---|
| 308 | (storedInputData[0] << 7)
|
---|
| 309 | + storedInputData[1]);
|
---|
| 310 | }
|
---|
| 311 | break;
|
---|
| 312 | case DIGITAL_MESSAGE:
|
---|
| 313 | if (currentDigitalCallback) {
|
---|
| 314 | (*currentDigitalCallback)(multiByteChannel,
|
---|
| 315 | (storedInputData[0] << 7)
|
---|
| 316 | + storedInputData[1]);
|
---|
| 317 | }
|
---|
| 318 | break;
|
---|
| 319 | case SET_PIN_MODE:
|
---|
| 320 | if (currentPinModeCallback)
|
---|
| 321 | (*currentPinModeCallback)(storedInputData[1], storedInputData[0]);
|
---|
| 322 | break;
|
---|
| 323 | case SET_DIGITAL_PIN_VALUE:
|
---|
| 324 | if (currentPinValueCallback)
|
---|
| 325 | (*currentPinValueCallback)(storedInputData[1], storedInputData[0]);
|
---|
| 326 | break;
|
---|
| 327 | case REPORT_ANALOG:
|
---|
| 328 | if (currentReportAnalogCallback)
|
---|
| 329 | (*currentReportAnalogCallback)(multiByteChannel, storedInputData[0]);
|
---|
| 330 | break;
|
---|
| 331 | case REPORT_DIGITAL:
|
---|
| 332 | if (currentReportDigitalCallback)
|
---|
| 333 | (*currentReportDigitalCallback)(multiByteChannel, storedInputData[0]);
|
---|
| 334 | break;
|
---|
| 335 | }
|
---|
| 336 | executeMultiByteCommand = 0;
|
---|
| 337 | }
|
---|
| 338 | } else {
|
---|
| 339 | // remove channel info from command byte if less than 0xF0
|
---|
| 340 | if (inputData < 0xF0) {
|
---|
| 341 | command = inputData & 0xF0;
|
---|
| 342 | multiByteChannel = inputData & 0x0F;
|
---|
| 343 | } else {
|
---|
| 344 | command = inputData;
|
---|
| 345 | // commands in the 0xF* range don't use channel data
|
---|
| 346 | }
|
---|
| 347 | switch (command) {
|
---|
| 348 | case ANALOG_MESSAGE:
|
---|
| 349 | case DIGITAL_MESSAGE:
|
---|
| 350 | case SET_PIN_MODE:
|
---|
| 351 | case SET_DIGITAL_PIN_VALUE:
|
---|
| 352 | waitForData = 2; // two data bytes needed
|
---|
| 353 | executeMultiByteCommand = command;
|
---|
| 354 | break;
|
---|
| 355 | case REPORT_ANALOG:
|
---|
| 356 | case REPORT_DIGITAL:
|
---|
| 357 | waitForData = 1; // one data byte needed
|
---|
| 358 | executeMultiByteCommand = command;
|
---|
| 359 | break;
|
---|
| 360 | case START_SYSEX:
|
---|
| 361 | parsingSysex = true;
|
---|
| 362 | sysexBytesRead = 0;
|
---|
| 363 | break;
|
---|
| 364 | case SYSTEM_RESET:
|
---|
| 365 | systemReset();
|
---|
| 366 | break;
|
---|
| 367 | case REPORT_VERSION:
|
---|
| 368 | Firmata.printVersion();
|
---|
| 369 | break;
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /**
|
---|
| 375 | * @return Returns true if the parser is actively parsing data.
|
---|
| 376 | */
|
---|
| 377 | boolean FirmataClass::isParsingMessage(void)
|
---|
| 378 | {
|
---|
| 379 | return (waitForData > 0 || parsingSysex);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | //------------------------------------------------------------------------------
|
---|
| 383 | // Output Stream Handling
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
| 386 | * Send an analog message to the Firmata host application. The range of pins is limited to [0..15]
|
---|
| 387 | * when using the ANALOG_MESSAGE. The maximum value of the ANALOG_MESSAGE is limited to 14 bits
|
---|
| 388 | * (16384). To increase the pin range or value, see the documentation for the EXTENDED_ANALOG
|
---|
| 389 | * message.
|
---|
| 390 | * @param pin The analog pin to send the value of (limited to pins 0 - 15).
|
---|
| 391 | * @param value The value of the analog pin (0 - 1024 for 10-bit analog, 0 - 4096 for 12-bit, etc).
|
---|
| 392 | * The maximum value is 14-bits (16384).
|
---|
| 393 | */
|
---|
| 394 | void FirmataClass::sendAnalog(byte pin, int value)
|
---|
| 395 | {
|
---|
| 396 | // pin can only be 0-15, so chop higher bits
|
---|
| 397 | FirmataStream->write(ANALOG_MESSAGE | (pin & 0xF));
|
---|
| 398 | sendValueAsTwo7bitBytes(value);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | /* (intentionally left out asterix here)
|
---|
| 402 | * STUB - NOT IMPLEMENTED
|
---|
| 403 | * Send a single digital pin value to the Firmata host application.
|
---|
| 404 | * @param pin The digital pin to send the value of.
|
---|
| 405 | * @param value The value of the pin.
|
---|
| 406 | */
|
---|
| 407 | void FirmataClass::sendDigital(byte pin, int value)
|
---|
| 408 | {
|
---|
| 409 | /* TODO add single pin digital messages to the protocol, this needs to
|
---|
| 410 | * track the last digital data sent so that it can be sure to change just
|
---|
| 411 | * one bit in the packet. This is complicated by the fact that the
|
---|
| 412 | * numbering of the pins will probably differ on Arduino, Wiring, and
|
---|
| 413 | * other boards.
|
---|
| 414 | */
|
---|
| 415 |
|
---|
| 416 | // TODO: the digital message should not be sent on the serial port every
|
---|
| 417 | // time sendDigital() is called. Instead, it should add it to an int
|
---|
| 418 | // which will be sent on a schedule. If a pin changes more than once
|
---|
| 419 | // before the digital message is sent on the serial port, it should send a
|
---|
| 420 | // digital message for each change.
|
---|
| 421 |
|
---|
| 422 | // if(value == 0)
|
---|
| 423 | // sendDigitalPortPair();
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 |
|
---|
| 427 | /**
|
---|
| 428 | * Send an 8-bit port in a single digital message (protocol v2 and later).
|
---|
| 429 | * Send 14-bits in a single digital message (protocol v1).
|
---|
| 430 | * @param portNumber The port number to send. Note that this is not the same as a "port" on the
|
---|
| 431 | * physical microcontroller. Ports are defined in order per every 8 pins in ascending order
|
---|
| 432 | * of the Arduino digital pin numbering scheme. Port 0 = pins D0 - D7, port 1 = pins D8 - D15, etc.
|
---|
| 433 | * @param portData The value of the port. The value of each pin in the port is represented by a bit.
|
---|
| 434 | */
|
---|
| 435 | void FirmataClass::sendDigitalPort(byte portNumber, int portData)
|
---|
| 436 | {
|
---|
| 437 | FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF));
|
---|
| 438 | FirmataStream->write((byte)portData % 128); // Tx bits 0-6 (protocol v1 and higher)
|
---|
| 439 | FirmataStream->write(portData >> 7); // Tx bits 7-13 (bit 7 only for protocol v2 and higher)
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /**
|
---|
| 443 | * Send a sysex message where all values after the command byte are packet as 2 7-bit bytes
|
---|
| 444 | * (this is not always the case so this function is not always used to send sysex messages).
|
---|
| 445 | * @param command The sysex command byte.
|
---|
| 446 | * @param bytec The number of data bytes in the message (excludes start, command and end bytes).
|
---|
| 447 | * @param bytev A pointer to the array of data bytes to send in the message.
|
---|
| 448 | */
|
---|
| 449 | void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev)
|
---|
| 450 | {
|
---|
| 451 | byte i;
|
---|
| 452 | startSysex();
|
---|
| 453 | FirmataStream->write(command);
|
---|
| 454 | for (i = 0; i < bytec; i++) {
|
---|
| 455 | sendValueAsTwo7bitBytes(bytev[i]);
|
---|
| 456 | }
|
---|
| 457 | endSysex();
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | /**
|
---|
| 461 | * Send a string to the Firmata host application.
|
---|
| 462 | * @param command Must be STRING_DATA
|
---|
| 463 | * @param string A pointer to the char string
|
---|
| 464 | */
|
---|
| 465 | void FirmataClass::sendString(byte command, const char *string)
|
---|
| 466 | {
|
---|
| 467 | if (command == STRING_DATA) {
|
---|
| 468 | sendSysex(command, strlen(string), (byte *)string);
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | /**
|
---|
| 473 | * Send a string to the Firmata host application.
|
---|
| 474 | * @param string A pointer to the char string
|
---|
| 475 | */
|
---|
| 476 | void FirmataClass::sendString(const char *string)
|
---|
| 477 | {
|
---|
| 478 | sendString(STRING_DATA, string);
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | /**
|
---|
| 482 | * A wrapper for Stream::available().
|
---|
| 483 | * Write a single byte to the output stream.
|
---|
| 484 | * @param c The byte to be written.
|
---|
| 485 | */
|
---|
| 486 | void FirmataClass::write(byte c)
|
---|
| 487 | {
|
---|
| 488 | FirmataStream->write(c);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | /**
|
---|
| 492 | * Attach a generic sysex callback function to a command (options are: ANALOG_MESSAGE,
|
---|
| 493 | * DIGITAL_MESSAGE, REPORT_ANALOG, REPORT DIGITAL, SET_PIN_MODE and SET_DIGITAL_PIN_VALUE).
|
---|
| 494 | * @param command The ID of the command to attach a callback function to.
|
---|
| 495 | * @param newFunction A reference to the callback function to attach.
|
---|
| 496 | */
|
---|
| 497 | void FirmataClass::attach(byte command, callbackFunction newFunction)
|
---|
| 498 | {
|
---|
| 499 | switch (command) {
|
---|
| 500 | case ANALOG_MESSAGE: currentAnalogCallback = newFunction; break;
|
---|
| 501 | case DIGITAL_MESSAGE: currentDigitalCallback = newFunction; break;
|
---|
| 502 | case REPORT_ANALOG: currentReportAnalogCallback = newFunction; break;
|
---|
| 503 | case REPORT_DIGITAL: currentReportDigitalCallback = newFunction; break;
|
---|
| 504 | case SET_PIN_MODE: currentPinModeCallback = newFunction; break;
|
---|
| 505 | case SET_DIGITAL_PIN_VALUE: currentPinValueCallback = newFunction; break;
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | /**
|
---|
| 510 | * Attach a callback function for the SYSTEM_RESET command.
|
---|
| 511 | * @param command Must be set to SYSTEM_RESET or it will be ignored.
|
---|
| 512 | * @param newFunction A reference to the system reset callback function to attach.
|
---|
| 513 | */
|
---|
| 514 | void FirmataClass::attach(byte command, systemResetCallbackFunction newFunction)
|
---|
| 515 | {
|
---|
| 516 | switch (command) {
|
---|
| 517 | case SYSTEM_RESET: currentSystemResetCallback = newFunction; break;
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /**
|
---|
| 522 | * Attach a callback function for the STRING_DATA command.
|
---|
| 523 | * @param command Must be set to STRING_DATA or it will be ignored.
|
---|
| 524 | * @param newFunction A reference to the string callback function to attach.
|
---|
| 525 | */
|
---|
| 526 | void FirmataClass::attach(byte command, stringCallbackFunction newFunction)
|
---|
| 527 | {
|
---|
| 528 | switch (command) {
|
---|
| 529 | case STRING_DATA: currentStringCallback = newFunction; break;
|
---|
| 530 | }
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | /**
|
---|
| 534 | * Attach a generic sysex callback function to sysex command.
|
---|
| 535 | * @param command The ID of the command to attach a callback function to.
|
---|
| 536 | * @param newFunction A reference to the sysex callback function to attach.
|
---|
| 537 | */
|
---|
| 538 | void FirmataClass::attach(byte command, sysexCallbackFunction newFunction)
|
---|
| 539 | {
|
---|
| 540 | currentSysexCallback = newFunction;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | /**
|
---|
| 544 | * Detach a callback function for a specified command (such as SYSTEM_RESET, STRING_DATA,
|
---|
| 545 | * ANALOG_MESSAGE, DIGITAL_MESSAGE, etc).
|
---|
| 546 | * @param command The ID of the command to detatch the callback function from.
|
---|
| 547 | */
|
---|
| 548 | void FirmataClass::detach(byte command)
|
---|
| 549 | {
|
---|
| 550 | switch (command) {
|
---|
| 551 | case SYSTEM_RESET: currentSystemResetCallback = NULL; break;
|
---|
| 552 | case STRING_DATA: currentStringCallback = NULL; break;
|
---|
| 553 | case START_SYSEX: currentSysexCallback = NULL; break;
|
---|
| 554 | default:
|
---|
| 555 | attach(command, (callbackFunction)NULL);
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
| 560 | * @param pin The pin to get the configuration of.
|
---|
| 561 | * @return The configuration of the specified pin.
|
---|
| 562 | */
|
---|
| 563 | byte FirmataClass::getPinMode(byte pin)
|
---|
| 564 | {
|
---|
| 565 | return pinConfig[pin];
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | /**
|
---|
| 569 | * Set the pin mode/configuration. The pin configuration (or mode) in Firmata represents the
|
---|
| 570 | * current function of the pin. Examples are digital input or output, analog input, pwm, i2c,
|
---|
| 571 | * serial (uart), etc.
|
---|
| 572 | * @param pin The pin to configure.
|
---|
| 573 | * @param config The configuration value for the specified pin.
|
---|
| 574 | */
|
---|
| 575 | void FirmataClass::setPinMode(byte pin, byte config)
|
---|
| 576 | {
|
---|
| 577 | if (pinConfig[pin] == PIN_MODE_IGNORE)
|
---|
| 578 | return;
|
---|
| 579 |
|
---|
| 580 | pinConfig[pin] = config;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | /**
|
---|
| 584 | * @param pin The pin to get the state of.
|
---|
| 585 | * @return The state of the specified pin.
|
---|
| 586 | */
|
---|
| 587 | int FirmataClass::getPinState(byte pin)
|
---|
| 588 | {
|
---|
| 589 | return pinState[pin];
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | /**
|
---|
| 593 | * Set the pin state. The pin state of an output pin is the pin value. The state of an
|
---|
| 594 | * input pin is 0, unless the pin has it's internal pull up resistor enabled, then the value is 1.
|
---|
| 595 | * @param pin The pin to set the state of
|
---|
| 596 | * @param state Set the state of the specified pin
|
---|
| 597 | */
|
---|
| 598 | void FirmataClass::setPinState(byte pin, int state)
|
---|
| 599 | {
|
---|
| 600 | pinState[pin] = state;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | // sysex callbacks
|
---|
| 604 | /*
|
---|
| 605 | * this is too complicated for analogReceive, but maybe for Sysex?
|
---|
| 606 | void FirmataClass::attachSysex(sysexFunction newFunction)
|
---|
| 607 | {
|
---|
| 608 | byte i;
|
---|
| 609 | byte tmpCount = analogReceiveFunctionCount;
|
---|
| 610 | analogReceiveFunction* tmpArray = analogReceiveFunctionArray;
|
---|
| 611 | analogReceiveFunctionCount++;
|
---|
| 612 | analogReceiveFunctionArray = (analogReceiveFunction*) calloc(analogReceiveFunctionCount, sizeof(analogReceiveFunction));
|
---|
| 613 | for(i = 0; i < tmpCount; i++) {
|
---|
| 614 | analogReceiveFunctionArray[i] = tmpArray[i];
|
---|
| 615 | }
|
---|
| 616 | analogReceiveFunctionArray[tmpCount] = newFunction;
|
---|
| 617 | free(tmpArray);
|
---|
| 618 | }
|
---|
| 619 | */
|
---|
| 620 |
|
---|
| 621 | //******************************************************************************
|
---|
| 622 | //* Private Methods
|
---|
| 623 | //******************************************************************************
|
---|
| 624 |
|
---|
| 625 | /**
|
---|
| 626 | * Resets the system state upon a SYSTEM_RESET message from the host software.
|
---|
| 627 | * @private
|
---|
| 628 | */
|
---|
| 629 | void FirmataClass::systemReset(void)
|
---|
| 630 | {
|
---|
| 631 | byte i;
|
---|
| 632 |
|
---|
| 633 | waitForData = 0; // this flag says the next serial input will be data
|
---|
| 634 | executeMultiByteCommand = 0; // execute this after getting multi-byte data
|
---|
| 635 | multiByteChannel = 0; // channel data for multiByteCommands
|
---|
| 636 |
|
---|
| 637 | for (i = 0; i < MAX_DATA_BYTES; i++) {
|
---|
| 638 | storedInputData[i] = 0;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | parsingSysex = false;
|
---|
| 642 | sysexBytesRead = 0;
|
---|
| 643 |
|
---|
| 644 | if (currentSystemResetCallback)
|
---|
| 645 | (*currentSystemResetCallback)();
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | /**
|
---|
| 649 | * Flashing the pin for the version number
|
---|
| 650 | * @private
|
---|
| 651 | * @param pin The pin the LED is attached to.
|
---|
| 652 | * @param count The number of times to flash the LED.
|
---|
| 653 | * @param onInterval The number of milliseconds for the LED to be ON during each interval.
|
---|
| 654 | * @param offInterval The number of milliseconds for the LED to be OFF during each interval.
|
---|
| 655 | */
|
---|
| 656 | void FirmataClass::strobeBlinkPin(byte pin, int count, int onInterval, int offInterval)
|
---|
| 657 | {
|
---|
| 658 | byte i;
|
---|
| 659 | for (i = 0; i < count; i++) {
|
---|
| 660 | delay(offInterval);
|
---|
| 661 | digitalWrite(pin, HIGH);
|
---|
| 662 | delay(onInterval);
|
---|
| 663 | digitalWrite(pin, LOW);
|
---|
| 664 | }
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | // make one instance for the user to use
|
---|
| 668 | FirmataClass Firmata;
|
---|