Code formatted

This commit is contained in:
Kai Lauterbach 2024-02-05 19:47:43 +01:00
parent c3b0d8bf26
commit 1a54d4bc59
2 changed files with 251 additions and 200 deletions

View file

@ -1,12 +1,14 @@
void peak_mean(uint16_t *i2s_buffer, uint32_t len, float * max_value, float * min_value, float *pt_mean) { void peak_mean(uint16_t *i2s_buffer, uint32_t len, float *max_value, float *min_value, float *pt_mean)
{
max_value[0] = i2s_buffer[0]; max_value[0] = i2s_buffer[0];
min_value[0] = i2s_buffer[0]; min_value[0] = i2s_buffer[0];
mean_filter filter(5); mean_filter filter(5);
filter.init(i2s_buffer[0]); filter.init(i2s_buffer[0]);
float mean = 0; float mean = 0;
for (uint32_t i = 1; i < len; i++) { for (uint32_t i = 1; i < len; i++)
{
float value = filter.filter((float)i2s_buffer[i]); float value = filter.filter((float)i2s_buffer[i]);
if (value > max_value[0]) if (value > max_value[0])
@ -21,25 +23,30 @@ void peak_mean(uint16_t *i2s_buffer, uint32_t len, float * max_value, float * mi
pt_mean[0] = mean; pt_mean[0] = mean;
} }
// true if digital/ false if analog // true if digital/ false if analog
bool digital_analog(uint16_t *i2s_buffer, uint32_t max_v, uint32_t min_v) { bool digital_analog(uint16_t *i2s_buffer, uint32_t max_v, uint32_t min_v)
{
uint32_t upper_threshold = max_v - 0.05 * (max_v - min_v); uint32_t upper_threshold = max_v - 0.05 * (max_v - min_v);
uint32_t lower_threshold = min_v + 0.05 * (max_v - min_v); uint32_t lower_threshold = min_v + 0.05 * (max_v - min_v);
uint32_t digital_data = 0; uint32_t digital_data = 0;
uint32_t analog_data = 0; uint32_t analog_data = 0;
for (uint32_t i = 0; i < BUFF_SIZE; i++) { for (uint32_t i = 0; i < BUFF_SIZE; i++)
if (i2s_buffer[i] > lower_threshold) { {
if (i2s_buffer[i] > upper_threshold) { if (i2s_buffer[i] > lower_threshold)
{
if (i2s_buffer[i] > upper_threshold)
{
// HIGH DIGITAL // HIGH DIGITAL
digital_data++; digital_data++;
} }
else { else
{
// ANALOG/TRANSITION // ANALOG/TRANSITION
analog_data++; analog_data++;
} }
} }
else { else
{
// LOW DIGITAL // LOW DIGITAL
digital_data++; digital_data++;
} }
@ -60,7 +67,8 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
float *pt_freq, float *pt_freq,
float *pt_period, float *pt_period,
uint32_t *pt_trigger0, uint32_t *pt_trigger0,
uint32_t *pt_trigger1) { uint32_t *pt_trigger1)
{
float freq = 0; float freq = 0;
float period = 0; float period = 0;
bool signal_side = false; bool signal_side = false;
@ -70,20 +78,24 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
uint32_t trigger_index = 0; uint32_t trigger_index = 0;
// get initial signal relative to the mean // get initial signal relative to the mean
if (to_voltage(i2s_buffer[0]) > mean) { if (to_voltage(i2s_buffer[0]) > mean)
{
signal_side = true; signal_side = true;
} }
// waveform repetitions calculation + get triggers time // waveform repetitions calculation + get triggers time
uint32_t wave_center = (max_v + min_v) / 2; uint32_t wave_center = (max_v + min_v) / 2;
for (uint32_t i = 1 ; i < BUFF_SIZE; i++) { for (uint32_t i = 1; i < BUFF_SIZE; i++)
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2) { {
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2)
{
signal_side = false; signal_side = false;
} }
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2) { else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
{
freq++; freq++;
if (trigger_count < trigger_num) { if (trigger_count < trigger_num)
{
trigger_temp[trigger_count] = i; trigger_temp[trigger_count] = i;
trigger_count++; trigger_count++;
} }
@ -92,22 +104,26 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
} }
// frequency calculation // frequency calculation
if (trigger_count < 2) { if (trigger_count < 2)
{
trigger_temp[0] = 0; trigger_temp[0] = 0;
trigger_index = 0; trigger_index = 0;
freq = 0; freq = 0;
period = 0; period = 0;
} }
else { else
{
// simple frequency calculation fair enough for frequencies over 2khz (20hz resolution) // simple frequency calculation fair enough for frequencies over 2khz (20hz resolution)
freq = freq * 1000 / 50; freq = freq * 1000 / 50;
period = (float)(sample_rate * 1000.0) / freq; // us period = (float)(sample_rate * 1000.0) / freq; // us
// from 2000 to 80 hz -> uses mean of the periods for precision // from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80) { if (freq < 2000 && freq > 80)
{
period = 0; period = 0;
for (uint32_t i = 1; i < trigger_count; i++) { for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1]; period += trigger_temp[i] - trigger_temp[i - 1];
} }
period /= (trigger_count - 1); period /= (trigger_count - 1);
@ -115,7 +131,8 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
} }
// under 80hz, single period for frequency calculation // under 80hz, single period for frequency calculation
else if (trigger_count > 1 && freq <= 80) { else if (trigger_count > 1 && freq <= 80)
{
period = trigger_temp[1] - trigger_temp[0]; period = trigger_temp[1] - trigger_temp[0];
freq = sample_rate * 1000 / period; freq = sample_rate * 1000 / period;
} }
@ -128,25 +145,24 @@ void trigger_freq_analog(uint16_t *i2s_buffer,
The resulting trigger gets a negative offset of 5% of the calculated period The resulting trigger gets a negative offset of 5% of the calculated period
*/ */
uint32_t trigger2 = 0; uint32_t trigger2 = 0;
if (trigger_temp[0] - period * 0.05 > 0 && trigger_count > 1) { if (trigger_temp[0] - period * 0.05 > 0 && trigger_count > 1)
{
trigger_index = trigger_temp[0] - period * 0.05; trigger_index = trigger_temp[0] - period * 0.05;
trigger2 = trigger_temp[1] - period * 0.05; trigger2 = trigger_temp[1] - period * 0.05;
} }
else if (trigger_count > 2) { else if (trigger_count > 2)
{
trigger_index = trigger_temp[1] - period * 0.05; trigger_index = trigger_temp[1] - period * 0.05;
if (trigger_count > 2) if (trigger_count > 2)
trigger2 = trigger_temp[2] - period * 0.05; trigger2 = trigger_temp[2] - period * 0.05;
} }
pt_trigger0[0] = trigger_index; pt_trigger0[0] = trigger_index;
pt_trigger1[0] = trigger2; pt_trigger1[0] = trigger2;
pt_freq[0] = freq; pt_freq[0] = freq;
pt_period[0] = period; pt_period[0] = period;
} }
void trigger_freq_digital(uint16_t *i2s_buffer, void trigger_freq_digital(uint16_t *i2s_buffer,
float sample_rate, float sample_rate,
float mean, float mean,
@ -154,7 +170,8 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
uint32_t min_v, uint32_t min_v,
float *pt_freq, float *pt_freq,
float *pt_period, float *pt_period,
uint32_t *pt_trigger0) { uint32_t *pt_trigger0)
{
float freq = 0; float freq = 0;
float period = 0; float period = 0;
@ -165,31 +182,37 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
uint32_t trigger_index = 0; uint32_t trigger_index = 0;
// get initial signal relative to the mean // get initial signal relative to the mean
if (to_voltage(i2s_buffer[0]) > mean) { if (to_voltage(i2s_buffer[0]) > mean)
{
signal_side = true; signal_side = true;
} }
// waveform repetitions calculation + get triggers time // waveform repetitions calculation + get triggers time
uint32_t wave_center = (max_v + min_v) / 2; uint32_t wave_center = (max_v + min_v) / 2;
bool normal_high = (mean > to_voltage(wave_center)) ? true : false; bool normal_high = (mean > to_voltage(wave_center)) ? true : false;
if (max_v - min_v > 4095 * (0.4 / 3.3)) { if (max_v - min_v > 4095 * (0.4 / 3.3))
for (uint32_t i = 1 ; i < BUFF_SIZE; i++) { {
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2) { for (uint32_t i = 1; i < BUFF_SIZE; i++)
{
if (signal_side && i2s_buffer[i] < wave_center - (wave_center - min_v) * 0.2)
{
// signal was high, fell -> trigger if normal high // signal was high, fell -> trigger if normal high
if (trigger_count < trigger_num && normal_high) { if (trigger_count < trigger_num && normal_high)
{
trigger_temp[trigger_count] = i; trigger_temp[trigger_count] = i;
trigger_count++; trigger_count++;
} }
signal_side = false; signal_side = false;
} }
else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2) { else if (!signal_side && i2s_buffer[i] > wave_center + (max_v - wave_center) * 0.2)
{
freq++; freq++;
// signal was low, rose -> trigger if normal low // signal was low, rose -> trigger if normal low
if (trigger_count < trigger_num && !normal_high) { if (trigger_count < trigger_num && !normal_high)
{
trigger_temp[trigger_count] = i; trigger_temp[trigger_count] = i;
trigger_count++; trigger_count++;
} }
@ -201,11 +224,14 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
freq = freq * 1000 / 50; freq = freq * 1000 / 50;
period = (float)(sample_rate * 1000.0) / freq; // us period = (float)(sample_rate * 1000.0) / freq; // us
if (trigger_count > 1) { if (trigger_count > 1)
{
// from 2000 to 80 hz -> uses mean of the periods for precision // from 2000 to 80 hz -> uses mean of the periods for precision
if (freq < 2000 && freq > 80) { if (freq < 2000 && freq > 80)
{
period = 0; period = 0;
for (uint32_t i = 1; i < trigger_count; i++) { for (uint32_t i = 1; i < trigger_count; i++)
{
period += trigger_temp[i] - trigger_temp[i - 1]; period += trigger_temp[i] - trigger_temp[i - 1];
} }
period /= (trigger_count - 1); period /= (trigger_count - 1);
@ -213,7 +239,8 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
} }
// under 80hz, single period for frequency calculation // under 80hz, single period for frequency calculation
else if (trigger_count > 1 && freq <= 80) { else if (trigger_count > 1 && freq <= 80)
{
period = trigger_temp[1] - trigger_temp[0]; period = trigger_temp[1] - trigger_temp[0];
freq = sample_rate * 1000 / period; freq = sample_rate * 1000 / period;
} }
@ -227,11 +254,7 @@ void trigger_freq_digital(uint16_t *i2s_buffer,
trigger_index = 0; trigger_index = 0;
} }
pt_trigger0[0] = trigger_index; pt_trigger0[0] = trigger_index;
pt_freq[0] = freq; pt_freq[0] = freq;
pt_period[0] = period; pt_period[0] = period;
} }

View file

@ -34,26 +34,33 @@ void menu_handler() {
button(); button();
} }
void button() { void button()
{
if (btnok == 1 || btnbk == 1 || btnpl == 1 || btnmn == 1) if (btnok == 1 || btnbk == 1 || btnpl == 1 || btnmn == 1)
{ {
menu_action = true; menu_action = true;
} }
if (menu == true) if (menu == true)
{ {
if (set_value) { if (set_value)
switch (opt) { {
switch (opt)
{
case Vdiv: case Vdiv:
if (btnpl == 1) { if (btnpl == 1)
{
volts_index++; volts_index++;
if (volts_index >= sizeof(voltage_division) / sizeof(*voltage_division)) { if (volts_index >= sizeof(voltage_division) / sizeof(*voltage_division))
{
volts_index = 0; volts_index = 0;
} }
btnpl = 0; btnpl = 0;
} }
else if (btnmn == 1) { else if (btnmn == 1)
{
volts_index--; volts_index--;
if (volts_index < 0) { if (volts_index < 0)
{
volts_index = sizeof(voltage_division) / sizeof(*voltage_division) - 1; volts_index = sizeof(voltage_division) / sizeof(*voltage_division) - 1;
} }
btnmn = 0; btnmn = 0;
@ -63,16 +70,20 @@ void button() {
break; break;
case Sdiv: case Sdiv:
if (btnmn == 1) { if (btnmn == 1)
{
tscale_index++; tscale_index++;
if (tscale_index >= sizeof(time_division) / sizeof(*time_division)) { if (tscale_index >= sizeof(time_division) / sizeof(*time_division))
{
tscale_index = 0; tscale_index = 0;
} }
btnmn = 0; btnmn = 0;
} }
else if (btnpl == 1) { else if (btnpl == 1)
{
tscale_index--; tscale_index--;
if (tscale_index < 0) { if (tscale_index < 0)
{
tscale_index = sizeof(time_division) / sizeof(*time_division) - 1; tscale_index = sizeof(time_division) / sizeof(*time_division) - 1;
} }
btnpl = 0; btnpl = 0;
@ -82,11 +93,13 @@ void button() {
break; break;
case Offset: case Offset:
if (btnmn == 1) { if (btnmn == 1)
{
offset += 0.1 * (v_div * 4) / 3300; offset += 0.1 * (v_div * 4) / 3300;
btnmn = 0; btnmn = 0;
} }
else if (btnpl == 1) { else if (btnpl == 1)
{
offset -= 0.1 * (v_div * 4) / 3300; offset -= 0.1 * (v_div * 4) / 3300;
btnpl = 0; btnpl = 0;
} }
@ -114,7 +127,6 @@ void button() {
default: default:
break; break;
} }
if (btnbk == 1) if (btnbk == 1)
{ {
@ -151,8 +163,10 @@ void button() {
hide_menu(); hide_menu();
btnbk = 0; btnbk = 0;
} }
if (btnok == 1) { if (btnok == 1)
switch (opt) { {
switch (opt)
{
case Autoscale: case Autoscale:
auto_scale = !auto_scale; auto_scale = !auto_scale;
break; break;
@ -211,7 +225,6 @@ void button() {
default: default:
break; break;
} }
btnok = 0; btnok = 0;
@ -238,71 +251,86 @@ void button() {
} }
btnbk = 0; btnbk = 0;
} }
if (btnpl == 1) { if (btnpl == 1)
{
volts_index++; volts_index++;
if (volts_index >= sizeof(voltage_division) / sizeof(*voltage_division)) { if (volts_index >= sizeof(voltage_division) / sizeof(*voltage_division))
{
volts_index = 0; volts_index = 0;
} }
btnpl = 0; btnpl = 0;
v_div = voltage_division[volts_index]; v_div = voltage_division[volts_index];
} }
if (btnmn == 1) { if (btnmn == 1)
{
tscale_index++; tscale_index++;
if (tscale_index >= sizeof(time_division) / sizeof(*time_division)) { if (tscale_index >= sizeof(time_division) / sizeof(*time_division))
{
tscale_index = 0; tscale_index = 0;
} }
btnmn = 0; btnmn = 0;
s_div = time_division[tscale_index]; s_div = time_division[tscale_index];
} }
} }
} }
void hide_menu() { void hide_menu()
{
menu = false; menu = false;
} }
void hide_all() { void hide_all()
{
menu = false; menu = false;
info = false; info = false;
} }
void show_menu() { void show_menu()
{
menu = true; menu = true;
} }
String strings_vdiv() { String strings_vdiv()
{
return ""; return "";
} }
String strings_sdiv() { String strings_sdiv()
{
return ""; return "";
} }
String strings_offset() { String strings_offset()
{
return ""; return "";
} }
String strings_toffset() { String strings_toffset()
{
return ""; return "";
} }
String strings_freq() { String strings_freq()
{
return ""; return "";
} }
String strings_peak() { String strings_peak()
{
return ""; return "";
} }
String strings_vmax() { String strings_vmax()
{
return ""; return "";
} }
String strings_vmin() { String strings_vmin()
{
return ""; return "";
} }
String strings_filter() { String strings_filter()
{
return ""; return "";
} }