Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LEDStream for SM16716 Leds #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Arduino/LEDStream_SM16716/FastLED.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "FastLED.h"


CFastLED LEDS;
CFastLED & FastSPI_LED = LEDS;
CFastLED & FastSPI_LED2 = LEDS;
CFastLED & FastLED = LEDS;

uint32_t CRGB::Squant = ((uint32_t)((__TIME__[4]-'0') * 28))<<16 | ((__TIME__[6]-'0')*50)<<8 | ((__TIME__[7]-'0')*28);

CFastLED::CFastLED() {
// clear out the array of led controllers
m_nControllers = NUM_CONTROLLERS;
m_nScale = 255;
memset8(m_Controllers, 0, m_nControllers * sizeof(CControllerInfo));
}

CLEDController *CFastLED::addLeds(CLEDController *pLed,
const struct CRGB *data,
int nLedsOrOffset, int nLedsIfOffset) {
int nOffset = (nLedsIfOffset > 0) ? nLedsOrOffset : 0;
int nLeds = (nLedsIfOffset > 0) ? nLedsIfOffset : nLedsOrOffset;

int target = -1;

// Figure out where to put the new led controller
for(int i = 0; i < m_nControllers; i++) {
if(m_Controllers[i].pLedController == NULL) {
target = i;
break;
}
}

// if we have a spot, use it!
if(target != -1) {
m_Controllers[target].pLedController = pLed;
m_Controllers[target].pLedData = data;
m_Controllers[target].nOffset = nOffset;
m_Controllers[target].nLeds = nLeds;
pLed->init();
return pLed;
}

return NULL;
}

void CFastLED::show(uint8_t scale) {
for(int i = 0; i < m_nControllers; i++) {
if(m_Controllers[i].pLedController != NULL) {
m_Controllers[i].pLedController->show(m_Controllers[i].pLedData + m_Controllers[i].nOffset,
m_Controllers[i].nLeds, scale);
} else {
return;
}
}
}

void CFastLED::showColor(const struct CRGB & color, uint8_t scale) {
for(int i = 0; i < m_nControllers; i++) {
if(m_Controllers[i].pLedController != NULL) {
m_Controllers[i].pLedController->showColor(color, m_Controllers[i].nLeds, scale);
} else {
return;
}
}
}

void CFastLED::clear(boolean writeData) {
if(writeData) {
showColor(CRGB(0,0,0), 0);
}
for(int i = 0; i < m_nControllers; i++) {
if(m_Controllers[i].pLedData != NULL) {
memset8((void*)m_Controllers[i].pLedData, 0, sizeof(struct CRGB) * m_Controllers[i].nLeds);
} else {
return;
}
}
}
147 changes: 147 additions & 0 deletions Arduino/LEDStream_SM16716/FastLED.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#ifndef __INC_FASTSPI_LED2_H
#define __INC_FASTSPI_LED2_H

#include "controller.h"
#include "fastpin.h"
#include "fastspi.h"
#include "clockless.h"
#include "clockless_trinket.h"
#include "lib8tion.h"
#include "hsv2rgb.h"
#include "chipsets.h"
#include "dmx.h"

enum ESPIChipsets {
LPD8806,
WS2801,
SM16716
};

enum EClocklessChipsets {
DMX,
TM1809,
TM1804,
TM1803,
WS2811,
WS2812,
WS2812B,
WS2811_400,
NEOPIXEL,
UCS1903
};

#define NUM_CONTROLLERS 8

class CFastLED {
struct CControllerInfo {
CLEDController *pLedController;
const struct CRGB *pLedData;
int nLeds;
int nOffset;
};

CControllerInfo m_Controllers[NUM_CONTROLLERS];
int m_nControllers;
uint8_t m_nScale;

public:
CFastLED();

CLEDController *addLeds(CLEDController *pLed, const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0);

template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN > CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
switch(CHIPSET) {
case LPD8806: { static LPD8806Controller<DATA_PIN, CLOCK_PIN> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case WS2801: { static WS2801Controller<DATA_PIN, CLOCK_PIN> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case SM16716: { static SM16716Controller<DATA_PIN, CLOCK_PIN> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
}
}

template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER > CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
switch(CHIPSET) {
case LPD8806: { static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case WS2801: { static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case SM16716: { static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
}
}

template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER, uint8_t SPI_DATA_RATE > CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
switch(CHIPSET) {
case LPD8806: { static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case WS2801: { static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
case SM16716: { static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> c; return addLeds(&c, data, nLedsOrOffset, nLedsIfOffset); }
}
}

#ifdef SPI_DATA
template<ESPIChipsets CHIPSET> CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
return addLeds<CHIPSET, SPI_DATA, SPI_CLOCK, RGB>(data, nLedsOrOffset, nLedsIfOffset);
}

template<ESPIChipsets CHIPSET, EOrder RGB_ORDER> CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
return addLeds<CHIPSET, SPI_DATA, SPI_CLOCK, RGB_ORDER>(data, nLedsOrOffset, nLedsIfOffset);
}

template<ESPIChipsets CHIPSET, EOrder RGB_ORDER, uint8_t SPI_DATA_RATE> CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
return addLeds<CHIPSET, SPI_DATA, SPI_CLOCK, RGB_ORDER, SPI_DATA_RATE>(data, nLedsOrOffset, nLedsIfOffset);
}

#endif

template<EClocklessChipsets CHIPSET, uint8_t DATA_PIN>
CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
switch(CHIPSET) {
#ifdef FASTSPI_USE_DMX_SIMPLE
case DMX: { static DMXController<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
#endif
case TM1809: { static TM1809Controller800Khz<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case TM1803: { static TM1803Controller400Khz<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case UCS1903: { static UCS1903Controller400Khz<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case WS2812:
case WS2812B:
case WS2811: { static WS2811Controller800Khz<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case NEOPIXEL: { static WS2811Controller800Khz<DATA_PIN, GRB> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case WS2811_400: { static WS2811Controller400Khz<DATA_PIN> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
}
}

template<EClocklessChipsets CHIPSET, uint8_t DATA_PIN, EOrder RGB_ORDER>
CLEDController *addLeds(const struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0) {
switch(CHIPSET) {
#ifdef FASTSPI_USE_DMX_SIMPLE
case DMX: {static DMXController<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
#endif
case TM1809: { static TM1809Controller800Khz<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case TM1803: { static TM1803Controller400Khz<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case UCS1903: { static UCS1903Controller400Khz<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case WS2812:
case WS2812B:
case NEOPIXEL:
case WS2811: { static WS2811Controller800Khz<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
case WS2811_400: { static WS2811Controller400Khz<DATA_PIN, RGB_ORDER> controller; return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset); }
}
}

void setBrightness(uint8_t scale) { m_nScale = scale; }
uint8_t getBrightness() { return m_nScale; }

/// Update all our controllers with the current led colors, using the passed in brightness
void show(uint8_t scale);

/// Update all our controllers with the current led colors
void show() { show(m_nScale); }

void clear(boolean writeData = false);

void showColor(const struct CRGB & color, uint8_t scale);

void showColor(const struct CRGB & color) { showColor(color, m_nScale); }

};

extern CFastLED & FastSPI_LED;
extern CFastLED & FastSPI_LED2;
extern CFastLED & FastLED;
extern CFastLED LEDS;

#endif
2 changes: 2 additions & 0 deletions Arduino/LEDStream_SM16716/FastSPI_LED2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#warning "This file is going away, please us FastLED.h in the future!"
#include<FastLED.h>
150 changes: 150 additions & 0 deletions Arduino/LEDStream_SM16716/LEDStream_SM16716.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* A 'fork' of the adalight code, with a nice case statement.
*
* Tested with the LM16716 100 led string
*/
#include "FastLED.h"

#define NUM_LEDS 100
/*
* pin 11 -> green for the SM16716
* pin 13 -> blue for the SM16716
*/

// Data pin that led data will be written out over
#define DATA_PIN 11

// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define CLOCK_PIN 13

// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];


#define PIN 4

#define MODE_HDRA 0
#define MODE_HDRd 1
#define MODE_HDRa 2
#define MODE_HDRhi 3
#define MODE_HDRlo 4
#define MODE_HDRchk 5
#define MODE_DATAr 6
#define MODE_DATAg 7
#define MODE_DATAb 8


unsigned long t,
startTime,
lastByteTime,
lastAckTime;

uint8_t hi, lo, chk,
mode = MODE_HDRA;

int channels,channel;

void setup()
{
Serial.begin(115200);
FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RBG>(leds, NUM_LEDS);
clearLeds();
Serial.print("Ada\n");
lastByteTime = startTime = lastAckTime = millis();
}

int readByte(){
while(Serial.available()==0){
;
//FastSPI_LED.stop();
}
//FastSPI_LED.start();
return Serial.read();
}

void clearLeds(){
memset(leds, 0, NUM_LEDS * 3);
FastLED.show();
delay(20);
}

void loop(){
int16_t c;

c = readByte();
t = millis();

if((t - lastAckTime) > 1000) {
Serial.print("Ada\n"); // Send ACK string to host
lastAckTime = t; // Reset counter
}
if ((t - lastByteTime) > 5000) {
mode = MODE_HDRA; // After 5 seconds reset to HDRA
}
lastByteTime = t;
lastAckTime = t;

switch(mode){
case MODE_HDRA:
if (c == 0x41) {
mode = MODE_HDRd;
}
break;
case MODE_HDRd:
if (c == 0x64) {
mode = MODE_HDRa;
}
break;
case MODE_HDRa:
if (c == 0x61) {
mode = MODE_HDRhi;
}
break;
case MODE_HDRhi:
hi = c;
mode = MODE_HDRlo;
break;
case MODE_HDRlo:
lo = c;
mode = MODE_HDRchk;
break;
case MODE_HDRchk:
chk = c;
if (chk == (hi ^ lo ^ 0x55)){
mode = MODE_DATAr;
channels = (long)hi*256+(long)lo;
channel = 0;
} else {
// wrong checksum, reset header
mode = MODE_HDRA;
}
break;
case MODE_DATAr:
// we are in the data business ;-)
// ignore all data for channels higher than NUM_LEDS
if (channel<NUM_LEDS) { leds[channel].r = c; }
mode = MODE_DATAb;
break;
case MODE_DATAb:
// we are in the data business ;-)
// ignore all data for channels higher than NUM_LEDS
if (channel<NUM_LEDS) { leds[channel].b = c; }
mode = MODE_DATAg;
break;
case MODE_DATAg:
// we are in the data business ;-)
// ignore all data for channels higher than NUM_LEDS
if (channel<NUM_LEDS) { leds[channel].g = c; }
channel++;
if (channel>channels) {
FastSPI_LED.show();
mode = MODE_HDRA;
} else {
mode = MODE_DATAr;
}
break;
default:
// I should not be here, back to
mode = MODE_HDRA;
}
}
Loading