-
Notifications
You must be signed in to change notification settings - Fork 12
/
scope.c
executable file
·116 lines (105 loc) · 2.65 KB
/
scope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "scope.h"
#include <string.h>
static volatile float scope_data[NUM_CHANNELS][MAX_SAMPLES];
static volatile uint16_t num_samples;
static volatile uint16_t samples_since_trigger[NUM_CHANNELS];
static volatile bool triggered;
static volatile uint16_t curr_index[NUM_CHANNELS];
static volatile float avg_sum_triggered[NUM_CHANNELS];
static volatile uint16_t trigger_start_curr_index[NUM_CHANNELS];
static volatile bool logging = false;
void scope_init(void)
{
num_samples = MAX_SAMPLES;
uint8_t i = 0;
for (i = 0; i < NUM_CHANNELS; i++)
{
samples_since_trigger[i] = 0;
curr_index[i] = 0;
avg_sum_triggered[i] = 0;
trigger_start_curr_index[i] = 0;
}
triggered = false;
logging = false;
memset((float*)scope_data, 0, sizeof(float) * NUM_CHANNELS * MAX_SAMPLES);
}
void scope_set_window(uint16_t samples)
{
if (samples > MAX_SAMPLES)
{
num_samples = MAX_SAMPLES;
return;
}
num_samples = samples;
}
void scope_clear(uint8_t channel)
{
memset((float*)scope_data[channel], 0, sizeof(float) * MAX_SAMPLES);
curr_index[channel] = 0;
}
void scope_arm(void)
{
triggered = false;
logging = true;
}
void scope_trigger(void)
{
triggered = true;
uint8_t i = 0;
for (i = 0; i < NUM_CHANNELS; i++)
{
samples_since_trigger[i] = 0;
avg_sum_triggered[i] = 0;
trigger_start_curr_index[i] = curr_index[i];
}
}
void scope_log(uint8_t channel, float data)
{
if (!logging)
{
return;
}
if (channel >= NUM_CHANNELS)
{
return;
}
scope_data[channel][curr_index[channel]] = data;
curr_index[channel] = (curr_index[channel] + 1) % num_samples;
if (triggered)
{
samples_since_trigger[channel]++;
avg_sum_triggered[channel] += data;
if (scope_is_triggered(channel))
{
logging = false;
}
}
}
bool scope_is_triggered(uint8_t channel)
{
return triggered && samples_since_trigger[channel] >= 64;
}
float scope_get_triggered_average(uint8_t channel)
{
if (samples_since_trigger[channel] == 0)
return 0.0;
return avg_sum_triggered[channel] / (float)samples_since_trigger[channel];
}
float scope_get_average(uint8_t channel)
{
uint16_t i = 0;
float sum = 0;
for (i = 0; i < num_samples; i++)
{
sum += scope_data[channel][i];
}
return sum / (float)num_samples;
}
uint32_t scope_get_data(uint8_t channel, uint16_t samples, float *buffer)
{
for (uint16_t i = 0; i < samples; i++)
{
buffer[i] = scope_data[channel][(curr_index[channel] + i + num_samples - samples) % num_samples];
}
return samples;
}