| 1 | // This demo is based on the vumeter demo in the Adafruit Circuit Playground library. |
|---|
| 2 | #ifndef VUMETERDEMO_H |
|---|
| 3 | #define VUMETERDEMO_H |
|---|
| 4 | |
|---|
| 5 | #include <math.h> |
|---|
| 6 | #include "Demo.h" |
|---|
| 7 | |
|---|
| 8 | #define SAMPLE_WINDOW 10 // Sample window for average level |
|---|
| 9 | #define PEAK_HANG 24 // Time of pause before peak dot falls |
|---|
| 10 | #define PEAK_FALL 4 // Rate of falling peak dot |
|---|
| 11 | #define INPUT_FLOOR 56 // Lower range of mic sensitivity in dB SPL |
|---|
| 12 | #define INPUT_CEILING 110 // Upper range of mic sensitivity in db SPL |
|---|
| 13 | |
|---|
| 14 | static byte peak = 16; // Peak level of column; used for falling dots |
|---|
| 15 | static unsigned int sample; |
|---|
| 16 | static byte dotCount = 0; //Frame counter for peak dot |
|---|
| 17 | static byte dotHangCount = 0; //Frame counter for holding peak dot |
|---|
| 18 | |
|---|
| 19 | static float mapf(float x, float in_min, float in_max, float out_min, float out_max); |
|---|
| 20 | static void drawLine(uint8_t from, uint8_t to, uint32_t c); |
|---|
| 21 | |
|---|
| 22 | class VUMeterDemo: public Demo { |
|---|
| 23 | public: |
|---|
| 24 | VUMeterDemo() { currentCeiling = 0; } |
|---|
| 25 | ~VUMeterDemo() {} |
|---|
| 26 | |
|---|
| 27 | virtual void loop() { |
|---|
| 28 | int numPixels = CircuitPlayground.strip.numPixels(); |
|---|
| 29 | float peakToPeak = 0; // peak-to-peak level |
|---|
| 30 | unsigned int c, y; |
|---|
| 31 | |
|---|
| 32 | //get peak sound pressure level over the sample window |
|---|
| 33 | peakToPeak = CircuitPlayground.mic.soundPressureLevel(SAMPLE_WINDOW); |
|---|
| 34 | |
|---|
| 35 | //limit to the floor value |
|---|
| 36 | peakToPeak = max(INPUT_FLOOR, peakToPeak); |
|---|
| 37 | |
|---|
| 38 | // Serial.println(peakToPeak); |
|---|
| 39 | |
|---|
| 40 | //Fill the strip with rainbow gradient |
|---|
| 41 | for (int i=0;i<=numPixels-1;i++){ |
|---|
| 42 | CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(map(i,0,numPixels-1,30,150))); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | c = mapf(peakToPeak, INPUT_FLOOR, INPUT_CEILING, numPixels, 0); |
|---|
| 46 | |
|---|
| 47 | // Turn off pixels that are below volume threshold. |
|---|
| 48 | if(c < peak) { |
|---|
| 49 | peak = c; // Keep dot on top |
|---|
| 50 | dotHangCount = 0; // make the dot hang before falling |
|---|
| 51 | } |
|---|
| 52 | if (c <= numPixels) { // Fill partial column with off pixels |
|---|
| 53 | drawLine(numPixels, numPixels-c, CircuitPlayground.strip.Color(0, 0, 0)); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // Set the peak dot to match the rainbow gradient |
|---|
| 57 | y = numPixels - peak; |
|---|
| 58 | CircuitPlayground.strip.setPixelColor(y-1,CircuitPlayground.colorWheel(map(y,0,numPixels-1,30,150))); |
|---|
| 59 | CircuitPlayground.strip.show(); |
|---|
| 60 | |
|---|
| 61 | // Frame based peak dot animation |
|---|
| 62 | if(dotHangCount > PEAK_HANG) { //Peak pause length |
|---|
| 63 | if(++dotCount >= PEAK_FALL) { //Fall rate |
|---|
| 64 | peak++; |
|---|
| 65 | dotCount = 0; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | else { |
|---|
| 69 | dotHangCount++; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | virtual void modePress() { |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | private: |
|---|
| 77 | int currentCeiling; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | static float mapf(float x, float in_min, float in_max, float out_min, float out_max) |
|---|
| 83 | { |
|---|
| 84 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | //Used to draw a line between two points of a given color |
|---|
| 88 | static void drawLine(uint8_t from, uint8_t to, uint32_t c) { |
|---|
| 89 | uint8_t fromTemp; |
|---|
| 90 | if (from > to) { |
|---|
| 91 | fromTemp = from; |
|---|
| 92 | from = to; |
|---|
| 93 | to = fromTemp; |
|---|
| 94 | } |
|---|
| 95 | for(int i=from; i<=to; i++){ |
|---|
| 96 | CircuitPlayground.strip.setPixelColor(i, c); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | #endif |
|---|