Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * PM - 2019
- * spectrumAnalyzer.c
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <avr/sleep.h>
- #include <stdint.h>
- #include <stdio.h>
- #include "lcd.h"
- #include "adc.h"
- #include "ws2812_config.h"
- #include "light_ws2812.h"
- #include "ffft.h"
- int16_t capture[FFT_N]; /* Wave captureing buffer */
- complex_t bfly_buff[FFT_N]; /* FFT buffer */
- uint16_t spektrum[FFT_N/2]; /* Spectrum output buffer */
- #define MAXPIX 120
- #define COLORLENGTH 40
- #define FADE (256/COLORLENGTH)
- struct cRGB colors[8];
- struct cRGB led[MAXPIX];
- volatile uint8_t audioInput = PA0;
- volatile uint8_t currentColor = 0;
- volatile uint8_t viewMode = 0;
- volatile uint16_t adcValue;
- volatile unsigned char adcIndex = 0;
- ISR(TIMER1_COMPB_vect, ISR_NAKED)
- {
- sei();
- sleep_cpu();
- reti();
- }
- ISR(TIMER1_COMPA_vect)
- {
- if(adcIndex < FFT_N) {
- capture[adcIndex++] = ADC - 512;
- ADCSRA |= (1 << ADSC);
- }
- }
- void changeSource()
- {
- char str[12];
- uint8_t i = 0;
- uint8_t k = 0;
- LCD_printAt(0x00, "*Audio Analyzer*");
- LCD_printAt(0x40, "Input:3.5mm Jack");
- while (1) {
- if ((PINC & (1 << PC7)) == 0) {
- if (audioInput == PA0) {
- audioInput = PA1;
- LCD_writeInstr(LCD_INSTR_clearDisplay);
- LCD_printAt(0x00, "*Audio Analyzer*");
- LCD_printAt(0x40, "Input:Microphone");
- } else if (audioInput == PA1) {
- audioInput = PA0;
- LCD_writeInstr(LCD_INSTR_clearDisplay);
- LCD_printAt(0x00, "*Audio Analyzer*");
- LCD_printAt(0x40, "Input:3.5mm Jack");
- }
- _delay_ms(150);
- }
- if ((PINC & (1 << PC0)) == 0) {
- viewMode = (viewMode + 1) % 3;
- _delay_ms(150);
- }
- if ((PINC & (1 << PC1)) == 0) {
- currentColor = (currentColor + 1) % 7;
- _delay_ms(150);
- }
- SET_ADC_CH(audioInput);
- if(adcIndex >= FFT_N){
- fft_input(capture, bfly_buff);
- fft_execute(bfly_buff);
- fft_output(bfly_buff, spektrum);
- adcIndex = 0;
- }
- for (i = MAXPIX; i > 0; i--)
- {
- led[i - 1].r = 0; led[i - 1].g = 0; led[i - 1].b = 0;
- }
- for(i = 2; i <= 11; i++){
- if(spektrum[i] > 192)
- spektrum[i] = 192;
- uint8_t ledsToLight = spektrum[i] / 16 + 1;
- uint8_t limit = (i - 2) * 12 + ledsToLight;
- if(viewMode == 0){
- for(k = (i - 2) * 12; k < limit; k++){
- led[k] = colors[currentColor + 1];
- }
- } else if (viewMode == 1){
- led[limit - 1] = colors[currentColor + 1];
- } else {
- if(ledsToLight > 2){
- led[limit - 1] = colors[currentColor + 1];
- led[limit - 2] = colors[currentColor + 1];
- led[limit - 3] = colors[currentColor + 1];
- } else if (ledsToLight > 1) {
- led[limit - 1] = colors[currentColor + 1];
- led[limit - 2] = colors[currentColor + 1];
- } else if (ledsToLight == 1){
- led[limit - 1] = colors[currentColor + 1];
- }
- }
- }
- ws2812_setleds(led, MAXPIX);
- }
- }
- void init_timer1()
- {
- OCR1A = 1665;
- OCR1B = 1640;
- TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B);
- TCCR1B = (1 << WGM12) | (1 << CS10);
- }
- void setup() {
- DDRD |= _BV(ws2812_pin);
- DDRB &= ~(1 << PB2);
- PORTB |= (1 << PB2);
- DDRC &= ~(1 << PC0);
- PORTC |= (1 << PC0);
- DDRC &= ~(1 << PC1);
- PORTC |= (1 << PC1);
- DDRC &= ~(1 << PC7);
- PORTC |= (1 << PC7);
- DDRC |= (1 << PC2);
- PORTC |= (1 << PC2);
- uint8_t i;
- for (i = MAXPIX; i > 0; i--)
- {
- led[i - 1].r = 0; led[i - 1].g = 0; led[i - 1].b = 0;
- }
- //Rainbowcolors
- colors[0].r = 150; colors[0].g = 150; colors[0].b = 150;
- colors[1].r = 205; colors[1].g = 000; colors[1].b = 000;//red
- colors[2].r = 205; colors[2].g = 50; colors[2].b = 000;//orange
- colors[3].r = 50; colors[3].g = 205; colors[3].b = 000;//yellow
- colors[4].r = 000; colors[4].g = 205; colors[4].b = 000;//green
- colors[5].r = 205; colors[5].g = 0; colors[5].b = 78;//pink
- colors[6].r = 000; colors[6].g = 000; colors[6].b = 205;//blue
- colors[7].r = 50; colors[7].g = 000; colors[7].b = 205;//violet
- LCD_init();
- InitADC();
- sei();
- set_sleep_mode(SLEEP_MODE_IDLE);
- sleep_enable();
- }
- int main(void) {
- setup();
- init_timer1();
- changeSource();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement