summaryrefslogtreecommitdiff
path: root/eq.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2019-12-17 23:59:05 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2019-12-17 23:59:05 -0800
commit63966da14ab66bf711547f94e4d185f2027ad253 (patch)
treefddaf4558fb2ed23952768ad549f9dff36422e53 /eq.c
parentc5047bb9952655dc8d0fabf6a54b9e278b243d8b (diff)
downloadchristmas_lights-63966da14ab66bf711547f94e4d185f2027ad253.tar.gz
christmas_lights-63966da14ab66bf711547f94e4d185f2027ad253.tar.xz
add sound-reactive mode and pattern-cycling interrupt
Diffstat (limited to 'eq.c')
-rw-r--r--eq.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/eq.c b/eq.c
new file mode 100644
index 0000000..eb7febd
--- /dev/null
+++ b/eq.c
@@ -0,0 +1,51 @@
+#include "config.h"
+#include "eq.h"
+
+#include <avr/io.h>
+#include <util/delay.h>
+
+unsigned char eq_levels[7];
+
+static const unsigned char thresholds[][2] = { EQ_THRESHOLDS };
+
+static void reset() {
+ PORTB |= (1 << EQ_PIN_RESET);
+ PORTB &= ~(1 << EQ_PIN_RESET);
+ _delay_us(36);
+}
+
+static void strobe() {
+ PORTB |= (1 << EQ_PIN_STROBE);
+ _delay_us(36);
+ PORTB &= ~(1 << EQ_PIN_STROBE);
+ _delay_us(36);
+}
+
+static unsigned char map_level(unsigned char val, unsigned char min, unsigned char max) {
+ if (val <= min)
+ return 0;
+ else if (val >= max)
+ return 255;
+ else
+ return val * 255 / (max - min);
+}
+
+void eq_read() {
+ reset();
+
+ for (int i = 0; i < 7; ++i) {
+ strobe();
+
+ ADCSRA |= (1 << ADSC);
+ while (ADCSRA & (1 << ADSC));
+ eq_levels[i] = map_level(ADCH, thresholds[i][0], thresholds[i][1]);
+ }
+}
+
+void eq_decay() {
+ reset();
+
+ for (int i = 0; i < 7; ++i) {
+ strobe();
+ }
+}