From 63966da14ab66bf711547f94e4d185f2027ad253 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 17 Dec 2019 23:59:05 -0800 Subject: add sound-reactive mode and pattern-cycling interrupt --- main.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 11 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index d1a466d..06c585e 100644 --- a/main.c +++ b/main.c @@ -1,17 +1,37 @@ -#define F_CPU 8000000UL - -#include -#include -#include - #include "config.h" +#include "eq.h" #include "led.h" +#include +#include +#include +#include +#include + static struct colour seq[6]; +static bool interrupted = false; -int main() { - DDRB |= (1 << PIN_LED); - PORTB &= ~(1 << PIN_LED); +void red_green() { + memset(seq, 0, 2 * sizeof(seq[0])); + + seq[0].r = 255; + seq[1].g = 255; + + while (1) { + for (int i = 0; i < LED_COUNT; ++i) { + if (interrupted == true) { + interrupted = false; + return; + } + + led_signal_sequence(LED_COUNT, seq, 2, 1, i); + _delay_ms(200); + } + } +} + +void rainbow() { + memset(seq, 0, 6 * sizeof(seq[0])); seq[0].r = 255; seq[1].r = 255; @@ -24,11 +44,71 @@ int main() { seq[5].b = 255; while (1) { - for (int i = 0; i < NUM_LEDS; ++i) { - signal_led_sequence(NUM_LEDS, seq, 6, 4, i); + for (int i = 0; i < LED_COUNT; ++i) { + if (interrupted == true) { + interrupted = false; + return; + } + + led_signal_sequence(LED_COUNT, seq, 6, 4, i); _delay_ms(100); } } +} + +void sound_reactive() { + memset(seq, 0, 3 * sizeof(seq[0])); + + while (1) { + if (interrupted == true) { + interrupted = false; + return; + } + + eq_read(); + + seq[0].r = (eq_levels[0] > eq_levels[1]) ? eq_levels[0] : eq_levels[1]; + unsigned char t = (eq_levels[2] > eq_levels[3]) ? eq_levels[2] : eq_levels[3]; + seq[1].g = (t > eq_levels[4]) ? t : eq_levels[4]; + seq[2].b = (eq_levels[5] > eq_levels[6]) ? eq_levels[5] : eq_levels[6]; + + led_signal_sequence(LED_COUNT, seq, 3, 1, 0); + for (int i = 0; i < 20; ++i) { + eq_decay(); + } + _delay_ms(50); + } +} + + +int main() { + DDRB |= (1 << LED_PIN); + PORTB &= ~(1 << LED_PIN); + + DDRB |= (1 << EQ_PIN_RESET); + PORTB &= ~(1 << EQ_PIN_RESET); + DDRB |= (1 << EQ_PIN_STROBE); + PORTB |= (1 << EQ_PIN_STROBE); + DDRB &= ~(1 << EQ_PIN_INPUT); + + DDRB &= ~(1 << ISR_PIN); + GIMSK |= (1 << PCIE); + PCMSK |= (1 << PCINT4); + sei(); + + ADMUX |= (1 << MUX0) | (1 << ADLAR); + ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); + + while (1) { + sound_reactive(); + red_green(); + rainbow(); + } return 0; } + +ISR(PCINT0_vect) { + if (!(PINB & (1 << ISR_PIN))) + interrupted = true; +} -- cgit v1.2.3-70-g09d2