WaterfallSpectrum.ino 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Waterfall Audio Spectrum Analyzer, adapted from Nathaniel Quillin's
  2. award winning (most over the top) Hackaday SuperCon 2015 Badge Hack.
  3. https://hackaday.io/project/8575-audio-spectrum-analyzer-a-supercon-badge
  4. https://github.com/nqbit/superconbadge
  5. ILI9341 Color TFT Display is used to display spectral data.
  6. Two pots on analog A2 and A3 are required to adjust sensitivity.
  7. Copyright (c) 2015 Nathaniel Quillin
  8. Permission is hereby granted, free of charge, to any person obtaining
  9. a copy of this software and associated documentation files
  10. (the "Software"), to deal in the Software without restriction,
  11. including without limitation the rights to use, copy, modify, merge,
  12. publish, distribute, sublicense, and/or sell copies of the Software,
  13. and to permit persons to whom the Software is furnished to do so,
  14. subject to the following conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <Audio.h>
  26. #include <ILI9341_t3.h>
  27. #include <SD.h>
  28. #include <SerialFlash.h>
  29. #include <SPI.h>
  30. #include <Wire.h>
  31. // ILI9341 Color TFT Display connections
  32. #define TFT_DC 20
  33. #define TFT_CS 21
  34. #define TFT_RST 255 // 255 = unused, connect to 3.3V
  35. #define TFT_MOSI 7
  36. #define TFT_SCLK 14
  37. #define TFT_MISO 12
  38. ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
  39. AudioInputI2S i2s1;
  40. AudioOutputI2S i2s2;
  41. AudioAnalyzeFFT1024 fft1024_1;
  42. AudioConnection patchCord5(i2s1, 0, i2s2, 0);
  43. AudioConnection patchCord6(i2s1, 0, i2s2, 1);
  44. AudioConnection patchCord7(i2s1, fft1024_1);
  45. AudioControlSGTL5000 sgtl5000_1;
  46. static int count = 0;
  47. static uint16_t line_buffer[320];
  48. static float scale = 10.0;
  49. static int knob = 0;
  50. static int vol = 0;
  51. void setup(void) {
  52. // Initialize the peripherals.
  53. tft.begin();
  54. tft.fillScreen(ILI9341_BLACK);
  55. tft.setTextColor(ILI9341_YELLOW);
  56. tft.setTextSize(2);
  57. AudioMemory(20);
  58. sgtl5000_1.enable();
  59. sgtl5000_1.volume(0.5);
  60. sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  61. sgtl5000_1.micGain(36);
  62. // Uncomment one these to try other window functions
  63. // fft1024_1.windowFunction(NULL);
  64. // fft1024_1.windowFunction(AudioWindowBartlett1024);
  65. // fft1024_1.windowFunction(AudioWindowFlattop1024);
  66. tft.println("Waterfall Spectrum");
  67. tft.println("adapted from");
  68. tft.println("Nathaniel Quillin");
  69. tft.println("SuperCon Badge Hack");
  70. for (int i = 0; i < 100; i++) {
  71. tft.setScroll(count++);
  72. count = count % 320;
  73. delay(12);
  74. }
  75. tft.setRotation(0);
  76. }
  77. void loop() {
  78. knob = analogRead(A2);
  79. vol = analogRead(A3);
  80. scale = 1024 - knob;
  81. sgtl5000_1.micGain((1024 - vol) / 4);
  82. if (fft1024_1.available()) {
  83. for (int i = 0; i < 240; i++) {
  84. line_buffer[240 - i - 1] = colorMap(fft1024_1.output[i]);
  85. }
  86. tft.writeRect(0, count, 240, 1, (uint16_t*) &line_buffer);
  87. tft.setScroll(count++);
  88. count = count % 320;
  89. }
  90. }
  91. uint16_t colorMap(uint16_t val) {
  92. float red;
  93. float green;
  94. float blue;
  95. float temp = val / 65536.0 * scale;
  96. if (temp < 0.5) {
  97. red = 0.0;
  98. green = temp * 2;
  99. blue = 2 * (0.5 - temp);
  100. } else {
  101. red = temp;
  102. green = (1.0 - temp);
  103. blue = 0.0;
  104. }
  105. return tft.color565(red * 256, green * 256, blue * 256);
  106. }