-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
514 lines (436 loc) · 20 KB
/
Form1.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Playback
{
public partial class Form1 : Form
{
private bool m_bInited = false;
private bool m_bPaused = false;
private int m_nStep = -1;
private string m_logFile = "";
private System.IO.StreamReader m_srLog = null;
private int m_nLastTimeStamp = -1;
private int[] m_red_buffer = new int[Algorithm30102.BUFFER_SIZE];
private int[] m_time_buffer = new int[Algorithm30102.BUFFER_SIZE];
private int[] m_ir_buffer = new int[Algorithm30102.BUFFER_SIZE];
private const int MAX_TEMP_SIZE = 10 * Algorithm30102.BUFFER_SIZE;
private int[] m_temp_red_buffer = new int[MAX_TEMP_SIZE];
private int[] m_temp_ir_buffer = new int[MAX_TEMP_SIZE];
private int[] m_temp_time_buffer = new int[MAX_TEMP_SIZE];
private int m_nBufferSize = 0;
private Algorithm30102 m_alg = new Algorithm30102();
private bool m_bShowHRSP = true;
private bool m_bShowACDC = true;
private bool m_bShowRaw = true;
private bool m_bShowNewHRSP = true;
private FilterData m_sp_filter = new FilterData(10, 8, 1.0, 50, 95.0, 5.0, 5, 1.0);
private FilterData m_hr_filter = new FilterData(10, 8, 1.5, 50, 50.0, 20.0, 5, 2.0);
private bool m_bHasHRData = false;
private bool m_bHasSPData = false;
public static DialogResult InputBox(string title, string promptText, ref string value)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
public Form1()
{
InitializeComponent();
label9.Text = "built on " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
comboBox1.SelectedIndex = 1;
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
m_logFile = openFileDialog1.FileName;
m_bInited = true;
label2.Text = m_logFile;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = false;
m_nStep = -1;
if (m_srLog != null)
{
m_srLog.Close();
m_srLog = null;
}
button7.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
// Select file
openFileDialog1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
// Play
if (m_bInited && m_nStep < 0)
{
try
{
m_srLog = new System.IO.StreamReader(m_logFile);
}
catch
{
MessageBox.Show("Error opening " + m_logFile);
m_bInited = false;
return;
}
m_nStep = 0;
m_nBufferSize = 0;
m_bHasHRData = false;
m_bHasSPData = false;
chart1.Series["HR"].Points.Clear();
chart1.Series["SP"].Points.Clear();
chart1.Series["newHR"].Points.Clear();
chart1.Series["newSP"].Points.Clear();
chart1.Series["Red"].Points.Clear();
chart1.Series["IR"].Points.Clear();
chart1.Series["Green"].Points.Clear();
chart1.Series["AD for Red"].Points.Clear();
chart1.Series["AD for IR"].Points.Clear();
chart1.Series["AD for Green"].Points.Clear();
button2.Enabled = false;
button4.Enabled = true;
timer1.Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
// Pause
m_bPaused = !m_bPaused;
if (m_bPaused)
button3.Text = "Resume";
else
button3.Text = "Pause";
}
private void button4_Click(object sender, EventArgs e)
{
// Restart
if (m_bInited && m_srLog != null)
{
m_nStep = -1;
m_srLog.Close();
m_srLog = null;
timer1.Stop();
m_bPaused = false;
m_bHasHRData = false;
m_bHasSPData = false;
button3.Text = "Pause";
button2_Click(sender, e);
}
}
private void button5_Click(object sender, EventArgs e)
{
// Exit
Application.Exit();
}
private void timer1_Tick(object sender, EventArgs e)
{
string line;
if (!m_bPaused && m_bInited && m_nStep >= 0 && m_srLog != null)
{
++m_nStep;
label7.Text = m_nStep.ToString();
int count = 0;
int nTempSize = 0;
while ((line = m_srLog.ReadLine()) != null)
{
Match match = Regex.Match(line, @"HR=(\d+), SP=(\d+)");
int time = -1, red = -1, ir = -1, hr = -1, sp = -1;
bool found_rawdata = false, found_hrsp = false;
if (match.Success && match.Groups.Count == 3)
{
found_hrsp = true;
hr = Convert.ToInt32(match.Groups[1].Value);
sp = Convert.ToInt32(match.Groups[2].Value);
chart1.Series["HR"].Points.AddXY(m_nLastTimeStamp, hr);
chart1.Series["SP"].Points.AddXY(m_nLastTimeStamp, sp);
// Original values
label4.Text = "HR = " + hr.ToString() + ", SP = " + sp.ToString();
}
else
{
match = Regex.Match(line, @"time=(\d+), red=(\d+), ir=(\d+)");
if (match.Success && match.Groups.Count == 4)
found_rawdata = true;
else
{
// CSV type
match = Regex.Match(line, @"\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)");
if (match.Success && match.Groups.Count == 4)
found_rawdata = true;
}
if (found_rawdata)
{
time = Convert.ToInt32(match.Groups[1].Value);
red = Convert.ToInt32(match.Groups[2].Value);
ir = Convert.ToInt32(match.Groups[3].Value);
if (count < MAX_TEMP_SIZE)
{
m_temp_red_buffer[count] = red;
m_temp_ir_buffer[count] = ir;
m_temp_time_buffer[count] = time;
nTempSize = count + 1;
chart1.Series["Red"].Points.AddXY(time, red);
chart1.Series["IR"].Points.AddXY(time, ir);
}
m_nLastTimeStamp = time;
++count;
}
}
if (found_hrsp || count > 100)
{
int nTotalSize = m_nBufferSize + nTempSize;
if (nTempSize >= Algorithm30102.BUFFER_SIZE)
{
// Temp is enough
int nLeftShift = nTempSize - Algorithm30102.BUFFER_SIZE;
for (int i = 0; i < Algorithm30102.BUFFER_SIZE; ++i)
{
m_red_buffer[i] = m_temp_red_buffer[i + nLeftShift];
m_ir_buffer[i] = m_temp_ir_buffer[i + nLeftShift];
m_time_buffer[i] = m_temp_time_buffer[i + nLeftShift];
}
m_nBufferSize = Algorithm30102.BUFFER_SIZE;
}
else if (nTotalSize > Algorithm30102.BUFFER_SIZE)
{
// Need to left-shift the most recent data on the system buffer
int nKeep = Algorithm30102.BUFFER_SIZE - nTempSize;
int nLeftShift = m_nBufferSize - nKeep;
for (int i = 0; i < nKeep; ++i)
{
m_red_buffer[i] = m_red_buffer[i + nLeftShift];
m_ir_buffer[i] = m_ir_buffer[i + nLeftShift];
m_time_buffer[i] = m_time_buffer[i + nLeftShift];
}
// Then copy the whole temp to fill the system buffer
for (int i = nKeep; i < Algorithm30102.BUFFER_SIZE; ++i)
{
m_red_buffer[i] = m_temp_red_buffer[i - nKeep];
m_ir_buffer[i] = m_temp_ir_buffer[i - nKeep];
m_time_buffer[i] = m_temp_time_buffer[i - nKeep];
}
m_nBufferSize = Algorithm30102.BUFFER_SIZE;
}
else
{
// Just add the temp to the system buffer
for (int i = m_nBufferSize; i < nTotalSize; ++i)
{
m_red_buffer[i] = m_temp_red_buffer[i - m_nBufferSize];
m_ir_buffer[i] = m_temp_ir_buffer[i - m_nBufferSize];
m_time_buffer[i] = m_temp_time_buffer[i - m_nBufferSize];
}
m_nBufferSize = nTotalSize;
}
int window = Convert.ToInt32(textBox1.Text);
if (window < 100)
window = 100;
else if (window > Algorithm30102.BUFFER_SIZE)
window = Algorithm30102.BUFFER_SIZE;
textBox1.Text = window.ToString();
if (m_nBufferSize >= window)
{
Debug.Assert(m_nBufferSize <= Algorithm30102.BUFFER_SIZE);
int nNewHR = -1, nNewSP = -1;
bool bHRValid, bSPValid;
try
{
int nStart = m_nBufferSize - window;
int nSelected = comboBox1.SelectedIndex, sr = 100;
if (nSelected == 0)
sr = 50;
else if (nSelected == 1)
sr = 100;
else if (nSelected == 2)
sr = 200;
RData rdata = new RData();
m_alg.maxim_heart_rate_and_oxygen_saturation(sr, m_ir_buffer.Skip(nStart), window,
m_red_buffer.Skip(nStart), out nNewSP, out bSPValid, out nNewHR,
out bHRValid, rdata);
for (int i = 0; i < rdata.m_size; ++i)
{
chart1.Series["AD for Red"].Points.AddXY(m_time_buffer[nStart + rdata.m_red_indices[i]],
rdata.m_red_ratios[i]);
chart1.Series["AD for IR"].Points.AddXY(m_time_buffer[nStart + rdata.m_ir_indices[i]],
rdata.m_ir_ratios[i]);
}
}
catch
{
bHRValid = bSPValid = false;
}
if (!bHRValid)
nNewHR = -1;
if (!bSPValid)
nNewSP = -1;
if (nNewHR > 20 && nNewHR <= 200)
{
if (m_hr_filter.AddPoint((double)nNewHR))
m_bHasHRData = true;
else
nNewHR = -1;
}
else
nNewHR = -1;
if (m_bHasHRData)
nNewHR = (int) Math.Round(m_hr_filter.GetValue());
if (nNewSP > 50 && nNewSP <= 100)
{
if (m_sp_filter.AddPoint((double)nNewSP))
m_bHasSPData = true;
else
nNewSP = -1;
}
else
nNewSP = -1;
if (m_bHasSPData)
nNewSP = (int) Math.Round(m_sp_filter.GetValue());
label3.Text = "HR = " + nNewHR.ToString() + ", SP = " + nNewSP.ToString();
if (nNewHR > 0)
chart1.Series["newHR"].Points.AddXY(m_nLastTimeStamp, nNewHR);
if (nNewSP > 0)
chart1.Series["newSP"].Points.AddXY(m_nLastTimeStamp, nNewSP);
}
else
label3.Text = "Not enough raw data";
break;
}
}
if (line == null) // EOF
{
button2.Enabled = true;
button3.Enabled = false;
button4.Enabled = false;
m_srLog.Close();
m_srLog = null;
m_nStep = -1;
timer1.Stop();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (m_srLog != null)
m_srLog.Close();
}
private void button6_Click(object sender, EventArgs e)
{
string value = "Chart Title";
if (InputBox("Enter Title", "Chart Title:", ref value) == DialogResult.OK)
{
chart1.Titles[0].Text = value;
}
}
private void button7_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", m_logFile);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// Raw reflectance
m_bShowRaw = checkBox1.Checked;
chart1.Series["Red"].Enabled = m_bShowRaw;
chart1.Series["IR"].Enabled = m_bShowRaw;
chart1.Series["Green"].Enabled = m_bShowRaw;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
// HR and SP
m_bShowHRSP = checkBox2.Checked;
chart1.Series["HR"].Enabled = m_bShowHRSP;
chart1.Series["SP"].Enabled = m_bShowHRSP;
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
// AC/DC
m_bShowACDC = checkBox3.Checked;
chart1.Series["AD for Red"].Enabled = m_bShowACDC;
chart1.Series["AD for IR"].Enabled = m_bShowACDC;
chart1.Series["AD for Green"].Enabled = m_bShowACDC;
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
// New HR & SP
m_bShowNewHRSP = checkBox4.Checked;
chart1.Series["newHR"].Enabled = m_bShowNewHRSP;
chart1.Series["newSP"].Enabled = m_bShowNewHRSP;
}
private void button8_Click(object sender, EventArgs e)
{
// Flip primarhy
chart1.SuspendLayout();
System.Windows.Forms.DataVisualization.Charting.AxisType old_raw_type = chart1.Series["IR"].YAxisType,
new_raw_type = old_raw_type == System.Windows.Forms.DataVisualization.Charting.AxisType.Primary ?
System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary :
System.Windows.Forms.DataVisualization.Charting.AxisType.Primary,
new_other_type = old_raw_type;
string old_primary_label = chart1.ChartAreas["ChartArea1"].AxisY.Title;
chart1.ChartAreas["ChartArea1"].AxisY.Title = chart1.ChartAreas["ChartArea1"].AxisY2.Title;
chart1.ChartAreas["ChartArea1"].AxisY2.Title = old_primary_label;
chart1.Series["Red"].YAxisType = new_raw_type;
chart1.Series["IR"].YAxisType = new_raw_type;
chart1.Series["Green"].YAxisType = new_raw_type;
chart1.Series["HR"].YAxisType = new_other_type;
chart1.Series["SP"].YAxisType = new_other_type;
chart1.Series["newHR"].YAxisType = new_other_type;
chart1.Series["newSP"].YAxisType = new_other_type;
chart1.Series["AD for Red"].YAxisType = new_other_type;
chart1.Series["AD for IR"].YAxisType = new_other_type;
chart1.Series["AD for Green"].YAxisType = new_other_type;
chart1.ResumeLayout();
// Restart
button4_Click(sender, e);
}
private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{
// Check selected chart element and set tooltip text for it
switch (e.HitTestResult.ChartElementType)
{
case System.Windows.Forms.DataVisualization.Charting.ChartElementType.DataPoint:
var dataPoint = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex];
e.Text = string.Format("X:\t{0}\nY:\t{1}", dataPoint.XValue, dataPoint.YValues[0]);
break;
}
}
}
}