forked from DiSlord/NanoVNA-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.c
1864 lines (1716 loc) · 61.6 KB
/
plot.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, Dmitry (DiSlord) [email protected]
* Based on TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "nanovna.h"
static void cell_draw_marker_info(int x0, int y0);
static void draw_battery_status(void);
static void draw_cal_status(void);
static void draw_frequencies(void);
static int cell_printf(int16_t x, int16_t y, const char *fmt, ...);
static void markmap_all_markers(void);
static int16_t grid_offset;
static int16_t grid_width;
static uint16_t redraw_request = 0; // contains REDRAW_XXX flags
static uint16_t area_width = AREA_WIDTH_NORMAL;
static uint16_t area_height = AREA_HEIGHT_NORMAL;
// Cell render use spi buffer
static pixel_t *cell_buffer;
// indicate dirty cells (not redraw if cell data not changed)
#define MAX_MARKMAP_X ((LCD_WIDTH+CELLWIDTH-1)/CELLWIDTH)
#define MAX_MARKMAP_Y ((LCD_HEIGHT+CELLHEIGHT-1)/CELLHEIGHT)
// Define markmap mask size
#if MAX_MARKMAP_X <= 8
typedef uint8_t map_t;
#elif MAX_MARKMAP_X <= 16
typedef uint16_t map_t;
#elif MAX_MARKMAP_X <= 32
typedef uint32_t map_t;
#endif
static map_t markmap[MAX_MARKMAP_Y];
// Trace data cache, for faster redraw cells
typedef struct {
uint16_t x;
uint16_t y;
} index_t;
static index_t trace_index[TRACE_INDEX_COUNT][SWEEP_POINTS_MAX];
#if 1
// All used in plot v > 0
#define float2int(v) ((int)((v)+0.5f))
#else
static int
float2int(float v)
{
if (v < 0) return v - 0.5;
if (v > 0) return v + 0.5;
return 0;
}
#endif
static bool
polar_grid(int x, int y)
{
uint32_t d = x*x + y*y;
if (d > P_RADIUS*P_RADIUS + P_RADIUS) return 0;
if (d > P_RADIUS*P_RADIUS - P_RADIUS) return 1;
// vertical and horizontal axis
if (x == 0 || y == 0) return 1;
if (d < P_RADIUS*P_RADIUS/25 - P_RADIUS/5) return 0;
if (d < P_RADIUS*P_RADIUS/25 + P_RADIUS/5) return 1;
if (d < P_RADIUS*P_RADIUS*4/25 - P_RADIUS*2/5) return 0;
if (d < P_RADIUS*P_RADIUS*4/25 + P_RADIUS*2/5) return 1;
// cross sloping lines
if (x == y || x == -y) return 1;
if (d < P_RADIUS*P_RADIUS*9/25 - P_RADIUS*3/5) return 0;
if (d < P_RADIUS*P_RADIUS*9/25 + P_RADIUS*3/5) return 1;
if (d < P_RADIUS*P_RADIUS*16/25 - P_RADIUS*4/5) return 0;
if (d < P_RADIUS*P_RADIUS*16/25 + P_RADIUS*4/5) return 1;
return 0;
}
static void
cell_polar_grid(int x0, int y0, int w, int h, pixel_t color)
{
int x, y;
// offset to center
x0 -= P_CENTER_X;
y0 -= P_CENTER_Y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
if (polar_grid(x + x0, y + y0)) cell_buffer[y * CELLWIDTH + x] = color;
}
/*
* Render Smith grid (if mirror by x possible get Admittance grid)
*/
static bool
smith_grid(int x, int y)
{
#if 0
int d;
// outer circle
d = circle_inout(x, y, P_RADIUS);
if (d < 0) return 0;
if (d == 0) return 1;
// horizontal axis
if (y == 0) return 1;
if (y < 0) y = -y; // mirror by y axis
if (x >= 0) { // valid only if x >= 0
if (x >= P_RADIUS/2){ // valid only if x >= P_RADIUS/2
// Constant Reactance Circle: 2j : R/2 = P_RADIUS/2 (mirror by y)
if (circle_inout(x - P_RADIUS, y - P_RADIUS/2, P_RADIUS/2) == 0) return 1;
// Constant Resistance Circle: 3 : R/4 = P_RADIUS/4
d = circle_inout(x - 3*P_RADIUS/4, y, P_RADIUS/4);
if (d > 0) return 0;
if (d == 0) return 1;
}
// Constant Reactance Circle: 1j : R = P_RADIUS (mirror by y)
d = circle_inout(x - P_RADIUS, y - P_RADIUS, P_RADIUS);
if (d == 0) return 1;
// Constant Resistance Circle: 1 : R/2
d = circle_inout(x - P_RADIUS/2, y, P_RADIUS/2);
if (d > 0) return 0;
if (d == 0) return 1;
}
// Constant Reactance Circle: 1/2j : R*2 (mirror by y)
if (circle_inout(x - P_RADIUS, y - P_RADIUS*2, P_RADIUS*2) == 0) return 1;
// Constant Resistance Circle: 1/3 : R*3/4
if (circle_inout(x - P_RADIUS/4, y, P_RADIUS*3/4) == 0) return 1;
return 0;
#else
uint16_t r = P_RADIUS;
// outer circle
uint32_t _r = x*x + y*y;
int32_t d = _r;
if (d > r*r + r) return 0;
if (d > r*r - r) return 1; // 1
// horizontal axis
if (y == 0) return 1;
if (y < 0) y = -y; // mirror by y axis
uint32_t r_y = r*y;
if (x >= 0) { // valid only if x >= 0
if (x >= r/2){
// Constant Reactance Circle: 2j : R/2 = P_RADIUS/2 (mirror by y)
d = _r - 2*r*x - r_y + r*r + r/2;
if ((uint32_t)d <= r) return 1; // 2
// Constant Resistance Circle: 3 : R/4 = P_RADIUS/4
d = _r - (3*r/2)*x + r*r/2 + r/4;
if (d < 0) return 0;
if (d <= r/2) return 1; // 3
}
// Constant Reactance Circle: 1j : R = P_RADIUS (mirror by y)
d = _r - 2*r*x - 2*r_y + r*r + r;
if ((uint32_t)d<=2*r) return 1; // 4
// Constant Resistance Circle: 1 : R/2
d = _r - r*x + r/2;
if (d < 0) return 0;
if (d <= r) return 1; // 5
}
// Constant Reactance Circle: 1/2j : R*2 (mirror by y)
d = _r - 2*r*x - 4*r_y + r*r + r*2;
if ((uint32_t) d<= r*4) return 1; // 6
// Constant Resistance Circle: 1/3 : R*3/4
d = _r - x*(r/2) - r*r/2 + r*3/4;
if ((uint32_t)d<=r*3/2) return 1; // 7
return 0;
#endif
}
static void
cell_smith_grid(int x0, int y0, int w, int h, pixel_t color)
{
int x, y;
// offset to center
x0-= P_CENTER_X;
y0-= P_CENTER_Y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
if (smith_grid(x + x0, y + y0)) cell_buffer[y * CELLWIDTH + x] = color;
}
static void
cell_admit_grid(int x0, int y0, int w, int h, pixel_t color)
{
int x, y;
// offset to center
x0 = P_CENTER_X - x0;
y0-= P_CENTER_Y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
if (smith_grid(- x + x0, y + y0)) cell_buffer[y * CELLWIDTH + x] = color;
}
void update_grid(freq_t fstart, freq_t fstop)
{
freq_t fspan = fstop - fstart;
freq_t grid;
if (fspan < 1000) {
grid_offset = 0;
grid_width = 0;
} else {
freq_t gdigit = 100000000;
while (gdigit > 100) {
grid = 5 * gdigit;
if (fspan / grid >= 4) break;
grid = 2 * gdigit;
if (fspan / grid >= 4) break;
grid = gdigit;
if (fspan / grid >= 4) break;
gdigit /= 10;
}
grid_offset = (WIDTH) * ((fstart % grid) / 100) / (fspan / 100);
grid_width = (WIDTH) * (grid / 100) / (fspan / 1000);
}
}
static inline int
rectangular_grid_x(int x)
{
x -= CELLOFFSETX;
if ((uint32_t)x > WIDTH) return 0;
if ((((x + grid_offset) * 10) % grid_width) < 10 || x == 0 || x == WIDTH)
return 1;
return 0;
}
static inline int
rectangular_grid_y(int y)
{
if (y < 0 || (y % GRIDY))
return 0;
return 1;
}
//**************************************************************************************
// NanoVNA measures
// This functions used for plot traces, and markers data output
// Also can used in measure calculations
//**************************************************************************************
#ifdef __VNA_Z_RENORMALIZATION__
#define PORT_Z current_props._portz
#else
#define PORT_Z 50.0f
#endif
// Help functions
static float get_l(float re, float im) {return (re*re + im*im);}
static float get_w(int i) {return 2 * VNA_PI * getFrequency(i);}
static float get_s11_r(float re, float im, float z) {return vna_fabsf(2.0f * z * re / get_l(re, im) - z);}
static float get_s21_r(float re, float im, float z) {return 1.0f * z * re / get_l(re, im) - z;}
static float get_s11_x(float re, float im, float z) {return -2.0f * z * im / get_l(re, im);}
static float get_s21_x(float re, float im, float z) {return -1.0f * z * im / get_l(re, im);}
//**************************************************************************************
// LINEAR = |S|
//**************************************************************************************
static float linear(int i, const float *v) {
(void) i;
return vna_sqrtf(get_l(v[0], v[1]));
}
//**************************************************************************************
// LOGMAG = 20*log10f(|S|)
//**************************************************************************************
static float logmag(int i, const float *v) {
(void) i;
// return log10f(get_l(v[0], v[1])) * 10.0f;
// return vna_logf(get_l(v[0], v[1])) * (10.0f / logf(10.0f));
return vna_log10f_x_10(get_l(v[0], v[1]));
}
//**************************************************************************************
// PHASE angle in degree = atan2(im, re) * 180 / PI
//**************************************************************************************
static float phase(int i, const float *v) {
(void) i;
return (180.0f / VNA_PI) * vna_atan2f(v[1], v[0]);
}
//**************************************************************************************
// Group delay
//**************************************************************************************
static float groupdelay(const float *v, const float *w, uint32_t deltaf) {
#if 1
// atan(w)-atan(v) = atan((w-v)/(1+wv)), for complex v and w result q = v / w
float r = w[0]*v[0] + w[1]*v[1];
float i = w[0]*v[1] - w[1]*v[0];
return vna_atan2f(i, r) / (2 * VNA_PI * deltaf);
#else
return (vna_atan2f(w[0], w[1]) - vna_atan2f(v[0], v[1])) / (2 * VNA_PI * deltaf);
#endif
}
//**************************************************************************************
// REAL
//**************************************************************************************
static float real(int i, const float *v) {
(void) i;
return v[0];
}
//**************************************************************************************
// IMAG
//**************************************************************************************
static float imag(int i, const float *v) {
(void) i;
return v[1];
}
//**************************************************************************************
// SWR = (1 + |S|)/(1 - |S|)
//**************************************************************************************
static float swr(int i, const float *v) {
(void) i;
float x = linear(i, v);
if (x > 0.99f)
return INFINITY;
return (1 + x)/(1 - x);
}
//**************************************************************************************
// Z parameters calculations from complex S
// Z = z0 * (1 + S) / (1 - S) = R + jX
// |Z| = sqrtf(R*R+X*X)
// Resolve this in complex give:
// let S` = 1 - S => re` = 1 - re and im` = -im
// l` = re` * re` + im` * im`
// Z = z0 * (2 - S`) / S` = z0 * 2 / S` - z0
// R = z0 * 2 * re` / l` - z0
// X =-z0 * 2 * im` / l`
// |Z| = z0 * sqrt(4 * re / l` + 1)
// Z phase = atan(X, R)
//**************************************************************************************
static float resistance(int i, const float *v) {
(void) i;
return get_s11_r(1.0f - v[0], -v[1], PORT_Z);
}
static float reactance(int i, const float *v) {
(void) i;
return get_s11_x(1.0f - v[0], -v[1], PORT_Z);
}
static float mod_z(int i, const float *v) {
(void) i;
const float z0 = PORT_Z;
const float l = get_l(1.0f - v[0], v[1]);
return z0 * vna_sqrtf(4.0f * v[0] / l + 1.0f); // always >= 0
}
static float phase_z(int i, const float *v) {
(void) i;
const float r = 1.0f - get_l(v[0], v[1]);
const float x = 2.0f * v[1];
return (180.0f / VNA_PI) * vna_atan2f(x, r);
}
//**************************************************************************************
// Use w = 2 * pi * frequency
// Get Series L and C from X
// C = -1 / (w * X)
// L = X / w
//**************************************************************************************
static float series_c(int i, const float *v) {
const float zi = reactance(i, v);
const float w = get_w(i);
return -1.0f / (w * zi);
}
static float series_l(int i, const float *v) {
const float zi = reactance(i, v);
const float w = get_w(i);
return zi / w;
}
//**************************************************************************************
// Q factor = abs(X / R)
// Q = 2 * im / (1 - re * re - im * im)
//**************************************************************************************
static float qualityfactor(int i, const float *v) {
(void) i;
const float r = 1.0f - get_l(v[0], v[1]);
const float x = 2.0f * v[1];
return vna_fabsf(x / r);
}
//**************************************************************************************
// Y parameters (conductance and susceptance) calculations from complex S
// Y = (1 / z0) * (1 - S) / (1 + S) = G + jB
// Resolve this in complex give:
// let S` = 1 + S => re` = 1 + re and im` = im
// l` = re` * re` + im` * im`
// z0` = (1 / z0)
// Y = z0` * (2 - S`) / S` = 2 * z0` / S` - z0`
// G = 2 * z0` * re` / l` - z0`
// B = -2 * z0` * im` / l`
// |Y| = 1 / |Z|
//**************************************************************************************
static float conductance(int i, const float *v) {
(void) i;
return get_s11_r(1.0f + v[0], v[1], 1.0f / PORT_Z);
}
static float susceptance(int i, const float *v) {
(void) i;
return get_s11_x(1.0f + v[0], v[1], 1.0f / PORT_Z);
}
//**************************************************************************************
// Parallel R and X calculations from Y
// Rp = 1 / G
// Xp =-1 / B
//**************************************************************************************
static float parallel_r(int i, const float *v) {
#if 1
return 1.0f / conductance(i, v);
#else
(void) i;
const float re = 1.0f + v[0], im = v[1];
const float z0 = PORT_Z;
const float l = get_l(re, im);
return z0 * l / (2.0f * re - l);
#endif
}
static float parallel_x(int i, const float *v) {
#if 1
return -1.0f / susceptance(i, v);
#else
(void) i;
const float z0 = PORT_Z;
return z0 * get_l(1.0f + v[0], v[1]) / (2.0f * v[1]);
#endif
}
//**************************************************************************************
// Use w = 2 * pi * frequency
// Get Parallel L and C from B
// C = B / w
// L = -1 / (w * B) = Xp / w
//**************************************************************************************
static float parallel_c(int i, const float *v) {
const float yi = susceptance(i, v);
const float w = get_w(i);
return yi / w;
}
static float parallel_l(int i, const float *v) {
const float xp = parallel_x(i, v);
const float w = get_w(i);
return xp / w;
}
static float mod_y(int i, const float *v) {
return 1.0f / mod_z(i, v); // always >= 0
}
//**************************************************************************************
// S21 series and shunt
// S21 shunt Z = 0.5f * z0 * S / (1 - S)
// replace S` = (1 - S)
// S21 shunt Z = 0.5f * z0 * (1 - S`) / S`
// S21 series Z = 2.0f * z0 * (1 - S ) / S
// Q21 = im / re
//**************************************************************************************
static float s21shunt_r(int i, const float *v) {
(void) i;
return get_s21_r(1.0f - v[0], -v[1], 0.5f * PORT_Z);
}
static float s21shunt_x(int i, const float *v) {
(void) i;
return get_s21_x(1.0f - v[0], -v[1], 0.5f * PORT_Z);
}
static float s21shunt_z(int i, const float *v) {
(void) i;
float l1 = get_l(v[0], v[1]);
float l2 = get_l(1.0f - v[0], v[1]);
return 0.5f * PORT_Z * vna_sqrtf(l1 / l2);
}
static float s21series_r(int i, const float *v) {
(void) i;
return get_s21_r(v[0], v[1], 2.0f * PORT_Z);
}
static float s21series_x(int i, const float *v) {
(void) i;
return get_s21_x(v[0], v[1], 2.0f * PORT_Z);
}
static float s21series_z(int i, const float *v) {
(void) i;
float l1 = get_l(v[0], v[1]);
float l2 = get_l(1.0f - v[0], v[1]);
return 2.0f * PORT_Z * vna_sqrtf(l2 / l1);
}
static float s21_qualityfactor(int i, const float *v) {
(void) i;
return vna_fabsf(v[1] / (v[0] - get_l(v[0], v[1])));
}
//**************************************************************************************
// Group delay
//**************************************************************************************
float groupdelay_from_array(int i, const float *v) {
int bottom = (i == 0) ? 0 : -1; // get prev point
int top = (i == sweep_points-1) ? 0 : 1; // get next point
freq_t deltaf = get_sweep_frequency(ST_SPAN) / ((sweep_points - 1) / (top - bottom));
return groupdelay(&v[2*bottom], &v[2*top], deltaf);
}
static inline void
cartesian_scale(const float *v, int16_t *xp, int16_t *yp, float scale) {
int16_t x = P_CENTER_X + float2int(v[0] * scale);
int16_t y = P_CENTER_Y - float2int(v[1] * scale);
if (x < 0) x = 0;
else if (x > WIDTH) x = WIDTH;
if (y < 0) y = 0;
else if (y > HEIGHT) y = HEIGHT;
*xp = x;
*yp = y;
}
#if MAX_TRACE_TYPE != 30
#error "Redefined trace_type list, need check format_list"
#endif
const trace_info_t trace_info_list[MAX_TRACE_TYPE] = {
// Type name format delta format symbol ref scale get value
[TRC_LOGMAG] = {"LOGMAG", "%.3F%s", S_DELTA "%.3F%s", S_dB, NGRIDY-1, 10.0f, logmag },
[TRC_PHASE] = {"PHASE", "%.2f%s", S_DELTA "%.2f%s", S_DEGREE, NGRIDY/2, 90.0f, phase },
[TRC_DELAY] = {"DELAY", "%.4F%s", "%.4F%s", S_SECOND, NGRIDY/2, 1e-9f, groupdelay_from_array},
[TRC_SMITH] = {"SMITH", NULL, NULL, "", 0, 1.00f, NULL }, // Custom
[TRC_POLAR] = {"POLAR", NULL, NULL, "", 0, 1.00f, NULL }, // Custom
[TRC_LINEAR] = {"LINEAR", "%.6f%s", S_DELTA "%.5f%s", "", 0, 0.125f, linear },
[TRC_SWR] = {"SWR", "%.3f%s", S_DELTA "%.3f%s", "", 0, 0.25f, swr },
[TRC_REAL] = {"REAL", "%.6f%s", S_DELTA "%.5f%s", "", NGRIDY/2, 0.25f, real },
[TRC_IMAG] = {"IMAG", "%.6fj%s",S_DELTA "%.5fj%s","", NGRIDY/2, 0.25f, imag },
[TRC_R] = {"R", "%.3F%s", S_DELTA "%.3F%s", S_OHM, 0, 100.0f, resistance },
[TRC_X] = {"X", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, reactance },
[TRC_Z] = {"|Z|", "%.3F%s", S_DELTA "%.3F%s", S_OHM, 0, 50.0f, mod_z },
[TRC_ZPHASE] = {"Z phase","%.1f%s", S_DELTA "%.2f%s", S_DEGREE, NGRIDY/2, 90.0f, phase_z },
[TRC_G] = {"G", "%.3F%s", S_DELTA "%.3F%s", S_SIEMENS, 0, 0.01f, conductance },
[TRC_B] = {"B", "%.3F%s", S_DELTA "%.3F%s", S_SIEMENS,NGRIDY/2, 0.01f, susceptance },
[TRC_Y] = {"|Y|", "%.3F%s", S_DELTA "%.3F%s", S_SIEMENS, 0, 0.02f, mod_y },
[TRC_Rp] = {"Rp", "%.3F%s", S_DELTA "%.3F%s", S_OHM, 0, 100.0f, parallel_r },
[TRC_Xp] = {"Xp", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, parallel_x },
[TRC_sC] = {"sC", "%.4F%s", S_DELTA "%.4F%s", S_FARAD, NGRIDY/2, 1e-8f, series_c },
[TRC_sL] = {"sL" , "%.4F%s", S_DELTA "%.4F%s", S_HENRY, NGRIDY/2, 1e-8f, series_l },
[TRC_pC] = {"pC", "%.4F%s", S_DELTA "%.4F%s", S_FARAD, NGRIDY/2, 1e-8f, parallel_c },
[TRC_pL] = {"pL" , "%.4F%s", S_DELTA "%.4F%s", S_HENRY, NGRIDY/2, 1e-8f, parallel_l },
[TRC_Q] = {"Q", "%.4f%s", S_DELTA "%.3f%s", "", 0, 10.0f, qualityfactor },
[TRC_Rser] = {"Rser", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21series_r },
[TRC_Xser] = {"Xser", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21series_x },
[TRC_Zser] = {"|Zser|", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21series_z },
[TRC_Rsh] = {"Rsh", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21shunt_r },
[TRC_Xsh] = {"Xsh", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21shunt_x },
[TRC_Zsh] = {"|Zsh|", "%.3F%s", S_DELTA "%.3F%s", S_OHM, NGRIDY/2, 100.0f, s21shunt_z },
[TRC_Qs21] = {"Q", "%.4f%s", S_DELTA "%.3f%s", "", 0, 10.0f, s21_qualityfactor },
};
const marker_info_t marker_info_list[MS_END] = {
// Type name format get real get imag
[MS_LIN] = {"LIN", "%.2f %+.1f" S_DEGREE, linear, phase },
[MS_LOG] = {"LOG", "%.1f" S_dB " %+.1f" S_DEGREE, logmag, phase },
[MS_REIM] = {"Re + Im", "%F%+jF", real, imag },
[MS_RX] = {"R + jX", "%F%+jF" S_OHM, resistance, reactance },
[MS_RLC] = {"R + L/C", "%F" S_OHM " %F%c", resistance, reactance }, // use LC calc for imag
[MS_GB] = {"G + jB", "%F%+jF" S_SIEMENS, conductance, susceptance },
[MS_GLC] = {"G + L/C", "%F" S_SIEMENS " %F%c", conductance, parallel_x }, // use LC calc for imag
[MS_RpXp] = {"Rp + jXp", "%F%+jF" S_OHM, parallel_r, parallel_x },
[MS_RpLC] = {"Rp + L/C", "%F" S_OHM " %F%c", parallel_r, parallel_x }, // use LC calc for imag
[MS_SHUNT_RX] = {"R+jX SHUNT", "%F%+jF" S_OHM, s21shunt_r, s21shunt_x },
[MS_SHUNT_RLC] = {"R+L/C SH..", "%F" S_OHM " %F%c", s21shunt_r, s21shunt_x }, // use LC calc for imag
[MS_SERIES_RX] = {"R+jX SERIES", "%F%+jF" S_OHM, s21series_r, s21series_x },
[MS_SERIES_RLC]= {"R+L/C SER..", "%F" S_OHM " %F%c", s21series_r, s21series_x }, // use LC calc for imag
};
const char *get_trace_typename(int t, int marker_smith_format)
{
if (t == TRC_SMITH && ADMIT_MARKER_VALUE(marker_smith_format)) return "ADMIT";
return trace_info_list[t].name;
}
const char *get_smith_format_names(int m)
{
return marker_info_list[m].name;
}
static void mark_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
x1/= CELLWIDTH; x2/= CELLWIDTH;
y1/= CELLHEIGHT; y2/= CELLHEIGHT;
if (x1 == x2 && y1 == y2) {
markmap[y1]|= 1 << x1;
return;
}
if (x1 > x2) SWAP(uint16_t, x1, x2);
uint32_t mask = ((1 << (x2 - x1 + 1)) - 1) << x1;
if (y1 > y2) SWAP(uint16_t, y1, y2);
for (; y1 <= y2; y1++)
markmap[y1]|= mask;
}
static void mark_set_index(index_t *index, uint16_t i, uint16_t x, uint16_t y) {
static uint16_t diff;
static index_t last_erase;
diff = (diff<<1);
if (index[i].x != x || index[i].y != y) diff|= 1;
if ((diff & 3) && i > 0) { // one of points for trace line change (only for > 0 index)
mark_line(last_erase.x, last_erase.y, index[i].x, index[i].y); // mark old line for erase
mark_line(index[i-1].x, index[i-1].y, x, y); // mark new line for draw
}
last_erase = index[i];
index[i].x = x;
index[i].y = y;
}
// Calculate and cache point coordinates for trace
static void
trace_into_index(int t) {
uint16_t start = 0, stop = sweep_points - 1, i;
float *array = &measured[trace[t].channel][0][0];
index_t *index = trace_index[t];
uint32_t type = 1<<trace[t].type;
get_value_cb_t c = trace_info_list[trace[t].type].get_value_cb; // Get callback for value calculation
float refpos = HEIGHT - (get_trace_refpos(t))*GRIDY + 0.5f; // 0.5 for pixel align
float scale = get_trace_scale(t);
if (type & RECTANGULAR_GRID_MASK) { // Run build for rect grid
const float dscale = GRIDY / scale;
if (type & (1<<TRC_SWR)) // For SWR need shift value by 1.0 down
refpos+= dscale;
uint32_t dx = ((WIDTH)<<16) / (sweep_points-1), x = (CELLOFFSETX<<16) + dx * start + 0x8000;
int32_t y;
for (i = start; i <= stop; i++, x+= dx) {
float v = 0;
if (c) v = c(i, &array[2*i]); // Get value
if (v == INFINITY) {
y = 0;
} else {
y = refpos - v * dscale;
if (y < 0) y = 0;
else if (y > HEIGHT) y = HEIGHT;
}
mark_set_index(index, i, (uint16_t)(x>>16), y);
}
return;
}
// Smith/Polar grid
if (type & ROUND_GRID_MASK) { // Need custom calculations
const float rscale = P_RADIUS / scale;
int16_t y, x;
for (i = start; i <= stop; i++){
cartesian_scale(&array[2*i], &x, &y, rscale);
mark_set_index(index, i, x, y);
}
return;
}
}
static void
format_smith_value(int xpos, int ypos, const float *coeff, uint16_t idx, uint16_t m)
{
char value = 0;
if (m >= MS_END) return;
get_value_cb_t re = marker_info_list[m].get_re_cb;
get_value_cb_t im = marker_info_list[m].get_im_cb;
const char *format = marker_info_list[m].format;
float zr = re(idx, coeff);
float zi = im(idx, coeff);
// Additional convert to L or C from zi for LC markers
if (LC_MARKER_VALUE(m)) {
float w = get_w(idx);
if (zi < 0) {zi =-1.0f / (w * zi); value = S_FARAD[0];} // Capacity
else {zi = zi / (w ); value = S_HENRY[0];} // Inductive
}
cell_printf(xpos, ypos, format, zr, zi, value);
}
static void
trace_print_value_string(int xpos, int ypos, int t, int index, int index_ref)
{
// Check correct input
uint8_t type = trace[t].type;
if (type >= MAX_TRACE_TYPE) return;
float (*array)[2] = measured[trace[t].channel];
float *coeff = array[index];
const char *format = index_ref >= 0 ? trace_info_list[type].dformat : trace_info_list[type].format; // Format string
get_value_cb_t c = trace_info_list[type].get_value_cb;
if (c){ // Run standard get value function from table
float v = c(index, coeff); // Get value
if (index_ref >= 0 && v != INFINITY) v-=c(index, array[index_ref]); // Calculate delta value
cell_printf(xpos, ypos, format, v, trace_info_list[type].symbol);
}
else { // Need custom marker format for SMITH / POLAR
format_smith_value(xpos, ypos, coeff, index, type == TRC_SMITH ? trace[t].smith_format : MS_REIM);
}
}
static int
trace_print_info(int xpos, int ypos, int t)
{
float scale = get_trace_scale(t);
const char *format;
int type = trace[t].type;
int smith = trace[t].smith_format;
const char *v = trace_info_list[trace[t].type].symbol;
switch (type) {
case TRC_SMITH:
case TRC_POLAR: format = (scale != 1.0f) ? "%s %0.1fFS" : "%s "; break;
default: format = "%s %F%s/"; break;
}
return cell_printf(xpos, ypos, format, get_trace_typename(type, smith), scale, v);
}
static float time_of_index(int idx)
{
freq_t span = get_sweep_frequency(ST_SPAN);
return (idx * (sweep_points-1)) / ((float)FFT_SIZE * span);
}
static float distance_of_index(int idx) {
return velocity_factor * (SPEED_OF_LIGHT / 200.0f) * time_of_index(idx);
}
static inline void clear_markmap(void) {
int n = MAX_MARKMAP_Y - 1;
do {markmap[n] = (map_t)0;} while(n--);
}
/*
* Force full screen update
*/
static inline void force_set_markmap(void) {
int n = MAX_MARKMAP_Y - 1;
do {markmap[n] = (map_t)-1;} while(n--);
}
/*
* Force region of screen update
*/
static void invalidate_rect_func(int x0, int y0, int x1, int y1) {
uint32_t mask = ((1 << (x1 - x0 + 1)) - 1) << x0;
for (; y0 <= y1; y0++)
if ((uint32_t)y0 < MAX_MARKMAP_Y)
markmap[y0]|= mask;
}
#define invalidate_rect(x0, y0, x1, y1) invalidate_rect_func((x0)/CELLWIDTH, (y0)/CELLHEIGHT, (x1)/CELLWIDTH, (y1)/CELLHEIGHT)
#if STORED_TRACES > 0
static uint8_t enabled_store_trace = 0;
void toogleStoredTrace(int idx) {
uint8_t mask = 1<<idx;
if (enabled_store_trace & mask) {
enabled_store_trace&= ~mask;
request_to_redraw(REDRAW_AREA);
return;
}
if (current_trace == TRACE_INVALID) return;
memcpy(trace_index[TRACES_MAX + idx], trace_index[current_trace], sizeof(trace_index[0]));
enabled_store_trace|= mask;
}
uint8_t getStoredTraces(void) {
return enabled_store_trace;
}
static bool needProcessTrace(uint16_t idx) {
if (idx < TRACES_MAX)
return trace[idx].enabled;
else if (idx < TRACE_INDEX_COUNT)
return enabled_store_trace & (1<<(idx-TRACES_MAX));
return false;
}
#else
#define enabled_store_trace 0
static bool needProcessTrace(uint16_t idx) {
return trace[idx].enabled;
}
#endif
void set_area_size(uint16_t w, uint16_t h) {
area_width = w;
area_height = h;
}
// Calculate marker area size depend from trace/marker count and options
static int marker_area_max(void) {
int t_count = 0, m_count = 0, i;
for (i = 0; i < TRACES_MAX; i++) if (trace[i].enabled) t_count++;
for (i = 0; i < MARKERS_MAX; i++) if (markers[i].enabled) m_count++;
int cnt = t_count > m_count ? t_count : m_count;
int extra = 0;
if (get_electrical_delay() != 0.0f) extra+= 2;
if (s21_offset != 0.0f) extra+= 2;
#ifdef __VNA_Z_RENORMALIZATION__
if (current_props._portz != 50.0f) extra+= 2;
#endif
if (extra < 2) extra = 2;
cnt = (cnt + extra + 1)>>1;
return cnt * FONT_STR_HEIGHT;
}
static inline void
markmap_upperarea(void) {
// Hardcoded, Text info from upper area
invalidate_rect(0, 0, AREA_WIDTH_NORMAL, marker_area_max());
}
#ifdef __VNA_FAST_LINES__
// Little faster on easy traces, 2x faster if need lot of clipping and draw long lines
#include "vna_modules/vna_lines.c"
#else
// Little slower on easy traces, but slow if need lot of clip and draw long lines
static inline void
cell_drawline(int x0, int y0, int x1, int y1, pixel_t c)
{
if (x0 < 0 && x1 < 0) return;
if (y0 < 0 && y1 < 0) return;
if (x0 >= CELLWIDTH && x1 >= CELLWIDTH) return;
if (y0 >= CELLHEIGHT && y1 >= CELLHEIGHT) return;
// Modified Bresenham's line algorithm, see https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
// Draw from top to bottom (most graph contain vertical lines)
if (y1 < y0) { SWAP(int, x0, x1); SWAP(int, y0, y1); }
int dx = (x0 - x1), sx = 1; if (dx > 0) { dx = -dx; sx = -sx; }
int dy = (y1 - y0);
int err = ((dy + dx) < 0 ? -dx : -dy) / 2;
// Fast skip points while y0 < 0
if (y0 < 0) {
while(1){
int e2 = err;
if (e2 > dx) { err-= dy; x0+=sx;}
if (e2 < dy) { err-= dx; y0++; if (y0 == 0) break;}
}
}
// align y by CELLWIDTH for faster calculations
y0*=CELLWIDTH;
y1*=CELLWIDTH;
while (1) {
if ((uint32_t)x0 < CELLWIDTH)
cell_buffer[y0 + x0] = c;
if (x0 + y0 == y1 + x1)
return;
int e2 = err;
if (e2 > dx) { err-= dy; x0+=sx;}
if (e2 < dy) { err-= dx; y0+=CELLWIDTH; if (y0>=CELLHEIGHT*CELLWIDTH) return;} // stop after cell bottom
}
}
#endif
// Give a little speedup then draw rectangular plot
// Write more difficult algorithm for search indexes not give speedup
static int search_index_range_x(int x1, int x2, index_t *index, int *i0, int *i1) {
int i;
int head = 0, tail = sweep_points;
// Search index point in cell
while (1) {
i = (head + tail)>>1;
if (index[i].x >= x2) { // index after cell
if (tail == i)
return false;
tail = i;
}
else if (index[i].x < x1) { // index before cell
if (head == i)
return false;
head = i;
}
else // index in cell (x =< idx_x < cell_end)
break;
}
*i0 = *i1 = i;
while (*i0 > 0 && x1 <= index[--*i0].x); // Search index left from point
while (*i1 < sweep_points-1 && x2 > index[++*i1].x); // Search index right from point
return TRUE;
}
static void
cell_blit_bitmap(int16_t x, int16_t y, uint16_t w, uint16_t h, const uint8_t *bmp)
{
if (x <= -w)
return;
int c = h + y;
if (c < 0) return;
if (c >= CELLHEIGHT) c = CELLHEIGHT; // clip bottom if need
if (y < 0) {bmp-= y*((w+7)>>3); y = 0;} // Clip top if need
for (uint8_t bits = 0; y < c; y++) {
for (int r = 0; r < w; r++, bits<<=1) {
if ((r&7)==0) bits = *bmp++;
if ((0x80 & bits) == 0) continue; // no pixel
if ((uint32_t)(x+r) >= CELLWIDTH ) continue; // x+r < 0 || x+r >= CELLWIDTH
cell_buffer[y*CELLWIDTH + x + r] = foreground_color;
}
}
}
#ifdef _USE_SHADOW_TEXT_
static void
cell_blit_bitmap_shadow(int16_t x, int16_t y, uint16_t w, uint16_t h, const uint8_t *bmp) {
int i;
if (x + w < 0 || h + y < 0) // Clipping
return;
// Prepare shadow bitmap
uint16_t dst[16];
uint16_t p0 = 0, p1 = 0, c = 16 - w;
uint16_t mask = (0xFFFF>>c)<<c;
if (h > ARRAY_COUNT(dst) - 2) h = ARRAY_COUNT(dst) - 2;
for (i = 0; i < h; i++) {
#if 1
c = (bmp[i]<<8) & mask; // extend from 8 bit width to 16 bit
#else
c = (((bmp[2*i]<<8)|bmp[2*i+1]) & mask); // extend from 16 bit width to 16 bit
#endif
c|= (c>>1) | (c>>2); // shadow horizontally
c = (c>>8) | (c<<8); // swap bytes (render do by 8 bit)
dst[i] = c | p0 | p1; // shadow vertically
p0 = p1; p1 = c; // shift data
}
dst[i ] = p0 | p1;
dst[i+1] = p1;
// Render shadow on cell
pixel_t t = foreground_color; // remember color
lcd_set_foreground(LCD_TXT_SHADOW_COLOR); // set shadow color
w+= 2; h+= 2; // Shadow size > by 2 pixel
cell_blit_bitmap(x-1, y-1, w < 9 ? 9 : w, h, (uint8_t *)dst);
foreground_color = t; // restore color
}
#endif
typedef struct {
const void *vmt;
int16_t x;
int16_t y;
} cellPrintStream;
static void put_normal(cellPrintStream *ps, uint8_t ch) {
uint16_t w = FONT_GET_WIDTH(ch);
#ifdef _USE_SHADOW_TEXT_
cell_blit_bitmap_shadow(ps->x, ps->y, w, FONT_GET_HEIGHT, FONT_GET_DATA(ch));
#endif
#if _USE_FONT_ < 3
cell_blit_bitmap(ps->x, ps->y, w, FONT_GET_HEIGHT, FONT_GET_DATA(ch));
#else
cell_blit_bitmap(ps->x, ps->y, w < 9 ? 9 : w, FONT_GET_HEIGHT, FONT_GET_DATA(ch));
#endif
ps->x+= w;
}
#if _USE_FONT_ != _USE_SMALL_FONT_
typedef void (*font_put_t)(cellPrintStream *ps, uint8_t ch);
static font_put_t put_char = put_normal;
static void put_small(cellPrintStream *ps, uint8_t ch) {
uint16_t w = sFONT_GET_WIDTH(ch);
#ifdef _USE_SHADOW_TEXT_
cell_blit_bitmap_shadow(ps->x, ps->y, w, sFONT_GET_HEIGHT, sFONT_GET_DATA(ch));
#endif
#if _USE_SMALL_FONT_ < 3
cell_blit_bitmap(ps->x, ps->y, w, sFONT_GET_HEIGHT, sFONT_GET_DATA(ch));
#else
cell_blit_bitmap(ps->x, ps->y, w < 9 ? 9 : w, sFONT_GET_HEIGHT, sFONT_GET_DATA(ch));
#endif
ps->x+= w;
}