TranceGeniK

Untitled

Oct 30th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.50 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. //------------------------------
  4. //Constantes pour déclaration strips
  5. //------------------------------
  6. #define NUM_LEDS_EXT 112
  7. #define NUM_LEDS_MIL 68
  8. #define NUM_LEDS_INT 67
  9. #define NUM_LEDS_METRONOME 120
  10. #define NUM_LEDS_MOOD 174
  11.  
  12. #define DATA_PIN_EXT 6
  13. #define DATA_PIN_MIL 7
  14. #define DATA_PIN_INT 8
  15. #define DATA_PIN_METRONOME 14
  16. #define DATA_PIN_MOOD 20
  17.  
  18. //------------------------------
  19. // Déclarations strips
  20. //------------------------------
  21. CRGB ledsExt[NUM_LEDS_EXT];
  22. CRGB ledsMil[NUM_LEDS_MIL];
  23. CRGB ledsInt[NUM_LEDS_INT];
  24.  
  25. CRGB ledsMetronome[NUM_LEDS_METRONOME];
  26. CRGB ledsMood[NUM_LEDS_MOOD];
  27.  
  28. CRGB refLedsMetronome[NUM_LEDS_METRONOME];
  29. CRGB refLedsMood[NUM_LEDS_MOOD];
  30.  
  31. //------------------------------
  32. // Couleurs coeur
  33. //------------------------------
  34. uint8_t rc = 255;
  35. uint8_t vc = 0;
  36. uint8_t bc = 0;
  37.  
  38. //------------------------------
  39. // Dim pour anim coeur
  40. //------------------------------
  41. uint8_t dim = 0 ;
  42. uint8_t dim2 = 0 ;
  43.  
  44. //------------------------------
  45. // Couleurs strip autour coeur
  46. //------------------------------
  47. uint8_t rac = 0;
  48. uint8_t vac = 255;
  49. uint8_t bac = 255;
  50.  
  51. //------------------------------
  52. // Dim strip autour coeur
  53. //------------------------------
  54. uint8_t dimAc = 255 ;
  55.  
  56. //------------------------------
  57. // Couleurs strip autour mood
  58. //------------------------------
  59. uint8_t ram = 255;
  60. uint8_t vam = 255;
  61. uint8_t bam = 0;
  62.  
  63. //------------------------------
  64. // Dim strip autour coeur
  65. //------------------------------
  66. uint8_t dimAm = 255 ;
  67.  
  68. uint8_t globalBrightness = 100 ;
  69. CRGB lastBgColor;
  70.  
  71. byte anim = 0 ;
  72.  
  73. void setup() {
  74.  
  75.       //Délai pour pouvoir connecter le port série
  76.       FastLED.delay(30000);
  77.      
  78.       FastLED.addLeds<WS2812, DATA_PIN_EXT, GRB>(ledsExt, NUM_LEDS_EXT);
  79.       FastLED.addLeds<WS2812, DATA_PIN_MIL, GRB>(ledsMil, NUM_LEDS_MIL);
  80.       FastLED.addLeds<WS2812, DATA_PIN_INT, GRB>(ledsInt, NUM_LEDS_INT);
  81.       FastLED.addLeds<WS2812, DATA_PIN_METRONOME, GRB>(ledsMetronome, NUM_LEDS_METRONOME);
  82.       FastLED.addLeds<WS2812, DATA_PIN_MOOD, GRB>(ledsMood, NUM_LEDS_MOOD);
  83.      
  84.       copyLeds(ledsMetronome, refLedsMetronome, NUM_LEDS_METRONOME) ;
  85.       copyLeds(ledsMood, refLedsMood, NUM_LEDS_MOOD) ;
  86.      
  87.       //On ne veut pas se faire mal aux yeux :)
  88.       //FastLED.setBrightness(globalBrightness) ;
  89.       //FastLED.setDither(0) ;
  90.      
  91.       fillAll(CRGB::Black) ;
  92.       Serial.begin(38400);
  93. }
  94.  
  95. void loop() {
  96.   if(anim == 0)
  97.   {
  98.     attente() ;
  99.   }
  100.   else
  101.   {
  102.     respiration() ;
  103.   }
  104.   animStrips() ;
  105.   FastLED.show() ;
  106.   serialRead() ;
  107. }
  108.  
  109. void serialRead()
  110. {
  111.   if(Serial.available())
  112.   {
  113.     char content[12];
  114.     char character;
  115.     char value[3] ;
  116.     char value2[3] ;
  117.     int idex = 0 ;
  118.     int val = 0 ;
  119.  
  120.     while(Serial.available() && character != 13) {
  121.         character = Serial.read();
  122.         content[idex] = character;
  123.         idex++ ;
  124.     }
  125.  
  126.     if (content != "") {
  127.       Serial.println(content);
  128.       if(content[0] != 'c' && content[0] != 'd' && content[0] != 'e')
  129.       {
  130.         for(int i = 1 ; i < strlen(content); i++)
  131.         {
  132.           value[i - 1] = content[i] ;
  133.         }
  134.         val = atoi(value) ;
  135.       }
  136.       switch(content[0])
  137.       {
  138.         //0 pour attente / 1 ou 2 pour animation
  139.         case 'a' :
  140.           fillHeart(CRGB::Black) ;
  141.           anim = val;
  142.           break ;
  143.          
  144.         //couleur coeur
  145.         case 'c' :
  146.           for(int i = 1 ; i < 4; i++)
  147.           {
  148.             value[i - 1] = content[i] ;
  149.           }
  150.           rc = atoi(value) ;
  151.           for(int i = 4 ; i < 7; i++)
  152.           {
  153.             value[i - 4] = content[i] ;
  154.           }
  155.           vc = atoi(value) ;
  156.           for(int i = 7 ; i < 10; i++)
  157.           {
  158.             value[i - 7] = content[i] ;
  159.           }
  160.           bc = atoi(value) ;
  161.           break ;
  162.          
  163.         //couleur autour coeur
  164.         case 'd' :
  165.           for(int i = 1 ; i < 4; i++)
  166.           {
  167.             value[i - 1] = content[i] ;
  168.           }
  169.           rac = atoi(value) ;
  170.           for(int i = 4 ; i < 7; i++)
  171.           {
  172.             value[i - 4] = content[i] ;
  173.           }
  174.           vac = atoi(value) ;
  175.           for(int i = 7 ; i < 10; i++)
  176.           {
  177.             value[i - 7] = content[i] ;
  178.           }
  179.           bac = atoi(value) ;
  180.           break ;
  181.          
  182.         //couleur autour mood
  183.         case 'e' :
  184.           for(int i = 1 ; i < 4; i++)
  185.           {
  186.             value[i - 1] = content[i] ;
  187.           }
  188.           ram = atoi(value) ;
  189.           for(int i = 4 ; i < 7; i++)
  190.           {
  191.             value[i - 4] = content[i] ;
  192.           }
  193.           vam = atoi(value) ;
  194.           for(int i = 7 ; i < 10; i++)
  195.           {
  196.             value[i - 7] = content[i] ;
  197.           }
  198.           bam = atoi(value) ;
  199.           break ;
  200.          
  201.         //dim pour anim coeur
  202.         case 'l' :
  203.           dim = val;
  204.           break ;
  205.          
  206.         //dim strip autour coeur
  207.         case 'm' :
  208.           dimAc = val ;
  209.           break ;
  210.        
  211.         //dim strip autour mood
  212.         case 'n' :
  213.           dimAm = val ;
  214.           break ;
  215.       }
  216.     }
  217.   }
  218. }
  219.  
  220. void fillAll(CRGB color)
  221. {
  222.   fill_solid(ledsMetronome, NUM_LEDS_METRONOME, color);
  223.   fill_solid(ledsMood, NUM_LEDS_MOOD, color);
  224.   fill_solid(ledsExt, NUM_LEDS_EXT, color);
  225.   fill_solid(ledsMil, NUM_LEDS_MIL, color);
  226.   fill_solid(ledsInt, NUM_LEDS_INT, color);
  227. }
  228.  
  229. void fillHeart(CRGB color)
  230. {
  231.   fill_solid(ledsExt, NUM_LEDS_EXT, color);
  232.   fill_solid(ledsMil, NUM_LEDS_MIL, color);
  233.   fill_solid(ledsInt, NUM_LEDS_INT, color);
  234. }
  235.  
  236. CRGB getBackgroundColor()
  237. {
  238.   return CRGB(rc, vc, bc) ;
  239. }
  240.  
  241. CRGB getMetronomeColor()
  242. {
  243.   return CRGB(rac, vac, bac) ;
  244. }
  245.  
  246. CRGB getMoodColor()
  247. {
  248.   return CRGB(ram, vam, bam) ;
  249. }
  250.  
  251. void animStrips()
  252. {
  253.   animStripHelper(refLedsMetronome, ledsMetronome, NUM_LEDS_METRONOME, getMetronomeColor(), dimAc) ;
  254.   animStripHelper(refLedsMood, ledsMood, NUM_LEDS_MOOD, getMoodColor(), dimAm) ;
  255. }
  256.  
  257. void animStripHelper(CRGB* refLeds, CRGB* leds, uint8_t num_leds, CRGB color, uint8_t dim)
  258. {
  259.   for(uint8_t i = num_leds / 2 - 1 ; i > 0 ; i--)
  260.   {
  261.     refLeds[i] = refLeds[i - 1] ;
  262.     refLeds[num_leds - 1 - i] = refLeds[num_leds - i] ;
  263.   }
  264.   refLeds[0] = color ;
  265.   refLeds[num_leds - 1] = color ;
  266.   copyLeds(refLeds, leds, num_leds) ;
  267.   dimStrip(leds, num_leds, dim) ;
  268. }
  269.  
  270. void copyLeds(CRGB* source, CRGB* dest, uint8_t num_leds)
  271. {
  272.   for(uint8_t i = 0 ; i < num_leds; i++)
  273.   {
  274.     dest[i] = source[i] ;
  275.   }
  276. }
  277.  
  278. void dimStrip(CRGB* leds, int nbPixels, uint8_t dim)
  279. {
  280.   for(uint8_t i = 0 ; i < nbPixels; i++)
  281.   {
  282.     leds[i] %= dim ;
  283.   }
  284. }
  285.  
  286. void attente()
  287. {  
  288.   oneHeartOneColor(ledsInt, NUM_LEDS_INT, getBackgroundColor()) ;
  289.  
  290.   uint16_t sinAttente = getSinValueAttente() ;
  291.   uint8_t brightness = (sinAttente * 200 / 65536) + 55 ;
  292.   dimStrip(ledsInt, NUM_LEDS_INT, brightness) ;
  293.  
  294.   uint8_t pos = (sinAttente * NUM_LEDS_EXT / 65536) ;
  295.   if(pos < NUM_LEDS_EXT / 2)
  296.   {
  297.     ledsExt[pos] = getBackgroundColor() ;
  298.     ledsExt[NUM_LEDS_EXT - 1 - pos] = getBackgroundColor() ;
  299.   }
  300.   else
  301.   {
  302.     ledsExt[pos] = CRGB::Black ;
  303.     ledsExt[NUM_LEDS_EXT - 1 - pos] = CRGB::Black ;
  304.   }
  305.  
  306.   uint8_t pos2 = (sinAttente * NUM_LEDS_MIL / 65536) ;
  307.   if(pos2 < NUM_LEDS_MIL / 2)
  308.   {
  309.     ledsMil[pos2] = CRGB::Black ;
  310.     ledsMil[NUM_LEDS_MIL - 1 - pos2] = CRGB::Black ;
  311.   }
  312.   else
  313.   {
  314.     ledsMil[pos2] = getBackgroundColor() ;
  315.     ledsMil[NUM_LEDS_MIL - 1 - pos2] = getBackgroundColor() ;
  316.   }
  317. }
  318.  
  319. uint16_t getSinValueAttente()
  320. {
  321.   uint16_t w16 = (millis() * 8) % 65536 ;
  322.   return sin16(w16) + 32768 ;
  323. }
  324.  
  325. void oneHeartOneColor(CRGB* heart, uint8_t num_leds, CRGB color)
  326. {
  327.   for(uint8_t i = 0 ; i < num_leds; i++)
  328.   {
  329.     heart[i] = color ;
  330.   }
  331. }
  332.  
  333. uint16_t getSinValue()
  334. {
  335.   uint16_t w16 = (millis() * 5) % 65536 ;
  336.   return sin16(w16) + 32768 ;
  337. }
  338.  
  339. uint16_t get16SinValue(uint16_t w16)
  340. {
  341.   return sin16(w16) + 32768 ;
  342. }
  343.  
  344. void respiration()
  345. {
  346.   oneHeartOneColor(ledsExt, NUM_LEDS_EXT, CRGB::Black) ;
  347.   oneHeartOneColor(ledsInt, NUM_LEDS_INT, getBackgroundColor()) ;
  348.   antiAliasedSinusHelper(ledsExt, NUM_LEDS_EXT) ;
  349.   if(anim == 1)
  350.   {
  351.     dimStrip(ledsInt, NUM_LEDS_INT, dim2) ;
  352.     dimStrip(ledsExt, NUM_LEDS_EXT, dim2) ;
  353.     if(dim2 < 255)
  354.     {
  355.       dim2++ ;
  356.     }
  357.   }
  358.   else
  359.   {
  360.     dimStrip(ledsInt, NUM_LEDS_INT, 255 - dim) ;
  361.     dimStrip(ledsMil, NUM_LEDS_MIL, dim) ;
  362.     dim2 = 0 ;
  363.   }
  364. }
  365.  
  366. void antiAliasedSinusHelper(CRGB* heart, int nbPixels)
  367. {
  368.   int width = nbPixels / 14 ;
  369.   int pixelGauche = get16PixelPosition(nbPixels / 2, width) ;
  370.   int pixelDroit = nbPixels * 16 - pixelGauche - 16 * width;
  371.   drawFractionalBar(heart, pixelGauche, width) ;
  372.   drawFractionalBar(heart, pixelDroit, width) ;
  373. }
  374.  
  375. int get16PixelPosition(int nbPixels, int width)
  376. {
  377.   return getSinValue() * (nbPixels * 16 - 16 * width) / 65536;
  378. }
  379.  
  380. void drawFractionalBar(CRGB* heart, int pos16, int width )
  381. {
  382.   int i = pos16 / 16;
  383.   uint8_t frac = pos16 & 0x0F;
  384.   uint8_t firstpixelbrightness = 255 - (frac * 16);
  385.   uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;
  386.   uint8_t bright;
  387.   for( int n = 0; n <= width; n++) {
  388.     if( n == 0 ) {
  389.       // first pixel in the bar
  390.       bright = firstpixelbrightness;
  391.     } else if( n == width ) {
  392.       // last pixel in the bar
  393.       bright = lastpixelbrightness;
  394.     } else {
  395.       // middle pixels
  396.       bright = 255;
  397.     }
  398.    
  399.     //heart[i] += CHSV( 0, 0, bright );
  400.     heart[i] = getBackgroundColor() ;
  401.     heart[i] %= bright;
  402.     i++;
  403.   }
  404. }
Advertisement
Add Comment
Please, Sign In to add comment