Skip to content

Instantly share code, notes, and snippets.

@KhaosT
Created April 23, 2014 07:57
Show Gist options
  • Save KhaosT/11206202 to your computer and use it in GitHub Desktop.
Save KhaosT/11206202 to your computer and use it in GitHub Desktop.
WS2812 support for CC254x
//
// WS2812.c
//
//
// Created by Khaos Tian on 4/23/14.
//
//
//osal_start_timerEx(simpleBLEPeripheral_TaskID, OLT_LED_EVT, 1000);
#define NUMBER_OF_LEDS 12
void delayUs(uint16 microSecs)
{
while(microSecs--)
{
/* 32 NOPs == 1 usecs */
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
asm("nop"); asm("nop");
}
}
void Reset(void)
{
P0_0=0;
delayUs(50);
}
void h_dat1()
{
P0_0 = 1;
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");
P0_0 = 0;
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");
}
void h_dat0()
{
P0_0 = 1;
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");
P0_0 = 0;
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
asm("NOP");asm("NOP");
}
void displayColors(uint8 reds[], uint8 greens[], uint8 blues[])
{
for(int i = 0; i < NUMBER_OF_LEDS;i++)
{
uint8 red = reds[i];
uint8 green = greens[i];
uint8 blue = blues[i];
if(((green >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
}
}
void displayColor(uint8 red, uint8 green, uint8 blue)
{
for(int i = 0; i < NUMBER_OF_LEDS;i++)
{
if(((green >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((green >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((red >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 7) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 6) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 5) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 4) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 3) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 2) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 1) & 0x01) == 1) h_dat1(); else h_dat0();
if(((blue >> 0) & 0x01) == 1) h_dat1(); else h_dat0();
}
}
void setUpLight()
{
//Setup Output
P0SEL = 0;
P0DIR = 0xFF;
P0 = 0;
delayUs(50);
//Clean Colors
displayColor(0,0,0);
}
void rainbowLED()
{
delayUs(50);
uint8 red[NUMBER_OF_LEDS];
uint8 green[NUMBER_OF_LEDS];
uint8 blue[NUMBER_OF_LEDS];
for(uint16 j=0; j<256*5; j++) {
for(uint8 i=0; i<NUMBER_OF_LEDS; i++) {
int a = (((i * 256 / NUMBER_OF_LEDS) + j) & 255);
if(a < 85) {
red[i] = a*3;
green[i] = 255-a*3;
blue[i] = 0;
} else if(a < 170) {
a -= 85;
red[i] = 255-a*3;
green[i] = 0;
blue[i] = a*3;
} else {
a -= 170;
red[i] = 0;
green[i] = a*3;
blue[i] = 255-a*3;
}
}
displayColors(red,green,blue);
delayUs(3000);
}
delayUs(50);
displayColor(0,0,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment