1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <unistd.h> |
---|
4 | #include <errno.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <signal.h> |
---|
7 | #include <termios.h> |
---|
8 | #include <sys/ioctl.h> |
---|
9 | |
---|
10 | struct scale { |
---|
11 | float read; |
---|
12 | float result; |
---|
13 | }; |
---|
14 | |
---|
15 | const struct scale *current_scale = NULL; |
---|
16 | const struct scale *voltage_scale = NULL; |
---|
17 | float current_r = 0; |
---|
18 | |
---|
19 | const struct scale current_scale_1 = {3851, 0.5367}; |
---|
20 | const float current_r_1 = 0.2; |
---|
21 | const struct scale current_scale_2 = {3651, 0.9761}; |
---|
22 | const float current_r_2 = 0.1; |
---|
23 | // const float current_r_3 = 0.05; |
---|
24 | const struct scale voltage_scale_1 = {4041.5, 5.996}; |
---|
25 | const struct scale voltage_scale_2 = {4063.5, 13.547}; |
---|
26 | const struct scale voltage_scale_3 = {4038, 23.672}; |
---|
27 | |
---|
28 | volatile int quit; |
---|
29 | const char *prog; |
---|
30 | |
---|
31 | static void |
---|
32 | set_quit(int s) |
---|
33 | { |
---|
34 | quit = 1; |
---|
35 | } |
---|
36 | |
---|
37 | static void |
---|
38 | usage() |
---|
39 | { |
---|
40 | errx(1, "usage: %s -c [1|2] -v [1|2|3] <device>", prog); |
---|
41 | } |
---|
42 | int |
---|
43 | main(int argc, char * const argv[]) |
---|
44 | { |
---|
45 | int d; |
---|
46 | int bytes; |
---|
47 | int nfds; |
---|
48 | char buf[256]; |
---|
49 | int i; |
---|
50 | fd_set rfds; |
---|
51 | char gpio[2], current[4], tension[4]; |
---|
52 | char *endp; |
---|
53 | int currenti, tensioni; |
---|
54 | float current_a, tension_v; |
---|
55 | int val; |
---|
56 | extern char *optarg; |
---|
57 | extern int optind; |
---|
58 | int ch; |
---|
59 | enum { |
---|
60 | INIT, |
---|
61 | GPIO, |
---|
62 | CURRENT, |
---|
63 | TENSION, |
---|
64 | MARK |
---|
65 | } state; |
---|
66 | |
---|
67 | struct termios t, ot; |
---|
68 | |
---|
69 | prog = argv[0]; |
---|
70 | state = INIT; |
---|
71 | currenti = tensioni = 0; |
---|
72 | |
---|
73 | while ((ch = getopt(argc, argv, "c:v:")) != -1) { |
---|
74 | switch (ch) { |
---|
75 | case 'c': |
---|
76 | val = atoi(optarg); |
---|
77 | if (val == 1) { |
---|
78 | current_scale = ¤t_scale_1; |
---|
79 | current_r = current_r_1; |
---|
80 | } else if (val == 2) { |
---|
81 | current_scale = ¤t_scale_2; |
---|
82 | current_r = current_r_2; |
---|
83 | } |
---|
84 | break; |
---|
85 | case 'v': |
---|
86 | val = atoi(optarg); |
---|
87 | if (val == 1) { |
---|
88 | voltage_scale = &voltage_scale_1; |
---|
89 | } else if (val == 2) { |
---|
90 | voltage_scale = &voltage_scale_2; |
---|
91 | } else if (val == 3) { |
---|
92 | voltage_scale = &voltage_scale_3; |
---|
93 | } |
---|
94 | break; |
---|
95 | default: |
---|
96 | usage(); |
---|
97 | } |
---|
98 | } |
---|
99 | argc -= optind; |
---|
100 | argv += optind; |
---|
101 | |
---|
102 | if (argc != 1 || current_scale == NULL || voltage_scale == NULL) |
---|
103 | usage(); |
---|
104 | |
---|
105 | d = open(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK, 0); |
---|
106 | if (d < 0) { |
---|
107 | err(1, "open %s", argv[0]); |
---|
108 | } |
---|
109 | fprintf(stderr, "o\n"); |
---|
110 | |
---|
111 | if (tcgetattr(d, &ot) < 0) { |
---|
112 | err(1, "tcgetattr"); |
---|
113 | } |
---|
114 | fprintf(stderr, "tg\n"); |
---|
115 | |
---|
116 | quit = 0; |
---|
117 | |
---|
118 | if (signal(SIGINT, set_quit) == SIG_ERR) { |
---|
119 | err(1, "signal(SIGINT)"); |
---|
120 | } |
---|
121 | if (signal(SIGQUIT, set_quit) == SIG_ERR) { |
---|
122 | err(1, "signal(SIGQUIT)"); |
---|
123 | } |
---|
124 | if (signal(SIGPIPE, set_quit) == SIG_ERR) { |
---|
125 | err(1, "signal(SIGPIPE)"); |
---|
126 | } |
---|
127 | if (signal(SIGTERM, set_quit) == SIG_ERR) { |
---|
128 | err(1, "signal(SIGTERM)"); |
---|
129 | } |
---|
130 | |
---|
131 | t = ot; |
---|
132 | cfmakeraw(&t); |
---|
133 | t.c_cflag |= CLOCAL; |
---|
134 | t.c_cflag &= ~CRTSCTS; |
---|
135 | cfsetspeed(&t, B921600); |
---|
136 | |
---|
137 | if (tcsetattr(d, TCSANOW | TCSAFLUSH, &t) < 0) { |
---|
138 | err(1, "tcsetattr"); |
---|
139 | } |
---|
140 | fprintf(stderr, "tty ready\n"); |
---|
141 | FD_ZERO(&rfds); |
---|
142 | while (quit == 0) { |
---|
143 | FD_SET(d, &rfds); |
---|
144 | nfds = select(d+1, &rfds, NULL, NULL, NULL); |
---|
145 | if (nfds > 0 && FD_ISSET(d, &rfds)) { |
---|
146 | bytes = read(d, buf, sizeof(buf)); |
---|
147 | if (bytes < 0 && errno != EAGAIN) { |
---|
148 | warn("read"); |
---|
149 | } |
---|
150 | for (i = 0; i < bytes; i++) { |
---|
151 | switch(state) { |
---|
152 | case INIT: |
---|
153 | /* look for an X mark */ |
---|
154 | if (buf[i] == 'X') |
---|
155 | state = GPIO; |
---|
156 | break; |
---|
157 | case GPIO: |
---|
158 | gpio[0] = buf[i]; |
---|
159 | gpio[1] = '\0'; |
---|
160 | val = strtol(gpio, &endp, 16); |
---|
161 | if (*endp != '\0') { |
---|
162 | fprintf(stderr, |
---|
163 | "gpio error: %c\n", |
---|
164 | *endp); |
---|
165 | state = INIT; |
---|
166 | } else { |
---|
167 | state = TENSION; |
---|
168 | tensioni = 0; |
---|
169 | printf("%2d ", val); |
---|
170 | } |
---|
171 | break; |
---|
172 | case CURRENT: |
---|
173 | current[currenti] = buf[i]; |
---|
174 | currenti++; |
---|
175 | if (currenti < 3) |
---|
176 | break; |
---|
177 | current[currenti] = '\0'; |
---|
178 | val = strtol(current, &endp, 16); |
---|
179 | if (*endp != '\0') { |
---|
180 | fprintf(stderr, |
---|
181 | "current error: %c\n", |
---|
182 | *endp); |
---|
183 | state = INIT; |
---|
184 | } else { |
---|
185 | if (val == 4095) { |
---|
186 | fprintf(stderr, |
---|
187 | "current saturation\n" |
---|
188 | ); |
---|
189 | } |
---|
190 | state = MARK; |
---|
191 | current_a = val / current_scale->read * current_scale->result; |
---|
192 | printf("%8f ", current_a); |
---|
193 | /* correct for in our resistor */ |
---|
194 | tension_v -= current_a * current_r; |
---|
195 | printf("%8f ", tension_v); |
---|
196 | printf("%8f\n", current_a * tension_v); |
---|
197 | } |
---|
198 | break; |
---|
199 | case TENSION: |
---|
200 | tension[tensioni] = buf[i]; |
---|
201 | tensioni++; |
---|
202 | if (tensioni < 3) |
---|
203 | break; |
---|
204 | tension[tensioni] = '\0'; |
---|
205 | val = strtol(tension, &endp, 16); |
---|
206 | if (*endp != '\0') { |
---|
207 | fprintf(stderr, |
---|
208 | "tension error: %c\n", |
---|
209 | *endp); |
---|
210 | state = INIT; |
---|
211 | } else { |
---|
212 | if (val == 4095) { |
---|
213 | fprintf(stderr, |
---|
214 | "tension saturation\n" |
---|
215 | ); |
---|
216 | } |
---|
217 | state = CURRENT; |
---|
218 | currenti = 0; |
---|
219 | tension_v = val / voltage_scale->read * voltage_scale->result; |
---|
220 | } |
---|
221 | break; |
---|
222 | case MARK: |
---|
223 | if (buf[i] != 'X') { |
---|
224 | fprintf(stderr, |
---|
225 | "mark error: %c\n", |
---|
226 | buf[i]); |
---|
227 | state = INIT; |
---|
228 | } else { |
---|
229 | state = GPIO; |
---|
230 | } |
---|
231 | break; |
---|
232 | } |
---|
233 | } |
---|
234 | } else if (nfds < 0) { |
---|
235 | warn("select"); |
---|
236 | } |
---|
237 | } |
---|
238 | if (tcsetattr(d, TCSANOW, &ot) < 0) { |
---|
239 | err(1, "restore tcsetattr"); |
---|
240 | } |
---|
241 | exit(0); |
---|
242 | } |
---|
243 | |
---|
244 | |
---|