Skip to content

Instantly share code, notes, and snippets.

@focalintent
Last active August 29, 2015 14:20
Show Gist options
  • Save focalintent/f1b1fc3c024b04fabbaf to your computer and use it in GitHub Desktop.
Save focalintent/f1b1fc3c024b04fabbaf to your computer and use it in GitHub Desktop.
APA102VController
template <uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER = BGR, uint8_t SPI_SPEED = DATA_RATE_MHZ(24)>
class APA102VController : public CLEDController {
typedef SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED> SPI;
SPI mSPI;
void startBoundary() { mSPI.writeWord(0); mSPI.writeWord(0); }
void endBoundary(int nLeds) { int nBytes = (nLeds/64); do { mSPI.writeByte(0xFF); mSPI.writeByte(0x00); mSPI.writeByte(0x00); mSPI.writeByte(0x00); } while(nBytes--); }
inline void writeLed(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t v) __attribute__((always_inline)) {
mSPI.writeByte(0xE0 | (v&0x1F)); mSPI.writeByte(b0); mSPI.writeByte(b1); mSPI.writeByte(b2);
}
public:
APA102VController() {}
virtual void init() {
mSPI.init();
}
virtual void clearLeds(int nLeds) {
showColor(CRGB(0,0,0), nLeds, CRGB(0,0,0),0);
}
/// show function w/integer brightness, will scale for color correction and temperature
void show(const struct CRGB *data, int nLeds, uint8_t brightness, const uint8_t *vdata) {
show(data, nLeds, getAdjustment(brightness), vdata);
}
/// show function w/integer brightness, will scale for color correction and temperature
void showColor(const struct CRGB &data, int nLeds, uint8_t brightness, uint8_t v) {
showColor(data, nLeds, getAdjustment(brightness), v);
}
protected:
virtual void showColor(const struct CRGB & data, int nLeds, CRGB scale) { showColor(data, nLeds, scale, 0x1F); }
virtual void showColor(const struct CRGB & data, int nLeds, CRGB scale, const uint8_t & v) {
PixelController<RGB_ORDER> pixels(data, nLeds, scale, getDither());
mSPI.select();
startBoundary();
for(int i = 0; i < nLeds; i++) {
writeLed(pixels.loadAndScale0(), pixels.loadAndScale1(), pixels.loadAndScale2(),v);
pixels.stepDithering();
}
endBoundary(nLeds);
mSPI.waitFully();
mSPI.release();
}
virtual void show(const struct CRGB *data, int nLeds, CRGB scale) {
show(data, nLeds, scale, NULL);
}
virtual void show(const struct CRGB *data, int nLeds, CRGB scale, const uint8_t *vdata) {
PixelController<RGB_ORDER> pixels(data, nLeds, scale, getDither());
mSPI.select();
startBoundary();
for(int i = 0; i < nLeds; i++) {
writeLed(pixels.loadAndScale0(), pixels.loadAndScale1(), pixels.loadAndScale2(),(vdata ? vdata[i] : 0x1F));
pixels.advanceData();
pixels.stepDithering();
}
endBoundary(nLeds);
mSPI.waitFully();
mSPI.release();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment