uniformisation du code

This commit is contained in:
Julien Cabillot 2017-03-12 01:44:34 +01:00 committed by Cabillot Julien
parent e9377a2be5
commit 99319782aa

View File

@ -24,7 +24,6 @@ WiFiClient espClient;
char message_buff[100]; char message_buff[100];
PubSubClient client(espClient); PubSubClient client(espClient);
///////////////////////////////////////////////
void setup() void setup()
{ {
@ -55,10 +54,10 @@ void setup()
client.loop(); client.loop();
} }
//////////////////////////////////////////////// ColorPalette //////////////////////////////// ColorPalette ///////////////////////////////
currentPalette = RainbowColors_p; currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND; currentBlending = LINEARBLEND;
///////////////////////////////////////////////////////////// //////////////////////////////// ColorPalette ///////////////////////////////
Serial.println("End of setup"); Serial.println("End of setup");
} }
@ -130,7 +129,6 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length)
// Si on ne repasse pas tout à noir, cela peut faire des effets surprenants // Si on ne repasse pas tout à noir, cela peut faire des effets surprenants
ledBlackAll(); ledBlackAll();
ledEffect = msgString; ledEffect = msgString;
// TODO : a vraiment tester
client.publish(MQTT_LED_EFFECT_STATE, message_buff, true); client.publish(MQTT_LED_EFFECT_STATE, message_buff, true);
} else if (stopic == MQTT_LED_BRIGHTNESS_COMMAND) { } else if (stopic == MQTT_LED_BRIGHTNESS_COMMAND) {
brightness = msgString.toInt(); brightness = msgString.toInt();
@ -157,8 +155,6 @@ void ledBlackAll()
FastLED.show(); FastLED.show();
} }
// TODO : faudra sortir toutes ces fonctions dans un autre fichier
void ledCylon() void ledCylon()
{ {
// Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante // Effet cylon : on allume une led, on attends, on eteinds, on passe à la suivante
@ -248,38 +244,42 @@ void ledError()
void ledFullColor() void ledFullColor()
{ {
fill_solid(leds, LED_NUM, color); fill_solid(leds, LED_NUM, color);
int breath = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.4; // TODO : il fadrait pas faire 0 -> 255 mais plutot 20 -> brightness
// Source : http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
// Augmenter 2000 augmente la fréquence (c'est en fait sin((temps / 1000) * Pi/2)
// 0.36787944 ?? censé correspondre au minimum
// 108.4 ?? censé correspondre au maximum
int breath = (exp(sin(millis() / 2000.0 * PI)) - 0.3678794) * 108.4;
Serial.print(breath);
Serial.println(" / 255");
FastLED.setBrightness(breath); FastLED.setBrightness(breath);
FastLED.delay(100 / speed); FastLED.delay(100 / speed);
} }
///////////////////////////////// ColorPalette ///////////////////// FastLED-3.1.5/examples/ColorPalette /////////////////////
void ledColorPattern() void ledColorPattern()
{ {
ChangePalettePeriodically(); ChangePalettePeriodically();
static uint8_t startIndex = 0; static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */ startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex); FillLEDsFromPaletteColors( startIndex);
FastLED.show(); FastLED.show();
FastLED.delay(1000 / speed); FastLED.delay(1000 / speed);
} }
void FillLEDsFromPaletteColors(uint8_t colorIndex) void FillLEDsFromPaletteColors(uint8_t colorIndex)
{ {
uint8_t brightness = 255; uint8_t brightness = 255;
for( int i = 0; i < LED_NUM; i++) { for( int i = 0; i < LED_NUM; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); leds[i] = ColorFromPalette(currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3; colorIndex += 3;
} }
} }
// There are several different palettes of colors demonstrated here. // There are several different palettes of colors demonstrated here.
// //
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p, // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
@ -290,31 +290,31 @@ void FillLEDsFromPaletteColors(uint8_t colorIndex)
void ChangePalettePeriodically() void ChangePalettePeriodically()
{ {
uint8_t secondHand = (millis() / 1000) % 60; uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99; static uint8_t lastSecond = 99;
if( lastSecond != secondHand) { if( lastSecond != secondHand) {
lastSecond = secondHand; lastSecond = secondHand;
if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } if (secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; } if (secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; } if (secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; } if (secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; } if (secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; } if (secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; } if (secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; } if (secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; } if (secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; } if (secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; } if (secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
} }
} }
// This function fills the palette with totally random colors. // This function fills the palette with totally random colors.
void SetupTotallyRandomPalette() void SetupTotallyRandomPalette()
{ {
for( int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
currentPalette[i] = CHSV( random8(), 255, random8()); currentPalette[i] = CHSV(random8(), 255, random8());
} }
} }
// This function sets up a palette of black and white stripes, // This function sets up a palette of black and white stripes,
@ -323,100 +323,96 @@ void SetupTotallyRandomPalette()
// to set them up. // to set them up.
void SetupBlackAndWhiteStripedPalette() void SetupBlackAndWhiteStripedPalette()
{ {
// 'black out' all 16 palette entries... // 'black out' all 16 palette entries...
fill_solid( currentPalette, 16, CRGB::Black); fill_solid(currentPalette, 16, CRGB::Black);
// and set every fourth one to white. // and set every fourth one to white.
currentPalette[0] = CRGB::White; currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White; currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White; currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White; currentPalette[12] = CRGB::White;
} }
// This function sets up a palette of purple and green stripes. // This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette() void SetupPurpleAndGreenPalette()
{ {
CRGB purple = CHSV( HUE_PURPLE, 255, 255); CRGB purple = CHSV(HUE_PURPLE, 255, 255);
CRGB green = CHSV( HUE_GREEN, 255, 255); CRGB green = CHSV(HUE_GREEN, 255, 255);
CRGB black = CRGB::Black; CRGB black = CRGB::Black;
currentPalette = CRGBPalette16( currentPalette = CRGBPalette16(
green, green, black, black, green, green, black, black,
purple, purple, black, black, purple, purple, black, black,
green, green, black, black, green, green, black, black,
purple, purple, black, black ); purple, purple, black, black
);
} }
///////////////////// FastLED-3.1.5/examples/ColorPalette /////////////////////
////////////////////////////////////////////////////////////////////////////// /////////////////// FastLED-3.1.5/examples/ColorTemperature ///////////////////
//////////////////////////////////////////////// ColorTemperature
void colorTemp() void colorTemp()
{ {
// draw a generic, no-name rainbow // draw a generic, no-name rainbow
static uint8_t starthue = 0; static uint8_t starthue = 0;
fill_rainbow( leds + 5, LED_NUM - 5, --starthue, 20); fill_rainbow(leds + 5, LED_NUM - 5, --starthue, 20);
// Choose which 'color temperature' profile to enable. // Choose which 'color temperature' profile to enable.
uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2); uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2);
if( secs < DISPLAYTIME) { if (secs < DISPLAYTIME) {
FastLED.setTemperature( TEMPERATURE_1 ); // first temperature FastLED.setTemperature(TEMPERATURE_1 ); // first temperature
leds[0] = TEMPERATURE_1; // show indicator pixel leds[0] = TEMPERATURE_1; // show indicator pixel
} else { } else {
FastLED.setTemperature( TEMPERATURE_2 ); // second temperature FastLED.setTemperature(TEMPERATURE_2 ); // second temperature
leds[0] = TEMPERATURE_2; // show indicator pixel leds[0] = TEMPERATURE_2; // show indicator pixel
} }
// Black out the LEDs for a few secnds between color changes // Black out the LEDs for a few secnds between color changes
// to let the eyes and brains adjust // to let the eyes and brains adjust
if( (secs % DISPLAYTIME) < BLACKTIME) { if((secs % DISPLAYTIME) < BLACKTIME) {
memset8( leds, 0, LED_NUM * sizeof(CRGB)); memset8(leds, 0, LED_NUM * sizeof(CRGB));
} }
FastLED.show(); FastLED.show();
FastLED.delay(8); FastLED.delay(8);
} }
//////////////////////////////////////////////////////////////////// /////////////////// FastLED-3.1.5/examples/ColorTemperature ///////////////////
//////////////////////// FastLED-3.1.5/examples/Fire202 ///////////////////////
///////////////////////////////////////////////Fire202
void fire() void fire()
{ {
// Array of temperature readings at each simulation cell // Array of temperature readings at each simulation cell
static byte heat[LED_NUM]; static byte heat[LED_NUM];
// Step 1. Cool down every cell a little // Step 1. Cool down every cell a little
for( int i = 0; i < LED_NUM; i++) { for (int i = 0; i < LED_NUM; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / LED_NUM) + 2)); heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / LED_NUM) + 2));
} }
// Step 2. Heat from each cell drifts 'up' and diffuses a little // Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= LED_NUM - 1; k >= 2; k--) { for (int k= LED_NUM - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
} }
// Step 3. Randomly ignite new 'sparks' of heat near the bottom // Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) { if (random8() < SPARKING ) {
int y = random8(7); int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) ); heat[y] = qadd8(heat[y], random8(160,255));
} }
// Step 4. Map from heat cells to LED colors // Step 4. Map from heat cells to LED colors
for( int j = 0; j < LED_NUM; j++) { for (int j = 0; j < LED_NUM; j++) {
CRGB color = HeatColor( heat[j]); CRGB color = HeatColor( heat[j]);
int pixelnumber; int pixelnumber;
if( gReverseDirection ) { if (gReverseDirection) {
pixelnumber = (LED_NUM - 1) - j; pixelnumber = (LED_NUM - 1) - j;
} else { } else {
pixelnumber = j; pixelnumber = j;
}
leds[pixelnumber] = color;
} }
leds[pixelnumber] = color;
}
FastLED.delay(1000 / speed); FastLED.delay(1000 / speed);
} }
///////////////////////////////////////////// //////////////////////// FastLED-3.1.5/examples/Fire202 ///////////////////////
void loop() { void loop() {
// MQTT // MQTT