Guest User

Teensy Audio FastLED

a guest
Dec 8th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. #define MATRIX
  2. //
  3.  
  4. #ifdef MATRIX
  5. #include <FastLED.h>
  6. #define LED_PIN 3
  7. #define COLOR_ORDER GRB
  8. #define CHIPSET WS2812
  9.  
  10. #define BRIGHTNESS 64
  11. // Params for width and height
  12. const uint8_t kMatrixWidth = 8;
  13. const uint8_t kMatrixHeight = 32;
  14.  
  15. // Param for different pixel layouts
  16. const bool kMatrixSerpentineLayout = true;
  17.  
  18. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  19. CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
  20. CRGB* const leds( leds_plus_safety_pixel + 1);
  21.  
  22.  
  23. uint16_t XY( uint8_t x, uint8_t y)
  24. {
  25. uint16_t i;
  26.  
  27. if( kMatrixSerpentineLayout == false) {
  28. i = (y * kMatrixWidth) + x;
  29. }
  30.  
  31. if( kMatrixSerpentineLayout == true) {
  32. if( y & 0x01) {
  33. // Odd rows run backwards
  34. uint8_t reverseX = (kMatrixWidth - 1) - x;
  35. i = (y * kMatrixWidth) + reverseX;
  36. } else {
  37. // Even rows run forwards
  38. i = (y * kMatrixWidth) + x;
  39. }
  40. }
  41.  
  42. return i;
  43. }
  44. #endif
  45.  
  46. #include <Audio.h>
  47.  
  48. ///////////////////////////////////
  49. // copy the Design Tool code here
  50. ///////////////////////////////////
  51.  
  52. #include <Audio.h>
  53. #include <Wire.h>
  54. #include <SPI.h>
  55. #include <SD.h>
  56. #include <SerialFlash.h>
  57.  
  58. // GUItool: begin automatically generated code
  59. AudioInputI2S i2s2; //xy=210,60
  60. AudioPlayMemory playMem1; //xy=261,210
  61. AudioSynthWaveform waveform1; //xy=284,269
  62. AudioMixer4 mixer1; //xy=450,196
  63. AudioOutputI2S i2s1; //xy=669,145
  64. AudioAnalyzeFFT1024 fft1024_1; //xy=677,201
  65. AudioConnection patchCord1(i2s2, 0, mixer1, 0);
  66. AudioConnection patchCord2(i2s2, 1, mixer1, 1);
  67. AudioConnection patchCord3(playMem1, 0, mixer1, 2);
  68. AudioConnection patchCord4(waveform1, 0, mixer1, 3);
  69. AudioConnection patchCord5(mixer1, 0, i2s1, 0);
  70. AudioConnection patchCord6(mixer1, 0, i2s1, 1);
  71. AudioConnection patchCord7(mixer1, fft1024_1);
  72. AudioControlSGTL5000 sgtl5000_1; //xy=526,301
  73. // GUItool: end automatically generated code
  74.  
  75.  
  76. int matrix_height = 8;
  77. int matrix_width = 32;
  78. const int lowerFFTBins[] = {0,1,2,3,4,5,6,8,10,12,15,18,22,27,32,38,45,53,63,74,87,102,119,138,160,186,216,250,289,334,385,444};
  79. const int upperFFTBins[] = {0,1,2,3,4,5,7,9,11,14,17,21,26,31,37,44,52,62,73,86,101,118,137,159,185,215,249,288,333,384,443,511};
  80.  
  81. // Use these with the Teensy Audio Shield
  82. #define SDCARD_CS_PIN 10
  83. #define SDCARD_MOSI_PIN 7
  84. #define SDCARD_SCK_PIN 14
  85.  
  86. // Use these with the Teensy 3.5 & 3.6 SD card
  87. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  88. //#define SDCARD_MOSI_PIN 11 // not actually used
  89. //#define SDCARD_SCK_PIN 13 // not actually used
  90.  
  91. // Use these for the SD+Wiz820 or other adaptors
  92. //#define SDCARD_CS_PIN 4
  93. //#define SDCARD_MOSI_PIN 11
  94. //#define SDCARD_SCK_PIN 13
  95. const int myInput = AUDIO_INPUT_LINEIN;
  96.  
  97. void setup() {
  98.  
  99. Serial.begin(57600);
  100.  
  101. #ifdef MATRIX
  102. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  103. FastLED.setBrightness( BRIGHTNESS );
  104.  
  105. for (int i=0;i<NUM_LEDS;i++) {
  106. leds[i] = CRGB( 0, 0, 64);
  107. delay(5);
  108. FastLED.show();
  109. }
  110.  
  111. for (int i=0;i<NUM_LEDS;i++) {
  112. leds[i] = CRGB( 0, 0, 0);
  113. }
  114. delay(100);
  115. FastLED.show();
  116.  
  117.  
  118. #endif
  119.  
  120. AudioMemory(12);
  121. sgtl5000_1.enable();
  122. sgtl5000_1.volume(0.5);
  123. // SPI.setMOSI(SDCARD_MOSI_PIN);
  124. // SPI.setSCK(SDCARD_SCK_PIN);
  125. // if (!(SD.begin(SDCARD_CS_PIN))) {
  126. // while (1) {
  127. // Serial.println("Unable to access the SD card");
  128. // delay(500);
  129. // }
  130. // }
  131.  
  132. // Enable audio in
  133. sgtl5000_1.enable();
  134. sgtl5000_1.inputSelect(myInput);
  135. sgtl5000_1.volume(1);
  136.  
  137. mixer1.gain(0, 1.0);
  138. mixer1.gain(1, 1.0);
  139. mixer1.gain(2, 0.0);
  140. mixer1.gain(3, 0.0);
  141. // Uncomment one these to try other window functions
  142. // fft1024_1.windowFunction(NULL);
  143. // fft1024_1.windowFunction(AudioWindowBartlett1024);
  144. // fft1024_1.windowFunction(AudioWindowFlattop1024);
  145. fft1024_1.windowFunction(AudioWindowHanning1024);
  146. delay(2000);
  147. //playSdWav1.play("SDTEST2.WAV");
  148.  
  149. }
  150.  
  151. void loop() {
  152.  
  153. if (fft1024_1.available()) {
  154.  
  155. for(int x=0; x< matrix_width ; x++){
  156. spectrum(x, fft1024_1.read(lowerFFTBins[x],upperFFTBins[x]));
  157.  
  158. }
  159.  
  160. #ifdef MATRIX
  161. FastLED.show();
  162. #endif
  163. }
  164.  
  165. #ifdef MATRIX
  166. for (int i=0;i<NUM_LEDS;i++) {
  167. leds[i] = CHSV( 0, 0, 0);
  168. }
  169. delay(10);
  170. FastLED.show();
  171. #endif
  172.  
  173. } // end of loop()
  174.  
  175.  
  176. void spectrum(int x, float n) {
  177.  
  178. // x = band
  179. // y = led height
  180.  
  181. int y = LEDnumber(n);
  182.  
  183. #ifdef MATRIX
  184. // use CHSV rainbow colours
  185.  
  186. switch (y) {
  187.  
  188. case 0:
  189. leds[XY(y,x)] = CHSV( 224, 255, 255);
  190. break;
  191. case 1:
  192. leds[XY(y,x)] = CHSV( 192, 255, 255);
  193. break;
  194. case 2:
  195. leds[XY(y,x)] = CHSV( 160, 255, 255);
  196. break;
  197. case 3:
  198. leds[XY(y,x)] = CHSV( 128, 255, 255);
  199. break;
  200. case 4:
  201. leds[XY(y,x)] = CHSV( 96, 255, 255);
  202. break;
  203. case 5:
  204. leds[XY(y,x)] = CHSV( 64, 255, 255);
  205. break;
  206. case 6:
  207. leds[XY(y,x)] = CHSV( 32, 255, 255);
  208. break;
  209. case 7:
  210. leds[XY(y,x)] = CHSV( 0, 255, 128);
  211. break;
  212. default:
  213. break;
  214.  
  215. }
  216. #endif
  217.  
  218. Serial.print(x);
  219. Serial.print(",");
  220. Serial.print(y);
  221. Serial.println();
  222. }
  223.  
  224. int LEDnumber(float n) {
  225. int led = 0;
  226. if (n > 0.250) {
  227. led = 0;
  228. } else if ( n > 0.220) {
  229. led = 0;
  230. } else if ( n > 0.180) {
  231. led = 1;
  232. } else if ( n > 0.100) {
  233. led = 2;
  234. } else if ( n > 0.060) {
  235. led = 3;
  236. } else if ( n > 0.030) {
  237. led = 4;
  238. } else if ( n > 0.010) {
  239. led = 5;
  240. } else if ( n > 0.008) {
  241. led = 6;
  242. } else if ( n < 0.009) {
  243. led = 7;
  244. }
  245.  
  246. return led;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment