Pārlūkot izejas kodu

Multicore and full rgb

Emil 4 gadi atpakaļ
vecāks
revīzija
53ebf813de
2 mainītis faili ar 46 papildinājumiem un 14 dzēšanām
  1. 1 0
      CMakeLists.txt
  2. 45 14
      adc_fft.c

+ 1 - 0
CMakeLists.txt

@@ -27,4 +27,5 @@ target_link_libraries(adc_fft
 	hardware_dma
 	kiss_fftr
 		hardware_pio
+		pico_multicore
 	)

+ 45 - 14
adc_fft.c

@@ -1,3 +1,4 @@
+#include <sys/cdefs.h>
 // Sample from the ADC continuously at a particular sample rate
 // and then compute an FFT over the data
 //
@@ -10,6 +11,7 @@
 #include "pico/stdlib.h"
 #include "hardware/adc.h"
 #include "hardware/dma.h"
+#include "pico/multicore.h"
 #include "kiss_fftr.h"
 
 #include <stdio.h>
@@ -64,36 +66,44 @@ float freqsInLog[NSAMP];
 float power[NSAMP / 2];
 float powerInLog[NUM_PIXELS];
 
+uint8_t cap_buf[NSAMP];
+uint8_t cap_res[NSAMP];
+
 uint64_t lastColorChange;
 
 void setup();
 
 void sample(uint8_t *capture_buf);
 
+void startSample();
+
 rgb hsv2rgb(double h, double s, double v);
 
 int main() {
 
 
-    uint8_t cap_buf[NSAMP];
     kiss_fft_scalar fft_in[NSAMP]; // kiss_fft_scalar is a float
     kiss_fft_cpx fft_out[NSAMP];
     kiss_fftr_cfg cfg = kiss_fftr_alloc(NSAMP, false, 0, 0);
 
+
     // setup ports and outputs
     setup();
+
+    multicore_reset_core1();
+    multicore_launch_core1(startSample);
     printf("Started\n");
     while (1) {
         //printf("Loop\n");
         // get NSAMP samples at FSAMP
         //uint64_t sampleStart = to_us_since_boot(get_absolute_time());
-        sample(cap_buf);
+        //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]; }
+        for (int i = 0; i < NSAMP; i++) { sum += cap_res[i]; }
         float avg = (float) sum / NSAMP;
-        for (int i = 0; i < NSAMP; i++) { fft_in[i] = (float) cap_buf[i] - avg; }
+        for (int i = 0; i < NSAMP; i++) { fft_in[i] = (float) cap_res[i] - avg; }
 
         // compute fast fourier transform
         kiss_fftr(cfg, fft_in, fft_out);
@@ -108,10 +118,10 @@ int main() {
         float f_max = FSAMP;
         float f_res = f_max / NSAMP;
         for (int i = 0; i < NUM_PIXELS; i++) {
-            float lowFreq = freqs[i]; //freqsInLog[i];
+            float lowFreq = freqsInLog[i];//freqs[i]; //freqsInLog[i];
             float highFreq;
             if (i != NUM_PIXELS - 1) {
-                highFreq = freqs[i+1]; //freqsInLog[i + 1];
+                highFreq = freqsInLog[i + 1];//freqs[i+1]; //freqsInLog[i + 1];
             } else {
                 highFreq = FSAMP / 2;
             }
@@ -124,7 +134,7 @@ 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] = div_power/1000.0; //20 * log(fmax(div_power - 1000000, 1));
+            powerInLog[i] = 20 * log(fmax(div_power/100000, 1));//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]);
@@ -136,12 +146,12 @@ int main() {
         }
 
         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);
+            //uint32_t value = (uint32_t)(fmin(255, powerInLog[i]/400.0 * 255));
+            rgb color = hsv2rgb(fmin(359, powerInLog[i]/200.0 * 359), 1, fmin(0.5, powerInLog[i]/100.0));
+            //printf("power = %f, color = %f, %f, %f\n", powerInLog[i]/200.0, color.r, color.g, color.b);
+            uint32_t value = ((uint32_t)(fmin(color.r, 1) * 255) << 8) | ((uint32_t)(fmin(color.g, 1) * 255) << 16) | (uint32_t)(fmin(color.b, 1) * 255);
+            //printf("b = %f\n", color.b*255);
+            //printf("Color = %lu, pixel = %d\n", value, i);
             put_pixel(value);
             put_pixel(value);
         }
@@ -227,6 +237,27 @@ void sample(uint8_t *capture_buf) {
     gpio_put(LED_PIN, 0);
 }
 
+void startSample() {
+    while(1) {
+        adc_fifo_drain();
+        adc_run(false);
+
+        dma_channel_configure(dma_chan, &cfg,
+                              cap_buf,    // dst
+                              &adc_hw->fifo,  // src
+                              NSAMP,          // transfer count
+                              true            // start immediately
+        );
+
+        gpio_put(LED_PIN, 1);
+        adc_run(true);
+        dma_channel_wait_for_finish_blocking(dma_chan);
+        gpio_put(LED_PIN, 0);
+
+        memcpy(cap_res, cap_buf, sizeof cap_res);
+    }
+}
+
 void setup() {
     stdio_init_all();
 
@@ -281,4 +312,4 @@ void setup() {
         freqsInLog[i] = exp(i / (float) NUM_PIXELS * (max_power - min_power) + min_power);
         printf("freqsInLog[%d] = %f.1\n", i, freqsInLog[i]);
     }
-}
+}