Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- //------------------------------
- //Constantes pour déclaration strips
- //------------------------------
- #define NUM_LEDS_EXT 112
- #define NUM_LEDS_MIL 68
- #define NUM_LEDS_INT 67
- #define NUM_LEDS_METRONOME 120
- #define NUM_LEDS_MOOD 174
- #define DATA_PIN_EXT 6
- #define DATA_PIN_MIL 7
- #define DATA_PIN_INT 8
- #define DATA_PIN_METRONOME 14
- #define DATA_PIN_MOOD 20
- //------------------------------
- // Déclarations strips
- //------------------------------
- CRGB ledsExt[NUM_LEDS_EXT];
- CRGB ledsMil[NUM_LEDS_MIL];
- CRGB ledsInt[NUM_LEDS_INT];
- CRGB ledsMetronome[NUM_LEDS_METRONOME];
- CRGB ledsMood[NUM_LEDS_MOOD];
- CRGB refLedsMetronome[NUM_LEDS_METRONOME];
- CRGB refLedsMood[NUM_LEDS_MOOD];
- //------------------------------
- // Couleurs coeur
- //------------------------------
- uint8_t rc = 255;
- uint8_t vc = 0;
- uint8_t bc = 0;
- //------------------------------
- // Dim pour anim coeur
- //------------------------------
- uint8_t dim = 0 ;
- uint8_t dim2 = 0 ;
- //------------------------------
- // Couleurs strip autour coeur
- //------------------------------
- uint8_t rac = 0;
- uint8_t vac = 255;
- uint8_t bac = 255;
- //------------------------------
- // Dim strip autour coeur
- //------------------------------
- uint8_t dimAc = 255 ;
- //------------------------------
- // Couleurs strip autour mood
- //------------------------------
- uint8_t ram = 255;
- uint8_t vam = 255;
- uint8_t bam = 0;
- //------------------------------
- // Dim strip autour coeur
- //------------------------------
- uint8_t dimAm = 255 ;
- uint8_t globalBrightness = 100 ;
- CRGB lastBgColor;
- byte anim = 0 ;
- void setup() {
- //Délai pour pouvoir connecter le port série
- FastLED.delay(30000);
- FastLED.addLeds<WS2812, DATA_PIN_EXT, GRB>(ledsExt, NUM_LEDS_EXT);
- FastLED.addLeds<WS2812, DATA_PIN_MIL, GRB>(ledsMil, NUM_LEDS_MIL);
- FastLED.addLeds<WS2812, DATA_PIN_INT, GRB>(ledsInt, NUM_LEDS_INT);
- FastLED.addLeds<WS2812, DATA_PIN_METRONOME, GRB>(ledsMetronome, NUM_LEDS_METRONOME);
- FastLED.addLeds<WS2812, DATA_PIN_MOOD, GRB>(ledsMood, NUM_LEDS_MOOD);
- copyLeds(ledsMetronome, refLedsMetronome, NUM_LEDS_METRONOME) ;
- copyLeds(ledsMood, refLedsMood, NUM_LEDS_MOOD) ;
- //On ne veut pas se faire mal aux yeux :)
- //FastLED.setBrightness(globalBrightness) ;
- //FastLED.setDither(0) ;
- fillAll(CRGB::Black) ;
- Serial.begin(38400);
- }
- void loop() {
- if(anim == 0)
- {
- attente() ;
- }
- else
- {
- respiration() ;
- }
- animStrips() ;
- FastLED.show() ;
- serialRead() ;
- }
- void serialRead()
- {
- if(Serial.available())
- {
- char content[12];
- char character;
- char value[3] ;
- char value2[3] ;
- int idex = 0 ;
- int val = 0 ;
- while(Serial.available() && character != 13) {
- character = Serial.read();
- content[idex] = character;
- idex++ ;
- }
- if (content != "") {
- Serial.println(content);
- if(content[0] != 'c' && content[0] != 'd' && content[0] != 'e')
- {
- for(int i = 1 ; i < strlen(content); i++)
- {
- value[i - 1] = content[i] ;
- }
- val = atoi(value) ;
- }
- switch(content[0])
- {
- //0 pour attente / 1 ou 2 pour animation
- case 'a' :
- fillHeart(CRGB::Black) ;
- anim = val;
- break ;
- //couleur coeur
- case 'c' :
- for(int i = 1 ; i < 4; i++)
- {
- value[i - 1] = content[i] ;
- }
- rc = atoi(value) ;
- for(int i = 4 ; i < 7; i++)
- {
- value[i - 4] = content[i] ;
- }
- vc = atoi(value) ;
- for(int i = 7 ; i < 10; i++)
- {
- value[i - 7] = content[i] ;
- }
- bc = atoi(value) ;
- break ;
- //couleur autour coeur
- case 'd' :
- for(int i = 1 ; i < 4; i++)
- {
- value[i - 1] = content[i] ;
- }
- rac = atoi(value) ;
- for(int i = 4 ; i < 7; i++)
- {
- value[i - 4] = content[i] ;
- }
- vac = atoi(value) ;
- for(int i = 7 ; i < 10; i++)
- {
- value[i - 7] = content[i] ;
- }
- bac = atoi(value) ;
- break ;
- //couleur autour mood
- case 'e' :
- for(int i = 1 ; i < 4; i++)
- {
- value[i - 1] = content[i] ;
- }
- ram = atoi(value) ;
- for(int i = 4 ; i < 7; i++)
- {
- value[i - 4] = content[i] ;
- }
- vam = atoi(value) ;
- for(int i = 7 ; i < 10; i++)
- {
- value[i - 7] = content[i] ;
- }
- bam = atoi(value) ;
- break ;
- //dim pour anim coeur
- case 'l' :
- dim = val;
- break ;
- //dim strip autour coeur
- case 'm' :
- dimAc = val ;
- break ;
- //dim strip autour mood
- case 'n' :
- dimAm = val ;
- break ;
- }
- }
- }
- }
- void fillAll(CRGB color)
- {
- fill_solid(ledsMetronome, NUM_LEDS_METRONOME, color);
- fill_solid(ledsMood, NUM_LEDS_MOOD, color);
- fill_solid(ledsExt, NUM_LEDS_EXT, color);
- fill_solid(ledsMil, NUM_LEDS_MIL, color);
- fill_solid(ledsInt, NUM_LEDS_INT, color);
- }
- void fillHeart(CRGB color)
- {
- fill_solid(ledsExt, NUM_LEDS_EXT, color);
- fill_solid(ledsMil, NUM_LEDS_MIL, color);
- fill_solid(ledsInt, NUM_LEDS_INT, color);
- }
- CRGB getBackgroundColor()
- {
- return CRGB(rc, vc, bc) ;
- }
- CRGB getMetronomeColor()
- {
- return CRGB(rac, vac, bac) ;
- }
- CRGB getMoodColor()
- {
- return CRGB(ram, vam, bam) ;
- }
- void animStrips()
- {
- animStripHelper(refLedsMetronome, ledsMetronome, NUM_LEDS_METRONOME, getMetronomeColor(), dimAc) ;
- animStripHelper(refLedsMood, ledsMood, NUM_LEDS_MOOD, getMoodColor(), dimAm) ;
- }
- void animStripHelper(CRGB* refLeds, CRGB* leds, uint8_t num_leds, CRGB color, uint8_t dim)
- {
- for(uint8_t i = num_leds / 2 - 1 ; i > 0 ; i--)
- {
- refLeds[i] = refLeds[i - 1] ;
- refLeds[num_leds - 1 - i] = refLeds[num_leds - i] ;
- }
- refLeds[0] = color ;
- refLeds[num_leds - 1] = color ;
- copyLeds(refLeds, leds, num_leds) ;
- dimStrip(leds, num_leds, dim) ;
- }
- void copyLeds(CRGB* source, CRGB* dest, uint8_t num_leds)
- {
- for(uint8_t i = 0 ; i < num_leds; i++)
- {
- dest[i] = source[i] ;
- }
- }
- void dimStrip(CRGB* leds, int nbPixels, uint8_t dim)
- {
- for(uint8_t i = 0 ; i < nbPixels; i++)
- {
- leds[i] %= dim ;
- }
- }
- void attente()
- {
- oneHeartOneColor(ledsInt, NUM_LEDS_INT, getBackgroundColor()) ;
- uint16_t sinAttente = getSinValueAttente() ;
- uint8_t brightness = (sinAttente * 200 / 65536) + 55 ;
- dimStrip(ledsInt, NUM_LEDS_INT, brightness) ;
- uint8_t pos = (sinAttente * NUM_LEDS_EXT / 65536) ;
- if(pos < NUM_LEDS_EXT / 2)
- {
- ledsExt[pos] = getBackgroundColor() ;
- ledsExt[NUM_LEDS_EXT - 1 - pos] = getBackgroundColor() ;
- }
- else
- {
- ledsExt[pos] = CRGB::Black ;
- ledsExt[NUM_LEDS_EXT - 1 - pos] = CRGB::Black ;
- }
- uint8_t pos2 = (sinAttente * NUM_LEDS_MIL / 65536) ;
- if(pos2 < NUM_LEDS_MIL / 2)
- {
- ledsMil[pos2] = CRGB::Black ;
- ledsMil[NUM_LEDS_MIL - 1 - pos2] = CRGB::Black ;
- }
- else
- {
- ledsMil[pos2] = getBackgroundColor() ;
- ledsMil[NUM_LEDS_MIL - 1 - pos2] = getBackgroundColor() ;
- }
- }
- uint16_t getSinValueAttente()
- {
- uint16_t w16 = (millis() * 8) % 65536 ;
- return sin16(w16) + 32768 ;
- }
- void oneHeartOneColor(CRGB* heart, uint8_t num_leds, CRGB color)
- {
- for(uint8_t i = 0 ; i < num_leds; i++)
- {
- heart[i] = color ;
- }
- }
- uint16_t getSinValue()
- {
- uint16_t w16 = (millis() * 5) % 65536 ;
- return sin16(w16) + 32768 ;
- }
- uint16_t get16SinValue(uint16_t w16)
- {
- return sin16(w16) + 32768 ;
- }
- void respiration()
- {
- oneHeartOneColor(ledsExt, NUM_LEDS_EXT, CRGB::Black) ;
- oneHeartOneColor(ledsInt, NUM_LEDS_INT, getBackgroundColor()) ;
- antiAliasedSinusHelper(ledsExt, NUM_LEDS_EXT) ;
- if(anim == 1)
- {
- dimStrip(ledsInt, NUM_LEDS_INT, dim2) ;
- dimStrip(ledsExt, NUM_LEDS_EXT, dim2) ;
- if(dim2 < 255)
- {
- dim2++ ;
- }
- }
- else
- {
- dimStrip(ledsInt, NUM_LEDS_INT, 255 - dim) ;
- dimStrip(ledsMil, NUM_LEDS_MIL, dim) ;
- dim2 = 0 ;
- }
- }
- void antiAliasedSinusHelper(CRGB* heart, int nbPixels)
- {
- int width = nbPixels / 14 ;
- int pixelGauche = get16PixelPosition(nbPixels / 2, width) ;
- int pixelDroit = nbPixels * 16 - pixelGauche - 16 * width;
- drawFractionalBar(heart, pixelGauche, width) ;
- drawFractionalBar(heart, pixelDroit, width) ;
- }
- int get16PixelPosition(int nbPixels, int width)
- {
- return getSinValue() * (nbPixels * 16 - 16 * width) / 65536;
- }
- void drawFractionalBar(CRGB* heart, int pos16, int width )
- {
- int i = pos16 / 16;
- uint8_t frac = pos16 & 0x0F;
- uint8_t firstpixelbrightness = 255 - (frac * 16);
- uint8_t lastpixelbrightness = 255 - firstpixelbrightness;
- uint8_t bright;
- for( int n = 0; n <= width; n++) {
- if( n == 0 ) {
- // first pixel in the bar
- bright = firstpixelbrightness;
- } else if( n == width ) {
- // last pixel in the bar
- bright = lastpixelbrightness;
- } else {
- // middle pixels
- bright = 255;
- }
- //heart[i] += CHSV( 0, 0, bright );
- heart[i] = getBackgroundColor() ;
- heart[i] %= bright;
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment