-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.c
1145 lines (942 loc) · 31.8 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019-2020, Alex Taradov <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*- Includes ----------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "gd32f4xx.h"
#include "hal_gpio.h"
#include "utils.h"
#include "lcd.h"
#include "timer.h"
#include "images.h"
#include "common.h"
#include "config.h"
#include "buttons.h"
#include "capture.h"
#include "scope.h"
/*- Definitions -------------------------------------------------------------*/
#define ZERO_POINT 0x80
#define GRID_CENTER_X 160
#define GRID_CENTER_Y 120
#define GRID_LEFT 10
#define GRID_RIGHT 310
#define GRID_TOP 20
#define GRID_BOTTOM 220
#define GRID_WIDTH 300
#define GRID_HEIGHT 200
#define GRID_DIV_PX 25
#define GRID_DIVS_H 12
#define GRID_DIVS_V 10
#define STATUS_LINE_Y 223
#define STATUS_LINE_HEIGHT 16
#define MINIVIEW_WIDTH 160
#define CALIB_AREA_LEFT 140
#define CALIB_AREA_WIDTH (LCD_WIDTH - CALIB_AREA_LEFT)
#define MAX_SAMPLE_RATE_LIMIT 13
#define TOAST_TIMEOUT 1500
#define TOAST_COLOR LCD_COLOR(255, 255, 0)
#define WAIT_STATE_HOLDOFF 100
#define BG_COLOR LCD_COLOR(0, 0, 0)
#define TRACE_COLOR LCD_COLOR(255, 255, 0)
#define TRACE_FILLED_COLOR LCD_COLOR(0, 255, 0)
#define TRACE_CLIP_COLOR LCD_COLOR(255, 0, 0)
#define TRACE_INVALID_COLOR LCD_COLOR(255, 0, 0)
#define GRID_BG_COLOR LCD_COLOR(0, 0, 0)
#define GRID_FG_COLOR LCD_COLOR(200, 200, 200)
#define MV_FRAME_COLOR LCD_COLOR(230, 230, 230)
#define HSCALE_COLOR LCD_COLOR(255, 255, 255)
#define HPOS_COLOR LCD_COLOR(255, 180, 50)
#define VSCALE_COLOR LCD_COLOR(255, 255, 0)
#define TRIGGER_LEVEL_COLOR LCD_COLOR(50, 255, 255)
#define TRIGGER_MODE_COLOR LCD_COLOR(0, 255, 0)
#define CAPTURE_STOP_COLOR LCD_COLOR(255, 0, 0)
#define CAPTURE_WAIT_COLOR LCD_COLOR(255, 180, 50)
#define CAPTURE_TRIG_COLOR LCD_COLOR(0, 255, 0)
#define SR_LIMIT_COLOR LCD_COLOR(250, 50, 50)
#define SR_COLOR LCD_COLOR(0, 230, 0)
#define MIN_TRIGGER_LEVEL -100 // px
#define MAX_TRIGGER_LEVEL 100 // px
#define MIN_HORIZONTAL_POSITION -2000000000 // 2 seconds
#define MAX_HORIZONTAL_POSITION 2000000000 // 2 seconds
#define MIN_VERTICAL_POSITION (-10 * GRID_DIV_PX)
#define MAX_VERTICAL_POSITION ( 10 * GRID_DIV_PX)
enum
{
CALIB_ZERO,
CALIB_DELTA,
CALIB_SCALE,
CALIB_OFFSET,
};
/*- Types -------------------------------------------------------------------*/
typedef struct
{
uint8_t min[GRID_WIDTH];
uint8_t max[GRID_WIDTH];
uint8_t flags[GRID_WIDTH];
} DisplayBuffer;
/*- Constants ---------------------------------------------------------------*/
static const char *hs_str[HS_COUNT] =
{
" 50\x01ns", // ns
"100\x01ns", "200\x01ns", "500\x01ns",
" 1\x01us", " 2\x01us", " 5\x01us", // us
" 10\x01us", " 20\x01us", " 50\x01us",
"100\x01us", "200\x01us", "500\x01us",
" 1\x01ms", " 2\x01ms", " 5\x01ms", // ms
" 10\x01ms", " 20\x01ms", " 50\x01ms",
"100\x01ms", "200\x01ms", "500\x01ms",
};
static const int hs_div_value[HS_COUNT] =
{
50, 100, 200, 500, // ns
1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, // us
1000000, 2000000, 5000000, 10000000, 20000000, 50000000, 100000000, 200000000, 500000000, // ms
};
static const int hs_px_value[HS_COUNT] = // in ns
{
2, 4, 8, 20, // ns
40, 80, 200, 400, 800, 2000, 4000, 8000, 20000, // us
40000, 80000, 200000, 400000, 800000, 2000000, 4000000, 8000000, 20000000, // ms
};
static const char *vs_str[VS_COUNT] =
{
" 50\x01mV", "100\x01mV", "200\x01mV", "500\x01mV", " 1\x01V ", " 2\x01V ", " 5\x01V ", " 10\x01V ",
};
static const int vs_px_value[VS_COUNT] =
{
2, 4, 8, 20, 40, 80, 200, 400,
};
/*- Variables ---------------------------------------------------------------*/
static uint16_t *g_grid_data[GRID_WIDTH];
static uint16_t g_grid_column_0[240];
static uint16_t g_grid_column_1[240];
static uint16_t g_grid_column_2[240];
static uint16_t g_grid_column_3[240];
static uint16_t g_grid_column_4[240];
static DataBuffer g_data_buffer;
static DisplayBuffer g_display_buffer;
static int g_trace_column = (GRID_WIDTH-1);
static bool g_toast_active = false;
static int g_toast_timer = TIMER_DISABLE;
static int g_state = -1;
static int g_state_timer = TIMER_DISABLE;
static bool g_calibration_mode = false;
static bool g_calibration_dual_channel = false;
static int g_calibration_parameter = CALIB_ZERO;
/*- Implementations ---------------------------------------------------------*/
//-----------------------------------------------------------------------------
static void grid_init(void)
{
// Setup column data
for (int y = 0; y < 199; y++)
g_grid_column_0[y] = GRID_BG_COLOR;
for (int y = 0; y < 199; y++)
g_grid_column_1[y] = (4 == (y % 5)) ? GRID_FG_COLOR : GRID_BG_COLOR;
for (int y = 0; y < 199; y++)
g_grid_column_2[y] = (24 == (y % 25)) ? GRID_FG_COLOR : GRID_BG_COLOR;
g_grid_column_2[0] = GRID_FG_COLOR;
g_grid_column_2[1] = GRID_FG_COLOR;
g_grid_column_2[98] = GRID_FG_COLOR;
g_grid_column_2[100] = GRID_FG_COLOR;
g_grid_column_2[197] = GRID_FG_COLOR;
g_grid_column_2[198] = GRID_FG_COLOR;
for (int y = 0; y < 199; y++)
g_grid_column_3[y] = (4 == (y % 5)) ? GRID_FG_COLOR : GRID_BG_COLOR;
g_grid_column_3[0] = GRID_FG_COLOR;
g_grid_column_3[1] = GRID_FG_COLOR;
g_grid_column_3[2] = GRID_FG_COLOR;
g_grid_column_3[98] = GRID_FG_COLOR;
g_grid_column_3[100] = GRID_FG_COLOR;
g_grid_column_3[196] = GRID_FG_COLOR;
g_grid_column_3[197] = GRID_FG_COLOR;
g_grid_column_3[198] = GRID_FG_COLOR;
for (int y = 0; y < 199; y++)
g_grid_column_4[y] = (24 == (y % 25)) ? GRID_FG_COLOR : GRID_BG_COLOR;
// Assign columns
for (int x = 0; x < 299; x++)
g_grid_data[x] = g_grid_column_0;
for (int x = 4; x < 299; x += 5)
g_grid_data[x] = g_grid_column_2;
for (int x = 24; x < 299; x += 25)
g_grid_data[x] = g_grid_column_3;
g_grid_data[0] = g_grid_column_1;
g_grid_data[1] = g_grid_column_1;
g_grid_data[2] = g_grid_column_4;
g_grid_data[148] = g_grid_column_1;
g_grid_data[150] = g_grid_column_1;
g_grid_data[296] = g_grid_column_4;
g_grid_data[297] = g_grid_column_1;
g_grid_data[298] = g_grid_column_1;
}
//-----------------------------------------------------------------------------
static void toast_show(void)
{
if (!g_toast_active)
lcd_fill_rect(GRID_LEFT, GRID_BOTTOM+1, GRID_WIDTH+1, STATUS_LINE_HEIGHT, BG_COLOR);
lcd_set_color(BG_COLOR, TOAST_COLOR);
g_toast_active = true;
g_toast_timer = TOAST_TIMEOUT;
}
//-----------------------------------------------------------------------------
static bool trace_ready(void)
{
return (g_trace_column == (GRID_WIDTH-1));
}
//-----------------------------------------------------------------------------
static void redraw_trace(void)
{
if (trace_ready())
g_trace_column = 0;
}
//-----------------------------------------------------------------------------
static void update_column_from_image(int index, uint16_t *column, int x, int y, const Image *image)
{
int image_col = index - x + image->ox + 1;
if (image_col < 0 || image_col > (image->width-1))
return;
for (int i = 0; i < image->height; i++)
{
int color = image->data[i * image->width + image_col];
if (color)
column[y + i] = color;
}
}
//-----------------------------------------------------------------------------
static void update_from_display_buffer(uint16_t *column, DisplayBuffer *db)
{
bool clip_h = db->flags[g_trace_column] & SAMPLE_FLAG_CLIP_H;
bool clip_l = db->flags[g_trace_column] & SAMPLE_FLAG_CLIP_L;
int color;
if (db->flags[g_trace_column] & SAMPLE_FLAG_VALID)
{
if (clip_h || clip_l)
color = TRACE_CLIP_COLOR;
else if (db->flags[g_trace_column] & SAMPLE_FLAG_FILLED)
color = TRACE_FILLED_COLOR;
else
color = TRACE_COLOR;
for (int y = db->min[g_trace_column]; y <= db->max[g_trace_column]; y++)
column[y] = color;
}
else
{
column[GRID_HEIGHT/2-1] = TRACE_INVALID_COLOR;
}
if (clip_h)
{
for (int i = 0; i < 3; i++)
column[i] = TRACE_CLIP_COLOR;
}
if (clip_l)
{
for (int i = 0; i < 3; i++)
column[GRID_HEIGHT-2 - i] = TRACE_CLIP_COLOR;
}
}
//-----------------------------------------------------------------------------
static void draw_trace(void)
{
uint16_t column[GRID_HEIGHT];
if (trace_ready())
return;
for (int i = 0; i < GRID_HEIGHT; i++)
column[i] = g_grid_data[g_trace_column][i];
update_from_display_buffer(column, &g_display_buffer);
if (config.horizontal_position_px < -(GRID_WIDTH/2-1))
{
update_column_from_image(g_trace_column, column, GRID_WIDTH-1, 1, &image_trigger_offset_right);
}
else if (config.horizontal_position_px > (GRID_WIDTH/2-1))
{
update_column_from_image(g_trace_column, column, 1, 1, &image_trigger_offset_left);
}
else
{
int pos = GRID_WIDTH/2 - config.horizontal_position_px;
update_column_from_image(g_trace_column, column, pos, 0, &image_trigger_offset);
}
lcd_draw_buf(GRID_LEFT+1 + g_trace_column, GRID_TOP+1, 1, GRID_HEIGHT-1, column);
g_trace_column++;
}
//-----------------------------------------------------------------------------
static void draw_grid_frame(void)
{
lcd_vline(10, 20, 220, GRID_FG_COLOR);
lcd_vline(310, 20, 220, GRID_FG_COLOR);
lcd_hline(10, 310, 20, GRID_FG_COLOR);
lcd_hline(10, 310, 220, GRID_FG_COLOR);
}
//-----------------------------------------------------------------------------
static void draw_ac_dc(void)
{
if (g_toast_active)
return;
lcd_draw_image(54, STATUS_LINE_Y, config.ac_coupling ? &image_ac : &image_dc);
}
//-----------------------------------------------------------------------------
static void draw_horizontal_scale(void)
{
if (g_toast_active)
return;
lcd_set_color(BG_COLOR, HSCALE_COLOR);
lcd_puts(82, STATUS_LINE_Y, hs_str[config.horizontal_scale]);
}
//-----------------------------------------------------------------------------
static void draw_horizontal_position(void)
{
char *str;
if (g_toast_active || g_calibration_mode)
return;
str = format_time(config.horizontal_position, true);
lcd_set_color(BG_COLOR, HPOS_COLOR);
lcd_puts(236, STATUS_LINE_Y, str);
}
//-----------------------------------------------------------------------------
static void draw_vertical_position(bool toast)
{
int y = GRID_CENTER_Y - config.vertical_position;
lcd_fill_rect(0, GRID_TOP-4, 9, GRID_HEIGHT+9, BG_COLOR);
if (y < GRID_TOP)
lcd_draw_image(GRID_LEFT-5, GRID_TOP, &image_reference_level_up);
else if (y > GRID_BOTTOM)
lcd_draw_image(GRID_LEFT-5, GRID_BOTTOM, &image_reference_level_down);
else
lcd_draw_image(GRID_LEFT-2, y, &image_reference_level);
if (toast)
{
char *str;
toast_show();
lcd_puts(GRID_LEFT, STATUS_LINE_Y, "Vertical position");
str = format_divisions((config.vertical_position * 100) / GRID_DIV_PX, true);
lcd_puts(GRID_LEFT + 140, STATUS_LINE_Y, str);
str = format_voltage(config.vertical_position_mv, true);
lcd_puts(GRID_LEFT + 226, STATUS_LINE_Y, str);
}
}
//-----------------------------------------------------------------------------
static void draw_vertical_scale(void)
{
if (g_toast_active)
return;
lcd_set_color(BG_COLOR, VSCALE_COLOR);
lcd_puts(10, STATUS_LINE_Y, vs_str[config.vertical_scale]);
}
//-----------------------------------------------------------------------------
static void draw_trigger_level(void)
{
char *str;
lcd_fill_rect(GRID_RIGHT+2, GRID_TOP-4, 8, GRID_HEIGHT+9, BG_COLOR);
lcd_draw_image(GRID_RIGHT+2, GRID_CENTER_Y - config.trigger_level,
&image_trigger_level);
if (g_toast_active || g_calibration_mode)
return;
str = format_voltage(config.trigger_level_mv - config.vertical_position_mv, true);
lcd_set_color(BG_COLOR, TRIGGER_LEVEL_COLOR);
lcd_puts(148, STATUS_LINE_Y, str);
}
//-----------------------------------------------------------------------------
static void draw_trigger_edge(void)
{
if (g_toast_active || g_calibration_mode)
return;
if (TRIGGER_EDGE_RISE == config.trigger_edge)
lcd_draw_image(140, STATUS_LINE_Y, &image_trigger_edge_rise);
else if (TRIGGER_EDGE_FALL == config.trigger_edge)
lcd_draw_image(140, STATUS_LINE_Y, &image_trigger_edge_fall);
else
lcd_draw_image(140, STATUS_LINE_Y, &image_trigger_edge_both);
}
//-----------------------------------------------------------------------------
static void draw_trigger_mode(void)
{
char *str;
if (TRIGGER_MODE_AUTO == config.trigger_mode)
str = "AUTO";
else if (TRIGGER_MODE_NORMAL == config.trigger_mode)
str = "NORM";
else
str = "SNGL";
lcd_set_color(BG_COLOR, TRIGGER_MODE_COLOR);
lcd_puts(10, 4, str);
}
//-----------------------------------------------------------------------------
static void draw_capture_state(void)
{
int state = capture_get_state();
int color = BG_COLOR;
char *str = "";
if (g_state == state)
return;
if (CAPTURE_STATE_STOP == state)
{
color = CAPTURE_STOP_COLOR;
str = "STOP";
}
else if (CAPTURE_STATE_WAIT == state)
{
color = CAPTURE_WAIT_COLOR;
str = "WAIT";
}
else if (CAPTURE_STATE_TRIG == state)
{
color = CAPTURE_TRIG_COLOR;
str = "TRIG";
}
lcd_set_color(BG_COLOR, color);
lcd_puts(46, 4, str);
g_state = state;
}
//-----------------------------------------------------------------------------
static void draw_miniview(int trigger_offset, int window_offset, int window_width)
{
static const uint8_t wave_pattern[8] = { 1, 0, 0, 1, 2, 3, 3, 2 };
uint16_t buf[8];
for (int x = -MINIVIEW_WIDTH/2+1; x < MINIVIEW_WIDTH/2; x++)
{
bool inside = ((x > window_offset) && (x < (window_offset + window_width)));
bool edge = ((x == window_offset) || (x == (window_offset + window_width - 1)));
if (edge)
{
for (int i = 0; i < 8; i++)
buf[i] = MV_FRAME_COLOR;
}
else
{
for (int i = 0; i < 8; i++)
buf[i] = BG_COLOR;
buf[2 + wave_pattern[x % sizeof(wave_pattern)]] = inside ? TRACE_COLOR : MV_FRAME_COLOR;
if (inside)
{
buf[0] = MV_FRAME_COLOR;
buf[7] = MV_FRAME_COLOR;
}
}
lcd_draw_buf(GRID_CENTER_X + x, 7, 1, 8, buf);
}
#define LEFT (GRID_CENTER_X - MINIVIEW_WIDTH/2)
#define RIGHT (GRID_CENTER_X + MINIVIEW_WIDTH/2)
lcd_fill_rect(LEFT - image_trigger_mv.width/2, 1, MINIVIEW_WIDTH + image_trigger_mv.width,
image_trigger_mv.height, BG_COLOR);
lcd_draw_image(GRID_CENTER_X + trigger_offset, 5, &image_trigger_mv);
lcd_vline(LEFT, 6, 15, MV_FRAME_COLOR);
lcd_hline(LEFT, LEFT+2, 6, MV_FRAME_COLOR);
lcd_hline(LEFT, LEFT+2, 15, MV_FRAME_COLOR);
lcd_vline(RIGHT, 6, 15, MV_FRAME_COLOR);
lcd_hline(RIGHT-2, RIGHT, 6, MV_FRAME_COLOR);
lcd_hline(RIGHT-2, RIGHT, 15, MV_FRAME_COLOR);
#undef LEFT
#undef RIGHT
}
//-----------------------------------------------------------------------------
static void draw_sample_rates(int sample_rate_limit, int sample_rate)
{
char *str;
lcd_set_font(FONT_SMALL);
str = format_sps(sample_rate_limit);
lcd_set_color(BG_COLOR, SR_LIMIT_COLOR);
lcd_puts(252, 2, str);
str = format_sps(sample_rate);
lcd_set_color(BG_COLOR, SR_COLOR);
lcd_puts(252, 10, str);
lcd_set_font(FONT_LARGE);
}
//-----------------------------------------------------------------------------
static void update_sample_rate(void)
{
int64_t hp_abs = (config.horizontal_position < 0) ? -config.horizontal_position : config.horizontal_position;
int64_t window_time = (int64_t)hs_div_value[config.horizontal_scale] * GRID_DIVS_H;
int64_t period = BASE_SAMPLE_PERIOD;
int64_t trigger_margin, trigger_offset;
int64_t buffer_time, required_time;
int64_t window_offset;
int64_t denom;
int sample_rate = BASE_SAMPLE_RATE;
int sample_rate_limit;
int trigger_offset_px, window_offset_px, window_width_px;
int sr_divider = config.sample_rate_limit;
for (int i = 0; i < config.sample_rate_limit; i++)
{
period *= 2;
sample_rate /= 2;
}
sample_rate_limit = sample_rate;
while (1)
{
trigger_margin = period * TRIGGER_MARGIN_SAMPLES;
required_time = trigger_margin + hp_abs + window_time/2;
buffer_time = (int64_t)CAPTURE_BUFFER_SIZE * period;
if (required_time < window_time)
required_time = window_time;
if (required_time < buffer_time)
break;
sr_divider++;
period *= 2;
sample_rate /= 2;
}
g_calibration_dual_channel = (sr_divider == 0);
denom = buffer_time - window_time/2 - trigger_margin;
trigger_offset = -config.horizontal_position * (buffer_time/2 - trigger_margin) / denom;
window_offset = trigger_offset + config.horizontal_position;
capture_set_horizontal_parameters(sr_divider, CAPTURE_BUFFER_SIZE/2 + trigger_offset / period);
denom = period * CAPTURE_BUFFER_SIZE;
trigger_offset_px = (trigger_offset * MINIVIEW_WIDTH) / denom;
window_offset_px = ((window_offset - window_time/2) * MINIVIEW_WIDTH) / denom;
window_width_px = (window_time * MINIVIEW_WIDTH) / denom;
if (window_offset_px == 0)
window_offset_px = -1;
if (window_width_px < 3)
window_width_px = 3;
draw_miniview(trigger_offset_px, window_offset_px, window_width_px);
draw_sample_rates(sample_rate_limit, sample_rate);
}
//-----------------------------------------------------------------------------
static int clip_for_display(int value)
{
value = GRID_HEIGHT/2-1 - value;
if (value > (GRID_HEIGHT-2))
value = (GRID_HEIGHT-2);
else if (value < 0)
value = 0;
return value;
}
//---------------------------------------------------------------------
static void close_gaps(DisplayBuffer *db)
{
for (int i = 0; i < GRID_WIDTH-1; i++)
{
if ((db->flags[i] & SAMPLE_FLAG_VALID) && (db->flags[i+1] & SAMPLE_FLAG_VALID))
{
if (db->max[i] < (db->min[i+1]-1))
{
int avg = (db->max[i] + db->min[i+1]) / 2;
db->max[i] = avg;
db->min[i+1] = avg+1;
}
else if (db->min[i] > (db->max[i+1] + 1))
{
int avg = (db->min[i] + db->max[i+1]) / 2;
db->min[i] = avg;
db->max[i+1] = avg-1;
}
}
}
}
//-----------------------------------------------------------------------------
static void update_display(void)
{
int scale = vs_px_value[config.vertical_scale];
g_data_buffer.size = GRID_WIDTH;
capture_get_data(&g_data_buffer);
for (int i = 0; i < GRID_WIDTH; i++)
{
int min = (g_data_buffer.min[i] - g_data_buffer.vertical_position) / scale + config.vertical_position;
int max = (g_data_buffer.max[i] - g_data_buffer.vertical_position) / scale + config.vertical_position;
g_display_buffer.min[i] = clip_for_display(max);
g_display_buffer.max[i] = clip_for_display(min);
g_display_buffer.flags[i] = g_data_buffer.flags[i];
}
close_gaps(&g_display_buffer);
redraw_trace();
}
//-----------------------------------------------------------------------------
static void change_horizontal_scale(int delta)
{
if ((delta < 0 && config.horizontal_scale == 0) ||
(delta > 0 && config.horizontal_scale == HS_LAST))
return;
config.horizontal_scale += delta;
config.horizontal_position_px = config.horizontal_position / hs_px_value[config.horizontal_scale];
config.horizontal_period = hs_px_value[config.horizontal_scale];
draw_horizontal_scale();
draw_horizontal_position();
update_sample_rate();
update_display();
}
//-----------------------------------------------------------------------------
static void change_horizontal_position(int delta)
{
int div = hs_px_value[config.horizontal_scale];
if ((delta < 0 && config.horizontal_position <= MIN_HORIZONTAL_POSITION) ||
(delta > 0 && config.horizontal_position >= MAX_HORIZONTAL_POSITION))
return;
config.horizontal_position_px = config.horizontal_position / div + delta;
config.horizontal_position = (int64_t)config.horizontal_position_px * div;
draw_horizontal_position();
update_sample_rate();
update_display();
}
//-----------------------------------------------------------------------------
static void change_vertical_scale(int delta)
{
if ((delta < 0 && config.vertical_scale == 0) ||
(delta > 0 && config.vertical_scale == VS_LAST))
return;
config.vertical_scale += delta;
config.vertical_mult = config.calib_vs_mult[config.vertical_scale];
config.vertical_position_mv = config.vertical_position * vs_px_value[config.vertical_scale];
capture_set_vertical_parameters();
capture_set_trigger_level(config.trigger_level_mv);
draw_vertical_scale();
draw_trigger_level();
update_display();
}
//-----------------------------------------------------------------------------
static void change_vertical_position(int delta)
{
if ((delta < 0 && config.vertical_position <= MIN_VERTICAL_POSITION) ||
(delta > 0 && config.vertical_position >= MAX_VERTICAL_POSITION))
return;
config.vertical_position += delta;
config.vertical_position_mv = config.vertical_position * vs_px_value[config.vertical_scale];
capture_set_vertical_parameters();
draw_vertical_position(true);
draw_trigger_level();
update_display();
}
//-----------------------------------------------------------------------------
static void change_trigger_level(int delta)
{
if ((delta < 0 && config.trigger_level == MIN_TRIGGER_LEVEL) ||
(delta > 0 && config.trigger_level == MAX_TRIGGER_LEVEL))
return;
config.trigger_level += delta;
config.trigger_level_mv = config.trigger_level * vs_px_value[config.vertical_scale];
capture_set_trigger_level(config.trigger_level_mv);
draw_trigger_level();
}
//-----------------------------------------------------------------------------
static void change_sample_rate_limit(int delta)
{
if ((delta < 0 && config.sample_rate_limit == 0) ||
(delta > 0 && config.sample_rate_limit == MAX_SAMPLE_RATE_LIMIT))
return;
config.sample_rate_limit += delta;
update_sample_rate();
}
//-----------------------------------------------------------------------------
static void change_calibration_value(int delta, bool shift)
{
if (shift)
{
if (g_calibration_parameter == CALIB_OFFSET)
g_calibration_parameter = CALIB_ZERO;
else
g_calibration_parameter++;
lcd_fill_rect(CALIB_AREA_LEFT, GRID_BOTTOM+1, CALIB_AREA_WIDTH, STATUS_LINE_HEIGHT, BG_COLOR);
return;
}
if (g_calibration_parameter == CALIB_ZERO)
{
config.calib_dac_zero += delta;
if (config.calib_dac_zero < 1900)
config.calib_dac_zero = 1900;
else if (config.calib_dac_zero > 2200)
config.calib_dac_zero = 2200;
}
else if (g_calibration_parameter == CALIB_DELTA)
{
config.calib_channel_delta += delta;
if (config.calib_channel_delta < -64)
config.calib_channel_delta = -64;
else if (config.calib_channel_delta > 64)
config.calib_channel_delta = 64;
}
else if (g_calibration_parameter == CALIB_SCALE)
{
config.calib_vs_mult[config.vertical_scale] += delta;
if (config.calib_vs_mult[config.vertical_scale] < 0)
config.calib_vs_mult[config.vertical_scale] = 0;
else if (config.calib_vs_mult[config.vertical_scale] > 4000000)
config.calib_vs_mult[config.vertical_scale] = 4000000;
}
else if (g_calibration_parameter == CALIB_OFFSET)
{
config.calib_dac_mult[config.vertical_scale] += delta;
if (config.calib_dac_mult[config.vertical_scale] < 0)
config.calib_dac_mult[config.vertical_scale] = 0;
else if (config.calib_dac_mult[config.vertical_scale] > 100000)
config.calib_dac_mult[config.vertical_scale] = 100000;
}
capture_set_vertical_parameters();
}
//-----------------------------------------------------------------------------
static void draw_calibration_info(void)
{
static const char *labels[] = { "Z", "D", "S", "O" };
char *str;
lcd_set_color(BG_COLOR, LCD_WHITE_COLOR);
if (!g_toast_active)
lcd_puts(CALIB_AREA_LEFT, STATUS_LINE_Y, labels[g_calibration_parameter]);
g_data_buffer.size = GRID_WIDTH;
if (g_calibration_parameter == CALIB_ZERO || g_calibration_parameter == CALIB_DELTA)
{
if ((g_calibration_parameter == CALIB_ZERO) == g_calibration_dual_channel)
lcd_set_color(BG_COLOR, LCD_RED_COLOR);
capture_get_raw_data(g_data_buffer.max, GRID_WIDTH/2);
if (g_calibration_dual_channel)
{
for (int i = 1; i < GRID_WIDTH/2; i += 2)
g_data_buffer.max[i] = rbit8(g_data_buffer.max[i]) + config.calib_channel_delta;
}
if (!g_toast_active)
{
str = format_raw_data(g_data_buffer.max, 4);
lcd_puts(CALIB_AREA_LEFT + 24, STATUS_LINE_Y, str);
}
#define PIXEL_SIZE 15
for (int i = 0; i < GRID_WIDTH/2; i++)
{
g_data_buffer.max[i] -= ZERO_POINT;
g_data_buffer.min[i] = g_data_buffer.max[i];
if (0 == i % PIXEL_SIZE)
{
g_data_buffer.max[GRID_WIDTH/2 + i] = 0;
g_data_buffer.min[GRID_WIDTH/2 + i] = 0;
}
else
{
int value = g_data_buffer.max[i / PIXEL_SIZE] * PIXEL_SIZE;
g_data_buffer.max[GRID_WIDTH/2 + i] = value + PIXEL_SIZE/2;
g_data_buffer.min[GRID_WIDTH/2 + i] = value - PIXEL_SIZE/2;
}
}
for (int i = 0; i < GRID_WIDTH; i++)
{
g_display_buffer.min[i] = clip_for_display(g_data_buffer.max[i]);
g_display_buffer.max[i] = clip_for_display(g_data_buffer.min[i]);
g_display_buffer.flags[i] = SAMPLE_FLAG_VALID;
}
redraw_trace();
}
else // if (g_calibration_parameter == CALIB_SCALE || g_calibration_parameter == CALIB_OFFSET)
{
update_display();
int vmin = g_data_buffer.min_value;
int vmax = g_data_buffer.max_value;
int vpos = g_data_buffer.vertical_position;
if (!g_toast_active)
{
str = format_voltage(vmin - vpos, true);
lcd_puts(CALIB_AREA_LEFT + 12, STATUS_LINE_Y, str);
str = format_voltage(vmax - vpos, true);
lcd_puts(CALIB_AREA_LEFT + 96, STATUS_LINE_Y, str);
}
}
}
//-----------------------------------------------------------------------------
static void draw_status_line(void)
{
lcd_fill_rect(GRID_LEFT, GRID_BOTTOM+1, GRID_WIDTH+1, STATUS_LINE_HEIGHT, BG_COLOR);
draw_vertical_scale();
draw_ac_dc();
draw_horizontal_scale();
draw_horizontal_position();
draw_trigger_level();
draw_trigger_edge();
}
//-----------------------------------------------------------------------------
void scope_buttons_handler(int buttons)
{
bool shift = (buttons & BTN_SHIFT);
bool repeat = (buttons & BTN_REPEAT);
if ((buttons & BTN_UP) && (buttons & BTN_DOWN))
{
config.vertical_position = 0;
config.vertical_position_mv = 0;
capture_set_vertical_parameters();
draw_vertical_position(true);
update_display();
}
else if (buttons & BTN_UP)
{
if (shift)
change_vertical_scale(1);
else
change_vertical_position(1);
}
else if (buttons & BTN_DOWN)
{
if (shift)
change_vertical_scale(-1);
else
change_vertical_position(-1);
}
else if ((buttons & BTN_LEFT) && (buttons & BTN_RIGHT))
{
config.horizontal_position = 0;
config.horizontal_position_px = 0;
draw_horizontal_position();
update_sample_rate();
update_display();
}
else if (buttons & BTN_LEFT)
{
if (shift)
change_horizontal_scale(-1);
else
change_horizontal_position(1);
}
else if (buttons & BTN_RIGHT)
{
if (shift)
change_horizontal_scale(1);
else
change_horizontal_position(-1);
}
else if (buttons & BTN_TRIG)
{
if (repeat || g_calibration_mode)
return;
if (config.trigger_mode == TRIGGER_MODE_SINGLE)
config.trigger_mode = TRIGGER_MODE_AUTO;