Emil 4 роки тому
батько
коміт
2f0e2b9442
6 змінених файлів з 107 додано та 6 видалено
  1. 2 0
      .idea/RPLedFFT.iml
  2. 4 0
      .idea/misc.xml
  3. 8 0
      .idea/modules.xml
  4. 6 0
      .idea/vcs.xml
  5. 1 1
      CMakeLists.txt
  6. 86 5
      adc_fft.c

+ 2 - 0
.idea/RPLedFFT.iml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module classpath="CMake" type="CPP_MODULE" version="4" />

+ 4 - 0
.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/RPLedFFT.iml" filepath="$PROJECT_DIR$/.idea/RPLedFFT.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 1 - 1
CMakeLists.txt

@@ -7,7 +7,7 @@ project(my_project)
 pico_sdk_init()
 
 
-add_executable(adc_fft adc_fft.c)
+add_executable(adc_fft adc_fft.c _kiss_fft_guts.h)
 add_library(kiss_fftr kiss_fftr.c)
 add_library(kiss_fft kiss_fft.c)
 

+ 86 - 5
adc_fft.c

@@ -30,6 +30,12 @@
 #define WS2812_PIN 6
 #endif
 
+typedef struct {
+    double r;       // a fraction between 0 and 1
+    double g;       // a fraction between 0 and 1
+    double b;       // a fraction between 0 and 1
+} rgb;
+
 static inline void put_pixel(uint32_t pixel_grb) {
     pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
 }
@@ -48,7 +54,7 @@ static inline void put_pixel(uint32_t pixel_grb) {
 // BE CAREFUL: anything over about 9000 here will cause things
 // to silently break. The code will compile and upload, but due
 // to memory issues nothing will work properly
-#define NSAMP 4096
+#define NSAMP 2048 //4096
 
 // globals
 dma_channel_config cfg;
@@ -58,10 +64,14 @@ float freqsInLog[NSAMP];
 float power[NSAMP / 2];
 float powerInLog[NUM_PIXELS];
 
+uint64_t lastColorChange;
+
 void setup();
 
 void sample(uint8_t *capture_buf);
 
+rgb hsv2rgb(double h, double s, double v);
+
 int main() {
 
 
@@ -76,7 +86,9 @@ int main() {
     while (1) {
         //printf("Loop\n");
         // get NSAMP samples at FSAMP
+        //uint64_t sampleStart = to_us_since_boot(get_absolute_time());
         sample(cap_buf);
+        //printf("Sample time: %llu \n", to_us_since_boot(get_absolute_time())-sampleStart);
         // fill fourier transform input while subtracting DC component
         uint64_t sum = 0;
         for (int i = 0; i < NSAMP; i++) { sum += cap_buf[i]; }
@@ -96,10 +108,10 @@ int main() {
         float f_max = FSAMP;
         float f_res = f_max / NSAMP;
         for (int i = 0; i < NUM_PIXELS; i++) {
-            float lowFreq = freqsInLog[i];
+            float lowFreq = freqs[i]; //freqsInLog[i];
             float highFreq;
             if (i != NUM_PIXELS - 1) {
-                highFreq = freqsInLog[i + 1];
+                highFreq = freqs[i+1]; //freqsInLog[i + 1];
             } else {
                 highFreq = FSAMP / 2;
             }
@@ -112,23 +124,92 @@ int main() {
             }
             //printf("lowInd = %d, highInd = %d, lowFreq = %f, highFreq = %f, freq[lowInd] = %f, freq[highInd] = %f\n", lowInd, highInd, lowFreq, highFreq, freqs[lowInd], freqs[highInd]);
             float div_power = totalPower / (highInd + 1 - lowInd);
-            powerInLog[i] = 20 * log(fmax(div_power - 1000000, 1));
+            powerInLog[i] = div_power/1000.0; //20 * log(fmax(div_power - 1000000, 1));
         }
         //for (int i = 0; i < NUM_PIXELS - 1; i++) {
         //    printf("Power for freq %f to %f = %f (%f)\n", freqsInLog[i], freqsInLog[i + 1], powerInLog[i], power[i]);
         //}
+        //uint64_t timmmme = to_us_since_boot(get_absolute_time()) - lastColorChange;
+        //printf("%llu \n", timmmme);
+        if (to_us_since_boot(get_absolute_time()) - lastColorChange < 300) {
+            sleep_us(300 - (to_us_since_boot(get_absolute_time()) - lastColorChange));
+        }
+
         for (int i = 0; i < NUM_PIXELS; i++) {
             uint32_t value = (uint32_t)(fmin(255, powerInLog[i]/400.0 * 255));
+            //uint32_t value = (uint32_t)(fmin(255, power[i]/400.0 * 255));
+            //rgb color = hsv2rgb(fmin(1, power[i]/400.0), 1, 0.5);
+            //rgb color = hsv2rgb(fmin(1, 0.1), 1, 0.5);
+            //uint32_t value = color.g * 256 * pow(2, 16) + color.r * 256 * pow(2, 8) + color.b * 256;
             //printf("Color = %d, pixel = %d\n", value, i);
             put_pixel(value);
+            put_pixel(value);
         }
-        sleep_ms(50);
+        lastColorChange = to_us_since_boot(get_absolute_time());
+        //sleep_ms(50);
     }
 
     // should never get here
     kiss_fft_free(cfg);
 }
 
+rgb hsv2rgb(double h, double s, double v) {
+    double      hh, p, q, t, ff;
+    long        i;
+    rgb        out;
+
+    if(s <= 0.0) {       // < is bogus, just shuts up warnings
+        out.r = v;
+        out.g = v;
+        out.b = v;
+        return out;
+    }
+    hh = h;
+    if(hh >= 360.0) hh = 0.0;
+    hh /= 60.0;
+    i = (long)hh;
+    ff = hh - i;
+    p = v * (1.0 - s);
+    q = v * (1.0 - (s * ff));
+    t = v * (1.0 - (s * (1.0 - ff)));
+
+    switch(i) {
+        case 0:
+            out.r = v;
+            out.g = t;
+            out.b = p;
+            break;
+        case 1:
+            out.r = q;
+            out.g = v;
+            out.b = p;
+            break;
+        case 2:
+            out.r = p;
+            out.g = v;
+            out.b = t;
+            break;
+
+        case 3:
+            out.r = p;
+            out.g = q;
+            out.b = v;
+            break;
+        case 4:
+            out.r = t;
+            out.g = p;
+            out.b = v;
+            break;
+        case 5:
+        default:
+            out.r = v;
+            out.g = p;
+            out.b = q;
+            break;
+    }
+    return out;
+}
+
 void sample(uint8_t *capture_buf) {
     adc_fifo_drain();
     adc_run(false);