-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_Oscilloscope.ino
302 lines (249 loc) · 6.52 KB
/
ESP32_Oscilloscope.ino
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <Arduino.h>
#include <driver/i2s.h>
#include <driver/adc.h>
#include <soc/syscon_reg.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include "esp_adc_cal.h"
#include "filters.h"
/*rotary encoder start*/
#include <ESP32RotaryEncoder.h>
#define DO_ENCODER_VCC -1
#define DI_ENCODER_SW 32 // connected to 32 but not handled by rotary encoder library
#define DI_ENCODER_B 15 // DT
#define DI_ENCODER_A 35 // CLK
const uint8_t LONG_PRESS = 500; // push button long press duration
RotaryEncoder rotaryEncoder( DI_ENCODER_A, DI_ENCODER_B, DI_ENCODER_SW, DO_ENCODER_VCC );
/*rotary encoder end*/
//#define DEBUG_SERIAL
//#define DEBUG_BUFF
#define DELAY 1000
// Width and height of sprite
#define WIDTH 240
#define HEIGHT 280
#define ADC_CHANNEL ADC1_CHANNEL_5 // GPIO33
#define NUM_SAMPLES 1000 // number of samples
#define I2S_NUM (0)
#define BUFF_SIZE 50000
#define B_MULT BUFF_SIZE/NUM_SAMPLES
TFT_eSPI tft = TFT_eSPI(); // Declare object "tft"
TFT_eSprite spr = TFT_eSprite(&tft); // Declare Sprite object "spr" with pointer to "tft" object
esp_adc_cal_characteristics_t adc_chars;
TaskHandle_t task_menu;
TaskHandle_t task_adc;
float v_div = 825;
float s_div = 10;
float offset = 0;
float toffset = 0;
uint8_t current_filter = 1;
//options handler
enum Option {
None,
Autoscale,
Vdiv,
Sdiv,
Offset,
TOffset,
Filter,
Stop,
Mode,
Single,
Clear,
Reset,
Probe,
UpdateF,
Cursor1,
Cursor2
};
int8_t volts_index = 0;
int8_t tscale_index = 0;
uint8_t opt = None;
bool menu = false;
bool info = true;
bool set_value = false;
float RATE = 1000; //in ksps --> 1000 = 1Msps
bool auto_scale = false;
bool full_pix = true;
bool stop = false;
bool stop_change = false;
uint16_t i2s_buff[BUFF_SIZE];
bool single_trigger = false;
bool data_trigger = false;
bool updating_screen = false;
bool new_data = false;
bool menu_action = false;
uint8_t digital_wave_option = 0; //0-auto | 1-analog | 2-digital data (SERIAL/SPI/I2C/etc)
int btnok,btnpl,btnmn,btnbk;
void IRAM_ATTR btok()
{
btnok = 1;
}
void IRAM_ATTR btplus()
{
btnpl = 1;
}
void IRAM_ATTR btminus()
{
btnmn = 1;
}
void IRAM_ATTR btback()
{
btnbk = 1;
}
void setup() {
/*rotary encoder start*/
rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );
rotaryEncoder.setBoundaries( -1, 1, false ); // reduces rotary encoder motion from min/max to up/down in endless loop
rotaryEncoder.onTurned( &knobCallback );
rotaryEncoder.onPressed( &buttonCallback );
rotaryEncoder.begin();
/*rotary encoder end*/
Serial.begin(115200);
configure_i2s(1000000);
setup_screen();
characterize_adc();
#ifdef DEBUG_BUF
debug_buffer();
#endif
xTaskCreatePinnedToCore(
core0_task,
"menu_handle",
10000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&task_menu, /* Task handle. */
0); /* Core where the task should run */
xTaskCreatePinnedToCore(
core1_task,
"adc_handle",
10000, /* Stack size in words */
NULL, /* Task input parameter */
3, /* Priority of the task */
&task_adc, /* Task handle. */
1); /* Core where the task should run */
}
void core0_task( void * pvParameters ) {
(void) pvParameters;
for (;;) {
menu_handler();
if (new_data || menu_action) {
new_data = false;
menu_action = false;
updating_screen = true;
update_screen(i2s_buff, RATE);
updating_screen = false;
vTaskDelay(pdMS_TO_TICKS(10));
Serial.println("CORE0");
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void core1_task( void * pvParameters ) {
(void) pvParameters;
for (;;) {
if (!single_trigger) {
while (updating_screen) {
vTaskDelay(pdMS_TO_TICKS(1));
}
if (!stop) {
if (stop_change) {
i2s_adc_enable(I2S_NUM_0);
stop_change = false;
}
ADC_Sampling(i2s_buff);
new_data = true;
}
else {
if (!stop_change) {
i2s_adc_disable(I2S_NUM_0);
i2s_zero_dma_buffer(I2S_NUM_0);
stop_change = true;
}
}
Serial.println("CORE1");
vTaskDelay(pdMS_TO_TICKS(300));
}
else {
float old_mean = 0;
while (single_trigger) {
stop = true;
ADC_Sampling(i2s_buff);
float mean = 0;
float max_v, min_v;
peak_mean(i2s_buff, BUFF_SIZE, &max_v, &min_v, &mean);
//signal captured (pp > 0.4V || changing mean > 0.2V) -> DATA ANALYSIS
if ((old_mean != 0 && fabs(mean - old_mean) > 0.2) || to_voltage(max_v) - to_voltage(min_v) > 0.05) {
float freq = 0;
float period = 0;
uint32_t trigger0 = 0;
uint32_t trigger1 = 0;
//if analog mode OR auto mode and wave recognized as analog
bool digital_data = !false;
if (digital_wave_option == 1) {
trigger_freq_analog(i2s_buff, RATE, mean, max_v, min_v, &freq, &period, &trigger0, &trigger1);
}
else if (digital_wave_option == 0) {
digital_data = digital_analog(i2s_buff, max_v, min_v);
if (!digital_data) {
trigger_freq_analog(i2s_buff, RATE, mean, max_v, min_v, &freq, &period, &trigger0, &trigger1);
}
else {
trigger_freq_digital(i2s_buff, RATE, mean, max_v, min_v, &freq, &period, &trigger0);
}
}
else {
trigger_freq_digital(i2s_buff, RATE, mean, max_v, min_v, &freq, &period, &trigger0);
}
single_trigger = false;
new_data = true;
Serial.println("Single GOT");
//return to normal execution in stop mode
}
vTaskDelay(pdMS_TO_TICKS(1)); //time for the other task to start (low priorit)
}
vTaskDelay(pdMS_TO_TICKS(300));
}
}
}
/*rotary encoder start*/
void knobCallback( int value )
{
if(rotaryEncoder.getEncoderValue() == 1){
//Serial.println("up"); // for debugging
btnpl = 1;
}
else if(rotaryEncoder.getEncoderValue() == -1){
//Serial.println("up"); // for debugging
btnmn = 1;
}
else {
btnpl = 0;
btnmn = 0;
}
//Serial.printf( "Value: %i\n", value ); // for debugging
}
void buttonShortPress()
{
btnbk = 0;
btnok = 1;
// Serial.println( "boop!" ); // for debugging
}
void buttonLongPress()
{
btnok = 0;
btnbk = 1;
// Serial.println( "BOOOOOOOOOOP!" ); // for debugging
}
void buttonCallback( unsigned long duration )
{
if( duration > LONG_PRESS )
{
buttonLongPress();
}
else
{
buttonShortPress();
}
}
/*rotary encoder end*/
void loop() {}