David Knight / Mbed 2 deprecated lichtspiel

Dependencies:   PololuLedStrip mbed

Committer:
thegink
Date:
Sat May 19 14:27:12 2018 +0000
Revision:
0:8dc213146b30
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thegink 0:8dc213146b30 1 #include "Effects.h"
thegink 0:8dc213146b30 2
thegink 0:8dc213146b30 3 Effects::Effects(LEDs *_leds)
thegink 0:8dc213146b30 4 {
thegink 0:8dc213146b30 5 lastUpdate = 0;
thegink 0:8dc213146b30 6 lastEffect = 0;
thegink 0:8dc213146b30 7 leds = _leds;
thegink 0:8dc213146b30 8 timer.start();
thegink 0:8dc213146b30 9 }
thegink 0:8dc213146b30 10
thegink 0:8dc213146b30 11 void Effects::doEffect(uint8_t effect, Rotary::Action action) {
thegink 0:8dc213146b30 12 uint32_t ms = timer.read_ms();
thegink 0:8dc213146b30 13
thegink 0:8dc213146b30 14 switch (effect) {
thegink 0:8dc213146b30 15 case 1:
thegink 0:8dc213146b30 16 scroll(ms, action);
thegink 0:8dc213146b30 17 break;
thegink 0:8dc213146b30 18 case 3:
thegink 0:8dc213146b30 19 makeRainbow(effect != lastEffect, ms, action);
thegink 0:8dc213146b30 20 break;
thegink 0:8dc213146b30 21 case 2:
thegink 0:8dc213146b30 22 blink(effect != lastEffect, action);
thegink 0:8dc213146b30 23 break;
thegink 0:8dc213146b30 24 case 4:
thegink 0:8dc213146b30 25 colorChase(ms, action);
thegink 0:8dc213146b30 26 break;
thegink 0:8dc213146b30 27 case 5:
thegink 0:8dc213146b30 28 knightRider(ms);
thegink 0:8dc213146b30 29 break;
thegink 0:8dc213146b30 30 case 6:
thegink 0:8dc213146b30 31 leftRightColorFade(ms, action);
thegink 0:8dc213146b30 32 default:
thegink 0:8dc213146b30 33 break;
thegink 0:8dc213146b30 34 }
thegink 0:8dc213146b30 35 lastEffect = effect;
thegink 0:8dc213146b30 36 }
thegink 0:8dc213146b30 37
thegink 0:8dc213146b30 38 void Effects::noEffect() {
thegink 0:8dc213146b30 39 if (lastEffect == 3) {
thegink 0:8dc213146b30 40 leds->loadStateFromEEPROMAndDim(0);
thegink 0:8dc213146b30 41 }
thegink 0:8dc213146b30 42 lastEffect = 0;
thegink 0:8dc213146b30 43 }
thegink 0:8dc213146b30 44
thegink 0:8dc213146b30 45 void Effects::scroll(uint32_t ms, Rotary::Action action) {
thegink 0:8dc213146b30 46 static int8_t MAX_SCROLL_SPEED = 8;
thegink 0:8dc213146b30 47 static uint16_t SCROLL_SPEED_TO_MS[8] = {
thegink 0:8dc213146b30 48 500, 400, 300, 200, 150, 100, 66, 33
thegink 0:8dc213146b30 49 };
thegink 0:8dc213146b30 50 static int8_t scrollSpeed = 2;
thegink 0:8dc213146b30 51
thegink 0:8dc213146b30 52 if (ms > SCROLL_SPEED_TO_MS[abs(scrollSpeed)]) {
thegink 0:8dc213146b30 53 if (scrollSpeed > 0) leds->scrollStripToRight(); else leds->scrollStripToLeft();
thegink 0:8dc213146b30 54 timer.reset();
thegink 0:8dc213146b30 55 }
thegink 0:8dc213146b30 56 switch (action) {
thegink 0:8dc213146b30 57 case Rotary::LEFT:
thegink 0:8dc213146b30 58 if (scrollSpeed > -MAX_SCROLL_SPEED + 1) scrollSpeed--;
thegink 0:8dc213146b30 59 break;
thegink 0:8dc213146b30 60 case Rotary::RIGHT:
thegink 0:8dc213146b30 61 if (scrollSpeed < MAX_SCROLL_SPEED - 1) scrollSpeed++;
thegink 0:8dc213146b30 62 break;
thegink 0:8dc213146b30 63 default:
thegink 0:8dc213146b30 64 break;
thegink 0:8dc213146b30 65 }
thegink 0:8dc213146b30 66 }
thegink 0:8dc213146b30 67
thegink 0:8dc213146b30 68 void Effects::knightRider(uint32_t ms) {
thegink 0:8dc213146b30 69 static uint8_t DELAY = 50;
thegink 0:8dc213146b30 70 static int8_t position = 0;
thegink 0:8dc213146b30 71 static int8_t direction = 1;
thegink 0:8dc213146b30 72
thegink 0:8dc213146b30 73 for (int8_t i = 0; i < LEDS_MAIN_STRIP_NUM_LEDS; i++) {
thegink 0:8dc213146b30 74 if (i == position) leds->setStripLEDRGB(i, 200, 0, 0);
thegink 0:8dc213146b30 75 else if (i == (position - direction)) leds->setStripLEDRGB(i, 100, 0, 0);
thegink 0:8dc213146b30 76 else if (i == (position - (direction * 2))) leds->setStripLEDRGB(i, 75, 0, 0);
thegink 0:8dc213146b30 77 else if (i == (position - (direction * 3))) leds->setStripLEDRGB(i, 50, 0, 0);
thegink 0:8dc213146b30 78 else if (i == (position - (direction * 4))) leds->setStripLEDRGB(i, 25, 0, 0);
thegink 0:8dc213146b30 79 else leds->setStripLEDRGB(i, 0, 0, 0);
thegink 0:8dc213146b30 80 }
thegink 0:8dc213146b30 81 leds->show();
thegink 0:8dc213146b30 82 if (ms > DELAY) {
thegink 0:8dc213146b30 83 position += direction;
thegink 0:8dc213146b30 84 if (position >= LEDS_MAIN_STRIP_NUM_LEDS) {
thegink 0:8dc213146b30 85 direction = -direction;
thegink 0:8dc213146b30 86 } else if (position < 0) {
thegink 0:8dc213146b30 87 direction = -direction;
thegink 0:8dc213146b30 88 }
thegink 0:8dc213146b30 89 timer.reset();
thegink 0:8dc213146b30 90 }
thegink 0:8dc213146b30 91 }
thegink 0:8dc213146b30 92
thegink 0:8dc213146b30 93 void Effects::blink(bool firstTime, Rotary::Action action) {
thegink 0:8dc213146b30 94 static uint8_t MAX_BLINK_SPEED = 4;
thegink 0:8dc213146b30 95 static int8_t blinkSpeed = 1;
thegink 0:8dc213146b30 96 static int16_t dimAmount = 0;
thegink 0:8dc213146b30 97 static int8_t direction = 1;
thegink 0:8dc213146b30 98 if (firstTime) leds->saveStateToEEPROM(); //TODO: Replace this with something else
thegink 0:8dc213146b30 99 leds->loadStateFromEEPROMAndDim(dimAmount);
thegink 0:8dc213146b30 100 dimAmount += direction * blinkSpeed * 2;
thegink 0:8dc213146b30 101 if (dimAmount > 255) {
thegink 0:8dc213146b30 102 dimAmount = 255;
thegink 0:8dc213146b30 103 direction = -direction;
thegink 0:8dc213146b30 104 } else if (dimAmount < 0) {
thegink 0:8dc213146b30 105 dimAmount = 0;
thegink 0:8dc213146b30 106 direction = -direction;
thegink 0:8dc213146b30 107 }
thegink 0:8dc213146b30 108 switch (action) {
thegink 0:8dc213146b30 109 case Rotary::LEFT:
thegink 0:8dc213146b30 110 if (blinkSpeed > 0 ) blinkSpeed--;
thegink 0:8dc213146b30 111 break;
thegink 0:8dc213146b30 112 case Rotary::RIGHT:
thegink 0:8dc213146b30 113 if (blinkSpeed < MAX_BLINK_SPEED) blinkSpeed++;
thegink 0:8dc213146b30 114 break;
thegink 0:8dc213146b30 115 default:
thegink 0:8dc213146b30 116 break;
thegink 0:8dc213146b30 117 }
thegink 0:8dc213146b30 118 }
thegink 0:8dc213146b30 119
thegink 0:8dc213146b30 120 void Effects::makeRainbow(bool firstTime, uint32_t ms, Rotary::Action action) {
thegink 0:8dc213146b30 121 static uint8_t MAX_SPEED = 8;
thegink 0:8dc213146b30 122 static uint16_t SPEED_TO_MS[8] = {
thegink 0:8dc213146b30 123 500, 400, 300, 200, 150, 100, 66, 33
thegink 0:8dc213146b30 124 };
thegink 0:8dc213146b30 125 static uint8_t speed = 4;
thegink 0:8dc213146b30 126 static int8_t step = 0;
thegink 0:8dc213146b30 127 static bool phase2 = false;
thegink 0:8dc213146b30 128
thegink 0:8dc213146b30 129 if (firstTime) {
thegink 0:8dc213146b30 130 step = 0;
thegink 0:8dc213146b30 131 phase2 = false;
thegink 0:8dc213146b30 132 speed = 4;
thegink 0:8dc213146b30 133 }
thegink 0:8dc213146b30 134 if (ms > SPEED_TO_MS[speed]) {
thegink 0:8dc213146b30 135 timer.reset();
thegink 0:8dc213146b30 136 if (step < LEDS_MAIN_STRIP_NUM_LEDS) {
thegink 0:8dc213146b30 137 for (uint8_t i = 0; i <= step; i++) {
thegink 0:8dc213146b30 138 if (phase2) {
thegink 0:8dc213146b30 139 leds->setStripLED(i, 0, 0, 0);
thegink 0:8dc213146b30 140 } else {
thegink 0:8dc213146b30 141 leds->setStripLED(i, 255.0 / LEDS_MAIN_STRIP_NUM_LEDS * i, 250, 200);
thegink 0:8dc213146b30 142 }
thegink 0:8dc213146b30 143 }
thegink 0:8dc213146b30 144 step++;
thegink 0:8dc213146b30 145 } else {
thegink 0:8dc213146b30 146 phase2 = !phase2;
thegink 0:8dc213146b30 147 step = 0;
thegink 0:8dc213146b30 148 }
thegink 0:8dc213146b30 149 }
thegink 0:8dc213146b30 150
thegink 0:8dc213146b30 151 switch (action) {
thegink 0:8dc213146b30 152 case Rotary::LEFT:
thegink 0:8dc213146b30 153 if (speed > 0 ) speed--;
thegink 0:8dc213146b30 154 break;
thegink 0:8dc213146b30 155 case Rotary::RIGHT:
thegink 0:8dc213146b30 156 if (speed < MAX_SPEED - 1) speed++;
thegink 0:8dc213146b30 157 break;
thegink 0:8dc213146b30 158 default:
thegink 0:8dc213146b30 159 break;
thegink 0:8dc213146b30 160 }
thegink 0:8dc213146b30 161 }
thegink 0:8dc213146b30 162
thegink 0:8dc213146b30 163 void Effects::colorChase(uint32_t ms, Rotary::Action action) {
thegink 0:8dc213146b30 164 static uint8_t DELAY_MS = 200;
thegink 0:8dc213146b30 165 static uint8_t COLOR_STEPS = 6;
thegink 0:8dc213146b30 166 static uint8_t step = 0;
thegink 0:8dc213146b30 167 static uint8_t colorStep = 0;
thegink 0:8dc213146b30 168
thegink 0:8dc213146b30 169 if (ms > DELAY_MS) {
thegink 0:8dc213146b30 170 timer.reset();
thegink 0:8dc213146b30 171 if (step > 0) leds->setStripLED(step - 1, 0, 0, 0);
thegink 0:8dc213146b30 172 if (step < LEDS_MAIN_STRIP_NUM_LEDS) {
thegink 0:8dc213146b30 173 leds->setStripLED(step, 255.0 / COLOR_STEPS * colorStep, 250, 200);
thegink 0:8dc213146b30 174 step++;
thegink 0:8dc213146b30 175 } else {
thegink 0:8dc213146b30 176 colorStep++;
thegink 0:8dc213146b30 177 if (colorStep >= COLOR_STEPS) colorStep = 0;
thegink 0:8dc213146b30 178 step = 0;
thegink 0:8dc213146b30 179 }
thegink 0:8dc213146b30 180 }
thegink 0:8dc213146b30 181
thegink 0:8dc213146b30 182 switch (action) {
thegink 0:8dc213146b30 183 case Rotary::LEFT:
thegink 0:8dc213146b30 184 if (step > 0) step--;
thegink 0:8dc213146b30 185 leds->scrollStripToLeft();
thegink 0:8dc213146b30 186 break;
thegink 0:8dc213146b30 187 case Rotary::RIGHT:
thegink 0:8dc213146b30 188 if (step < LEDS_MAIN_STRIP_NUM_LEDS) step++;
thegink 0:8dc213146b30 189 leds->scrollStripToRight();
thegink 0:8dc213146b30 190 break;
thegink 0:8dc213146b30 191 default:
thegink 0:8dc213146b30 192 break;
thegink 0:8dc213146b30 193 }
thegink 0:8dc213146b30 194 }
thegink 0:8dc213146b30 195
thegink 0:8dc213146b30 196 void Effects::leftRightColorFade(uint32_t ms, Rotary::Action action) {
thegink 0:8dc213146b30 197 static uint8_t DELAY_MS = 50;
thegink 0:8dc213146b30 198 static uint8_t COLOR_STEPS = 8;
thegink 0:8dc213146b30 199 static uint8_t center = LEDS_MAIN_STRIP_NUM_LEDS / 2;
thegink 0:8dc213146b30 200 static uint8_t step = 0;
thegink 0:8dc213146b30 201 static uint8_t colorStep = 0;
thegink 0:8dc213146b30 202 static bool turnOff = false;
thegink 0:8dc213146b30 203
thegink 0:8dc213146b30 204 if (ms > DELAY_MS) {
thegink 0:8dc213146b30 205 timer.reset();
thegink 0:8dc213146b30 206 for (uint8_t i = 0; i < step; i++) {
thegink 0:8dc213146b30 207 if (i <= center) {
thegink 0:8dc213146b30 208 leds->setStripLED(center - i, 255.0 / COLOR_STEPS * colorStep, 200, turnOff ? 0 : 200);
thegink 0:8dc213146b30 209 }
thegink 0:8dc213146b30 210 if (center + i < LEDS_MAIN_STRIP_NUM_LEDS) {
thegink 0:8dc213146b30 211 leds->setStripLED(center + i, 255.0 / COLOR_STEPS * colorStep, 200, turnOff ? 0 : 200);
thegink 0:8dc213146b30 212 }
thegink 0:8dc213146b30 213 }
thegink 0:8dc213146b30 214 leds->setStripLED(center, 0, 0, 200);
thegink 0:8dc213146b30 215 if (step > center && center + step >= LEDS_MAIN_STRIP_NUM_LEDS) {
thegink 0:8dc213146b30 216 step = 0;
thegink 0:8dc213146b30 217 if (turnOff) {
thegink 0:8dc213146b30 218 colorStep++;
thegink 0:8dc213146b30 219 if (colorStep == COLOR_STEPS) colorStep = 0;
thegink 0:8dc213146b30 220 }
thegink 0:8dc213146b30 221 turnOff = !turnOff;
thegink 0:8dc213146b30 222 } else {
thegink 0:8dc213146b30 223 step++;
thegink 0:8dc213146b30 224 }
thegink 0:8dc213146b30 225 }
thegink 0:8dc213146b30 226
thegink 0:8dc213146b30 227 switch (action) {
thegink 0:8dc213146b30 228 case Rotary::LEFT:
thegink 0:8dc213146b30 229 if (center > 5) center--;
thegink 0:8dc213146b30 230 break;
thegink 0:8dc213146b30 231 case Rotary::RIGHT:
thegink 0:8dc213146b30 232 if (center < 9) center++;
thegink 0:8dc213146b30 233 break;
thegink 0:8dc213146b30 234 default:
thegink 0:8dc213146b30 235 break;
thegink 0:8dc213146b30 236 }
thegink 0:8dc213146b30 237 }