This serial port is driving me crazy... The project is for Adalight.
Here is the code:
#include <FastSPI_LED.h>
//------------------------------------------------------
// Config
//------------------------------------------------------
//LED communication-pin
#define PIN 4
//Number of LEDs
#define NUM 50
//Key byte to syncronize begining of frame with BobLight (prefix)
#define KEY 0xFE
//Change order of RGB below for some LED pixel brands.
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *leds;
//int pixel = 0;
//int color = 0;
//int buffer;
//------------------------------------------------------
// Setup - Main Code
//------------------------------------------------------
void setup()
{
//Baud Rate
Serial.begin(115200);
//FastSPI Setup (choose correct Chipset)
FastSPI_LED.setLeds(NUM);
FastSPI_LED.setChipset(CFastSPI_LED::SPI_TM1809);
FastSPI_LED.setPin(PIN);
FastSPI_LED.init();
FastSPI_LED.start();
leds = (struct CRGB*)FastSPI_LED.getRGBData();
memset(leds, 0, NUM * 3);
FastSPI_LED.show();
//Startup (brighten to full white, then off)
for(int i=0; i<150; i++)
{for(int j=0; j<NUM; j++)
{leds[j].r = i;
leds[j].g = i;
leds[j].b = i;}
FastSPI_LED.show();
delay(15);}
memset(leds, 0, NUM*3);
FastSPI_LED.show();
//Send "Ada" for auto port-detection by BobLight
Serial.print("Ada\n");
delay(500);
//Display Placment Pattern for 50-leds at 16x9 (15x8 + 4 corners)
leds[0].r = 255; leds[0].g = 255; //Yellow Center Bottom
leds[1].g = 255; leds[2].g = 255; leds[3].g = 255; leds[4].g = 255; leds[5].g = 255; leds[6].g = 255; leds[7].g = 255;
leds[8].r = 255; //Red Corner
leds[9].b = 255; leds[10].b = 255; leds[11].b = 255; leds[11].r = 63; leds[12].b = 255; leds[12].r = 255; leds[13].b = 255; leds[13].r = 255; leds[14].b = 255; leds[14].r = 63; leds[15].b = 255; leds[16].b = 255;
leds[17].r = 255; //Red Corner
leds[18].g = 255; leds[19].g = 255; leds[20].g = 255; leds[21].g = 255; leds[22].g = 255; leds[23].g = 255; leds[24].g = 255;
leds[25].r = 255; leds[25].g = 255; //Yellow Center Top
leds[26].g = 255; leds[27].g = 255; leds[28].g = 255; leds[29].g = 255; leds[30].g = 255; leds[31].g = 255; leds[32].g = 255;
leds[33].r = 255; //Red Corner
leds[34].b = 255; leds[35].b = 255; leds[36].b = 255; leds[36].r = 63; leds[37].b = 255; leds[37].r = 255; leds[38].b = 255; leds[38].r = 255; leds[39].b = 255; leds[39].r = 63; leds[40].b = 255; leds[41].b = 255;
leds[42].r = 255; //Red Corner
leds[43].g = 255; leds[44].g = 255; leds[45].g = 255; leds[46].g = 255; leds[47].g = 255; leds[48].g = 255; leds[49].g = 255;
FastSPI_LED.show();
//MainLoop
for(;;)
{if (Serial.available())
{if (Serial.read()==KEY)
{Serial.write(KEY); delay(1);
if (!Serial.available()) {}
if (Serial.read()==0x00)
{Serial.write(0xFD); delay(1);
if (!Serial.available()) {}
if (Serial.read()==0x00)
{Serial.write(0xFC); delay(1);
//Now Load All Bytes
for (int pixel=0; pixel<NUM; pixel++)
{if (!Serial.available()) {}
leds[pixel].r = Serial.read();
Serial.write(leds[pixel].r); delay(1);
if (!Serial.available()) {}
leds[pixel].b = Serial.read();
Serial.write(leds[pixel].b); delay(1);
if (!Serial.available()) {}
leds[pixel].g = Serial.read();
Serial.write(leds[pixel].g); delay(1);
}
FastSPI_LED.show();
//Serial.flush();
} } } } } }
//------------------------------------------------------
// End
//------------------------------------------------------
void loop()
{
// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
}
Here is the output when sending it a HEX file of 153bytes at 115200:
FE FD FC 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 70 82 94
A6 B8 C6 D8 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF
You can see it starts dropping bytes after 66.
It gets to 73 at 57600, it gets to A9 at 19200.
At 9600 it doesn't work with delay(1). With delay(2) it get to C9.
At 4800 it needs delay(3) it works for my test, but when I run BobLight at 4800 with a 1sec delay between packets, I just see the second half as white pixels for a second, and then they all go off except the last pixel which stays blue.
I would love to run at 115200 and get my 150byte-packets, one packet a second