Created
March 18, 2014 00:34
-
-
Save uwekamper/9611344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lass LedChainWS : public Component { | |
public: | |
LedChainWS() | |
: Component(outPorts, 2) | |
, pindata(-1) | |
, pinclk(-1) | |
, number(-1) | |
, initialized(false) | |
, currentPixelAddress(-1) | |
#ifdef HAVE_ADAFRUIT_WS2801 | |
, ws(50) // N,DPIN,CLKPIN. We update these things later | |
#endif | |
{} | |
virtual void process(Packet in, MicroFlo::PortId port) { | |
using namespace LedChainWSPorts; | |
if (port == InPorts::in) { | |
if (currentPixelAddress == -1 && in.isNumber()) { | |
currentPixelAddress = in.asInteger(); | |
} else if (currentPixelAddress != -1 && in.isInteger()) { | |
updateCurrentPixel((uint32_t)in.asInteger()); | |
currentPixelAddress = -1; | |
} else if (in.isEndBracket() || in.isStartBracket()) { | |
// To recover in case someone sent us non-grouped | |
// data which got us into a bogus state | |
currentPixelAddress = -1; | |
} | |
} else if (port == InPorts::pindata) { | |
pindata = in.asInteger(); | |
initialized = false; | |
send(Packet(initialized), OutPorts::ready); | |
tryInitialize(); | |
} else if (port == InPorts::pinclk) { | |
pinclk = in.asInteger(); | |
initialized = false; | |
send(Packet(initialized), OutPorts::ready); | |
tryInitialize(); | |
} else if (port == InPorts::pixels) { | |
number = in.asInteger(); | |
initialized = false; | |
send(Packet(initialized), OutPorts::ready); | |
tryInitialize(); | |
} else if (port == InPorts::show) { | |
if (initialized) { | |
#ifdef HAVE_ADAFRUIT_WS2801 | |
ws.show(); | |
#endif | |
} | |
} | |
} | |
private: | |
void tryInitialize() { | |
using namespace LedChainWSPorts; | |
if (initialized) { // || number < 0 || pindata < 0 || pinclk < 0) { | |
return; | |
} | |
#ifdef HAVE_ADAFRUIT_WS2801 | |
// ws.updateLength(number); | |
// ws.updatePins(pindata, pinclk); | |
ws.begin(); | |
int i; | |
int32_t c; | |
byte r; | |
r = 255; | |
byte g; | |
g = 0; | |
byte b; | |
b = 0; | |
c = r; | |
c <<= 8; | |
c |= g; | |
c <<= 8; | |
c |= b; | |
for (i=0; i < ws.numPixels(); i++) { | |
ws.setPixelColor(i, c); | |
ws.show(); | |
delay(10); | |
} | |
r = 0; | |
g = 0; | |
b = 0; | |
c = r; | |
c <<= 8; | |
c |= g; | |
c <<= 8; | |
c |= b; | |
for (i=0; i < ws.numPixels(); i++) { | |
ws.setPixelColor(i, c); | |
ws.show(); | |
delay(10); | |
} | |
#endif | |
initialized = true; | |
send(Packet(initialized), OutPorts::ready); | |
} | |
void updateCurrentPixel(uint32_t rgb) { | |
using namespace LedChainWSPorts; | |
if (!initialized || currentPixelAddress <= 0 | |
|| currentPixelAddress >= number) { | |
return; | |
} | |
#ifdef HAVE_ADAFRUIT_WS2801 | |
ws.setPixelColor(currentPixelAddress, rgb); | |
ws.show(); | |
#endif | |
const MicroFlo::PortId p = OutPorts::pixelset; | |
send(Packet(MsgBracketStart), p); | |
send(Packet((long)currentPixelAddress)); | |
send(Packet((long)rgb)); | |
send(Packet(MsgBracketEnd), p); | |
} | |
private: | |
int8_t pindata; | |
int8_t pinclk; | |
int8_t number; | |
bool initialized; | |
int currentPixelAddress; // -1 means waiting for pixel index, else waiting for value | |
Connection outPorts[2]; | |
#ifdef HAVE_ADAFRUIT_WS2801 | |
Adafruit_WS2801 ws; | |
#endif | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment