-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proto_memo_save_text.ino
332 lines (265 loc) · 9.34 KB
/
Proto_memo_save_text.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <CircularBuffer.hpp>
#include <Wire.h>
#define TFT_DC 10
#define TFT_CS 8
#define TFT_RST 9
#define CONVST_PIN 3
#define ADC_ADDRESS 0x20
#define CONFIG_REG 0x02
#define CONV_RESULT_REG 0x00
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const uint8_t trigPin = A1;
const uint8_t timePin = A2;
const uint8_t AScalePin = A3;
// const uint8_t screenWidth = 320;
const uint8_t screenHeight = 240;
const uint16_t numSamples = 315;
bool checkForTrigger = true;
uint16_t sinceDisplayUpdated = 0;
CircularBuffer<uint8_t, numSamples> sampleBuffer;
uint8_t prevSampleBuffer[numSamples+1];
// char dispMode = 'l';
float minValue = 5.0; // Using integer millivolts to save dynamic memory
float maxValue = 0;
float peakToPeak = 0;
float frequency = 0;
float dutyCycle = 0;
float t0, t1;
// char triggerMode = 'a';
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.setClock(400000);
pinMode(CONVST_PIN, OUTPUT);
digitalWrite(CONVST_PIN, LOW);
int status = Configure();
if (status == 0) {
Serial.println(F("Configuration successful, ACK received."));
} else {
Serial.println(F("Error during configuration, no ACK received."));
return;
}
tft.begin();
tft.setRotation(5);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 10);
tft.print(F("Mini DSO | Oscillonauts"));
tft.fillRect(0, 210, 320, 30, ILI9341_YELLOW);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(1);
tft.setCursor(10, 215);
tft.print(F("Min: "));
tft.setCursor(10, 230);
tft.print(F("Max: "));
tft.setCursor(120, 215);
tft.print("P-P: ");
tft.setCursor(120, 230);
tft.print(F("T: "));
tft.setCursor(230, 215);
tft.print(F("Freq: "));
tft.setCursor(230, 230);
tft.print(F("Duty: "));
for (uint16_t i = 0; i < numSamples; i++) {
sampleBuffer.unshift(0);
prevSampleBuffer[i] = 0;
}
pinMode(trigPin, INPUT);
}
void loop() {
t0 = micros();
uint8_t newSampleY = readADCresult(30, screenHeight - 30);
sampleBuffer.unshift(newSampleY);
sinceDisplayUpdated++;
// uint8_t triggerLevel = 120;
uint8_t triggerLevel = read(trigPin, 30, 210);
if ( checkForTrigger == true && sinceDisplayUpdated >= numSamples && newSampleY + 5 > triggerLevel && newSampleY - 5 < triggerLevel && sampleBuffer[1] < newSampleY ) {
float time_period = calculateSignalProperties(t1);
uint8_t timeScale = read(timePin, 1, 5);
float VoltageScale = float(read(AScalePin, 5, 40))/10.0;
// updateScreen(dispMode, 3, 1);
// updateScreen(dispMode, timeScale, VoltageScale, triggerLevel); // Time , Voltage, triggerLevel
updateScreen(timeScale, VoltageScale, triggerLevel); // Time , Voltage, triggerLevel
updateInfoBox(time_period);
checkForTrigger = false;
} else if (newSampleY < triggerLevel) {
checkForTrigger = true;
}
// else if(sinceDisplayUpdated > 8192){
// tft.drawLine(0, triggerLevel, 5, triggerLevel, ILI9341_WHITE);
// tft.drawLine(0, triggerLevel+1 , 5, triggerLevel+1, ILI9341_WHITE);
// tft.drawLine(0, triggerLevel+2, 5, triggerLevel+2, ILI9341_WHITE);
// }
t1 = micros() - t0;
}
float calculateSignalProperties(float t1) {
float adcmax = 1023.0;
float scale = 5.0/adcmax;
minValue = 5.0;
maxValue = 0.0;
float timePeriod;
int aboveThreshold = 0;
int totalSamples = sampleBuffer.size();
for (int i = 0; i < totalSamples; i++) {
float sample = (scale) * map(sampleBuffer[i], 30, screenHeight - 30, 0, 1023);
if (sample < minValue) minValue = sample;
if (sample > maxValue) maxValue = sample;
if (sample > 2.5) aboveThreshold++;
}
peakToPeak = (maxValue - minValue);
dutyCycle = ((float)aboveThreshold / totalSamples) * 100.0;
int crossingCount = 0;
int firstCrossing = -1;
int lastCrossing = -1;
for (int i = 1; i < totalSamples; i++) {
float sampBuferUnMapped_i = scale * map(sampleBuffer[i], 30, screenHeight - 30, 0, 1023);
float sampBuferUnMapped_i_minus_1 = scale * map(sampleBuffer[i-1], 30, screenHeight - 30, 0, 1023);
if ((sampBuferUnMapped_i_minus_1 < 2.5 && sampBuferUnMapped_i >= 2.5) || (sampBuferUnMapped_i_minus_1 > 2.5 && sampBuferUnMapped_i <= 2.5)) {
crossingCount++;
if (crossingCount == 1) firstCrossing = i;
if (crossingCount == 2) {
lastCrossing = i;
break;
}
}
}
if (lastCrossing != -1) {
timePeriod = (float)((lastCrossing - firstCrossing) * 2*t1);
frequency = 1000.0 / timePeriod;
} else {
timePeriod = 0;
frequency = 0;
}
return timePeriod;
}
// Function to update the yellow info box with calculated values
void updateInfoBox(float timePeriod) {
tft.fillRect(45, 215, 65, 30, ILI9341_YELLOW); // Clear the previous values
tft.fillRect(155, 215, 65, 30, ILI9341_YELLOW); // Clear P-P, T
tft.fillRect(260, 215, 60, 30, ILI9341_YELLOW); // Clear Freq, Duty
tft.fillRect(45, 250, 65, 30, ILI9341_YELLOW); // Clear TimeScale
tft.fillRect(155, 250, 65, 30, ILI9341_YELLOW); // Clear VoltageScale
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(1);
// Update Min, Max, P-P, T, Freq, Duty Cycle values
tft.setCursor(45, 215);
tft.print(minValue);
// tft.print(" V");
tft.setCursor(45, 230);
tft.print(maxValue);
// tft.print(" V");
tft.setCursor(155, 215);
tft.print(peakToPeak);
// tft.print(" V");
tft.setCursor(155, 230);
if (timePeriod > 0) {
tft.print(timePeriod, 2);
// tft.print(" s");
} else {
tft.print("---");
}
tft.setCursor(260, 215);
if (frequency > 0) {
tft.print(frequency, 2);
// tft.print(" Hz");
} else {
tft.print("---");
}
tft.setCursor(260, 230);
tft.print(dutyCycle, 1);
// tft.print("%");
// Add TimeScale and VoltageScale to the display
tft.setCursor(45, 250);
tft.print(tS);
// tft.print(" us/div");
tft.setCursor(155, 250);
tft.print(vS, 1);
// tft.print(" V/div");
}
int read(int Pin, int HRange, int LRange){
int orig = analogRead(Pin);
return map(orig, 0, 1023, LRange, HRange);
}
uint8_t readADCresult(uint16_t LRange, uint16_t HRange){
triggerConversion();
uint16_t data = 0;
Wire.beginTransmission(ADC_ADDRESS);
Wire.write(CONV_RESULT_REG);
uint8_t error = Wire.endTransmission();
Wire.requestFrom(ADC_ADDRESS, 2);
if (Wire.available() == 2) {
uint8_t msb = Wire.read();
uint8_t lsb = Wire.read();
data = (msb << 8) | lsb;
uint16_t adcValue = data & 0x0FFF;
data = adcValue;
} else {
Serial.println(F("Error: ADC result not available."));
data = 1000;
}
return map(data, 0, 4095, LRange, HRange);
}
uint8_t Configure(){
Wire.beginTransmission(ADC_ADDRESS);
Wire.write(CONFIG_REG);
Wire.write(0x18);
uint8_t error = Wire.endTransmission();
return error;
}
void triggerConversion() {
digitalWrite(CONVST_PIN, HIGH);
delayMicroseconds(0);
digitalWrite(CONVST_PIN, LOW);
delayMicroseconds(0);
}
// void updateScreen(char mode, uint8_t offSet, float currScale, uint8_t triggerLevel) {
void updateScreen(uint8_t offSet, float currScale, uint8_t triggerLevel) {
static uint8_t prevOffSet = offSet;
static uint8_t prevTriggerLevel = triggerLevel;
static float prevScale = currScale;
tft.drawLine(0, 30, 320, 30, ILI9341_BLACK);
tft.drawLine(0, 210, 320, 210, ILI9341_YELLOW);
// if(mode == 'd') {
// for (uint16_t x = 0; x < numSamples; x++) {
// tft.drawPixel(x, prevSampleBuffer[x], ILI9341_BLACK);
// tft.drawPixel(x, sampleBuffer[x], ILI9341_GREEN);
// prevSampleBuffer[x] = sampleBuffer[x];
// }
// } else
{
for (uint16_t x = 0; x < numSamples - 1; x++){
if (x * prevOffSet < numSamples){
int16_t prevScaledY1 = (prevSampleBuffer[x] - 120) * prevScale + 120;
prevScaledY1 = prevScaledY1 > 210 ? 210 : prevScaledY1;
prevScaledY1 = prevScaledY1 < 0 ? 0 : prevScaledY1;
int16_t prevScaledY2 = (prevSampleBuffer[x + 1] - 120) * prevScale + 120;
prevScaledY2 = prevScaledY2 > 210 ? 210 : prevScaledY2;
prevScaledY2 = prevScaledY2 < 0 ? 0 : prevScaledY2;
tft.drawLine(x * prevOffSet, prevScaledY1, (x + 1) * prevOffSet, prevScaledY2, ILI9341_BLACK);
}
if (x * offSet < numSamples){
int16_t currScaledY1 = (sampleBuffer[x] - 120) * currScale + 120;
currScaledY1 = currScaledY1 > 210 ? 210 : currScaledY1;
currScaledY1 = currScaledY1 < 30 ? 30 : currScaledY1;
int16_t currScaledY2 = (sampleBuffer[x + 1] - 120) * currScale + 120;
currScaledY2 = currScaledY2 > 210 ? 210 : currScaledY2;
currScaledY2 = currScaledY2 < 30 ? 30 : currScaledY2;
tft.drawLine(x * offSet, currScaledY1, (x + 1) * offSet, currScaledY2, ILI9341_GREEN);
}
prevSampleBuffer[x] = sampleBuffer[x];
}
}
sinceDisplayUpdated = 0;
prevOffSet = offSet;
prevScale = currScale;
tft.drawLine(0, (prevTriggerLevel-1)*prevScale, 3, (prevTriggerLevel-1)*prevScale, ILI9341_YELLOW);
tft.drawLine(0, prevTriggerLevel*prevScale, 3, (prevTriggerLevel)*prevScale, ILI9341_YELLOW);
tft.drawLine(0, (prevTriggerLevel+1)*prevScale , 3, (prevTriggerLevel+1)*prevScale, ILI9341_YELLOW);
tft.drawLine(0, (triggerLevel-1)*currScale, 3, (triggerLevel-1)*currScale, ILI9341_RED);
tft.drawLine(0, (triggerLevel)*currScale, 3, (triggerLevel)*currScale, ILI9341_RED);
tft.drawLine(0, (triggerLevel+1)*currScale , 3, (triggerLevel+1)*currScale, ILI9341_RED);
return;
}