I'm using both SPI1 and SPI2 on a Maple r5 to control some WS2801 LED strips.
I'm able to update both strips, but data from one is bleeding into the other.
What could be causing this?
Here is the code:
#include <stdlib.h>
#include <string.h>
struct CRGB {
uint8 b;
uint8 g;
uint8 r;
};
struct CRGB *leds;
struct CRGB *leds_2;
float Rval,Gval,Bval;
int inByte = 0; // incoming serial byte
// Use SPI port number 1
HardwareSPI spi_1(1);
HardwareSPI spi_2(2);
//const int numLEDs_1 = 50;
const int numLEDs_1 = 248;
//const int numLEDs_1 = 518;
//const int numLEDs_1 = 270;
const int numLEDs_2 = 270;
int time_since_last = 0;
//uint8 * pixels_1;
void setup() {
leds = (struct CRGB*)malloc(numLEDs_1);
leds_2 = (struct CRGB*)malloc(numLEDs_2);
//spi_1.begin(SPI_562_500KHZ, MSBFIRST, SPI_MODE_0);
//spi_1.begin(SPI_1_125MHZ, MSBFIRST, SPI_MODE_0);
spi_1.begin(SPI_2_25MHZ, MSBFIRST, SPI_MODE_0);
spi_2.begin(SPI_2_25MHZ, MSBFIRST, SPI_MODE_0);
memset(leds, 0, numLEDs_1 * 3);
memset(leds_2, 0, numLEDs_2 * 3);
show_leds();
//establishContact(); // Send a byte to establish contact until receiver responds
}
void loop() {
/*
if (SerialUSB.available() > 0) {
// get incoming byte:
inByte = SerialUSB.read();
if (inByte == 'A') {
blue_strip();
time_since_last = 0;
}
else if (inByte == 'B') {
rainbow_hsl();
time_since_last = 0;
}
}
else {
if (time_since_last > 10000000) {
rainbow_hsl();
}
time_since_last++;
}
*/
/*
for (int i=0; i<3; i++) {
memset(leds, 0, numLEDs_1 * 3);
switch(i % 3) {
case 0:
leds[0].r = 255;
break;
case 1:
leds[0].g = 255;
break;
case 2:
leds[0].b = 255;
break;
}
delay(500);
}
*/
rainbow_hsl();
//rainbow_hsl2();
//blue_strip2();
//delay(10);
}
void audio_response(int amp) {
int scaled = map(amp, 0, 127, 0, numLEDs_1);
memset(leds, 0, numLEDs_1 * 3);
leds[scaled].b = 255;
show_leds();
delay(10);
}
void blue_strip() {
// blue one at a time down the strand
for (int i=0; i<numLEDs_1; i++) {
memset(leds, 0, numLEDs_1 * 3);
leds[i].b = 255;
show_leds();
delay(100);
}
}
void blue_strip2() {
// blue one at a time down the strand
for (int i=0; i<numLEDs_2; i++) {
memset(leds, 0, numLEDs_1 * 3);
memset(leds_2, 0, numLEDs_2 * 3);
leds_2[i].b = 255;
show_leds();
delay(10);
}
}
/*
void two_strip_test() {
// blue one at a time down the strand
for (int i=0; i<numLEDs_2; i++) {
memset(leds_2, 0, numLEDs_2 * 3);
leds_2[i].b = 255;
show_leds();
delay(100);
}
}
*/
void rainbow_hsl() {
// fade red in
memset(leds, 0, numLEDs_1 * 3);
for(int k = 0; k < 256; k++) {
for(int i = 0; i < numLEDs_1; i++ ) {
leds[i].r = k;
}
show_leds();
//show_leds_2();
delay(10);
}
// cycle through colors
float hue = 0.0;
for (int i=0; i<100; i++) {
memset(leds, 0, numLEDs_1 * 3);
hue = i / 100.0;
hslToRgb(hue, 1.0, 0.5);
for (int j=0; j<numLEDs_1; j++) {
leds[j].r = Rval;
leds[j].g = Gval;
leds[j].b = Bval;
}
show_leds();
//show_leds_2();
delay(50);
}
// fade red out
memset(leds, 0, numLEDs_1 * 3);
for(int k = 255; k >= 0; k--) {
for(int i = 0; i < numLEDs_1; i++ ) {
leds[i].r = k;
}
show_leds();
//show_leds_2();
delay(10);
}
}
void rainbow_hsl2() {
// fade red in
memset(leds, 0, numLEDs_1 * 3);
memset(leds_2, 0, numLEDs_2 * 3);
for(int k = 0; k < 256; k++) {
for(int i = 0; i < numLEDs_2; i++ ) {
leds_2[i].r = k;
}
show_leds();
delay(10);
}
// cycle through colors
float hue = 0.0;
for (int i=0; i<100; i++) {
memset(leds_2, 0, numLEDs_2 * 3);
hue = i / 100.0;
hslToRgb(hue, 1.0, 0.5);
for (int j=0; j<numLEDs_2; j++) {
leds_2[j].r = Rval;
leds_2[j].g = Gval;
leds_2[j].b = Bval;
}
show_leds();
delay(50);
}
// fade red out
memset(leds_2, 0, numLEDs_2 * 3);
for(int k = 255; k >= 0; k--) {
for(int i = 0; i < numLEDs_2; i++ ) {
leds_2[i].r = k;
}
show_leds();
delay(10);
}
}
void show_leds() {
for (int j=0; j<numLEDs_1; j++) {
spi_1.write(leds[j].b);
spi_1.write(leds[j].g);
spi_1.write(leds[j].r);
}
/*
for (int j=0; j<numLEDs_2; j++) {
spi_2.write(leds_2[j].b);
spi_2.write(leds_2[j].g);
spi_2.write(leds_2[j].r);
}
*/
}
void show_leds_2() {
for (int j=0; j<numLEDs_2; j++) {
spi_2.write(leds_2[j].b);
spi_2.write(leds_2[j].g);
spi_2.write(leds_2[j].r);
}
}
void hslToRgb(float h, float s, float l){
float r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}
else{
float q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
float p = 2.0 * l - q;
r = hue2rgb(p, q, h + 1.0/3.0);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1.0/3.0);
}
Rval = r * 255.0;
Gval = g * 255.0;
Bval = b * 255.0;
}
float hue2rgb(float p, float q, float t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1.0/6.0) return p + (q - p) * 6.0 * t;
if(t < 1.0/2.0) return q;
if(t < 2.0/3.0) return p + (q - p) * (2.0/3.0 - t) * 6.0;
return p;
}
void establishContact() {
while (SerialUSB.available() <= 0) {
SerialUSB.print('A', BYTE); // send a capital A
delay(300);
}
}