From 73998982a255eee165630359ca4e970c92707a8d Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Thu, 4 Dec 2014 21:12:20 -0800 Subject: [PATCH 1/5] initial boilerplate for neopixelFirmata --- Firmata.h | 4 +++- utility/NeopixelFirmata.cpp | 38 +++++++++++++++++++++++++++++++++++++ utility/NeopixelFirmata.h | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 utility/NeopixelFirmata.cpp create mode 100644 utility/NeopixelFirmata.h diff --git a/Firmata.h b/Firmata.h index 80f37259..d96dad8a 100644 --- a/Firmata.h +++ b/Firmata.h @@ -42,6 +42,7 @@ // extended command set using sysex (0-127/0x00-0x7F) /* 0x00-0x0F reserved for user-defined commands */ #define ENCODER_DATA 0x61 // reply with encoders current positions +#define NEOPIXEL_DATA 0x62 // control neo pixels #define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq #define STRING_DATA 0x71 // a string message with 14-bits per char #define STEPPER_DATA 0x72 // control a stepper motor @@ -80,8 +81,9 @@ #define ONEWIRE 0x07 // pin configured for 1-wire #define STEPPER 0x08 // pin configured for stepper motor #define ENCODER 0x09 // pin configured for encoders +#define NEOPIXEL 0x0A // pine configured for neopixels #define IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse -#define TOTAL_PIN_MODES 11 +#define TOTAL_PIN_MODES 12 extern "C" { // callback function types diff --git a/utility/NeopixelFirmata.cpp b/utility/NeopixelFirmata.cpp new file mode 100644 index 00000000..40510d14 --- /dev/null +++ b/utility/NeopixelFirmata.cpp @@ -0,0 +1,38 @@ +#include "NeopixelFirmata.h" + +class NeopixelFirmataImpl : public NeopixelFirmata +{ +public: + NeopixelFirmataImpl(); + virtual void handleCapability(byte pin); + virtual boolean handlePinMode(byte pin, int mode); + virtual boolean handleSysex(byte command, byte argc, byte* argv); + virtual void reset(); +}; + +NeopixelFirmataImpl::NeopixelFirmataImpl() +{ +} + +void NeopixelFirmataImpl::handleCapability(byte pin) +{ +} + +boolean NeopixelFirmataImpl::handlePinMode(byte pin, int mode) +{ + return false; +} + +boolean NeopixelFirmataImpl::handleSysex(byte command, byte argc, byte* argv) +{ + return false; +} + +void NeopixelFirmataImpl::reset() +{ +} + +NeopixelFirmata* neopixelFirmataFactory() +{ + return new NeopixelFirmataImpl(); +} \ No newline at end of file diff --git a/utility/NeopixelFirmata.h b/utility/NeopixelFirmata.h new file mode 100644 index 00000000..e8410332 --- /dev/null +++ b/utility/NeopixelFirmata.h @@ -0,0 +1,19 @@ +#ifndef NeopixelFirmata_H +#define NeopixelFirmata_H + +#include +#include "FirmataExt.h" + +class NeopixelFirmata : public FirmataFeature +{ +public: + NeopixelFirmata() {} + virtual void handleCapability(byte pin) = 0; + virtual boolean handlePinMode(byte pin, int mode) = 0; + virtual boolean handleSysex(byte command, byte argc, byte* argv) = 0; + virtual void reset() = 0; +}; + +NeopixelFirmata* neopixelFirmataFactory(); + +#endif // NeopixelFirmata_H From 3513fc72cffc4f371932dcfadbba7da7cc92aa7d Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Thu, 4 Dec 2014 22:25:56 -0800 Subject: [PATCH 2/5] Initial Checkin for Neopixel that is complet-ish --- utility/NeopixelFirmata.cpp | 112 +++++++++++++++++++++++++++++++++++- utility/NeopixelFirmata.h | 18 ++++++ 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/utility/NeopixelFirmata.cpp b/utility/NeopixelFirmata.cpp index 40510d14..3753fbda 100644 --- a/utility/NeopixelFirmata.cpp +++ b/utility/NeopixelFirmata.cpp @@ -1,4 +1,7 @@ #include "NeopixelFirmata.h" +#include + +#define TOBYTE(loc) ((argv[loc] + (argv[loc+1] << 7))&0xFF) class NeopixelFirmataImpl : public NeopixelFirmata { @@ -8,28 +11,135 @@ class NeopixelFirmataImpl : public NeopixelFirmata virtual boolean handlePinMode(byte pin, int mode); virtual boolean handleSysex(byte command, byte argc, byte* argv); virtual void reset(); + +private: + Adafruit_NeoPixel* pixels; + void initialize(byte pin, byte count, byte order, byte speed); + void reportBrightness(); + void setPixel(byte position, byte red, byte green, byte blue); }; NeopixelFirmataImpl::NeopixelFirmataImpl() { + pixels = NULL; } void NeopixelFirmataImpl::handleCapability(byte pin) { + if (IS_PIN_DIGITAL(pin)) + { + Firmata.write(NEOPIXEL); + Firmata.write(1); // Not sure if this is necessary + } } boolean NeopixelFirmataImpl::handlePinMode(byte pin, int mode) { + if (mode == NEOPIXEL && IS_PIN_DIGITAL(pin)) + { + return true; + } return false; } boolean NeopixelFirmataImpl::handleSysex(byte command, byte argc, byte* argv) { - return false; + byte tmp; + + if (command != NEOPIXEL_DATA) + { + return false; + } + + byte npCommand = argv[0]; + if (pixels == NULL && npCommand != NEOPIXEL_CMD_INIT) + { + // Firmata.sendString("NeoPixel: Not Initialized"); + return false; + } + + switch(npCommand) { + case NEOPIXEL_CMD_INIT: + initialize( + argv[NEOPIXEL_INIT_PARAM_PIN], + TOBYTE(NEOPIXEL_INIT_PARAM_COUNT), + argv[NEOPIXEL_INIT_PARAM_ORDER], + argv[NEOPIXEL_INIT_PARAM_SPEED] + ); + break; + + case NEOPIXEL_CMD_CLEAR: + pixels->clear(); + break; + + case NEOPIXEL_CMD_SHOW: + pixels->show(); + break; + + case NEOPIXEL_CMD_BRIGHTNESS: + if (argc>2) { + pixels->setBrightness(TOBYTE(NEOPIXEL_BRIGHTNESS_PARAM_VALUE)); + } else { + reportBrightness(); + } + break; + + case NEOPIXEL_CMD_PIXEL: + tmp = TOBYTE(NEOPIXEL_PIXEL_PARAM_LOCATION); + + if(tmp > pixels->numPixels()) + { + // Firmata.sendString("NeoPixel: Location out of range"); + return false; + } + + setPixel( + tmp, + TOBYTE(NEOPIXEL_PIXEL_PARAM_RED), + TOBYTE(NEOPIXEL_PIXEL_PARAM_GREEN), + TOBYTE(NEOPIXEL_PIXEL_PARAM_BLUE) + ); + break; + + default: + return false; + } + + return true; + } void NeopixelFirmataImpl::reset() { + if (pixels != NULL) + { + delete pixels; + pixels = NULL; + } +} + +void NeopixelFirmataImpl::initialize(byte pin, byte count, byte order, byte speed) +{ + reset(); + + pixels = new Adafruit_NeoPixel(count, pin, order + speed); + pixels->begin(); +} + +void NeopixelFirmataImpl::reportBrightness() +{ + byte brightness = pixels->getBrightness(); + Firmata.write(START_SYSEX); + Firmata.write(NEOPIXEL_DATA); + Firmata.write(NEOPIXEL_CMD_BRIGHTNESS); + Firmata.write(brightness & 0x7F); + Firmata.write((brightness >> 7) & 0x7F); + Firmata.write(END_SYSEX); +} + +void NeopixelFirmataImpl::setPixel(byte location, byte red, byte green, byte blue) +{ + pixels->setPixelColor(location, red, green, blue); } NeopixelFirmata* neopixelFirmataFactory() diff --git a/utility/NeopixelFirmata.h b/utility/NeopixelFirmata.h index e8410332..0ef31af8 100644 --- a/utility/NeopixelFirmata.h +++ b/utility/NeopixelFirmata.h @@ -4,6 +4,24 @@ #include #include "FirmataExt.h" +#define NEOPIXEL_CMD_INIT 0x01 +#define NEOPIXEL_CMD_PIXEL 0x02 +#define NEOPIXEL_CMD_CLEAR 0x04 +#define NEOPIXEL_CMD_SHOW 0x05 +#define NEOPIXEL_CMD_BRIGHTNESS 0x06 + +#define NEOPIXEL_INIT_PARAM_PIN 0x01 +#define NEOPIXEL_INIT_PARAM_COUNT 0x02 +#define NEOPIXEL_INIT_PARAM_ORDER 0x04 +#define NEOPIXEL_INIT_PARAM_SPEED 0x05 + +#define NEOPIXEL_PIXEL_PARAM_LOCATION 0x01 +#define NEOPIXEL_PIXEL_PARAM_RED 0x03 +#define NEOPIXEL_PIXEL_PARAM_GREEN 0x05 +#define NEOPIXEL_PIXEL_PARAM_BLUE 0x07 + +#define NEOPIXEL_BRIGHTNESS_PARAM_VALUE 0x01 + class NeopixelFirmata : public FirmataFeature { public: From f740d66a475a7afd67ee081597352a2995deac35 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Fri, 5 Dec 2014 22:49:54 -0800 Subject: [PATCH 3/5] Adding a report done and tweaking small things to get it working --- utility/NeopixelFirmata.cpp | 18 ++++++++++++++++++ utility/NeopixelFirmata.h | 1 + 2 files changed, 19 insertions(+) diff --git a/utility/NeopixelFirmata.cpp b/utility/NeopixelFirmata.cpp index 3753fbda..af3d23fc 100644 --- a/utility/NeopixelFirmata.cpp +++ b/utility/NeopixelFirmata.cpp @@ -17,6 +17,7 @@ class NeopixelFirmataImpl : public NeopixelFirmata void initialize(byte pin, byte count, byte order, byte speed); void reportBrightness(); void setPixel(byte position, byte red, byte green, byte blue); + void reportDone(); }; NeopixelFirmataImpl::NeopixelFirmataImpl() @@ -102,6 +103,7 @@ boolean NeopixelFirmataImpl::handleSysex(byte command, byte argc, byte* argv) break; default: + // Firmata.sendString("NeoPixel: Unknown Command"); return false; } @@ -124,6 +126,14 @@ void NeopixelFirmataImpl::initialize(byte pin, byte count, byte order, byte spee pixels = new Adafruit_NeoPixel(count, pin, order + speed); pixels->begin(); + for(int i=0; isetPixelColor(i, 0, 0, 255); + pixels->show(); + delay(50); + } + pixels->clear(); + pixels->show(); + reportDone(); } void NeopixelFirmataImpl::reportBrightness() @@ -137,6 +147,14 @@ void NeopixelFirmataImpl::reportBrightness() Firmata.write(END_SYSEX); } +void NeopixelFirmataImpl::reportDone() +{ + Firmata.write(START_SYSEX); + Firmata.write(NEOPIXEL_DATA); + Firmata.write(NEOPIXEL_CMD_DONE); + Firmata.write(END_SYSEX); +} + void NeopixelFirmataImpl::setPixel(byte location, byte red, byte green, byte blue) { pixels->setPixelColor(location, red, green, blue); diff --git a/utility/NeopixelFirmata.h b/utility/NeopixelFirmata.h index 0ef31af8..f78ab7c4 100644 --- a/utility/NeopixelFirmata.h +++ b/utility/NeopixelFirmata.h @@ -9,6 +9,7 @@ #define NEOPIXEL_CMD_CLEAR 0x04 #define NEOPIXEL_CMD_SHOW 0x05 #define NEOPIXEL_CMD_BRIGHTNESS 0x06 +#define NEOPIXEL_CMD_DONE 0x07 // Response #define NEOPIXEL_INIT_PARAM_PIN 0x01 #define NEOPIXEL_INIT_PARAM_COUNT 0x02 From 43ea815ae4690ef4cb17ee78e0074b6a69393905 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Sat, 6 Dec 2014 08:12:23 -0800 Subject: [PATCH 4/5] hacked up version of configurablefirmata that adds just neopixel support --- test/neopixel_test/neopixel_test.ino | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/neopixel_test/neopixel_test.ino diff --git a/test/neopixel_test/neopixel_test.ino b/test/neopixel_test/neopixel_test.ino new file mode 100644 index 00000000..cb7e2cb7 --- /dev/null +++ b/test/neopixel_test/neopixel_test.ino @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +#include +FirmataExt firmataExt; + +#include "utility/neopixelFirmata.h" +NeopixelFirmata* neopixelFirmata = neopixelFirmataFactory(); + +/*============================================================================== + * FUNCTIONS + *============================================================================*/ + +void systemResetCallback() +{ + // initialize a defalt state + + // pins with analog capability default to analog input + // otherwise, pins default to digital output + for (byte i=0; i < TOTAL_PINS; i++) { + } + + firmataExt.reset(); +} + +/*============================================================================== + * SETUP() + *============================================================================*/ + +void setup() +{ + Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION); + firmataExt.addFeature(*neopixelFirmata); + Firmata.attach(SYSTEM_RESET, systemResetCallback); + + // start up the default Firmata using Serial interface: + Firmata.begin(57600); + systemResetCallback(); // reset to default config +} + +/*============================================================================== + * LOOP() + *============================================================================*/ +void loop() +{ + + while(Firmata.available()) { + Firmata.processInput(); + } + +} + From 1f94a60e95645618b03240488482b487b22e4d16 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Mon, 8 Dec 2014 07:51:39 -0800 Subject: [PATCH 5/5] refactoring to a more generic implementation, added query command for strip type --- Firmata.h | 4 +- utility/LedStripFirmata.cpp | 75 +++++++++++++++++++++++++++++ utility/LedStripFirmata.h | 50 +++++++++++++++++++ utility/NeopixelFirmata.cpp | 96 ++++++++----------------------------- utility/NeopixelFirmata.h | 46 +++++++----------- 5 files changed, 163 insertions(+), 108 deletions(-) create mode 100644 utility/LedStripFirmata.cpp create mode 100644 utility/LedStripFirmata.h diff --git a/Firmata.h b/Firmata.h index d96dad8a..a7598e4b 100644 --- a/Firmata.h +++ b/Firmata.h @@ -42,7 +42,7 @@ // extended command set using sysex (0-127/0x00-0x7F) /* 0x00-0x0F reserved for user-defined commands */ #define ENCODER_DATA 0x61 // reply with encoders current positions -#define NEOPIXEL_DATA 0x62 // control neo pixels +#define LED_STRIP_DATA 0x62 // control neo pixels #define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq #define STRING_DATA 0x71 // a string message with 14-bits per char #define STEPPER_DATA 0x72 // control a stepper motor @@ -81,7 +81,7 @@ #define ONEWIRE 0x07 // pin configured for 1-wire #define STEPPER 0x08 // pin configured for stepper motor #define ENCODER 0x09 // pin configured for encoders -#define NEOPIXEL 0x0A // pine configured for neopixels +#define LED_STRIP 0x0A // pin configured for led strips #define IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse #define TOTAL_PIN_MODES 12 diff --git a/utility/LedStripFirmata.cpp b/utility/LedStripFirmata.cpp new file mode 100644 index 00000000..0a681ba8 --- /dev/null +++ b/utility/LedStripFirmata.cpp @@ -0,0 +1,75 @@ +#include "LedStripFirmata.h" +// Choose one pixel subtype to include +#include "NeopixelFirmata.h" +// #include "Ws2801PixelFirmata.h" // Not completed yet + +void LedStripFirmata::handleCapability(byte pin) +{ + if (IS_PIN_DIGITAL(pin)) + { + Firmata.write(LED_STRIP); + Firmata.write(1); // Not sure if this is necessary + } +} + +boolean LedStripFirmata::handlePinMode(byte pin, int mode) +{ + if (mode == LED_STRIP && IS_PIN_DIGITAL(pin)) + { + return true; + } + return false; +} + +void LedStripFirmata::reportDone() +{ + Firmata.write(START_SYSEX); + Firmata.write(LED_STRIP_DATA); + Firmata.write(LED_STRIP_CMD_DONE); + Firmata.write(END_SYSEX); +} + +void LedStripFirmata::reportType() +{ + Firmata.write(START_SYSEX); + Firmata.write(LED_STRIP_DATA); + Firmata.write(LED_STRIP_CMD_QUERY); + Firmata.write(ledType()); + Firmata.write(END_SYSEX); +} + + +boolean LedStripFirmata::handleSysex(byte command, byte argc, byte* argv) +{ + if (command != LED_STRIP_DATA) + { + return false; + } + + byte npCommand = argv[0]; + if (!initialized && npCommand != LED_STRIP_CMD_INIT && npCommand != LED_STRIP_CMD_QUERY) + { + // Firmata.sendString("Led Strip Not Initialized: Not Initialized"); + return false; + } + + switch(npCommand) { + case LED_STRIP_CMD_QUERY: + reportType(); + return true; + + default: + return handleStripCommand(npCommand, argc, argv); + } + + return true; + +} + + +LedStripFirmata* ledStripFirmataFactory() +{ + #ifdef NeopixelFirmata_H + return new NeopixelFirmataImpl(); + #endif +} diff --git a/utility/LedStripFirmata.h b/utility/LedStripFirmata.h new file mode 100644 index 00000000..fb21da34 --- /dev/null +++ b/utility/LedStripFirmata.h @@ -0,0 +1,50 @@ +#ifndef LedStripFirmata_H +#define LedStripFirmata_H + +#include +#include "FirmataExt.h" + +#define LED_STRIP_CMD_QUERY 0x00 +#define LED_STRIP_CMD_INIT 0x01 +#define LED_STRIP_CMD_PIXEL 0x02 +#define LED_STRIP_CMD_CLEAR 0x04 +#define LED_STRIP_CMD_SHOW 0x05 +#define LED_STRIP_CMD_BRIGHTNESS 0x06 +#define LED_STRIP_CMD_DONE 0x07 // Response + +#define LED_STRIP_INIT_PARAM_PIN 0x01 +#define LED_STRIP_INIT_PARAM_COUNT 0x02 +#define LED_STRIP_INIT_PARAM_ORDER 0x04 +#define LED_STRIP_INIT_PARAM_SPEED 0x05 + +#define LED_STRIP_PIXEL_PARAM_LOCATION 0x01 +#define LED_STRIP_PIXEL_PARAM_RED 0x03 +#define LED_STRIP_PIXEL_PARAM_GREEN 0x05 +#define LED_STRIP_PIXEL_PARAM_BLUE 0x07 + +#define LED_STRIP_BRIGHTNESS_PARAM_VALUE 0x01 + +#define LED_STRIP_TYPE_NEOPIXEL 0x00 +#define LED_STRIP_TYPE_WS2801 0x01 + +class LedStripFirmata : public FirmataFeature +{ +public: + LedStripFirmata() {} + void handleCapability(byte pin); + boolean handlePinMode(byte pin, int mode); + boolean handleSysex(byte command, byte argc, byte* argv); + virtual void reset() = 0; + +protected: + virtual boolean handleStripCommand(byte command, byte argc, byte* argv) = 0; + void reportDone(); + void reportType(); + virtual byte ledType() = 0; + + boolean initialized = false; +}; + +LedStripFirmata* ledStripFirmataFactory(); + +#endif // LedStripFirmata_H diff --git a/utility/NeopixelFirmata.cpp b/utility/NeopixelFirmata.cpp index af3d23fc..97e6d924 100644 --- a/utility/NeopixelFirmata.cpp +++ b/utility/NeopixelFirmata.cpp @@ -1,92 +1,47 @@ +#include "LedStripFirmata.h" #include "NeopixelFirmata.h" #include #define TOBYTE(loc) ((argv[loc] + (argv[loc+1] << 7))&0xFF) -class NeopixelFirmataImpl : public NeopixelFirmata -{ -public: - NeopixelFirmataImpl(); - virtual void handleCapability(byte pin); - virtual boolean handlePinMode(byte pin, int mode); - virtual boolean handleSysex(byte command, byte argc, byte* argv); - virtual void reset(); - -private: - Adafruit_NeoPixel* pixels; - void initialize(byte pin, byte count, byte order, byte speed); - void reportBrightness(); - void setPixel(byte position, byte red, byte green, byte blue); - void reportDone(); -}; - NeopixelFirmataImpl::NeopixelFirmataImpl() { pixels = NULL; } -void NeopixelFirmataImpl::handleCapability(byte pin) -{ - if (IS_PIN_DIGITAL(pin)) - { - Firmata.write(NEOPIXEL); - Firmata.write(1); // Not sure if this is necessary - } -} - -boolean NeopixelFirmataImpl::handlePinMode(byte pin, int mode) -{ - if (mode == NEOPIXEL && IS_PIN_DIGITAL(pin)) - { - return true; - } - return false; -} - -boolean NeopixelFirmataImpl::handleSysex(byte command, byte argc, byte* argv) +boolean NeopixelFirmataImpl::handleStripCommand(byte npCommand, byte argc, byte* argv) { byte tmp; - if (command != NEOPIXEL_DATA) - { - return false; - } - - byte npCommand = argv[0]; - if (pixels == NULL && npCommand != NEOPIXEL_CMD_INIT) - { - // Firmata.sendString("NeoPixel: Not Initialized"); - return false; - } - switch(npCommand) { - case NEOPIXEL_CMD_INIT: + + case LED_STRIP_CMD_INIT: initialize( - argv[NEOPIXEL_INIT_PARAM_PIN], - TOBYTE(NEOPIXEL_INIT_PARAM_COUNT), - argv[NEOPIXEL_INIT_PARAM_ORDER], - argv[NEOPIXEL_INIT_PARAM_SPEED] + argv[LED_STRIP_INIT_PARAM_PIN], + TOBYTE(LED_STRIP_INIT_PARAM_COUNT), + argv[LED_STRIP_INIT_PARAM_ORDER], + argv[LED_STRIP_INIT_PARAM_SPEED] ); break; - case NEOPIXEL_CMD_CLEAR: + case LED_STRIP_CMD_CLEAR: pixels->clear(); break; - case NEOPIXEL_CMD_SHOW: + case LED_STRIP_CMD_SHOW: pixels->show(); break; - case NEOPIXEL_CMD_BRIGHTNESS: + case LED_STRIP_CMD_BRIGHTNESS: if (argc>2) { - pixels->setBrightness(TOBYTE(NEOPIXEL_BRIGHTNESS_PARAM_VALUE)); + pixels->setBrightness(TOBYTE(LED_STRIP_BRIGHTNESS_PARAM_VALUE)); } else { reportBrightness(); } break; - case NEOPIXEL_CMD_PIXEL: - tmp = TOBYTE(NEOPIXEL_PIXEL_PARAM_LOCATION); + case LED_STRIP_CMD_PIXEL: + tmp = TOBYTE(LED_STRIP_PIXEL_PARAM_LOCATION); if(tmp > pixels->numPixels()) { @@ -96,9 +51,9 @@ boolean NeopixelFirmataImpl::handleSysex(byte command, byte argc, byte* argv) setPixel( tmp, - TOBYTE(NEOPIXEL_PIXEL_PARAM_RED), - TOBYTE(NEOPIXEL_PIXEL_PARAM_GREEN), - TOBYTE(NEOPIXEL_PIXEL_PARAM_BLUE) + TOBYTE(LED_STRIP_PIXEL_PARAM_RED), + TOBYTE(LED_STRIP_PIXEL_PARAM_GREEN), + TOBYTE(LED_STRIP_PIXEL_PARAM_BLUE) ); break; @@ -140,27 +95,14 @@ void NeopixelFirmataImpl::reportBrightness() { byte brightness = pixels->getBrightness(); Firmata.write(START_SYSEX); - Firmata.write(NEOPIXEL_DATA); - Firmata.write(NEOPIXEL_CMD_BRIGHTNESS); + Firmata.write(LED_STRIP_DATA); + Firmata.write(LED_STRIP_CMD_BRIGHTNESS); Firmata.write(brightness & 0x7F); Firmata.write((brightness >> 7) & 0x7F); Firmata.write(END_SYSEX); } -void NeopixelFirmataImpl::reportDone() -{ - Firmata.write(START_SYSEX); - Firmata.write(NEOPIXEL_DATA); - Firmata.write(NEOPIXEL_CMD_DONE); - Firmata.write(END_SYSEX); -} - void NeopixelFirmataImpl::setPixel(byte location, byte red, byte green, byte blue) { pixels->setPixelColor(location, red, green, blue); } - -NeopixelFirmata* neopixelFirmataFactory() -{ - return new NeopixelFirmataImpl(); -} \ No newline at end of file diff --git a/utility/NeopixelFirmata.h b/utility/NeopixelFirmata.h index f78ab7c4..e4fabe40 100644 --- a/utility/NeopixelFirmata.h +++ b/utility/NeopixelFirmata.h @@ -1,38 +1,26 @@ #ifndef NeopixelFirmata_H #define NeopixelFirmata_H -#include -#include "FirmataExt.h" +#include "LedStripFirmata.h" +#include "Adafruit_Neopixel.h" -#define NEOPIXEL_CMD_INIT 0x01 -#define NEOPIXEL_CMD_PIXEL 0x02 -#define NEOPIXEL_CMD_CLEAR 0x04 -#define NEOPIXEL_CMD_SHOW 0x05 -#define NEOPIXEL_CMD_BRIGHTNESS 0x06 -#define NEOPIXEL_CMD_DONE 0x07 // Response - -#define NEOPIXEL_INIT_PARAM_PIN 0x01 -#define NEOPIXEL_INIT_PARAM_COUNT 0x02 -#define NEOPIXEL_INIT_PARAM_ORDER 0x04 -#define NEOPIXEL_INIT_PARAM_SPEED 0x05 - -#define NEOPIXEL_PIXEL_PARAM_LOCATION 0x01 -#define NEOPIXEL_PIXEL_PARAM_RED 0x03 -#define NEOPIXEL_PIXEL_PARAM_GREEN 0x05 -#define NEOPIXEL_PIXEL_PARAM_BLUE 0x07 - -#define NEOPIXEL_BRIGHTNESS_PARAM_VALUE 0x01 - -class NeopixelFirmata : public FirmataFeature +class NeopixelFirmataImpl : public LedStripFirmata { public: - NeopixelFirmata() {} - virtual void handleCapability(byte pin) = 0; - virtual boolean handlePinMode(byte pin, int mode) = 0; - virtual boolean handleSysex(byte command, byte argc, byte* argv) = 0; - virtual void reset() = 0; + NeopixelFirmataImpl(); + virtual void reset(); + +protected: + byte ledType() { return LED_STRIP_TYPE_NEOPIXEL; } + virtual boolean handleStripCommand(byte command, byte argc, byte* argv); + bool initialized; + +private: + Adafruit_NeoPixel* pixels; + void initialize(byte pin, byte count, byte order, byte speed); + void reportBrightness(); + void setPixel(byte position, byte red, byte green, byte blue); }; -NeopixelFirmata* neopixelFirmataFactory(); - #endif // NeopixelFirmata_H +