Compare commits

...

2 commits

Author SHA1 Message Date
Killorin
bd79be6eac toggle fft 2025-04-17 12:59:20 -04:00
Killorin
d3abffa13d fix fft
cast in high priority task was causing data issues + buffer overflow causing crazy issues
2025-04-17 12:53:51 -04:00

20
main.c
View file

@ -59,12 +59,14 @@ static kiss_fft_cpx in[FFT_BUF_LEN], out[FFT_BUF_LEN];
typedef struct
{
bool fft;
uint8_t voltage_scale;
uint8_t time_scale;
uint8_t trigger_mode;
} Options;
Options options = {
.fft = false, // oscilloscope mode
.voltage_scale = 4, // 2v
.time_scale = 5, // 20us
.trigger_mode = 1, // rising
@ -185,12 +187,11 @@ void capture_waveform(UArg arg1, UArg arg2)
int32_t adc_current_index;
int32_t i;
if (true) {
if (options.fft) {
adc_current_index = gADCBufferIndex;
int32_t j = 0;
for(i=adc_current_index-FFT_BUF_LEN; i<adc_current_index; i++) {
//in[i].r = sinf(20*PI*i/FFT_BUF_LEN);
in[i].r = (float) gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + i)];
in[i].i = 0;
adc_buffer_fft_sample[j++] = gADCBuffer[ADC_BUFFER_WRAP(adc_current_index + i)];
}
} else {
// single read of options is atomic
@ -229,9 +230,11 @@ void process_waveform(UArg arg1, UArg arg2) {
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CONSTRAIN(x) MAX(MIN(HEIGHT - 1, x), 0)
if(true) { // fft
if(options.fft) { // fft
for(i=0; i<FFT_BUF_LEN; i++) {
in[i].r *= w[i];
//in[i].r = sinf(20*PI*i/FFT_BUF_LEN);
in[i].r = (float) adc_buffer_fft_sample[i] * w[i];
in[i].i = 0;
}
kiss_fft(cfg, in, out);
@ -240,7 +243,7 @@ void process_waveform(UArg arg1, UArg arg2) {
#define DB(out) 10 * log10f(out.r * out.r + out.i * out.i)
//adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int)roundf(0.5 * DB(out[i])));
adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int) (DB(out[i])) + 64);
adc_buffer_processed[i] = CONSTRAIN(HEIGHT- (int) (DB(out[i])) );
}
@ -271,6 +274,9 @@ void handle_user_input() {
case S1: // toggle edge
local_options.trigger_mode = (local_options.trigger_mode + 1) % 3;
break;
case S2: // toggle fft
local_options.fft ^= 1;
break;
case Up: // next scale
local_options.voltage_scale = (local_options.voltage_scale + 1) % VOLTAGE_SCALES;
break;