Advertisement
Guest User

Untitled

a guest
May 21st, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. /*
  2. * PM - 2019
  3. * spectrumAnalyzer.c
  4. */
  5.  
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include <avr/interrupt.h>
  9. #include <avr/sleep.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include "lcd.h"
  13. #include "adc.h"
  14. #include "ws2812_config.h"
  15. #include "light_ws2812.h"
  16. #include "ffft.h"
  17.  
  18.  
  19. int16_t capture[FFT_N]; /* Wave captureing buffer */
  20. complex_t bfly_buff[FFT_N]; /* FFT buffer */
  21. uint16_t spektrum[FFT_N/2]; /* Spectrum output buffer */
  22.  
  23.  
  24. #define MAXPIX 120
  25. #define COLORLENGTH 40
  26. #define FADE (256/COLORLENGTH)
  27.  
  28. struct cRGB colors[8];
  29. struct cRGB led[MAXPIX];
  30.  
  31. volatile uint8_t audioInput = PA0;
  32. volatile uint8_t currentColor = 0;
  33. volatile uint8_t viewMode = 0;
  34. volatile uint16_t adcValue;
  35. volatile unsigned char adcIndex = 0;
  36.  
  37.  
  38. ISR(TIMER1_COMPB_vect, ISR_NAKED)
  39. {
  40. sei();
  41. sleep_cpu();
  42. reti();
  43. }
  44.  
  45. ISR(TIMER1_COMPA_vect)
  46. {
  47. if(adcIndex < FFT_N) {
  48. capture[adcIndex++] = ADC - 512;
  49. ADCSRA |= (1 << ADSC);
  50. }
  51. }
  52.  
  53. void changeSource()
  54. {
  55. char str[12];
  56. uint8_t i = 0;
  57. uint8_t k = 0;
  58. LCD_printAt(0x00, "*Audio Analyzer*");
  59. LCD_printAt(0x40, "Input:3.5mm Jack");
  60.  
  61. while (1) {
  62.  
  63. if ((PINC & (1 << PC7)) == 0) {
  64. if (audioInput == PA0) {
  65. audioInput = PA1;
  66. LCD_writeInstr(LCD_INSTR_clearDisplay);
  67. LCD_printAt(0x00, "*Audio Analyzer*");
  68. LCD_printAt(0x40, "Input:Microphone");
  69. } else if (audioInput == PA1) {
  70. audioInput = PA0;
  71. LCD_writeInstr(LCD_INSTR_clearDisplay);
  72. LCD_printAt(0x00, "*Audio Analyzer*");
  73. LCD_printAt(0x40, "Input:3.5mm Jack");
  74. }
  75. _delay_ms(150);
  76. }
  77.  
  78. if ((PINC & (1 << PC0)) == 0) {
  79. viewMode = (viewMode + 1) % 3;
  80. _delay_ms(150);
  81. }
  82.  
  83. if ((PINC & (1 << PC1)) == 0) {
  84. currentColor = (currentColor + 1) % 7;
  85. _delay_ms(150);
  86. }
  87.  
  88.  
  89.  
  90. SET_ADC_CH(audioInput);
  91.  
  92. if(adcIndex >= FFT_N){
  93. fft_input(capture, bfly_buff);
  94. fft_execute(bfly_buff);
  95. fft_output(bfly_buff, spektrum);
  96. adcIndex = 0;
  97. }
  98.  
  99. for (i = MAXPIX; i > 0; i--)
  100. {
  101. led[i - 1].r = 0; led[i - 1].g = 0; led[i - 1].b = 0;
  102. }
  103.  
  104. for(i = 2; i <= 11; i++){
  105. if(spektrum[i] > 192)
  106. spektrum[i] = 192;
  107. uint8_t ledsToLight = spektrum[i] / 16 + 1;
  108. uint8_t limit = (i - 2) * 12 + ledsToLight;
  109. if(viewMode == 0){
  110. for(k = (i - 2) * 12; k < limit; k++){
  111. led[k] = colors[currentColor + 1];
  112. }
  113. } else if (viewMode == 1){
  114. led[limit - 1] = colors[currentColor + 1];
  115. } else {
  116. if(ledsToLight > 2){
  117. led[limit - 1] = colors[currentColor + 1];
  118. led[limit - 2] = colors[currentColor + 1];
  119. led[limit - 3] = colors[currentColor + 1];
  120. } else if (ledsToLight > 1) {
  121. led[limit - 1] = colors[currentColor + 1];
  122. led[limit - 2] = colors[currentColor + 1];
  123. } else if (ledsToLight == 1){
  124. led[limit - 1] = colors[currentColor + 1];
  125. }
  126. }
  127. }
  128.  
  129. ws2812_setleds(led, MAXPIX);
  130.  
  131. }
  132. }
  133.  
  134. void init_timer1()
  135. {
  136. OCR1A = 1665;
  137. OCR1B = 1640;
  138. TIMSK1 = (1 << OCIE1A) | (1 << OCIE1B);
  139. TCCR1B = (1 << WGM12) | (1 << CS10);
  140. }
  141.  
  142. void setup() {
  143. DDRD |= _BV(ws2812_pin);
  144.  
  145.  
  146. DDRB &= ~(1 << PB2);
  147. PORTB |= (1 << PB2);
  148. DDRC &= ~(1 << PC0);
  149. PORTC |= (1 << PC0);
  150. DDRC &= ~(1 << PC1);
  151. PORTC |= (1 << PC1);
  152. DDRC &= ~(1 << PC7);
  153. PORTC |= (1 << PC7);
  154. DDRC |= (1 << PC2);
  155. PORTC |= (1 << PC2);
  156.  
  157. uint8_t i;
  158. for (i = MAXPIX; i > 0; i--)
  159. {
  160. led[i - 1].r = 0; led[i - 1].g = 0; led[i - 1].b = 0;
  161. }
  162.  
  163. //Rainbowcolors
  164. colors[0].r = 150; colors[0].g = 150; colors[0].b = 150;
  165. colors[1].r = 205; colors[1].g = 000; colors[1].b = 000;//red
  166. colors[2].r = 205; colors[2].g = 50; colors[2].b = 000;//orange
  167. colors[3].r = 50; colors[3].g = 205; colors[3].b = 000;//yellow
  168. colors[4].r = 000; colors[4].g = 205; colors[4].b = 000;//green
  169. colors[5].r = 205; colors[5].g = 0; colors[5].b = 78;//pink
  170. colors[6].r = 000; colors[6].g = 000; colors[6].b = 205;//blue
  171. colors[7].r = 50; colors[7].g = 000; colors[7].b = 205;//violet
  172.  
  173. LCD_init();
  174. InitADC();
  175. sei();
  176. set_sleep_mode(SLEEP_MODE_IDLE);
  177. sleep_enable();
  178. }
  179.  
  180. int main(void) {
  181. setup();
  182. init_timer1();
  183. changeSource();
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement