/* ************************************************************
* WS2801_lighting_by_Address: Lighting specificed address of LED with specified color.
* ledColor : Color of LED
* ledAddr : Address of LED (0-31, 0xFF: all LEDs)
* ************************************************************ */
void WS2801_lighting_by_Address (long ledColor, short ledAddr) {
for(short ledNo = 0; ledNo<STRIP_LENGTH; ledNo++) {
for(short color_bit = 23; color_bit >= 0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if(((ledNo == ledAddr) || ledAddr == 0xFF) && bitRead(ledColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
//Pull clock low for 500us or more causes the IC to post the data.
PORTD &= B11110111; //digitalWrite(CKI, LOW);
delay(1);
}
B. 單向依序顯示指定數量的LED
/* ************************************************************
* WS2801_sequencing_Multi_LED: Lights on specified number of
* continuous LEDs with specified color.
* ledColor : Color of specified LED
* backColor : Color of non-specified LED
* ledAddr : Starting address of LED to be lighted with ledColor(0-31)
* ledCnt : Number of continuous LED from ledAddr to be lighted on (range from 1 to 3).
* ************************************************************ */
void WS2801_sequencing_Multi_LED (long ledColor, long backColor, short ledAddr, short ledCnt) {
if (ledCnt > 3) ledCnt = 3;
else if (ledCnt < 1) ledCnt = 1;
for(short ledNo = 0; ledNo<STRIP_LENGTH; ledNo++) {
if (ledNo >= ledAddr && ledNo <= ledAddr+ledCnt-1) {
for(short color_bit = 23; color_bit >= 0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if (bitRead(ledColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
else {
for(short color_bit = 23; color_bit >= 0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if (bitRead(backColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
}
//Pull clock low for 500us or more causes the IC to post the data.
PORTD &= B11110111; //digitalWrite(CKI, LOW);
delay(1);
}
C. 雙向依序顯示指定數量的 LED
/* ************************************************************
* WS2801_2waySequencing_Multi_LED: Two directions to sequence light specified number of
* continuous LEDs with specified color.
* ledColor : Color of specified LED
* backColor : Color of non-specified LED
* ledCnt : Number of continuous LED from ledAddr to be lighted on (range from 1 to 3).
* ************************************************************ */
void WS2801_2waySequencing_Multi_LED (long ledColor, long backColor, short ledCnt) {
if (ledCnt > 3) ledCnt = 3;
else if (ledCnt < 1) ledCnt = 1;
for(short ledAddr=0; ledAddr<STRIP_LENGTH; ledAddr++) {
for(short ledNo=0; ledNo<STRIP_LENGTH; ledNo++) {
if ((ledNo >= ledAddr && ledNo <= ledAddr+ledCnt-1) ||
(ledNo >= STRIP_LENGTH-1-ledAddr-ledCnt+1 && ledNo <= STRIP_LENGTH-1-ledAddr))
{
for(short color_bit=23; color_bit>=0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if (bitRead(ledColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
else {
for(short color_bit=23; color_bit>=0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if (bitRead(backColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
}
//Pull clock low for 500us or more causes the IC to post the data.
PORTD &= B11110111; //digitalWrite(CKI, LOW);
delay(1);
delay(50);
}
}
D. 逐次增加LED的顯示數量
/* ************************************************************
* WS2801_stepping_LED: Stepping light on LED with specified color.
* ledColor : Color of LED
* speedMillis : Address of LED (0-31, 0xFF: all LEDs)
* ************************************************************ */
void WS2801_stepping_LED (long ledColor, boolean stepForward, long speedMillis) {
boolean isCompleted = false;
short ledAddr;
ledAddr = (stepForward?0:STRIP_LENGTH-1);
//for(short ledAddr=0; ledAddr<STRIP_LENGTH; ledAddr++) {
while(!isCompleted) {
for(short ledNo=0; ledNo<STRIP_LENGTH; ledNo++) {
for(short color_bit = 23; color_bit >= 0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if(((stepForward && ledNo <= ledAddr) && bitRead(ledColor, color_bit)) ||
((!stepForward && ledNo >= ledAddr) && bitRead(ledColor, color_bit)))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
//Pull clock low for 500us or more causes the IC to post the data.
PORTD &= B11110111; //digitalWrite(CKI, LOW);
delay(1);
delay(speedMillis);
if(stepForward) {
ledAddr++;
if(ledAddr>STRIP_LENGTH-1) isCompleted = true;
}
else {
ledAddr--;
if(ledAddr<0) isCompleted = true;
}
}
}
E. 隨機閃爍LED
/* ************************************************************
* WS2801_twinkling_LED: Twinkling LED with random color.
* delayMillis : Delay of milliseconds to light on
* ************************************************************ */
void WS2801_twinkling_LED (long delayMillis) {
for(short ledNo = 0; ledNo<STRIP_LENGTH; ledNo++) {
long xledColor = ledSColor[random(7)];
for(short color_bit = 23; color_bit >= 0 ; color_bit--) {
PORTD &= B11110111; //digitalWrite(CKI, LOW);
if(bitRead(xledColor, color_bit))
PORTD |= B00000100; //digitalWrite(SDI, HIGH);
else PORTD &= B11111011; //digitalWrite(SDI, LOW);
PORTD |= B00001000; //digitalWrite(CKI, HIGH);
}
}
//Pull clock low for 500us or more causes the IC to post the data.
PORTD &= B11110111; //digitalWrite(CKI, LOW);
delay(1);
delay(delayMillis);
}
沒有留言:
張貼留言