diff options
Diffstat (limited to 'libraries/Firmata/Firmata.cpp')
-rw-r--r-- | libraries/Firmata/Firmata.cpp | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/libraries/Firmata/Firmata.cpp b/libraries/Firmata/Firmata.cpp index e30deae..e81c10b 100644 --- a/libraries/Firmata/Firmata.cpp +++ b/libraries/Firmata/Firmata.cpp @@ -14,9 +14,8 @@ //* Includes //****************************************************************************** -#include "Arduino.h" -#include "HardwareSerial.h" #include "Firmata.h" +#include "HardwareSerial.h" extern "C" { #include <string.h> @@ -27,27 +26,27 @@ extern "C" { //* Support Functions //****************************************************************************** -void sendValueAsTwo7bitBytes(int value) +void FirmataClass::sendValueAsTwo7bitBytes(int value) { - Serial.print(value & B01111111, BYTE); // LSB - Serial.print(value >> 7 & B01111111, BYTE); // MSB + FirmataSerial.write(value & B01111111); // LSB + FirmataSerial.write(value >> 7 & B01111111); // MSB } -void startSysex(void) +void FirmataClass::startSysex(void) { - Serial.print(START_SYSEX, BYTE); + FirmataSerial.write(START_SYSEX); } -void endSysex(void) +void FirmataClass::endSysex(void) { - Serial.print(END_SYSEX, BYTE); + FirmataSerial.write(END_SYSEX); } //****************************************************************************** //* Constructors //****************************************************************************** -FirmataClass::FirmataClass(void) +FirmataClass::FirmataClass(Stream &s) : FirmataSerial(s) { firmwareVersionCount = 0; systemReset(); @@ -66,33 +65,36 @@ void FirmataClass::begin(void) /* begin method for overriding default serial bitrate */ void FirmataClass::begin(long speed) { -#if defined(__AVR_ATmega128__) // Wiring - Serial.begin((uint32_t)speed); -#else Serial.begin(speed); -#endif + FirmataSerial = Serial; blinkVersion(); - delay(300); + printVersion(); + printFirmwareVersion(); +} + +void FirmataClass::begin(Stream &s) +{ + FirmataSerial = s; + systemReset(); printVersion(); printFirmwareVersion(); } // output the protocol version message to the serial port void FirmataClass::printVersion(void) { - Serial.print(REPORT_VERSION, BYTE); - Serial.print(FIRMATA_MAJOR_VERSION, BYTE); - Serial.print(FIRMATA_MINOR_VERSION, BYTE); + FirmataSerial.write(REPORT_VERSION); + FirmataSerial.write(FIRMATA_MAJOR_VERSION); + FirmataSerial.write(FIRMATA_MINOR_VERSION); } void FirmataClass::blinkVersion(void) { // flash the pin with the protocol version pinMode(VERSION_BLINK_PIN,OUTPUT); - pin13strobe(FIRMATA_MAJOR_VERSION, 200, 400); - delay(300); - pin13strobe(2,1,4); // separator, a quick burst - delay(300); - pin13strobe(FIRMATA_MINOR_VERSION, 200, 400); + pin13strobe(FIRMATA_MAJOR_VERSION, 40, 210); + delay(250); + pin13strobe(FIRMATA_MINOR_VERSION, 40, 210); + delay(125); } void FirmataClass::printFirmwareVersion(void) @@ -101,9 +103,9 @@ void FirmataClass::printFirmwareVersion(void) if(firmwareVersionCount) { // make sure that the name has been set before reporting startSysex(); - Serial.print(REPORT_FIRMWARE, BYTE); - Serial.print(firmwareVersionVector[0]); // major version number - Serial.print(firmwareVersionVector[1]); // minor version number + FirmataSerial.write(REPORT_FIRMWARE); + FirmataSerial.write(firmwareVersionVector[0]); // major version number + FirmataSerial.write(firmwareVersionVector[1]); // minor version number for(i=2; i<firmwareVersionCount; ++i) { sendValueAsTwo7bitBytes(firmwareVersionVector[i]); } @@ -141,7 +143,7 @@ void FirmataClass::setFirmwareNameAndVersion(const char *name, byte major, byte int FirmataClass::available(void) { - return Serial.available(); + return FirmataSerial.available(); } @@ -175,7 +177,7 @@ void FirmataClass::processSysexMessage(void) void FirmataClass::processInput(void) { - int inputData = Serial.read(); // this is 'int' to handle -1 when no data + int inputData = FirmataSerial.read(); // this is 'int' to handle -1 when no data int command; // TODO make sure it handles -1 properly @@ -267,7 +269,7 @@ void FirmataClass::processInput(void) void FirmataClass::sendAnalog(byte pin, int value) { // pin can only be 0-15, so chop higher bits - Serial.print(ANALOG_MESSAGE | (pin & 0xF), BYTE); + FirmataSerial.write(ANALOG_MESSAGE | (pin & 0xF)); sendValueAsTwo7bitBytes(value); } @@ -298,9 +300,9 @@ void FirmataClass::sendDigital(byte pin, int value) // send an 8-bit port in a single digital message (protocol v2) void FirmataClass::sendDigitalPort(byte portNumber, int portData) { - Serial.print(DIGITAL_MESSAGE | (portNumber & 0xF),BYTE); - Serial.print((byte)portData % 128, BYTE); // Tx bits 0-6 - Serial.print(portData >> 7, BYTE); // Tx bits 7-13 + FirmataSerial.write(DIGITAL_MESSAGE | (portNumber & 0xF)); + FirmataSerial.write((byte)portData % 128); // Tx bits 0-6 + FirmataSerial.write(portData >> 7); // Tx bits 7-13 } @@ -308,7 +310,7 @@ void FirmataClass::sendSysex(byte command, byte bytec, byte* bytev) { byte i; startSysex(); - Serial.print(command, BYTE); + FirmataSerial.write(command); for(i=0; i<bytec; i++) { sendValueAsTwo7bitBytes(bytev[i]); } @@ -437,6 +439,6 @@ void FirmataClass::pin13strobe(int count, int onInterval, int offInterval) // make one instance for the user to use -FirmataClass Firmata; +FirmataClass Firmata(Serial); |