-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_atten.c
More file actions
160 lines (140 loc) · 4.59 KB
/
Copy pathexample_atten.c
File metadata and controls
160 lines (140 loc) · 4.59 KB
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
#include "liblinht-ctrl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void sweep(void)
{
printf("\n=== Sweep: 0 -> 31.5 -> 0 dB (0.5 dB steps) ===\n");
for (int s = 0; s <= 63; s++) {
double db = s * 0.5;
printf("[UP] %5.1f dB (%2d/63)\r", db, s);
fflush(stdout);
linht_ctrl_atten_set(1, db);
linht_ctrl_atten_set(2, db);
usleep(1000);
}
sleep(1);
for (int s = 63; s >= 0; s--) {
double db = s * 0.5;
printf("[DN] %5.1f dB (%2d/63)\r", db, s);
fflush(stdout);
linht_ctrl_atten_set(1, db);
linht_ctrl_atten_set(2, db);
usleep(1000);
}
sleep(1);
}
static void interactive(void)
{
printf("\n=== PE4312 Interactive Attenuator Test ===\n");
printf(" 1 <dB> - set CHIP1 (e.g. 1 10.5)\n");
printf(" 2 <dB> - set CHIP2 (e.g. 2 0)\n");
printf(" b <dB> - set BOTH\n");
printf(" s <0|1> - RF switch (0=A, 1=B)\n");
printf(" sweep - run sweep\n");
printf(" q - quit\n");
printf("------------------------------------------\n");
char buf[128];
while (1) {
printf("atten> ");
fflush(stdout);
if (!fgets(buf, sizeof(buf), stdin)) break;
size_t n = strlen(buf);
while (n > 0 && (buf[n-1] == '\n' || buf[n-1] == '\r'))
buf[--n] = '\0';
if (n == 0) continue;
if (buf[0] == 'q' || buf[0] == 'Q') break;
if (strcmp(buf, "sweep") == 0) { sweep(); continue; }
char cmd[16];
double val = 0;
if (sscanf(buf, "%15s %lf", cmd, &val) < 1) continue;
if (cmd[0] == '1' && cmd[1] == '\0')
linht_ctrl_atten_set(1, val);
else if (cmd[0] == '2' && cmd[1] == '\0')
linht_ctrl_atten_set(2, val);
else if (cmd[0] == 'b' && cmd[1] == '\0') {
linht_ctrl_atten_set(1, val);
linht_ctrl_atten_set(2, val);
}
else if (cmd[0] == 's' && cmd[1] == '\0')
linht_ctrl_tx_rx_switch_set((bool)val);
else
printf(" ? try: 1/2/b <dB>, s <0|1>, sweep, q\n");
}
}
static void usage(const char *prog)
{
printf("Usage: %s [options]\n\n", prog);
printf("Options:\n");
printf(" -1 <dB> set CHIP1 attenuation\n");
printf(" -2 <dB> set CHIP2 attenuation\n");
printf(" -b <dB> set both chips\n");
printf(" -s <0|1> RF switch (0=A, 1=B)\n");
printf(" --sweep run sweep and exit\n");
printf(" -h this help\n");
printf("\nNo options: interactive mode.\n");
}
int main(int argc, char **argv)
{
int do_sweep = 0, chip = 0;
double atten = 0;
int sw_path = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
usage(argv[0]);
return 0;
}
if (strcmp(argv[i], "--sweep") == 0) {
do_sweep = 1;
continue;
}
if (strcmp(argv[i], "-1") == 0 || strcmp(argv[i], "-2") == 0 ||
strcmp(argv[i], "-b") == 0) {
if (++i >= argc) {
fprintf(stderr, "ERROR: %s requires a value\n", argv[i-1]);
return 1;
}
char *end;
atten = strtod(argv[i], &end);
if (end == argv[i] || *end != '\0') {
fprintf(stderr, "ERROR: bad number '%s'\n", argv[i]);
return 1;
}
chip = (argv[i-1][1] == '1') ? 1 :
(argv[i-1][1] == '2') ? 2 : 3;
continue;
}
if (strcmp(argv[i], "-s") == 0) {
if (++i >= argc) {
fprintf(stderr, "ERROR: -s requires 0 or 1\n");
return 1;
}
sw_path = atoi(argv[i]);
if (sw_path != 0 && sw_path != 1) {
fprintf(stderr, "ERROR: -s must be 0 or 1\n");
return 1;
}
continue;
}
fprintf(stderr, "ERROR: unknown option '%s'. Try -h.\n", argv[i]);
return 1;
}
if (linht_ctrl_atten_init() < 0) {
fprintf(stderr, "ERROR: attenuator init failed\n");
return 1;
}
linht_ctrl_tx_rx_switch_set((bool)sw_path);
int ret = 0;
if (do_sweep) {
sweep();
} else if (chip) {
if (chip == 1) ret = linht_ctrl_atten_set(1, atten);
else if (chip == 2) ret = linht_ctrl_atten_set(2, atten);
else { linht_ctrl_atten_set(1, atten); linht_ctrl_atten_set(2, atten); }
} else {
interactive();
}
linht_ctrl_atten_cleanup();
return ret;
}