[136] | 1 | /*
|
---|
| 2 | * Firmata is a generic protocol for communicating with microcontrollers
|
---|
| 3 | * from software on a host computer. It is intended to work with
|
---|
| 4 | * any host computer software package.
|
---|
| 5 | *
|
---|
| 6 | * To download a host software package, please clink on the following link
|
---|
| 7 | * to open the download page in your default browser.
|
---|
| 8 | *
|
---|
| 9 | * http://firmata.org/wiki/Download
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | /*
|
---|
| 13 | Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
|
---|
| 14 | Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved.
|
---|
| 15 | Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
|
---|
| 16 | Copyright (C) 2009-2011 Jeff Hoefs. All rights reserved.
|
---|
| 17 |
|
---|
| 18 | This library is free software; you can redistribute it and/or
|
---|
| 19 | modify it under the terms of the GNU Lesser General Public
|
---|
| 20 | License as published by the Free Software Foundation; either
|
---|
| 21 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 22 |
|
---|
| 23 | See file LICENSE.txt for further informations on licensing terms.
|
---|
| 24 |
|
---|
| 25 | formatted using the GNU C formatting and indenting
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | /*
|
---|
| 29 | * TODO: use Program Control to load stored profiles from EEPROM
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | #include <Servo.h>
|
---|
| 33 | #include <Wire.h>
|
---|
| 34 | #include <Firmata.h>
|
---|
| 35 |
|
---|
| 36 | // move the following defines to Firmata.h?
|
---|
| 37 | #define I2C_WRITE B00000000
|
---|
| 38 | #define I2C_READ B00001000
|
---|
| 39 | #define I2C_READ_CONTINUOUSLY B00010000
|
---|
| 40 | #define I2C_STOP_READING B00011000
|
---|
| 41 | #define I2C_READ_WRITE_MODE_MASK B00011000
|
---|
| 42 | #define I2C_10BIT_ADDRESS_MODE_MASK B00100000
|
---|
| 43 |
|
---|
| 44 | #define MAX_QUERIES 8
|
---|
| 45 | #define MINIMUM_SAMPLING_INTERVAL 10
|
---|
| 46 |
|
---|
| 47 | #define REGISTER_NOT_SPECIFIED -1
|
---|
| 48 |
|
---|
| 49 | /*==============================================================================
|
---|
| 50 | * GLOBAL VARIABLES
|
---|
| 51 | *============================================================================*/
|
---|
| 52 |
|
---|
| 53 | /* analog inputs */
|
---|
| 54 | int analogInputsToReport = 0; // bitwise array to store pin reporting
|
---|
| 55 |
|
---|
| 56 | /* digital input ports */
|
---|
| 57 | byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence
|
---|
| 58 | byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent
|
---|
| 59 |
|
---|
| 60 | /* pins configuration */
|
---|
| 61 | byte pinConfig[TOTAL_PINS]; // configuration of every pin
|
---|
| 62 | byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else
|
---|
| 63 | int pinState[TOTAL_PINS]; // any value that has been written
|
---|
| 64 |
|
---|
| 65 | /* timer variables */
|
---|
| 66 | unsigned long currentMillis; // store the current value from millis()
|
---|
| 67 | unsigned long previousMillis; // for comparison with currentMillis
|
---|
| 68 | int samplingInterval = 19; // how often to run the main loop (in ms)
|
---|
| 69 |
|
---|
| 70 | /* i2c data */
|
---|
| 71 | struct i2c_device_info {
|
---|
| 72 | byte addr;
|
---|
| 73 | byte reg;
|
---|
| 74 | byte bytes;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | /* for i2c read continuous more */
|
---|
| 78 | i2c_device_info query[MAX_QUERIES];
|
---|
| 79 |
|
---|
| 80 | byte i2cRxData[32];
|
---|
| 81 | boolean isI2CEnabled = false;
|
---|
| 82 | signed char queryIndex = -1;
|
---|
| 83 | unsigned int i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom()
|
---|
| 84 |
|
---|
| 85 | Servo servos[MAX_SERVOS];
|
---|
| 86 | /*==============================================================================
|
---|
| 87 | * FUNCTIONS
|
---|
| 88 | *============================================================================*/
|
---|
| 89 |
|
---|
| 90 | void readAndReportData(byte address, int theRegister, byte numBytes) {
|
---|
| 91 | // allow I2C requests that don't require a register read
|
---|
| 92 | // for example, some devices using an interrupt pin to signify new data available
|
---|
| 93 | // do not always require the register read so upon interrupt you call Wire.requestFrom()
|
---|
| 94 | if (theRegister != REGISTER_NOT_SPECIFIED) {
|
---|
| 95 | Wire.beginTransmission(address);
|
---|
| 96 | #if ARDUINO >= 100
|
---|
| 97 | Wire.write((byte)theRegister);
|
---|
| 98 | #else
|
---|
| 99 | Wire.send((byte)theRegister);
|
---|
| 100 | #endif
|
---|
| 101 | Wire.endTransmission();
|
---|
| 102 | // do not set a value of 0
|
---|
| 103 | if (i2cReadDelayTime > 0) {
|
---|
| 104 | // delay is necessary for some devices such as WiiNunchuck
|
---|
| 105 | delayMicroseconds(i2cReadDelayTime);
|
---|
| 106 | }
|
---|
| 107 | } else {
|
---|
| 108 | theRegister = 0; // fill the register with a dummy value
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | Wire.requestFrom(address, numBytes); // all bytes are returned in requestFrom
|
---|
| 112 |
|
---|
| 113 | // check to be sure correct number of bytes were returned by slave
|
---|
| 114 | if (numBytes == Wire.available()) {
|
---|
| 115 | i2cRxData[0] = address;
|
---|
| 116 | i2cRxData[1] = theRegister;
|
---|
| 117 | for (int i = 0; i < numBytes; i++) {
|
---|
| 118 | #if ARDUINO >= 100
|
---|
| 119 | i2cRxData[2 + i] = Wire.read();
|
---|
| 120 | #else
|
---|
| 121 | i2cRxData[2 + i] = Wire.receive();
|
---|
| 122 | #endif
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | else {
|
---|
| 126 | if (numBytes > Wire.available()) {
|
---|
| 127 | Firmata.sendString("I2C Read Error: Too many bytes received");
|
---|
| 128 | } else {
|
---|
| 129 | Firmata.sendString("I2C Read Error: Too few bytes received");
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // send slave address, register and received bytes
|
---|
| 134 | Firmata.sendSysex(SYSEX_I2C_REPLY, numBytes + 2, i2cRxData);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | void outputPort(byte portNumber, byte portValue, byte forceSend)
|
---|
| 138 | {
|
---|
| 139 | // pins not configured as INPUT are cleared to zeros
|
---|
| 140 | portValue = portValue & portConfigInputs[portNumber];
|
---|
| 141 | // only send if the value is different than previously sent
|
---|
| 142 | if (forceSend || previousPINs[portNumber] != portValue) {
|
---|
| 143 | Firmata.sendDigitalPort(portNumber, portValue);
|
---|
| 144 | previousPINs[portNumber] = portValue;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /* -----------------------------------------------------------------------------
|
---|
| 149 | * check all the active digital inputs for change of state, then add any events
|
---|
| 150 | * to the Serial output queue using Serial.print() */
|
---|
| 151 | void checkDigitalInputs(void)
|
---|
| 152 | {
|
---|
| 153 | /* Using non-looping code allows constants to be given to readPort().
|
---|
| 154 | * The compiler will apply substantial optimizations if the inputs
|
---|
| 155 | * to readPort() are compile-time constants. */
|
---|
| 156 | if (TOTAL_PORTS > 0 && reportPINs[0]) outputPort(0, readPort(0, portConfigInputs[0]), false);
|
---|
| 157 | if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false);
|
---|
| 158 | if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false);
|
---|
| 159 | if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false);
|
---|
| 160 | if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false);
|
---|
| 161 | if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false);
|
---|
| 162 | if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false);
|
---|
| 163 | if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false);
|
---|
| 164 | if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false);
|
---|
| 165 | if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false);
|
---|
| 166 | if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false);
|
---|
| 167 | if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false);
|
---|
| 168 | if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false);
|
---|
| 169 | if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false);
|
---|
| 170 | if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false);
|
---|
| 171 | if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | // -----------------------------------------------------------------------------
|
---|
| 175 | /* sets the pin mode to the correct state and sets the relevant bits in the
|
---|
| 176 | * two bit-arrays that track Digital I/O and PWM status
|
---|
| 177 | */
|
---|
| 178 | void setPinModeCallback(byte pin, int mode)
|
---|
| 179 | {
|
---|
| 180 | if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
|
---|
| 181 | // disable i2c so pins can be used for other functions
|
---|
| 182 | // the following if statements should reconfigure the pins properly
|
---|
| 183 | disableI2CPins();
|
---|
| 184 | }
|
---|
| 185 | if (IS_PIN_SERVO(pin) && mode != SERVO && servos[PIN_TO_SERVO(pin)].attached()) {
|
---|
| 186 | servos[PIN_TO_SERVO(pin)].detach();
|
---|
| 187 | }
|
---|
| 188 | if (IS_PIN_ANALOG(pin)) {
|
---|
| 189 | reportAnalogCallback(PIN_TO_ANALOG(pin), mode == ANALOG ? 1 : 0); // turn on/off reporting
|
---|
| 190 | }
|
---|
| 191 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 192 | if (mode == INPUT) {
|
---|
| 193 | portConfigInputs[pin / 8] |= (1 << (pin & 7));
|
---|
| 194 | } else {
|
---|
| 195 | portConfigInputs[pin / 8] &= ~(1 << (pin & 7));
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | pinState[pin] = 0;
|
---|
| 199 | switch (mode) {
|
---|
| 200 | case ANALOG:
|
---|
| 201 | if (IS_PIN_ANALOG(pin)) {
|
---|
| 202 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 203 | pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
|
---|
| 204 | digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
|
---|
| 205 | }
|
---|
| 206 | pinConfig[pin] = ANALOG;
|
---|
| 207 | }
|
---|
| 208 | break;
|
---|
| 209 | case INPUT:
|
---|
| 210 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 211 | pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
|
---|
| 212 | digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
|
---|
| 213 | pinConfig[pin] = INPUT;
|
---|
| 214 | }
|
---|
| 215 | break;
|
---|
| 216 | case OUTPUT:
|
---|
| 217 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 218 | digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM
|
---|
| 219 | pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
|
---|
| 220 | pinConfig[pin] = OUTPUT;
|
---|
| 221 | }
|
---|
| 222 | break;
|
---|
| 223 | case PWM:
|
---|
| 224 | if (IS_PIN_PWM(pin)) {
|
---|
| 225 | pinMode(PIN_TO_PWM(pin), OUTPUT);
|
---|
| 226 | analogWrite(PIN_TO_PWM(pin), 0);
|
---|
| 227 | pinConfig[pin] = PWM;
|
---|
| 228 | }
|
---|
| 229 | break;
|
---|
| 230 | case SERVO:
|
---|
| 231 | if (IS_PIN_SERVO(pin)) {
|
---|
| 232 | pinConfig[pin] = SERVO;
|
---|
| 233 | if (!servos[PIN_TO_SERVO(pin)].attached()) {
|
---|
| 234 | servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin));
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | break;
|
---|
| 238 | case I2C:
|
---|
| 239 | if (IS_PIN_I2C(pin)) {
|
---|
| 240 | // mark the pin as i2c
|
---|
| 241 | // the user must call I2C_CONFIG to enable I2C for a device
|
---|
| 242 | pinConfig[pin] = I2C;
|
---|
| 243 | }
|
---|
| 244 | break;
|
---|
| 245 | default:
|
---|
| 246 | Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM
|
---|
| 247 | }
|
---|
| 248 | // TODO: save status to EEPROM here, if changed
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | void analogWriteCallback(byte pin, int value)
|
---|
| 252 | {
|
---|
| 253 | if (pin < TOTAL_PINS) {
|
---|
| 254 | switch (pinConfig[pin]) {
|
---|
| 255 | case SERVO:
|
---|
| 256 | if (IS_PIN_SERVO(pin))
|
---|
| 257 | servos[PIN_TO_SERVO(pin)].write(value);
|
---|
| 258 | pinState[pin] = value;
|
---|
| 259 | break;
|
---|
| 260 | case PWM:
|
---|
| 261 | if (IS_PIN_PWM(pin))
|
---|
| 262 | analogWrite(PIN_TO_PWM(pin), value);
|
---|
| 263 | pinState[pin] = value;
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | void digitalWriteCallback(byte port, int value)
|
---|
| 270 | {
|
---|
| 271 | byte pin, lastPin, mask = 1, pinWriteMask = 0;
|
---|
| 272 |
|
---|
| 273 | if (port < TOTAL_PORTS) {
|
---|
| 274 | // create a mask of the pins on this port that are writable.
|
---|
| 275 | lastPin = port * 8 + 8;
|
---|
| 276 | if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS;
|
---|
| 277 | for (pin = port * 8; pin < lastPin; pin++) {
|
---|
| 278 | // do not disturb non-digital pins (eg, Rx & Tx)
|
---|
| 279 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 280 | // only write to OUTPUT and INPUT (enables pullup)
|
---|
| 281 | // do not touch pins in PWM, ANALOG, SERVO or other modes
|
---|
| 282 | if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
|
---|
| 283 | pinWriteMask |= mask;
|
---|
| 284 | pinState[pin] = ((byte)value & mask) ? 1 : 0;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | mask = mask << 1;
|
---|
| 288 | }
|
---|
| 289 | writePort(port, (byte)value, pinWriteMask);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | // -----------------------------------------------------------------------------
|
---|
| 295 | /* sets bits in a bit array (int) to toggle the reporting of the analogIns
|
---|
| 296 | */
|
---|
| 297 | //void FirmataClass::setAnalogPinReporting(byte pin, byte state) {
|
---|
| 298 | //}
|
---|
| 299 | void reportAnalogCallback(byte analogPin, int value)
|
---|
| 300 | {
|
---|
| 301 | if (analogPin < TOTAL_ANALOG_PINS) {
|
---|
| 302 | if (value == 0) {
|
---|
| 303 | analogInputsToReport = analogInputsToReport &~ (1 << analogPin);
|
---|
| 304 | } else {
|
---|
| 305 | analogInputsToReport = analogInputsToReport | (1 << analogPin);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | // TODO: save status to EEPROM here, if changed
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | void reportDigitalCallback(byte port, int value)
|
---|
| 312 | {
|
---|
| 313 | if (port < TOTAL_PORTS) {
|
---|
| 314 | reportPINs[port] = (byte)value;
|
---|
| 315 | }
|
---|
| 316 | // do not disable analog reporting on these 8 pins, to allow some
|
---|
| 317 | // pins used for digital, others analog. Instead, allow both types
|
---|
| 318 | // of reporting to be enabled, but check if the pin is configured
|
---|
| 319 | // as analog when sampling the analog inputs. Likewise, while
|
---|
| 320 | // scanning digital pins, portConfigInputs will mask off values from any
|
---|
| 321 | // pins configured as analog
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /*==============================================================================
|
---|
| 325 | * SYSEX-BASED commands
|
---|
| 326 | *============================================================================*/
|
---|
| 327 |
|
---|
| 328 | void sysexCallback(byte command, byte argc, byte *argv)
|
---|
| 329 | {
|
---|
| 330 | byte mode;
|
---|
| 331 | byte slaveAddress;
|
---|
| 332 | byte slaveRegister;
|
---|
| 333 | byte data;
|
---|
| 334 | unsigned int delayTime;
|
---|
| 335 |
|
---|
| 336 | switch (command) {
|
---|
| 337 | case I2C_REQUEST:
|
---|
| 338 | mode = argv[1] & I2C_READ_WRITE_MODE_MASK;
|
---|
| 339 | if (argv[1] & I2C_10BIT_ADDRESS_MODE_MASK) {
|
---|
| 340 | Firmata.sendString("10-bit addressing mode is not yet supported");
|
---|
| 341 | return;
|
---|
| 342 | }
|
---|
| 343 | else {
|
---|
| 344 | slaveAddress = argv[0];
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | switch (mode) {
|
---|
| 348 | case I2C_WRITE:
|
---|
| 349 | Wire.beginTransmission(slaveAddress);
|
---|
| 350 | for (byte i = 2; i < argc; i += 2) {
|
---|
| 351 | data = argv[i] + (argv[i + 1] << 7);
|
---|
| 352 | #if ARDUINO >= 100
|
---|
| 353 | Wire.write(data);
|
---|
| 354 | #else
|
---|
| 355 | Wire.send(data);
|
---|
| 356 | #endif
|
---|
| 357 | }
|
---|
| 358 | Wire.endTransmission();
|
---|
| 359 | delayMicroseconds(70);
|
---|
| 360 | break;
|
---|
| 361 | case I2C_READ:
|
---|
| 362 | if (argc == 6) {
|
---|
| 363 | // a slave register is specified
|
---|
| 364 | slaveRegister = argv[2] + (argv[3] << 7);
|
---|
| 365 | data = argv[4] + (argv[5] << 7); // bytes to read
|
---|
| 366 | readAndReportData(slaveAddress, (int)slaveRegister, data);
|
---|
| 367 | }
|
---|
| 368 | else {
|
---|
| 369 | // a slave register is NOT specified
|
---|
| 370 | data = argv[2] + (argv[3] << 7); // bytes to read
|
---|
| 371 | readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data);
|
---|
| 372 | }
|
---|
| 373 | break;
|
---|
| 374 | case I2C_READ_CONTINUOUSLY:
|
---|
| 375 | if ((queryIndex + 1) >= MAX_QUERIES) {
|
---|
| 376 | // too many queries, just ignore
|
---|
| 377 | Firmata.sendString("too many queries");
|
---|
| 378 | break;
|
---|
| 379 | }
|
---|
| 380 | queryIndex++;
|
---|
| 381 | query[queryIndex].addr = slaveAddress;
|
---|
| 382 | query[queryIndex].reg = argv[2] + (argv[3] << 7);
|
---|
| 383 | query[queryIndex].bytes = argv[4] + (argv[5] << 7);
|
---|
| 384 | break;
|
---|
| 385 | case I2C_STOP_READING:
|
---|
| 386 | byte queryIndexToSkip;
|
---|
| 387 | // if read continuous mode is enabled for only 1 i2c device, disable
|
---|
| 388 | // read continuous reporting for that device
|
---|
| 389 | if (queryIndex <= 0) {
|
---|
| 390 | queryIndex = -1;
|
---|
| 391 | } else {
|
---|
| 392 | // if read continuous mode is enabled for multiple devices,
|
---|
| 393 | // determine which device to stop reading and remove it's data from
|
---|
| 394 | // the array, shifiting other array data to fill the space
|
---|
| 395 | for (byte i = 0; i < queryIndex + 1; i++) {
|
---|
| 396 | if (query[i].addr = slaveAddress) {
|
---|
| 397 | queryIndexToSkip = i;
|
---|
| 398 | break;
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | for (byte i = queryIndexToSkip; i < queryIndex + 1; i++) {
|
---|
| 403 | if (i < MAX_QUERIES) {
|
---|
| 404 | query[i].addr = query[i + 1].addr;
|
---|
| 405 | query[i].reg = query[i + 1].addr;
|
---|
| 406 | query[i].bytes = query[i + 1].bytes;
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 | queryIndex--;
|
---|
| 410 | }
|
---|
| 411 | break;
|
---|
| 412 | default:
|
---|
| 413 | break;
|
---|
| 414 | }
|
---|
| 415 | break;
|
---|
| 416 | case I2C_CONFIG:
|
---|
| 417 | delayTime = (argv[0] + (argv[1] << 7));
|
---|
| 418 |
|
---|
| 419 | if (delayTime > 0) {
|
---|
| 420 | i2cReadDelayTime = delayTime;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | if (!isI2CEnabled) {
|
---|
| 424 | enableI2CPins();
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | break;
|
---|
| 428 | case SERVO_CONFIG:
|
---|
| 429 | if (argc > 4) {
|
---|
| 430 | // these vars are here for clarity, they'll optimized away by the compiler
|
---|
| 431 | byte pin = argv[0];
|
---|
| 432 | int minPulse = argv[1] + (argv[2] << 7);
|
---|
| 433 | int maxPulse = argv[3] + (argv[4] << 7);
|
---|
| 434 |
|
---|
| 435 | if (IS_PIN_SERVO(pin)) {
|
---|
| 436 | if (servos[PIN_TO_SERVO(pin)].attached())
|
---|
| 437 | servos[PIN_TO_SERVO(pin)].detach();
|
---|
| 438 | servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse);
|
---|
| 439 | setPinModeCallback(pin, SERVO);
|
---|
| 440 | }
|
---|
| 441 | }
|
---|
| 442 | break;
|
---|
| 443 | case SAMPLING_INTERVAL:
|
---|
| 444 | if (argc > 1) {
|
---|
| 445 | samplingInterval = argv[0] + (argv[1] << 7);
|
---|
| 446 | if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) {
|
---|
| 447 | samplingInterval = MINIMUM_SAMPLING_INTERVAL;
|
---|
| 448 | }
|
---|
| 449 | } else {
|
---|
| 450 | //Firmata.sendString("Not enough data");
|
---|
| 451 | }
|
---|
| 452 | break;
|
---|
| 453 | case EXTENDED_ANALOG:
|
---|
| 454 | if (argc > 1) {
|
---|
| 455 | int val = argv[1];
|
---|
| 456 | if (argc > 2) val |= (argv[2] << 7);
|
---|
| 457 | if (argc > 3) val |= (argv[3] << 14);
|
---|
| 458 | analogWriteCallback(argv[0], val);
|
---|
| 459 | }
|
---|
| 460 | break;
|
---|
| 461 | case CAPABILITY_QUERY:
|
---|
| 462 | Firmata.write(START_SYSEX);
|
---|
| 463 | Firmata.write(CAPABILITY_RESPONSE);
|
---|
| 464 | for (byte pin = 0; pin < TOTAL_PINS; pin++) {
|
---|
| 465 | if (IS_PIN_DIGITAL(pin)) {
|
---|
| 466 | Firmata.write((byte)INPUT);
|
---|
| 467 | Firmata.write(1);
|
---|
| 468 | Firmata.write((byte)OUTPUT);
|
---|
| 469 | Firmata.write(1);
|
---|
| 470 | }
|
---|
| 471 | if (IS_PIN_ANALOG(pin)) {
|
---|
| 472 | Firmata.write(ANALOG);
|
---|
| 473 | Firmata.write(10);
|
---|
| 474 | }
|
---|
| 475 | if (IS_PIN_PWM(pin)) {
|
---|
| 476 | Firmata.write(PWM);
|
---|
| 477 | Firmata.write(8);
|
---|
| 478 | }
|
---|
| 479 | if (IS_PIN_SERVO(pin)) {
|
---|
| 480 | Firmata.write(SERVO);
|
---|
| 481 | Firmata.write(14);
|
---|
| 482 | }
|
---|
| 483 | if (IS_PIN_I2C(pin)) {
|
---|
| 484 | Firmata.write(I2C);
|
---|
| 485 | Firmata.write(1); // to do: determine appropriate value
|
---|
| 486 | }
|
---|
| 487 | Firmata.write(127);
|
---|
| 488 | }
|
---|
| 489 | Firmata.write(END_SYSEX);
|
---|
| 490 | break;
|
---|
| 491 | case PIN_STATE_QUERY:
|
---|
| 492 | if (argc > 0) {
|
---|
| 493 | byte pin = argv[0];
|
---|
| 494 | Firmata.write(START_SYSEX);
|
---|
| 495 | Firmata.write(PIN_STATE_RESPONSE);
|
---|
| 496 | Firmata.write(pin);
|
---|
| 497 | if (pin < TOTAL_PINS) {
|
---|
| 498 | Firmata.write((byte)pinConfig[pin]);
|
---|
| 499 | Firmata.write((byte)pinState[pin] & 0x7F);
|
---|
| 500 | if (pinState[pin] & 0xFF80) Firmata.write((byte)(pinState[pin] >> 7) & 0x7F);
|
---|
| 501 | if (pinState[pin] & 0xC000) Firmata.write((byte)(pinState[pin] >> 14) & 0x7F);
|
---|
| 502 | }
|
---|
| 503 | Firmata.write(END_SYSEX);
|
---|
| 504 | }
|
---|
| 505 | break;
|
---|
| 506 | case ANALOG_MAPPING_QUERY:
|
---|
| 507 | Firmata.write(START_SYSEX);
|
---|
| 508 | Firmata.write(ANALOG_MAPPING_RESPONSE);
|
---|
| 509 | for (byte pin = 0; pin < TOTAL_PINS; pin++) {
|
---|
| 510 | Firmata.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127);
|
---|
| 511 | }
|
---|
| 512 | Firmata.write(END_SYSEX);
|
---|
| 513 | break;
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | void enableI2CPins()
|
---|
| 518 | {
|
---|
| 519 | byte i;
|
---|
| 520 | // is there a faster way to do this? would probaby require importing
|
---|
| 521 | // Arduino.h to get SCL and SDA pins
|
---|
| 522 | for (i = 0; i < TOTAL_PINS; i++) {
|
---|
| 523 | if (IS_PIN_I2C(i)) {
|
---|
| 524 | // mark pins as i2c so they are ignore in non i2c data requests
|
---|
| 525 | setPinModeCallback(i, I2C);
|
---|
| 526 | }
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | isI2CEnabled = true;
|
---|
| 530 |
|
---|
| 531 | // is there enough time before the first I2C request to call this here?
|
---|
| 532 | Wire.begin();
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | /* disable the i2c pins so they can be used for other functions */
|
---|
| 536 | void disableI2CPins() {
|
---|
| 537 | isI2CEnabled = false;
|
---|
| 538 | // disable read continuous mode for all devices
|
---|
| 539 | queryIndex = -1;
|
---|
| 540 | // uncomment the following if or when the end() method is added to Wire library
|
---|
| 541 | // Wire.end();
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | /*==============================================================================
|
---|
| 545 | * SETUP()
|
---|
| 546 | *============================================================================*/
|
---|
| 547 |
|
---|
| 548 | void systemResetCallback()
|
---|
| 549 | {
|
---|
| 550 | // initialize a defalt state
|
---|
| 551 | // TODO: option to load config from EEPROM instead of default
|
---|
| 552 | if (isI2CEnabled) {
|
---|
| 553 | disableI2CPins();
|
---|
| 554 | }
|
---|
| 555 | for (byte i = 0; i < TOTAL_PORTS; i++) {
|
---|
| 556 | reportPINs[i] = false; // by default, reporting off
|
---|
| 557 | portConfigInputs[i] = 0; // until activated
|
---|
| 558 | previousPINs[i] = 0;
|
---|
| 559 | }
|
---|
| 560 | // pins with analog capability default to analog input
|
---|
| 561 | // otherwise, pins default to digital output
|
---|
| 562 | for (byte i = 0; i < TOTAL_PINS; i++) {
|
---|
| 563 | if (IS_PIN_ANALOG(i)) {
|
---|
| 564 | // turns off pullup, configures everything
|
---|
| 565 | setPinModeCallback(i, ANALOG);
|
---|
| 566 | } else {
|
---|
| 567 | // sets the output to 0, configures portConfigInputs
|
---|
| 568 | setPinModeCallback(i, OUTPUT);
|
---|
| 569 | }
|
---|
| 570 | }
|
---|
| 571 | // by default, do not report any analog inputs
|
---|
| 572 | analogInputsToReport = 0;
|
---|
| 573 |
|
---|
| 574 | /* send digital inputs to set the initial state on the host computer,
|
---|
| 575 | * since once in the loop(), this firmware will only send on change */
|
---|
| 576 | /*
|
---|
| 577 | TODO: this can never execute, since no pins default to digital input
|
---|
| 578 | but it will be needed when/if we support EEPROM stored config
|
---|
| 579 | for (byte i=0; i < TOTAL_PORTS; i++) {
|
---|
| 580 | outputPort(i, readPort(i, portConfigInputs[i]), true);
|
---|
| 581 | }
|
---|
| 582 | */
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | void setup()
|
---|
| 586 | {
|
---|
| 587 | Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);
|
---|
| 588 |
|
---|
| 589 | Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
|
---|
| 590 | Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
|
---|
| 591 | Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
|
---|
| 592 | Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
|
---|
| 593 | Firmata.attach(SET_PIN_MODE, setPinModeCallback);
|
---|
| 594 | Firmata.attach(START_SYSEX, sysexCallback);
|
---|
| 595 | Firmata.attach(SYSTEM_RESET, systemResetCallback);
|
---|
| 596 |
|
---|
| 597 | Firmata.begin(57600);
|
---|
| 598 | systemResetCallback(); // reset to default config
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | /*==============================================================================
|
---|
| 602 | * LOOP()
|
---|
| 603 | *============================================================================*/
|
---|
| 604 | void loop()
|
---|
| 605 | {
|
---|
| 606 | byte pin, analogPin;
|
---|
| 607 |
|
---|
| 608 | /* DIGITALREAD - as fast as possible, check for changes and output them to the
|
---|
| 609 | * FTDI buffer using Serial.print() */
|
---|
| 610 | checkDigitalInputs();
|
---|
| 611 |
|
---|
| 612 | /* SERIALREAD - processing incoming messagse as soon as possible, while still
|
---|
| 613 | * checking digital inputs. */
|
---|
| 614 | while (Firmata.available())
|
---|
| 615 | Firmata.processInput();
|
---|
| 616 |
|
---|
| 617 | /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
|
---|
| 618 | * 60 bytes. use a timer to sending an event character every 4 ms to
|
---|
| 619 | * trigger the buffer to dump. */
|
---|
| 620 |
|
---|
| 621 | currentMillis = millis();
|
---|
| 622 | if (currentMillis - previousMillis > samplingInterval) {
|
---|
| 623 | previousMillis += samplingInterval;
|
---|
| 624 | /* ANALOGREAD - do all analogReads() at the configured sampling interval */
|
---|
| 625 | for (pin = 0; pin < TOTAL_PINS; pin++) {
|
---|
| 626 | if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
|
---|
| 627 | analogPin = PIN_TO_ANALOG(pin);
|
---|
| 628 | if (analogInputsToReport & (1 << analogPin)) {
|
---|
| 629 | Firmata.sendAnalog(analogPin, analogRead(analogPin));
|
---|
| 630 | }
|
---|
| 631 | }
|
---|
| 632 | }
|
---|
| 633 | // report i2c data for all device with read continuous mode enabled
|
---|
| 634 | if (queryIndex > -1) {
|
---|
| 635 | for (byte i = 0; i < queryIndex + 1; i++) {
|
---|
| 636 | readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 | }
|
---|