xLAB - acutators / led-mrf-osc-legacy-code

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include<string.h>
00003 #include <vector>
00004 #include "rtos.h"
00005 #include "OSCmsg.h"
00006 #include "MRF24J40.h"
00007 #include "LPD8806.h"
00008 #include <stdio.h>      /* printf, scanf, puts, NULL */
00009 #include <stdlib.h>     /* srand, rand */
00010 #include <time.h>       /* time */
00011 
00012 #define ROWS 8          // Rows of leds
00013 #define COLS 66         // number of leds per row
00014 
00015 Serial pc(USBTX, USBRX);    // Serial communication for debugging purposes
00016 MRF24J40 mrf(p11,p12,p13,p14,p21);
00017 Mutex stdio_mutex; 
00018 
00019 // Total leds used for light effects; ROWS * COLS
00020 LPD8806 strip = LPD8806(528);
00021 
00022 // Since the led strip is daisy chained, it is harder to create effects with a list compared to a regular 2d matrix.
00023 static int grid[8][66] = {10};
00024 
00025 
00026 /****************************************
00027 * finding_nemo_set_position
00028 *
00029 * @param p      position of the head led within the row  
00030 *
00031 *
00032 ****************************************/
00033 void finding_nemo_set_position(int p)
00034 {
00035     uint8_t r = 0; uint8_t g = 0; uint8_t b = 255;
00036     int pos;
00037     for(pos = 1; pos < 66; pos++)
00038         strip.setPixelColor(p-pos, strip.Color(r/(pos), g/(pos), b/(pos)));
00039 }
00040 
00041 /****************************************
00042 * finding_nemo
00043 *
00044 * creates a water-flow effect that is in sync with the scene from the movie finding nemo
00045 * 
00046 * execution time is approximately 50 ms
00047 *
00048 * @param r      red color value
00049 * @param g      green color  
00050 * @param b      blue color   
00051 * @param delay  delay between updating the led layout to "move the water" by 1 led
00052 *
00053 ****************************************/
00054 void finding_nemo(uint8_t r, uint8_t g, uint8_t b, uint8_t delay) {
00055     int i, j;
00056 
00057     int pos = 0;
00058     int pos2 = pos+66;
00059     int pos3 = pos2+66;
00060     int pos4 = pos3+66;
00061     int pos5 = pos4+66;
00062     int pos6 = pos5+66;
00063     int pos7 = pos6+66;
00064     int pos8 = pos7+66;
00065     
00066     int dir = 1;
00067     int dir2 = 1;
00068     int dir3 = 1;
00069     int dir4 = 1;
00070     int dir5 = 1;
00071     int dir6 = 1;
00072     int dir7 = 1;
00073     int dir8 = 1;
00074     
00075     
00076     for (i=0; i<=((strip.numPixels()/8)); i++) 
00077     {
00078         
00079         finding_nemo_set_position(pos);
00080         finding_nemo_set_position(pos2);
00081         finding_nemo_set_position(pos3);
00082         finding_nemo_set_position(pos4);
00083         finding_nemo_set_position(pos5);
00084         finding_nemo_set_position(pos6);
00085         finding_nemo_set_position(pos7);
00086         finding_nemo_set_position(pos8);
00087         strip.show();
00088         wait_ms(delay);
00089 
00090         for (j=0; j< 66; j++)
00091         {
00092             strip.setPixelColor(pos-j, strip.Color(0,0,0));
00093             strip.setPixelColor(pos2-j, strip.Color(0,0,0));
00094             strip.setPixelColor(pos3-j, strip.Color(0,0,0));
00095             strip.setPixelColor(pos4-j, strip.Color(0,0,0));
00096             strip.setPixelColor(pos5-j, strip.Color(0,0,0));  
00097         }
00098         // Bounce off ends of strip
00099         pos += dir;
00100         pos2 += dir2;
00101         pos3 += dir3;
00102         pos4 += dir4;
00103         pos5 += dir5;
00104         pos6 += dir6;
00105         pos7 += dir7;
00106         pos8 += dir8;
00107         
00108         
00109         if(pos < 0)
00110         {
00111             pos = 1;
00112             dir = -dir;   
00113         }
00114         else if (pos >= strip.numPixels()/8)
00115         {
00116             pos = strip.numPixels()/8 - 2;
00117             dir = -dir;
00118         }
00119         
00120         if(pos2 < strip.numPixels()/8)
00121         {
00122             pos2 = strip.numPixels()/8;
00123             dir2 = -dir2;   
00124         }
00125         else if (pos2 >= (strip.numPixels()/8) * 2)
00126         {
00127             pos2 = (strip.numPixels()/8) * 2;
00128             dir2 = -dir2;
00129         }
00130         
00131         if(pos3 < (strip.numPixels()/8) * 2 + 1)
00132         {
00133             pos3 = (strip.numPixels()/8) * 2;
00134             dir3 = -dir3;   
00135         }
00136         else if (pos3 >= (strip.numPixels()/8) * 3)
00137         {
00138             pos3 = (strip.numPixels()/8) * 3;
00139             dir3 = -dir3;
00140         }
00141         
00142         if(pos4 < (strip.numPixels()/8) * 3 + 1)
00143         {
00144             pos4 = (strip.numPixels()/8) * 3 + 1;
00145             dir4 = -dir4;   
00146         }
00147         else if (pos4 >= (strip.numPixels()/8) * 4)
00148         {
00149             pos4 = (strip.numPixels()/8) * 4;
00150             dir4 = -dir4;
00151         }
00152         
00153         if(pos5 < (strip.numPixels()/8) * 4 + 1)
00154         {
00155             pos5 = (strip.numPixels()/8) * 4 + 1;
00156             dir5 = -dir5;   
00157         }
00158         else if (pos5 >= (strip.numPixels()/8) * 5)
00159         {
00160             pos5 = (strip.numPixels()/8) * 5;
00161             dir5 = -dir5;
00162         }
00163         
00164         if(pos6 < (strip.numPixels()/8) * 5 + 1)
00165         {
00166             pos6 = (strip.numPixels()/8) * 5 + 1;
00167             dir6 = -dir6;   
00168         }
00169         else if (pos6 >= (strip.numPixels()/8) * 6)
00170         {
00171             pos6 = (strip.numPixels()/8) * 6;
00172             dir6 = -dir6;
00173         }
00174         
00175         if(pos7 < (strip.numPixels()/8) * 6 + 1)
00176         {
00177             pos7 = (strip.numPixels()/8) * 6 + 1;
00178             dir7 = -dir7;   
00179         }
00180         else if (pos7 >= (strip.numPixels()/8) * 7)
00181         {
00182             pos7 = (strip.numPixels()/8) * 7;
00183             dir7 = -dir7;
00184         }
00185         
00186         if(pos8 < (strip.numPixels()/8) * 7 + 1)
00187         {
00188             pos8 = (strip.numPixels()/8) * 7 + 1;
00189             dir8 = -dir8;   
00190         }
00191         else if (pos8 >= (strip.numPixels()/8) * 8)
00192         {
00193             pos8 = (strip.numPixels()/8) * 8;
00194             dir8 = -dir8;
00195         }
00196         
00197     }
00198 }
00199 
00200 /****************************************
00201 * init_grid()
00202 *
00203 * converting an array into a grid to make effect creating easier
00204 * 
00205 * execution time is approximately 10 ms
00206 *
00207 *
00208 ****************************************/
00209 
00210 void init_grid()
00211 {
00212     int col;
00213     
00214     for(col = 0; col < strip.numPixels()/ROWS; col++)
00215         grid[0][col] = col;
00216         
00217     for(col = 0; col < strip.numPixels()/ROWS; col++)
00218         grid[1][col] = strip.numPixels()/ROWS * 2 - 1 - col;
00219         
00220     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00221         grid[2][col] = strip.numPixels()/ROWS * 2 + col;
00222         
00223     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00224         grid[3][col] = strip.numPixels()/ROWS * 4 - 1 - col;
00225         
00226     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00227         grid[4][col] = strip.numPixels()/ROWS * 4 + col;
00228         
00229     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00230         grid[5][col] = strip.numPixels()/ROWS * 6 - 1 - col;
00231         
00232     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00233         grid[6][col] = strip.numPixels()/ROWS * 6 + col;
00234         
00235     for(col = 0; col < strip.numPixels()/ROWS-1; col++)
00236         grid[7][col] = strip.numPixels()/ROWS * 8 - 1 - col;        
00237 }
00238 
00239 /****************************************
00240 * rf_receive
00241 *
00242 * function to recieve the command from the main mbeda
00243 * the appropriate effect is executed based on that command
00244 *
00245 * @param data       actualy command value padded with the protocol headers
00246 * @param maxLength  data maxLength 
00247 *
00248 ****************************************/
00249 int rf_receive(char *data, uint8_t maxLength)
00250 {
00251     uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
00252     uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
00253     if(len > 10) {
00254         //Remove the header and footer of the message
00255         for(uint8_t i = 0; i < len-2; i++) {
00256             if(i<8) {
00257                 //Make sure our header is valid first
00258                 if(data[i] != header[i])
00259                     return 0;
00260             } else {
00261                 data[i-8] = data[i];
00262             }
00263         }
00264 
00265         //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
00266     }
00267     return ((int)len)-10;
00268 }
00269 
00270 
00271 /****************************************
00272 * super_bowl_wave
00273 *
00274 * creates a flow effect that is in sync with the super bowl touchdown scene; 
00275 * the rgb value can be changed so that the color can match the color of the team that scored
00276 * 
00277 * execution time is approximately 25 ms
00278 *
00279 * @param r      red color value
00280 * @param g      green color  
00281 * @param b      blue color   
00282 * @param delay  delay between updating the led layout to "move the color" by 1 led
00283 *
00284 ****************************************/
00285 void super_bowl_wave(uint8_t r, uint8_t g, uint8_t b, uint8_t delay) {
00286     int i;
00287 
00288     int pos = 0;
00289     int pos2 = pos+strip.numPixels()/ROWS * 2 - 1;
00290     int pos3 = pos2+1;
00291     int pos4 = pos3+strip.numPixels()/ROWS * 2 - 1;
00292     int pos5 = pos4+1;
00293     int pos6 = pos5+strip.numPixels()/ROWS * 2 - 1; 
00294     int pos7 = pos6+1;
00295     int pos8 = pos7+strip.numPixels()/ROWS * 2 - 1;
00296 
00297     for (i=0; i<8; i++)
00298     {
00299         strip.setPixelColor(pos4, strip.Color(r, g, b));
00300         strip.setPixelColor(pos5, strip.Color(r, g, b));
00301         pos4--;
00302         pos5++;
00303         strip.show();
00304         wait_ms(delay);
00305     }
00306     pos4 = pos3+strip.numPixels()/ROWS * 2 - 1;
00307     pos5 = pos4+1;
00308     for (i=0; i<(strip.numPixels()/ROWS); i++) 
00309     {
00310         
00311         strip.setPixelColor(pos-2, strip.Color(r, g, b));
00312         strip.setPixelColor(pos2+4, strip.Color(r, g, b));
00313         strip.setPixelColor(pos3, strip.Color(r, g, b));
00314         strip.setPixelColor(pos4-8, strip.Color(r, g, b));
00315         strip.setPixelColor(pos5+8, strip.Color(r, g, b));
00316         strip.setPixelColor(pos6, strip.Color(r, g, b));
00317         strip.setPixelColor(pos7-4, strip.Color(r, g, b));
00318         strip.setPixelColor(pos8+2, strip.Color(r, g, b));
00319     
00320         strip.show();
00321         wait_ms(delay);
00322         pos++;
00323         pos2--;
00324         pos3++;
00325         pos4--;
00326         pos5++;
00327         pos6--;
00328         pos7++;
00329         pos8--;   
00330     }
00331     
00332     for (i=0; i<((strip.numPixels())); i++)
00333         strip.setPixelColor(i, strip.Color(0,0,0));
00334     strip.show();
00335 }
00336 
00337 
00338 // Used for sending and receiving
00339 char txBuffer[128];
00340 char rxBuffer[128];
00341 int rxLen;
00342 
00343 typedef enum {NEMO,ROCKET,BEYONCE,BOWL,NILL1,FLASH1,DEFAULT,OFF} stat;
00344 volatile stat status = DEFAULT;
00345         
00346 
00347 
00348 /****************************************
00349 * led_control
00350 *
00351 * the control function; after decoding the command recieved from the main mbed, 
00352 * this functions makes the appropriate function call to saisfy that command;
00353 *
00354 * @param args      decoded command received from mbed
00355 *
00356 ****************************************/
00357 void led_control(void const* args){
00358     while(1){
00359       // printf("in led control   : %s\r\n",status);
00360         if(status == NEMO){
00361             printf("NEMO\r\n");
00362            //finding_nemo(0,0,255,50);
00363         }
00364          if(status == BEYONCE){
00365            stdio_mutex.lock();
00366            pc.printf("BEYONCE\r\n");
00367            super_bowl_wave(0,255,0, 1);
00368            stdio_mutex.unlock();
00369         }
00370         
00371          if(status == ROCKET){
00372            printf("ROCKET\r\n"); 
00373            //rocket_launch(63, 255, 255,120);
00374            status = DEFAULT;  
00375         }
00376         
00377          if(status == BOWL){
00378           printf("BOWL\r\n");
00379           //super_bowl_wave(0,255,0, 1);
00380          
00381          
00382         }
00383          if(status == NILL1){
00384            printf("nill\r\n");
00385            //turnOffStrip();
00386            status = DEFAULT;
00387         }
00388          
00389         if(status == OFF){
00390            //switch_off();
00391            //turnOffStrip();
00392            //strobe(strip.Color(255,0, 0),100);
00393            status = DEFAULT;
00394         }          
00395 }
00396 
00397 }
00398 
00399 
00400 /****************************************
00401 * beyonce_inctro
00402 *
00403 * creates a light-flash effect to mimic the camera flashes when beyonce enters the stage in the concert scene
00404 * 
00405 * execution time is approximately 5 ms
00406 *
00407 * @param r      red color value
00408 * @param g      green color  
00409 * @param b      blue color   
00410 * @param delay  delay to hold the random leds for a specific time before clearing; very small as this function mimics a camera flash
00411 *
00412 ****************************************/
00413 void beyonce_intro(uint8_t r, uint8_t g, uint8_t b, int delay) 
00414 {
00415     int grey, lGrey, dGrey, white, i;
00416     for (i=0; i<((strip.numPixels())); i++)
00417         strip.setPixelColor(i, strip.Color(0,0,0));
00418     strip.show();
00419     
00420     for (i=0; i<(8); i++)
00421     {
00422         grey = rand() % strip.numPixels();
00423         lGrey = rand() % strip.numPixels();
00424         dGrey = rand() % strip.numPixels();
00425         white = rand() % strip.numPixels();
00426         strip.setPixelColor(grey, strip.Color(64,64,64));
00427         strip.setPixelColor(grey+1, strip.Color(64,64,64));
00428         strip.setPixelColor(lGrey, strip.Color(8,8,8));
00429         strip.setPixelColor(lGrey+1, strip.Color(8,8,8));
00430         strip.setPixelColor(dGrey, strip.Color(192,192,192));
00431         strip.setPixelColor(dGrey+1, strip.Color(192,192,192));
00432         strip.setPixelColor(white, strip.Color(255,255,255));
00433     }
00434     strip.show();
00435     wait_ms(delay);
00436 }
00437 
00438 /****************************************
00439 * rocket_launch
00440 *
00441 * a sequence of light effects measured out o match the rocket launch sequence
00442 * the light turns from a light blue shade to a dark purple shade as the rocket blasts off in to the space
00443 * as the sequence shifts to a scene from gravity, the lights change in brightness to give a twinkling star effect
00444 * 
00445 * execution time is approximately 50 seconds
00446 *
00447 * @param r      red color value
00448 * @param g      green color  
00449 * @param b      blue color   
00450 * @param delay  delay between changing the shade to match the sky background in the rocket launch sequence
00451 *
00452 ****************************************/
00453 void rocket_launch(uint8_t r, uint8_t g, uint8_t b, int delay) 
00454 {
00455     int i, j;
00456     int array[30] = {0};
00457     int star = 0;
00458                 
00459     while(r)
00460     {
00461         wait_ms(delay);
00462         for (i=0; i<((strip.numPixels())); i++)
00463         {
00464             strip.setPixelColor(i, strip.Color(r,g,b));
00465         } 
00466         strip.show(); 
00467         g--;
00468         r--;
00469     }
00470 
00471     while(g>128)
00472     {
00473         wait_ms(delay);
00474         for (i=0; i<((strip.numPixels())); i++)
00475         {
00476             strip.setPixelColor(i, strip.Color(r,g,b));
00477         } 
00478         strip.show(); 
00479         g--;
00480     }
00481 
00482     while(b>128)
00483     {
00484         wait_ms(delay);
00485         for (i=0; i<((strip.numPixels())); i++)
00486         {
00487             strip.setPixelColor(i, strip.Color(r,g,b));
00488         } 
00489         strip.show(); 
00490         b--;
00491     }
00492     for(i = 0; i<25; i++)
00493     {
00494         array[i] = rand() % strip.numPixels();
00495         star = rand() % 255;
00496         strip.setPixelColor(array[i], strip.Color(star, star, star));
00497     }
00498     strip.show();
00499     for(i =0; i < 500; i++)
00500     {
00501         for(j = 0; j < 5; j++)
00502         {
00503             star = rand() % 255;
00504             strip.setPixelColor(array[rand() % 25], strip.Color(star, star, star));
00505         }
00506         strip.show();
00507         wait_ms(99);
00508     }
00509     
00510     /*for(i = 0; i < 500; i++)
00511     {
00512         for(j = 0; j < 5; j++)
00513         {
00514             star = rand() % 255;
00515             strip.setPixelColor(array[rand() % 25], strip.Color(star, star, star));
00516         }
00517         strip.show();
00518         wait_ms(500);
00519     } */
00520 }
00521 
00522 /****************************************
00523 * beyonce_middle
00524 *
00525 * creates a red-light effect to match the middle portion of beyonce's concert sequence
00526 * 
00527 * execution time is approximately 10 ms
00528 *
00529 * @param r      red color value
00530 * @param g      green color  
00531 * @param b      blue color   
00532 * @param delay  delay between updating the led layout to "move the effect" by 1 led
00533 *
00534 ****************************************/
00535 void beyonce_middle(uint8_t r, uint8_t g, uint8_t b, int delay) 
00536 {
00537     int row, col;
00538     int x;
00539     int y = 10;
00540     for(x = 0; x < 33; x++)
00541     {
00542         for(row = 0; row < ROWS; row++)
00543             for(col = 0; col < x; col++)
00544             {
00545                 strip.setPixelColor(grid[row][col], strip.Color(r,g,b));
00546                 strip.setPixelColor(grid[row][COLS - 1 - col], strip.Color(r,g,b));
00547             }
00548                 
00549         strip.show();
00550         wait_ms(delay);
00551         for (row=0; row<((strip.numPixels())); row++)
00552             strip.setPixelColor(row, strip.Color(0,0,0));
00553     }
00554     
00555     for(x = 33; x >0; x--)
00556     {
00557         for(row = 0; row < ROWS; row++)
00558             for(col = 0; col < x; col++)
00559             {
00560                 strip.setPixelColor(grid[row][col], strip.Color(r,g,b));
00561                 strip.setPixelColor(grid[row][COLS - 1 - col], strip.Color(r,g,b));
00562             }
00563                 
00564         strip.show();
00565         wait_ms(delay);
00566         for (row=0; row<((strip.numPixels())); row++)
00567             strip.setPixelColor(row, strip.Color(0,0,0));
00568     }
00569 }
00570 
00571 
00572 /****************************************
00573 * fill_color_blue
00574 *
00575 * creates a under water effect that is in sync with some scenes from the movie finding nemo
00576 * 
00577 * execution time is approximately 1 ms
00578 *
00579 * @param r      red color value
00580 * @param g      green color  
00581 * @param b      blue color   
00582 * @param delay  delay not used in this sequence anymore
00583 *
00584 ****************************************/
00585 void fill_color_blue(uint8_t r, uint8_t g, uint8_t b, int delay) 
00586 {
00587     int x;
00588     /*
00589     int row, col;
00590     
00591     int y = 10;
00592     for(x = 0; x < 33; x++)
00593     {
00594         for(row = 0; row < ROWS; row++)
00595             for(col = 0; col < x; col++)
00596             {
00597                 strip.setPixelColor(grid[row][col], strip.Color(r,g,b));
00598                 strip.setPixelColor(grid[row][COLS - 1 - col], strip.Color(r,g,b));
00599             }
00600                 
00601         strip.show();
00602         wait_ms(delay);
00603     }
00604     */
00605     for(x = 0; x < (strip.numPixels()); x++)
00606     {
00607         strip.setPixelColor(x, strip.Color(r,g,b));
00608     }
00609     strip.show();
00610     
00611 }
00612 
00613 /****************************************
00614 * finding_nemo
00615 *
00616 * clears all the leds. picth black ceiling
00617 * 
00618 * execution time is approximately 1 ms
00619 *
00620 ****************************************/
00621 void clear()
00622 {
00623     int x;
00624     for (x=0; x<((strip.numPixels())); x++)
00625         strip.setPixelColor(x, strip.Color(0,0,0));
00626     strip.show();
00627 }
00628 
00629 
00630 /****************************************
00631 * equalizer
00632 *
00633 * creates a equalizer effect that is in sync with the ending part of beyonce's concert sequence
00634 * 
00635 * execution time is approximately 15 ms
00636 *
00637 * @param r      red color value
00638 * @param g      green color  
00639 * @param b      blue color   
00640 * @param delay  delay between updating the led layout to "move the equalizer" by 1 led up or down
00641 *
00642 ****************************************/
00643 void equalizer(uint8_t r, uint8_t g, uint8_t b, int delay)
00644 {
00645     int i;
00646     int row = 0;
00647     int col = 0;
00648     
00649     int randValues[14] = {0};
00650     for(i = 0 ; i < 14; i++)
00651         randValues[i] = rand() % 7;
00652     int temp = rand() % 7;
00653     
00654     for(i = temp; i >=0; i--)
00655     {
00656         for(row = 7; row > i; row--)
00657         {
00658             for(col = 0; col < 3; col++)
00659             {
00660                 strip.setPixelColor(grid[row+randValues[0]][col], strip.Color(r,g,b));
00661                 strip.setPixelColor(grid[row+randValues[1]][col+5], strip.Color(r,g,b));
00662                 strip.setPixelColor(grid[row+randValues[2]][col+10], strip.Color(r,g+r,b));
00663                 strip.setPixelColor(grid[row+randValues[3]][col+15], strip.Color(r,g+r,b));
00664                 strip.setPixelColor(grid[row+randValues[4]][col+20], strip.Color(r,g+r,b));
00665                 strip.setPixelColor(grid[row+randValues[5]][col+25], strip.Color(r,g,b));
00666                 strip.setPixelColor(grid[row+randValues[6]][col+30], strip.Color(r,g,b));
00667                 strip.setPixelColor(grid[row+randValues[7]][col+35], strip.Color(r,g+r,b));
00668                 strip.setPixelColor(grid[row+randValues[8]][col+40], strip.Color(r,g,b));
00669                 strip.setPixelColor(grid[row+randValues[9]][col+45], strip.Color(r,g+r,b));
00670                 strip.setPixelColor(grid[row+randValues[10]][col+50], strip.Color(r,g,b));
00671                 strip.setPixelColor(grid[row+randValues[11]][col+55], strip.Color(r,g,b));
00672                 strip.setPixelColor(grid[row+randValues[12]][col+60], strip.Color(r,g+r,b));
00673                 strip.setPixelColor(grid[row+randValues[13]][col+65], strip.Color(r,g,b));
00674             }
00675         }
00676         
00677 
00678         strip.show();
00679         wait_ms(delay);
00680         for (row=0; row<((strip.numPixels())); row++)
00681             strip.setPixelColor(row, strip.Color(0,0,0));
00682     }
00683     
00684     for(i = 0; i < temp; i++)
00685     {
00686         for(row = 7; row > i; row--)
00687         {
00688             for(col = 0; col < 3; col++)
00689             {
00690                 strip.setPixelColor(grid[row+randValues[0]][col], strip.Color(r,g,b));
00691                 strip.setPixelColor(grid[row+randValues[1]][col+5], strip.Color(r,g,b));
00692                 strip.setPixelColor(grid[row+randValues[2]][col+10], strip.Color(r,g+r,b));
00693                 strip.setPixelColor(grid[row+randValues[3]][col+15], strip.Color(r,g+r,b));
00694                 strip.setPixelColor(grid[row+randValues[4]][col+20], strip.Color(r,g+r,b));
00695                 strip.setPixelColor(grid[row+randValues[5]][col+25], strip.Color(r,g,b));
00696                 strip.setPixelColor(grid[row+randValues[6]][col+30], strip.Color(r,g,b));
00697                 strip.setPixelColor(grid[row+randValues[7]][col+35], strip.Color(r,g+r,b));
00698                 strip.setPixelColor(grid[row+randValues[8]][col+40], strip.Color(r,g,b));
00699                 strip.setPixelColor(grid[row+randValues[9]][col+45], strip.Color(r,g+r,b));
00700                 strip.setPixelColor(grid[row+randValues[10]][col+50], strip.Color(r,g,b));
00701                 strip.setPixelColor(grid[row+randValues[11]][col+55], strip.Color(r,g,b));
00702                 strip.setPixelColor(grid[row+randValues[12]][col+60], strip.Color(r,g+r,b));
00703                 strip.setPixelColor(grid[row+randValues[13]][col+65], strip.Color(r,g,b));
00704             }
00705         }
00706         strip.show();
00707         wait_ms(delay);
00708         for (row=0; row<((strip.numPixels())); row++)
00709             strip.setPixelColor(row, strip.Color(0,0,0));
00710     }
00711     
00712 }
00713 
00714 
00715 /****************************************
00716 * spider_man
00717 *
00718 * creates a sequence of effects that is in sync with the trailer from the movie the mazing spider man 2
00719 * 
00720 * execution time is approximately 20 seconds
00721 *
00722 * @param r      red color value
00723 * @param g      green color  
00724 * @param b      blue color   
00725 * @param delay  delay between updating the led layout to match the effect timings in the sequence
00726 *
00727 ****************************************/
00728 void spider_man(uint8_t r, uint8_t g, uint8_t b, int delay)
00729 {
00730     wait_ms(8000);
00731     int row, col, num, repeat;
00732     int color;
00733     for(repeat = 0; repeat < 1; repeat++)
00734     {
00735         for(num = 0; num < 4; num++)
00736         {
00737             for(row = 3-num; row < 5+num; row++)
00738             {
00739                 for(col = 25-num*6; col < 41+num*6; col++)
00740                 {
00741                     strip.setPixelColor(grid[row][col], strip.Color(255,192,0));
00742                 }
00743             }
00744               strip.show();
00745               wait_ms(delay);
00746         }
00747         for (row=0; row<((strip.numPixels())); row++)
00748             strip.setPixelColor(row, strip.Color(0,0,0));
00749         strip.show();
00750     }
00751     for (row=0; row<((strip.numPixels())); row++)
00752         strip.setPixelColor(row, strip.Color(0,0,0));
00753     strip.show();
00754     wait_ms(8250);
00755     
00756     for(num = 0; num < 8; num++)
00757     {
00758         for(row = 0; row < num; row++)
00759         {
00760             for(col = 0; col < 8*num; col++)
00761             {
00762                 color = rand() % 255;
00763                 strip.setPixelColor(grid[row][col], strip.Color(color,color,color));
00764             }
00765         }
00766           strip.show();
00767           wait_ms(100);
00768     }
00769     
00770     
00771     for (row=0; row<((strip.numPixels())); row++)
00772         strip.setPixelColor(row, strip.Color(0,0,0));
00773     strip.show();
00774     wait_ms(4750);
00775     
00776     for(row = 0; row < ROWS; row++)
00777     {
00778         for(col = 0; col < 50; col++)
00779         {
00780             strip.setPixelColor(grid[row][col], strip.Color(192+rand()%60,0,0));
00781         }
00782     }
00783     strip.show();
00784     wait_ms(2000);
00785     for (row=0; row<((strip.numPixels())); row++)
00786         strip.setPixelColor(row, strip.Color(0,0,0));
00787     strip.show();
00788 }
00789 
00790 
00791 /****************************************
00792 * finding_nemo_stream
00793 *
00794 * creates a water-flow effect that is in sync with the scene from the movie finding nemo
00795 * 
00796 * execution time is approximately 50 ms
00797 *
00798 * @param r      red color value
00799 * @param g      green color  
00800 * @param b      blue color   
00801 * @param delay  delay between updating the led layout to "move the water" by 1 led
00802 *
00803 ****************************************/
00804 void nemo_stream(uint8_t r, uint8_t g, uint8_t b, int delay) 
00805 {
00806     int i;
00807     int row = 0;
00808     int col = 0;
00809     int color= 0;
00810     int randCols[20] = {0};
00811     int randRows[20] = {0};
00812     for(i = 0 ; i < 20; i++)
00813     {
00814         randCols[i] = rand() % 66;
00815         randRows[i] = rand() % 7;
00816     }
00817     
00818     for(i = 7; i >=0; i--)
00819     {
00820         color = rand() % 10;
00821         for(row = 7; row > i; row--)
00822         {
00823             for(col = 0; col < 30; col++)
00824             {
00825                 
00826                 strip.setPixelColor(grid[row][randCols[col]], strip.Color(r,(rand()%32)/2/row,b/row));
00827                 if(color<2)
00828                     strip.setPixelColor(grid[row][randCols[col]], strip.Color(16,16,16));
00829                 strip.setPixelColor(grid[row+6][randCols[col]],strip.Color(0,0,0));
00830             }           
00831         }
00832         
00833         strip.show();
00834         wait_ms(50);
00835     }
00836     
00837     for (row=0; row<((strip.numPixels())); row++)
00838         strip.setPixelColor(row, strip.Color(0,0,10));
00839 }
00840 
00841 /****************************************
00842 * gravity_falling_to_earth
00843 *
00844 * creates a debris effect that is in sync with spacecraft falling back to earth scene from the movie gravity
00845 * 
00846 * execution time is approximately 20 ms
00847 *
00848 * @param r      red color value
00849 * @param g      green color  
00850 * @param b      blue color   
00851 * @param delay  delay between updating the led layout to "move the debris" by 1 led
00852 *
00853 ****************************************/
00854 void gravity_falling_to_earth(uint8_t r, uint8_t g, uint8_t b, int delay) 
00855 {
00856     int i;
00857     int row = 0;
00858     int col = 0;
00859     int color= 0;
00860     int randCols[20] = {0};
00861     int randRows[20] = {0};
00862     for(i = 0 ; i < 20; i++)
00863     {
00864         randCols[i] = rand() % 66;
00865         randRows[i] = rand() % 7;
00866     }
00867     
00868     for(i = 7; i >=0; i--)
00869     {
00870         for(row = 7; row > i; row--)
00871         {
00872             for(col = 0; col < 4; col++)
00873             {
00874                 strip.setPixelColor(grid[row][randCols[col]], strip.Color(r/row,(rand()%192)/2/row,b));
00875                 strip.setPixelColor(grid[row+6][randCols[col]],strip.Color(0,0,0));
00876             }           
00877         }
00878         
00879         strip.show();
00880         wait_ms(50);
00881     }
00882     
00883     for (row=0; row<((strip.numPixels())); row++)
00884         strip.setPixelColor(row, strip.Color(0,0,0));
00885     
00886     
00887     
00888     /*
00889     // fire code
00890     
00891     int red, orange, yellow, lime, i;
00892     
00893     for (i=rand()%8; i<((strip.numPixels())-8); i+=8)
00894     {
00895         strip.setPixelColor(i, strip.Color(255,0,0));
00896         strip.setPixelColor(i+1, strip.Color(239,16,0));
00897         strip.setPixelColor(i+2, strip.Color(223,32,0));
00898         strip.setPixelColor(i+3, strip.Color(207,48,0)); 
00899     }
00900     strip.show();
00901     wait_ms(delay);
00902     */
00903 }
00904 
00905 
00906 /****************************************
00907 * main
00908 *
00909 * sets up the communication between the  main mbed
00910 * inits the leds to check if the leds are working properly
00911 * continuosly checks for a command from the main mbed
00912 * if new command received, then executes the appropriate function
00913 *
00914 ****************************************/
00915 int main(){
00916     strip.begin();
00917     init_grid();
00918 
00919         
00920     OSCmsg m;
00921     OSCmsg *recv;
00922 
00923     OSCclass *c=new OSCclass;
00924     pc.baud(115200);
00925     pc.printf("Start----- Light COntroller!\r\n");
00926     
00927     //Thread t1(led_control);
00928     char *command=(char *)malloc(128);
00929     char mode;
00930     char color[2];
00931     //initialize table
00932     printf("Start----- Light COntroller!\r\n");
00933     int value;
00934     int i;
00935     for (i=0; i < strip.numPixels(); i++) 
00936     {
00937         strip.setPixelColor(i, strip.Color(255,255,255));
00938         wait_ms(1);  
00939         strip.show(); 
00940     }   
00941     
00942     
00943     wait_ms(2000);
00944     for (i=0; i < strip.numPixels(); i++) {
00945         strip.setPixelColor(i, 0);  // turn all pixels off   
00946     } 
00947     strip.show(); 
00948     
00949     int r = 0;
00950     int g = 255;
00951     int b = 0;
00952     
00953     /****OSC message temp***/
00954     char add[5];
00955     
00956     int flag = 0;
00957     int flag1 = 0;
00958 
00959     while(1) {
00960         rxLen = rf_receive(rxBuffer, 128);
00961         
00962         if(rxLen > 0) {
00963             pc.printf("received rxBuffer = %s \n", rxBuffer);     
00964             recv= c->getOSCmsg(rxBuffer);
00965             printf("Address is %s with type %c and msg %c \r\n",recv->getAddr(),recv->getType(),recv->getArgs());
00966             strncpy(add,recv->getAddr(),5);
00967            
00968            //check if the message is for LED Control
00969            pc.printf("add 2 is %c\r \n",add[1]);
00970            pc.printf("modes is %c\r\n",add[3]);
00971            if(add[1] == 'A'){
00972                 //check for the mode-strobe,on,flash,wave
00973                  mode = add[3]; 
00974                
00975                switch(mode){
00976                     
00977                     case'O':  //switch on LED
00978                                color[0] = recv->getArgs();
00979                                pc.printf("color is: %s and \r\n",color);
00980                               pc.printf("Status %d\r\n",status);
00981                               
00982                                break;
00983                      case'S':  //switch on LED
00984                                color[0] = recv->getArgs();
00985                                pc.printf("color is %s \r\n",color);
00986                                break;
00987                      case'W':  //switch on LED
00988                                color[0] = recv->getArgs();
00989                                pc.printf("color is %s \r\n",color);
00990                                break;
00991                     case'R':  //switch on LED
00992                                 color[0] = recv->getArgs();
00993                                pc.printf("color is %s \r\n",color);
00994                                break;
00995                       case'K':  //switch on LED
00996                                color[0] = recv->getArgs();
00997                                pc.printf("color is %s \r\n",color);
00998                                break;
00999                      case 'N':  //switch on LED
01000                                 color[0] = recv->getArgs();
01001                                //pc.printf("color is: %s and K\r\n",color);
01002                                pc.printf("color is %s \r\n",color);
01003                               //values = color_table.find(atoi(color))->second;
01004                               status = OFF;
01005                                printf("Status %d\r\n",status);
01006                                break;
01007                     case 'P':  //switch on LED
01008                                 value = recv->getArgs() - '0';
01009                                printf("Value is %d\r\n",value);
01010                                pc.printf("color is %s \r\n",color);
01011                               switch(value){
01012                                     case 0: clear();
01013                                             pc.printf("Clear\r\n");
01014                                             break;
01015                                             
01016                                     case 1: clear();
01017                                             nemo_stream(0,0,255,500);
01018                                             pc.printf("finding nemo %d   %d\r\n",flag,flag1);
01019                                             flag = 0;
01020                                             flag1 = 0;
01021                                             break;
01022                                             
01023                                     case 2: if(flag == 0)
01024                                             {
01025                                                 clear();
01026                                                 rocket_launch(63, 255, 255,120);
01027                                                 pc.printf("rocket launch%d   %d\r\n",flag,flag1);
01028                                                 flag = 1;
01029                                             }
01030                                             break;
01031                                             
01032                                     case 3: clear();
01033                                             beyonce_intro(128, 128, 128,25);
01034                                             pc.printf("beyonce intro%d   %d\r\n",flag,flag1);
01035                                             break;
01036                                             
01037                                     case 4: clear();
01038                                             super_bowl_wave(16,0,255, 1);
01039                                             pc.printf("super bow%d   %d\r\n",flag,flag1);
01040                                             break;
01041                                             
01042                                     case 5: clear();
01043                                             equalizer(255,0,0,40);
01044                                             pc.printf("Equalizer\r\n");
01045                                             break;
01046                                             
01047                                     case 6: clear();
01048                                             beyonce_middle(255,0,0,1);
01049                                             pc.printf("beyonce middle\r\n");
01050                                             break;
01051                                             
01052                                     case 7: 
01053                                             fill_color_blue(0,0,255,1);
01054                                             pc.printf("beyonce middle\r\n");
01055                                             flag1 = 0;
01056                                             break;
01057                                             
01058                                     case 8: if(flag1 == 0)
01059                                             {
01060                                             flag1 = 1;
01061                                             clear();
01062                                             spider_man(0,0,0,200);  
01063                                             pc.printf("Spider Man\r\n");
01064                                             }
01065                                             break;
01066                                             
01067                                     case 9: clear();
01068                                             gravity_falling_to_earth(255,0,0,500);
01069                                             pc.printf("gravity falling\r\n");
01070                                             break;
01071                                             
01072                                     default: break;
01073                                 }
01074                                break;
01075                                
01076                     default:  break;
01077                 }
01078             
01079             }
01080             
01081       }
01082     }             
01083 }  
01084