|
| 1 | +#include <EEPROM.h> |
| 2 | + |
| 3 | +#include <FastLED.h> |
| 4 | +#include <Animation.h> |
| 5 | + |
| 6 | +#include "BlinkyTape.h" |
| 7 | +#include "ColorLoop.h" |
| 8 | +#include "SerialLoop.h" |
| 9 | +#include "Scanner.h" |
| 10 | +#include "Flashlight.h" |
| 11 | + |
| 12 | +struct CRGB leds[LED_COUNT]; |
| 13 | + |
| 14 | +#define BRIGHT_STEP_COUNT 8 |
| 15 | +#define STARTING_BRIGHTNESS 4 |
| 16 | +volatile uint8_t brightnesSteps[BRIGHT_STEP_COUNT] = {5,15,40,70,93, 70, 40, 15}; |
| 17 | + |
| 18 | +uint8_t brightness = 4; |
| 19 | +uint8_t lastBrightness = 4; |
| 20 | + |
| 21 | + |
| 22 | +// For fading in a new sketch |
| 23 | +long lastTime; |
| 24 | + |
| 25 | +float fadeIndex; |
| 26 | +#define FADE_STEPS 50 |
| 27 | + |
| 28 | +// Button interrupt variables and Interrupt Service Routine |
| 29 | +uint8_t buttonState = 0; |
| 30 | +bool buttonDebounced; |
| 31 | +long buttonDownTime = 0; |
| 32 | +long buttonPressTime = 0; |
| 33 | + |
| 34 | +#define BUTTON_BRIGHTNESS_SWITCH_TIME 1 // Time to hold the button down to switch brightness |
| 35 | +#define BUTTON_PATTERN_SWITCH_TIME 1000 // Time to hold the button down to switch patterns |
| 36 | + |
| 37 | + |
| 38 | +#define EEPROM_START_ADDRESS 0 |
| 39 | +#define EEPROM_MAGIG_BYTE_0 0x12 |
| 40 | +#define EEPROM_MAGIC_BYTE_1 0x34 |
| 41 | +#define PATTERN_EEPROM_ADDRESS EEPROM_START_ADDRESS + 2 |
| 42 | + |
| 43 | +uint8_t currentPattern = 0; |
| 44 | +uint8_t patternCount = 0; |
| 45 | +#define MAX_PATTERNS 10 |
| 46 | +Pattern* patterns[MAX_PATTERNS]; |
| 47 | + |
| 48 | + |
| 49 | +// Register a pattern |
| 50 | +void registerPattern(Pattern* newPattern) { |
| 51 | + // If there is space for this pattern |
| 52 | + if(MAX_PATTERNS <= patternCount) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + patterns[patternCount] = newPattern; |
| 57 | + patternCount++; |
| 58 | +} |
| 59 | + |
| 60 | +// Change the current pattern |
| 61 | +void initializePattern(uint8_t newPattern) { |
| 62 | + // Check if this is a valid pattern |
| 63 | + if(newPattern >= MAX_PATTERNS) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + EEPROM.write(PATTERN_EEPROM_ADDRESS, newPattern); |
| 68 | + |
| 69 | + currentPattern = newPattern; |
| 70 | + patterns[currentPattern]->reset(); |
| 71 | + |
| 72 | + lastTime = millis(); |
| 73 | + fadeIndex = 0; |
| 74 | +} |
| 75 | + |
| 76 | +// Run one step of the current pattern |
| 77 | +void runPattern() { |
| 78 | + patterns[currentPattern]->draw(leds); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// Called when the button is both pressed and released. |
| 83 | +ISR(PCINT0_vect){ |
| 84 | + buttonState = !(PINB & (1 << PINB6)); // Reading state of the PB6 (remember that HIGH == released) |
| 85 | + |
| 86 | + if(buttonState) { |
| 87 | + // On button down, record the time so we can convert this into a gesture later |
| 88 | + buttonDownTime = millis(); |
| 89 | + buttonDebounced = false; |
| 90 | + |
| 91 | + // And configure and start timer4 interrupt. |
| 92 | + TCCR4B = 0x0F; // Slowest prescaler |
| 93 | + TCCR4D = _BV(WGM41) | _BV(WGM40); // Fast PWM mode |
| 94 | + OCR4C = 0x10; // some random percentage of the clock |
| 95 | + TCNT4 = 0; // Reset the counter |
| 96 | + TIMSK4 = _BV(TOV4); // turn on the interrupt |
| 97 | + |
| 98 | + } |
| 99 | + else { |
| 100 | + TIMSK4 = 0; // turn off the interrupt |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// This is called every xx ms while the button is being held down; it counts down then displays a |
| 105 | +// visual cue and changes the pattern. |
| 106 | +ISR(TIMER4_OVF_vect) { |
| 107 | + // If the user is still holding down the button after the first cycle, they were serious about it. |
| 108 | + if(buttonDebounced == false) { |
| 109 | + buttonDebounced = true; |
| 110 | + lastBrightness = brightness; |
| 111 | + brightness = (brightness + 1) % BRIGHT_STEP_COUNT; |
| 112 | + LEDS.setBrightness(brightnesSteps[brightness]); |
| 113 | + } |
| 114 | + |
| 115 | + // If we've waited long enough, switch the pattern |
| 116 | + // TODO: visual indicator |
| 117 | + buttonPressTime = millis() - buttonDownTime; |
| 118 | + if(buttonPressTime > BUTTON_PATTERN_SWITCH_TIME) { |
| 119 | + // first unroll the brightness! |
| 120 | + brightness = lastBrightness; |
| 121 | + LEDS.setBrightness(brightnesSteps[brightness]); |
| 122 | + |
| 123 | + initializePattern((currentPattern+1)%patternCount); |
| 124 | + |
| 125 | + // Finally, reset the button down time, so we don't advance again too quickly |
| 126 | + buttonDownTime = millis(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +ColorLoop originalRainbow(1,1,1); |
| 131 | +ColorLoop blueRainbow(.2,1,1); |
| 132 | +Scanner scanner(4, CRGB(255,0,0)); |
| 133 | +Flashlight flashlight(CRGB(255,255,255)); |
| 134 | + |
| 135 | +void setup() |
| 136 | +{ |
| 137 | + Serial.begin(57600); |
| 138 | + |
| 139 | + LEDS.addLeds<WS2811, LED_OUT, GRB>(leds, LED_COUNT); |
| 140 | + brightness = STARTING_BRIGHTNESS; |
| 141 | + LEDS.setBrightness(brightnesSteps[brightness]); |
| 142 | + LEDS.show(); |
| 143 | + |
| 144 | + pinMode(BUTTON_IN, INPUT_PULLUP); |
| 145 | + pinMode(ANALOG_INPUT, INPUT_PULLUP); |
| 146 | + pinMode(EXTRA_PIN_A, INPUT_PULLUP); |
| 147 | + pinMode(EXTRA_PIN_B, INPUT_PULLUP); |
| 148 | + |
| 149 | + // Interrupt set-up; see Atmega32u4 datasheet section 11 |
| 150 | + PCIFR |= (1 << PCIF0); // Just in case, clear interrupt flag |
| 151 | + PCMSK0 |= (1 << PCINT6); // Set interrupt mask to the button pin (PCINT6) |
| 152 | + PCICR |= (1 << PCIE0); // Enable interrupt |
| 153 | + |
| 154 | + registerPattern(&originalRainbow); |
| 155 | + registerPattern(&blueRainbow); |
| 156 | + registerPattern(&scanner); |
| 157 | + registerPattern(&flashlight); |
| 158 | + |
| 159 | + |
| 160 | + // Attempt to read in the last used pattern; if it's an invalid |
| 161 | + // number, initialize it to 0 instead. |
| 162 | + if((EEPROM.read(EEPROM_START_ADDRESS) == EEPROM_MAGIG_BYTE_0) |
| 163 | + && (EEPROM.read(EEPROM_START_ADDRESS + 1) == EEPROM_MAGIC_BYTE_1)) { |
| 164 | + currentPattern = EEPROM.read(PATTERN_EEPROM_ADDRESS); |
| 165 | + if(currentPattern >= patternCount) { |
| 166 | + currentPattern = 0; |
| 167 | + } |
| 168 | + } |
| 169 | + else { |
| 170 | + EEPROM.write(EEPROM_START_ADDRESS, EEPROM_MAGIG_BYTE_0); |
| 171 | + EEPROM.write(EEPROM_START_ADDRESS, EEPROM_MAGIC_BYTE_1); |
| 172 | + currentPattern = 0; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + initializePattern(currentPattern); |
| 177 | +} |
| 178 | + |
| 179 | +void loop() |
| 180 | +{ |
| 181 | + // If'n we get some data, switch to passthrough mode |
| 182 | + if(Serial.available() > 0) { |
| 183 | + serialLoop(leds); |
| 184 | + } |
| 185 | + |
| 186 | + // Draw the current pattern |
| 187 | + runPattern(); |
| 188 | + |
| 189 | + if((millis() - lastTime > 15) && fadeIndex < FADE_STEPS) { |
| 190 | + lastTime = millis(); |
| 191 | + fadeIndex++; |
| 192 | + |
| 193 | + LEDS.setBrightness(brightnesSteps[brightness]*(fadeIndex/FADE_STEPS)); |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + LEDS.show(); |
| 198 | +} |
| 199 | + |
0 commit comments