Created
October 18, 2011 19:21
-
-
Save chrisns/1296410 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
RAC arrays | |
*/ | |
#define ASIZE 32 /* Maximum array size */ | |
byte index; | |
byte i = 0; | |
byte j = 0; | |
byte isIndex = 1; | |
char inByte; | |
char index_str[4]; | |
char number_str[9]; | |
unsigned long value; | |
int SDI = 2; //Red wire (not the red 5V wire!) | |
int CKI = 3; //Green wire | |
int ledPin = 13; //On board LED | |
/* This is the main array that will store the numbers */ | |
unsigned long numbers[ASIZE]; | |
void setup() { | |
Serial.begin(115200); | |
/* Initialize strings to NULL */ | |
memset(&index_str[0], 0, 4); | |
memset(&number_str[0], 0, 9); | |
pinMode(SDI, OUTPUT); | |
pinMode(CKI, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
for(int x = 0 ; x < ASIZE ; x++) { | |
numbers[x] = 0; | |
} | |
post_frame(); | |
} | |
void loop() { | |
if(Serial.available() > 0) { | |
inByte = Serial.read(); | |
if(inByte != 124) { /* 124 = the '|' character */ | |
if(isIndex) { | |
index_str[i] = inByte; | |
i++; | |
} | |
else { | |
number_str[j] = inByte; | |
j++; | |
} | |
} | |
else { | |
if(isIndex) { | |
index_str[i] = 0; | |
i = 0; | |
isIndex = 0; | |
} | |
else { | |
number_str[j] = 0; | |
j = 0; | |
isIndex = 1; | |
/* Add the value into the array */ | |
index = atoi(index_str); | |
value = atol(number_str); | |
if(index < ASIZE) { | |
numbers[index] = value; | |
post_frame(); | |
} | |
} | |
} | |
} | |
} | |
//Takes the current strip color array and pushes it out | |
void post_frame (void) { | |
//Each LED requires 24 bits of data | |
//MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 | |
//Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor | |
//Pulling the clock low for 500us or more causes the IC to post the data. | |
for(int LED_number = 0 ; LED_number < ASIZE ; LED_number++) { | |
long this_led_color = numbers[LED_number]; //24 bits of color data | |
for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) { | |
//Feed color bit 23 first (red data MSB) | |
digitalWrite(CKI, LOW); //Only change data when clock is low | |
long mask = 1L << color_bit; | |
//The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit. | |
if(numbers[LED_number] & mask) | |
digitalWrite(SDI, HIGH); | |
else | |
digitalWrite(SDI, LOW); | |
digitalWrite(CKI, HIGH); //Data is latched when clock goes high | |
} | |
} | |
//Pull clock low to put strip into reset/post mode | |
digitalWrite(CKI, LOW); | |
delayMicroseconds(600); //Wait for 500us to go into reset | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment