David Knight / Mbed 2 deprecated lichtspiel

Dependencies:   PololuLedStrip mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Effects.cpp Source File

Effects.cpp

00001 #include "Effects.h"
00002 
00003 Effects::Effects(LEDs *_leds)
00004 {
00005     lastUpdate = 0;
00006     lastEffect = 0;
00007     leds = _leds;
00008     timer.start();
00009 }
00010 
00011 void Effects::doEffect(uint8_t effect, Rotary::Action action) {
00012   uint32_t ms = timer.read_ms();
00013 
00014   switch (effect) {
00015     case 1:
00016       scroll(ms, action);
00017       break;
00018     case 3:
00019       makeRainbow(effect != lastEffect, ms, action);
00020       break;
00021     case 2:
00022       blink(effect != lastEffect, action);
00023       break;
00024     case 4:
00025       colorChase(ms, action);
00026       break;
00027     case 5:
00028       knightRider(ms);
00029       break;
00030     case 6:
00031       leftRightColorFade(ms, action);
00032     default:
00033       break;
00034   }
00035   lastEffect = effect;
00036 }
00037 
00038 void Effects::noEffect() {
00039   if (lastEffect == 3) {
00040     leds->loadStateFromEEPROMAndDim(0);
00041   }
00042   lastEffect = 0;
00043 }
00044 
00045 void Effects::scroll(uint32_t ms, Rotary::Action action) {
00046   static int8_t MAX_SCROLL_SPEED = 8;
00047   static uint16_t SCROLL_SPEED_TO_MS[8] = {
00048     500, 400, 300, 200, 150, 100, 66, 33
00049   };
00050   static int8_t scrollSpeed = 2;
00051 
00052   if (ms > SCROLL_SPEED_TO_MS[abs(scrollSpeed)]) {
00053     if (scrollSpeed > 0) leds->scrollStripToRight(); else leds->scrollStripToLeft();
00054     timer.reset();
00055   }
00056   switch (action) {
00057     case Rotary::LEFT:
00058       if (scrollSpeed > -MAX_SCROLL_SPEED + 1) scrollSpeed--;
00059       break;
00060     case Rotary::RIGHT:
00061       if (scrollSpeed < MAX_SCROLL_SPEED - 1) scrollSpeed++;
00062       break;
00063     default:
00064       break;
00065   }
00066 }
00067 
00068 void Effects::knightRider(uint32_t ms) {
00069   static uint8_t DELAY = 50;
00070   static int8_t position = 0;
00071   static int8_t direction = 1;
00072 
00073   for (int8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) {
00074     if      (i == position)                     leds->setStripLEDRGB(i, 200, 0, 0);
00075     else if (i == (position - direction))       leds->setStripLEDRGB(i, 100, 0, 0);
00076     else if (i == (position - (direction * 2))) leds->setStripLEDRGB(i, 75, 0, 0);
00077     else if (i == (position - (direction * 3))) leds->setStripLEDRGB(i, 50, 0, 0);
00078     else if (i == (position - (direction * 4))) leds->setStripLEDRGB(i, 25, 0, 0);
00079     else leds->setStripLEDRGB(i, 0, 0, 0);
00080   }
00081   leds->show();
00082   if (ms > DELAY) {
00083     position += direction;
00084     if (position >= LEDS_MAIN_STRIP_NUM_LEDS) {
00085       direction = -direction;
00086     } else if (position < 0) {
00087       direction = -direction;
00088     }
00089     timer.reset();
00090   }
00091 }
00092 
00093 void Effects::blink(bool firstTime, Rotary::Action action) {
00094   static uint8_t MAX_BLINK_SPEED = 4;
00095   static int8_t blinkSpeed = 1;
00096   static int16_t dimAmount = 0;
00097   static int8_t direction = 1;
00098   if (firstTime) leds->saveStateToEEPROM();           //TODO: Replace this with something else
00099   leds->loadStateFromEEPROMAndDim(dimAmount);
00100   dimAmount += direction * blinkSpeed * 2;
00101   if (dimAmount > 255) {
00102     dimAmount = 255;
00103     direction = -direction;
00104   } else if (dimAmount < 0) {
00105     dimAmount = 0;
00106     direction = -direction;
00107   }
00108   switch (action) {
00109     case Rotary::LEFT:
00110       if (blinkSpeed > 0 ) blinkSpeed--;
00111       break;
00112     case Rotary::RIGHT:
00113       if (blinkSpeed < MAX_BLINK_SPEED) blinkSpeed++;
00114       break;
00115     default:
00116       break;
00117   }
00118 }
00119 
00120 void Effects::makeRainbow(bool firstTime, uint32_t ms, Rotary::Action action) {
00121   static uint8_t MAX_SPEED = 8;
00122   static uint16_t SPEED_TO_MS[8] = {
00123     500, 400, 300, 200, 150, 100, 66, 33
00124   };
00125   static uint8_t speed = 4;
00126   static int8_t step = 0;
00127   static bool phase2 = false;
00128 
00129   if (firstTime) {
00130     step = 0;
00131     phase2 = false;
00132     speed = 4;
00133   }
00134   if (ms > SPEED_TO_MS[speed]) {
00135     timer.reset();
00136     if (step < LEDS_MAIN_STRIP_NUM_LEDS) {
00137       for (uint8_t i = 0; i <= step; i++) {
00138         if (phase2) {
00139           leds->setStripLED(i, 0, 0, 0);
00140         } else {
00141           leds->setStripLED(i, 255.0 / LEDS_MAIN_STRIP_NUM_LEDS * i, 250, 200);
00142         }
00143       }
00144       step++;
00145     } else {
00146       phase2 = !phase2;
00147       step = 0;
00148     }
00149   }
00150 
00151   switch (action) {
00152     case Rotary::LEFT:
00153       if (speed > 0 ) speed--;
00154       break;
00155     case Rotary::RIGHT:
00156       if (speed < MAX_SPEED - 1) speed++;
00157       break;
00158     default:
00159       break;
00160   }
00161 }
00162 
00163 void Effects::colorChase(uint32_t ms, Rotary::Action action) {
00164   static uint8_t DELAY_MS = 200;
00165   static uint8_t COLOR_STEPS = 6;
00166   static uint8_t step = 0;
00167   static uint8_t colorStep = 0;
00168 
00169   if (ms > DELAY_MS) {
00170     timer.reset();
00171     if (step > 0) leds->setStripLED(step - 1, 0, 0, 0);
00172     if (step < LEDS_MAIN_STRIP_NUM_LEDS) {
00173       leds->setStripLED(step, 255.0 / COLOR_STEPS * colorStep, 250, 200);
00174       step++;
00175     } else {
00176       colorStep++;
00177       if (colorStep >= COLOR_STEPS) colorStep = 0;
00178       step = 0;
00179     }
00180   }
00181 
00182   switch (action) {
00183     case Rotary::LEFT:
00184       if (step > 0) step--;
00185       leds->scrollStripToLeft();
00186       break;
00187     case Rotary::RIGHT:
00188       if (step < LEDS_MAIN_STRIP_NUM_LEDS) step++;
00189       leds->scrollStripToRight();
00190       break;
00191     default:
00192       break;
00193   }
00194 }
00195 
00196 void Effects::leftRightColorFade(uint32_t ms, Rotary::Action action) {
00197   static uint8_t DELAY_MS = 50;
00198   static uint8_t COLOR_STEPS = 8;
00199   static uint8_t center = LEDS_MAIN_STRIP_NUM_LEDS / 2;
00200   static uint8_t step = 0;
00201   static uint8_t colorStep = 0;
00202   static bool turnOff = false;
00203 
00204   if (ms > DELAY_MS) {
00205     timer.reset();
00206     for (uint8_t i = 0; i < step; i++) {
00207       if (i <= center) {
00208         leds->setStripLED(center - i, 255.0 / COLOR_STEPS * colorStep, 200, turnOff ? 0 : 200);
00209       }
00210       if (center + i < LEDS_MAIN_STRIP_NUM_LEDS) {
00211         leds->setStripLED(center + i, 255.0 / COLOR_STEPS * colorStep, 200, turnOff ? 0 : 200);
00212       }
00213     }
00214     leds->setStripLED(center, 0, 0, 200);
00215     if (step > center && center + step >= LEDS_MAIN_STRIP_NUM_LEDS) {
00216       step = 0;
00217       if (turnOff) {
00218         colorStep++;
00219         if (colorStep == COLOR_STEPS) colorStep = 0;
00220       }
00221       turnOff = !turnOff;
00222     } else {
00223       step++;
00224     }
00225   }
00226 
00227   switch (action) {
00228     case Rotary::LEFT:
00229       if (center > 5) center--;
00230       break;
00231     case Rotary::RIGHT:
00232       if (center < 9) center++;
00233       break;
00234     default:
00235       break;
00236   }
00237 }