Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "msp.h"
- #include "stdbool.h"
- #include "stdint.h"
- #include "stdio.h"
- #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
- /* #DEFINES */
- #define RGB_MAX 255
- #define LED_PORT GPIO_PORT_P2
- #define LED_PIN GPIO_PIN3
- #define LIGHTS 300
- enum State{Stopped, Progressing}; //The various blinking states the strip can be in
- /* Prototype function calls */
- void setColor(uint8_t g, uint8_t r, uint8_t b);
- void setGRB();
- void set_led_white();
- void set_led_off();
- void setAnimation();
- void icicleAnimation();
- //void hokieStone();
- void mardisGras();
- void icicleStill();
- void icicle2();
- void icicleAnimation2();
- struct LED {
- uint8_t red, green, blue;
- };
- struct LED led_strip[LIGHTS]; //initialize the struct
- uint32_t frame = 0;
- /**
- * main.c
- */
- void main(void)
- {
- WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
- //Initialize ports
- MAP_GPIO_setAsOutputPin(LED_PORT, LED_PIN);
- //struct LED led_strip[LIGHTS]; //initialize the struct
- //start the first color as white
- set_led_off();
- __delay_cycles(4000000);
- set_led_white();
- //set_led_white();
- // uint32_t frameRate = BPM/60*20.8*10^9; //20.8 ns for one frame. 1 bpm =
- __delay_cycles(200000);
- __delay_cycles(4000000);
- //do all calculations before entering the while loop
- while(1) {
- //do the color set here, since frame rate doesn't matter
- /* DO ALL THE CALCULATIONS FOR THE COLOR OF THE LIGHTS */
- //setGRB();
- // __delay_cycles(40000000);
- // set_led_white();
- // __delay_cycles(40000000);
- // set_led_off();
- // __delay_cycles(40000000);
- // hokieStone();
- // mardisGras();
- // icicleStill();
- // icicle2();
- // icicleAnimation2();
- icicleAnimation3();
- //SET EACH OF THE LEDS
- int i;
- int noOps;
- /* SET ONE FRAME ONE FRAME */
- // for(i = 0; i < LIGHTS; i++) {
- //
- // //icicleAnimation();
- // setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- // //move to next light in strip
- // __delay_cycles(1000);
- //
- //
- // }
- //go to next frame
- //__delay_cycles(4000000);
- /**
- for(i = 0; i < frameRate; i++) {
- __no_operation();
- }
- */
- }
- }
- /**
- * Cycles through to turn on one GRB light
- */
- void setColor(uint8_t g, uint8_t r, uint8_t b) {
- //bit shift down
- uint32_t color = (g<<16) | (r<<8) | b;
- uint8_t i;
- uint8_t ar[24];
- for(i = 0; i < 8; i++) {
- ar[i] = (g >> (7 - i)) & 0b00000001;
- }
- for(i = 0; i < 8; i++) {
- ar[i+8] = (r >> (7 - i)) & 0b00000001;
- }
- for(i = 0; i < 8; i++) {
- ar[i+16] = (b >> (7 - i)) & 0b00000001;
- }
- uint8_t *p = ar;
- for(i = 0; i < 24; i++) {
- //bit shift, starting from MSB in g, r, then b
- if(*(p+i)) {
- //Turn high on, so 700 ns or 19 no-op calls
- P2->OUT |= 0x08;
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- //set off, so 600 ns or 15 no-op calls
- P2->OUT &= ~0x08;
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- }
- else {
- //Set high, so 350 ns or 3 no-op calls
- P2->OUT |= 0x08;
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- //running at 48MHz, each NOP causes 20.8 ns delay
- //set off, so 600 ns or 15 no-op calls
- P2->OUT &= ~0x08;
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- __no_operation();
- }
- // p++;
- }
- //move to next pixel
- //send high signal
- }
- void set_led_off() {
- int i;
- for(i = 0; i < LIGHTS; i++)
- {
- led_strip[i].red = 0;
- led_strip[i].blue = 0;
- led_strip[i].green = 0;
- }
- for(i = 0; i < LIGHTS; i++)
- {
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- }
- }
- void set_led_white() {
- int i;
- for(i = 0; i < LIGHTS; i++)
- {
- led_strip[i].red = RGB_MAX;
- led_strip[i].blue = RGB_MAX;
- led_strip[i].green = RGB_MAX;
- }
- for(i = 0; i < LIGHTS; i++)
- {
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- }
- }
- void setGRB() {
- int j;
- for(j=0; j < LIGHTS; j++) {
- /* Set every first light to green */
- if(j%3 == 0) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = RGB_MAX;
- }
- /* Set every second light to red*/
- if(j%3 == 1) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = RGB_MAX;
- }
- /* Set every third light to blue*/
- if(j%3 == 2) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = RGB_MAX;
- }
- }
- /* Call to set the colors */
- int i;
- for(i = 0; i < LIGHTS; i++) {
- //icicleAnimation();
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- //move to next light in strip
- __delay_cycles(1000);
- }
- }
- void mardisGras() {
- int j;
- for(j=0; j < LIGHTS; j++) {
- /* Set every first light to green */
- if(j%5 == 0) {
- led_strip[j].green =255;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- /* Set every second light to red*/
- if(j%5 == 1) {
- led_strip[j].green =69;
- led_strip[j].red = 139;
- led_strip[j].blue = 19;
- }
- /* Set every third light to blue*/
- if(j%5 == 2) {
- led_strip[j].green =0;
- led_strip[j].red = 255;
- led_strip[j].blue = 255;
- }
- if(j%5 == 3) {
- led_strip[j].green =100;
- led_strip[j].red = 100;
- led_strip[j].blue = 0;
- }
- if(j%5 == 4) {
- led_strip[j].green =215;
- led_strip[j].red = 255;
- led_strip[j].blue = 0;
- }
- }
- /* Call to set the colors */
- int i;
- for(i = 0; i < LIGHTS; i++) {
- //icicleAnimation();
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- //move to next light in strip
- __delay_cycles(1000);
- }
- }
- void setAnimation() {
- static uint8_t frame = 0;
- int j;
- for(j=0; j < LIGHTS; j++) {
- //set every first light to green
- if(j%3 == 0) {
- led_strip[j].green =0;
- led_strip[j].red =0;
- led_strip[j].blue = 0;
- }
- if(j%3 == 1) {
- led_strip[j].green =0;
- led_strip[j].red =0;
- led_strip[j].blue = 0;
- }
- if(j%3 == 2) {
- // led_strip[j].green =RGB_MAX;
- // led_strip[j].red =RGB_MAX;
- // led_strip[j].blue = RGB_MAX;
- led_strip[j].green =0;
- led_strip[j].red =0;
- led_strip[j].blue = 0;
- }
- }
- }
- void icecicleAnimation() {
- frame = (frame+1)%LIGHTS;
- uint8_t b1 = 255;
- uint8_t g1 = 0;
- uint8_t r1 = 0;
- uint8_t b2 = 255;
- uint8_t g2 = 153;
- uint8_t r2 = 51;
- uint8_t b3 = 255;
- uint8_t g3 = 178;
- uint8_t r3 = 102;
- uint8_t b4 = 255;
- uint8_t g4 = 203;
- uint8_t r4 = 153;
- uint8_t b5 = 255;
- uint8_t g5 = 229;
- uint8_t r5 = 204;
- led_strip[0].blue = b1;
- led_strip[0].red = r1;
- led_strip[0].green = g1;
- led_strip[(frame+1)%LIGHTS].blue = b2;
- led_strip[(frame+1)%LIGHTS].red = r2;
- led_strip[(frame+1)%LIGHTS].green = g2;
- led_strip[(frame+2)%LIGHTS].blue = b3;
- led_strip[(frame+2)%LIGHTS].red = r3;
- led_strip[(frame+2)%LIGHTS].green = g3;
- led_strip[(frame+3)%LIGHTS].blue = b4;
- led_strip[(frame+3)%LIGHTS].red = r4;
- led_strip[(frame+3)%LIGHTS].green = g4;
- led_strip[(frame+4)%LIGHTS].blue = b5;
- led_strip[(frame+4)%LIGHTS].red = r5;
- led_strip[(frame+4)%LIGHTS].green = g5;
- int i;
- for(i = 0; i < LIGHTS; i++) {
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- __delay_cycles(1000);
- }
- }
- void icicle2() {
- int j;
- for(j=0; j < LIGHTS; j++) {
- /* Set every first light to green */
- if(j%5 == 0) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = 255;
- }
- /* Set every second light to red*/
- if(j%5 == 1) {
- led_strip[j].green =153;
- led_strip[j].red = 51;
- led_strip[j].blue = 255;
- }
- /* Set every third light to blue*/
- if(j%5 == 2) {
- led_strip[j].green =RGB_MAX;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- if(j%5 == 3) {
- led_strip[j].green =RGB_MAX;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- if(j%5 == 4) {
- led_strip[j].green =RGB_MAX;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- }
- /* Call to set the colors */
- int i;
- for(i = 0; i < LIGHTS; i++) {
- //icicleAnimation();
- setColor(led_strip[i].green, led_strip[i].red, led_strip[i].blue);
- //move to next light in strip
- //__delay_cycles(1000);
- }
- }
- void icicleAnimation2() {
- /* SET COLOR ARRAY */
- int j;
- for(j=0; j < LIGHTS; j++) {
- /* Set every first light to green */
- if(j%3 == 0) {
- led_strip[j].green =RGB_MAX;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- /* Set every second light to red*/
- if(j%3 == 1) {
- led_strip[j].green =0;
- led_strip[j].red = RGB_MAX;
- led_strip[j].blue = 0;
- }
- /* Set every third light to blue*/
- if(j%3 == 2) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = RGB_MAX;
- }
- }
- /* NOW RUN FRAMES*/
- uint32_t frame = 0;
- uint32_t i;
- while(frame < LIGHTS) {
- for(i = 0; i < LIGHTS; i++) {
- setColor(led_strip[LIGHTS-frame+i].green, led_strip[LIGHTS-frame+i].red, led_strip[LIGHTS-frame+i].blue);
- }
- frame++;
- __delay_cycles(4000000);
- }
- }
- void icicleAnimation3() {
- /* SET COLOR ARRAY */
- int j;
- led_strip[0].green =255;
- led_strip[0].red = 0;
- led_strip[0].blue = 0;
- led_strip[1].green =255;
- led_strip[1].red = 153;
- led_strip[1].blue = 51;
- led_strip[2].green =255;
- led_strip[2].red = 178;
- led_strip[2].blue = 102;
- led_strip[3].green =255;
- led_strip[3].red = 203;
- led_strip[3].blue = 153;
- led_strip[4].green =255;
- led_strip[4].red = 229;
- led_strip[4].blue = 204;
- for(j=5; j < LIGHTS; j++) {
- /* Set every first light to green */
- if(j%3 == 0) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- /* Set every second light to red*/
- if(j%3 == 1) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- /* Set every third light to blue*/
- if(j%3 == 2) {
- led_strip[j].green =0;
- led_strip[j].red = 0;
- led_strip[j].blue = 0;
- }
- }
- /* NOW RUN FRAMES*/
- uint32_t frame = 0;
- uint32_t i;
- while(frame < LIGHTS) {
- for(i = 0; i < LIGHTS; i++) {
- setColor(led_strip[(LIGHTS-frame+i)%LIGHTS].green, led_strip[(LIGHTS-frame+i)%LIGHTS].red, led_strip[(LIGHTS-frame+i)%LIGHTS].blue);
- }
- frame++;
- __delay_cycles(4000000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement