Created
May 10, 2012 19:28
-
-
Save sodabrew/2655292 to your computer and use it in GitHub Desktop.
Arduino code for P9813 pixels (e.g. Grove chainable RGB module)
This file contains hidden or 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
/* Arduino code for P9813 pixels (e.g. Grove chainable RGB module) | |
* | |
* Originally from: | |
* http://auschristmaslighting.com/forums/index.php/topic,1261.msg15498.html#msg15498 | |
*/ | |
#define uint8 unsigned char | |
#define uint16 unsigned int | |
#define uint32 unsigned long int | |
// connect to digital 0,1 | |
int Clkpin = 5; | |
int Datapin = 6; | |
void ClkProduce(void) | |
{ | |
digitalWrite(Clkpin, LOW); | |
delayMicroseconds(20); | |
digitalWrite(Clkpin, HIGH); | |
delayMicroseconds(20); | |
} | |
void Send32Zero(void) | |
{ | |
unsigned char i; | |
for (i=0; i<32; i++) { | |
digitalWrite(Datapin, LOW); | |
ClkProduce(); | |
} | |
} | |
uint8 TakeAntiCode(uint8 dat) | |
{ | |
uint8 tmp = 0; | |
if ((dat & 0x80) == 0) { | |
tmp |= 0x02; | |
} | |
if ((dat & 0x40) == 0) { | |
tmp |= 0x01; | |
} | |
return tmp; | |
} | |
// gray data | |
void DatSend(uint32 dx) | |
{ | |
uint8 i; | |
for (i=0; i<32; i++) { | |
if ((dx & 0x80000000) != 0) { | |
digitalWrite(Datapin, HIGH); | |
} else { | |
digitalWrite(Datapin, LOW); | |
} | |
dx <<= 1; | |
ClkProduce(); | |
} | |
} | |
// data processing | |
void DataDealWithAndSend(uint8 r, uint8 g, uint8 b) | |
{ | |
uint32 dx = 0; | |
dx |= (uint32)0x03 << 30; // highest two bits 1,flag bits | |
dx |= (uint32)TakeAntiCode(b) << 28; | |
dx |= (uint32)TakeAntiCode(g) << 26; | |
dx |= (uint32)TakeAntiCode(r) << 24; | |
dx |= (uint32)b << 16; | |
dx |= (uint32)g << 8; | |
dx |= r; | |
DatSend(dx); | |
} | |
int t = 0; // Loop counter | |
int incomingByte[10]; | |
void setup() | |
{ | |
Serial.begin(9600); // set up Serial at 9600 bps | |
pinMode(Datapin, OUTPUT); | |
pinMode(Clkpin, OUTPUT); | |
Send32Zero(); // blank all outputs in string | |
//Feel free to leave this next section out until ^^^^ if you don't want to see when the Arduino inits | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
DataDealWithAndSend(255, 255, 255); | |
Send32Zero(); | |
delay(50); | |
Send32Zero(); // blank all outputs in string | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
DataDealWithAndSend(0, 0, 0); | |
Send32Zero(); | |
// ^^^^^^^ | |
} | |
void loop() | |
{ | |
if (Serial.available() >= 36) { | |
// read the oldest byte in the serial buffer: | |
for (int t=1; t<37; t++) { | |
// read each byte | |
incomingByte[t] = Serial.read(); | |
} | |
Send32Zero(); // output data to first 3 nodes (The others are neglected for testing purposes) | |
DataDealWithAndSend(incomingByte[1], incomingByte[2], incomingByte[3]); | |
DataDealWithAndSend(incomingByte[4], incomingByte[5], incomingByte[6]); | |
DataDealWithAndSend(incomingByte[7], incomingByte[8], incomingByte[9]); | |
DataDealWithAndSend(incomingByte[10], incomingByte[11], incomingByte[12]); | |
DataDealWithAndSend(incomingByte[13], incomingByte[14], incomingByte[15]); | |
DataDealWithAndSend(incomingByte[16], incomingByte[17], incomingByte[18]); | |
DataDealWithAndSend(incomingByte[19], incomingByte[20], incomingByte[21]); | |
DataDealWithAndSend(incomingByte[22], incomingByte[23], incomingByte[24]); | |
DataDealWithAndSend(incomingByte[25], incomingByte[26], incomingByte[27]); | |
DataDealWithAndSend(incomingByte[28], incomingByte[29], incomingByte[30]); | |
DataDealWithAndSend(incomingByte[31], incomingByte[32], incomingByte[33]); | |
DataDealWithAndSend(incomingByte[34], incomingByte[35], incomingByte[36]); | |
Send32Zero(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment