Changeset 12
- Timestamp:
- Jul 31, 2019, 5:44:55 PM (5 years ago)
- Location:
- trunk/software/tty
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/tty/readtty_power.c
r1 r12 2 2 #include <stdlib.h> 3 3 #include <unistd.h> 4 #include <string.h> 4 5 #include <errno.h> 6 #include <err.h> 5 7 #include <fcntl.h> 6 8 #include <signal.h> 7 9 #include <termios.h> 8 10 #include <sys/ioctl.h> 11 #include <sys/select.h> 9 12 10 13 struct scale { … … 13 16 }; 14 17 15 const struct scale *current_scale = NULL;16 const struct scale *voltage_scale = NULL;18 struct scale current_scale; 19 struct scale voltage_scale; 17 20 float current_r = 0; 18 21 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}; 22 const float current_ra[] = {0.2, 0.1, 0.05, 0.02}; 27 23 28 24 volatile int quit; … … 38 34 usage() 39 35 { 40 errx(1, "usage: %s -c [1|2] -v [1|2|3] <device>", prog); 41 } 36 errx(1, "usage: %s -c[1|2|3|4] -v[1|2|3] [-n] <device>", prog); 37 } 38 39 static void 40 get_cal_data(const char *b, int ci, int vi) 41 { 42 int cr[4], vr[3]; 43 float ca[4], vv[3]; 44 int ret; 45 46 ret = sscanf(b, "cal_data %d %f %d %f %d %f %d %f %d %f %d %f %d %f", 47 &cr[0], &ca[0], &cr[1], &ca[1], &cr[2], &ca[2], &cr[3], &ca[3], 48 &vr[0], &vv[0], &vr[1], &vv[1], &vr[2], &vv[2]); 49 if (ret != 14) { 50 errx(1, "cal_data: converted only %d\n", ret); 51 } 52 for (int i = 0; i < 4; i++) { 53 printf("cr %d ca %f\n", cr[i], ca[i]); 54 } 55 for (int i = 0; i < 3; i++) { 56 printf("vr %d vv %f\n", vr[i], vv[i]); 57 } 58 current_scale.read = cr[ci - 1]; 59 current_scale.result = ca[ci - 1]; 60 voltage_scale.read = vr[vi - 1]; 61 voltage_scale.result = vv[vi - 1]; 62 } 63 64 static void 65 readline(int d, char *buf, int l) 66 { 67 int i; 68 int bytes; 69 do { 70 for (i = 0; i < l; i++) { 71 do { 72 bytes = read(d, &buf[i], 1); 73 if (quit) 74 return; 75 } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r'); 76 if (bytes != 1) { 77 fprintf(stderr, "readline: read %d\n", bytes); 78 err(1, "read line"); 79 } 80 if (buf[i] == '\n') 81 break; 82 } 83 } while (i == 0); 84 if (buf[i] != '\n') { 85 warnx("realine: end of line not found"); 86 } 87 buf[i] = '\0'; 88 } 89 42 90 int 43 91 main(int argc, char * const argv[]) … … 46 94 int bytes; 47 95 int nfds; 96 int lineno = 0; 97 int nopt = 0; 48 98 char buf[256]; 49 99 int i; 50 100 fd_set rfds; 51 char gpio[ 2], current[4], tension[4];101 char gpio[3], current[4], tension[4]; 52 102 char *endp; 53 int currenti, tensioni;103 int gpioi, currenti, tensioni; 54 104 float current_a, tension_v; 55 int val; 105 int val, gpio_val; 106 int c_opt = 0, v_opt = 0; 56 107 extern char *optarg; 57 108 extern int optind; … … 69 120 prog = argv[0]; 70 121 state = INIT; 71 currenti = tensioni = 0;72 73 while ((ch = getopt(argc, argv, " c:v:")) != -1) {122 gpioi = currenti = tensioni = 0; 123 124 while ((ch = getopt(argc, argv, "np:c:v:")) != -1) { 74 125 switch (ch) { 126 case 'n': 127 nopt = 1; 128 break; 75 129 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 } 130 c_opt = atoi(optarg); 131 if (c_opt < 1 || c_opt > 4) 132 usage(); 133 current_r = current_ra[c_opt -1]; 84 134 break; 85 135 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 } 136 v_opt = atoi(optarg); 137 if (v_opt < 1 || v_opt > 3) 138 usage(); 94 139 break; 95 140 default: … … 100 145 argv += optind; 101 146 102 if (argc != 1 || c urrent_scale == NULL || voltage_scale == NULL)147 if (argc != 1 || c_opt == 0 || v_opt == 0) 103 148 usage(); 104 149 … … 139 184 } 140 185 fprintf(stderr, "tty ready\n"); 186 do { 187 write(d, "M0\n", 3); 188 readline(d, buf, sizeof(buf)); 189 if (quit) 190 goto quit; 191 } while (memcmp(buf, "OK", 2) != 0); 192 write(d, "C\n", 2); 193 readline(d, buf, sizeof(buf)); 194 if (quit) 195 goto quit; 196 printf("cal_data: %s\n", buf); 197 get_cal_data(buf, c_opt, v_opt); 198 printf("cal_data selected: %f %f, %f %f\n", 199 current_scale.read, current_scale.result, 200 voltage_scale.read, voltage_scale.result); 201 202 /* start measures */ 203 write(d, "M1\n", 3); 141 204 FD_ZERO(&rfds); 142 205 while (quit == 0) { … … 152 215 case INIT: 153 216 /* look for an X mark */ 154 if (buf[i] == 'X') 217 if (buf[i] == 'X') { 155 218 state = GPIO; 219 gpioi = 0; 220 } 156 221 break; 157 222 case GPIO: 158 gpio[0] = buf[i]; 159 gpio[1] = '\0'; 160 val = strtol(gpio, &endp, 16); 223 gpio[gpioi] = buf[i]; 224 gpioi++; 225 if (gpioi < 2) 226 break; 227 gpio[gpioi] = '\0'; 228 gpio_val = strtol(gpio, &endp, 16); 161 229 if (*endp != '\0') { 162 230 fprintf(stderr, … … 167 235 state = TENSION; 168 236 tensioni = 0; 169 printf("%2d ", val);170 237 } 171 238 break; … … 189 256 } 190 257 state = MARK; 191 current_a = val / current_scale->read * current_scale->result; 192 printf("%8f ", current_a); 258 current_a = val / current_scale.read * current_scale.result; 193 259 /* correct for in our resistor */ 194 260 tension_v -= current_a * current_r; 195 printf("%8f ", tension_v); 196 printf("%8f\n", current_a * tension_v); 261 if (nopt) { 262 printf("%8f ", (float)lineno / 5000); 263 lineno++; 264 } 265 printf("%3d %8f %8f %8f\n", 266 gpio_val, 267 current_a, 268 tension_v, 269 current_a * tension_v); 197 270 } 198 271 break; … … 217 290 state = CURRENT; 218 291 currenti = 0; 219 tension_v = val / voltage_scale ->read * voltage_scale->result;292 tension_v = val / voltage_scale.read * voltage_scale.result; 220 293 } 221 294 break; … … 228 301 } else { 229 302 state = GPIO; 303 gpioi = 0; 230 304 } 231 305 break; … … 236 310 } 237 311 } 312 quit: 313 /* stop measures */ 314 write(d, "M0\n", 3); 315 if (fflush(stdout) == EOF) { 316 warn("fflush"); 317 } 238 318 if (tcsetattr(d, TCSANOW, &ot) < 0) { 239 319 err(1, "restore tcsetattr"); … … 241 321 exit(0); 242 322 } 243 244 -
trunk/software/tty/readtty_timer.c
r1 r12 3 3 #include <unistd.h> 4 4 #include <errno.h> 5 #include <err.h> 5 6 #include <fcntl.h> 6 7 #include <signal.h> 8 #include <string.h> 7 9 #include <termios.h> 8 10 #include <sys/ioctl.h> 11 #include <sys/select.h> 9 12 10 13 volatile int quit; … … 14 17 { 15 18 quit = 1; 19 } 20 21 static void 22 readline(int d, char *buf, int l) 23 { 24 int i; 25 int bytes; 26 do { 27 for (i = 0; i < l; i++) { 28 do { 29 bytes = read(d, &buf[i], 1); 30 if (quit) 31 return; 32 } while ((bytes < 0 && errno == EAGAIN) || buf[i] == '\r'); 33 if (bytes != 1) { 34 fprintf(stderr, "readline: read %d\n", bytes); 35 err(1, "read line"); 36 } 37 if (buf[i] == '\n') 38 break; 39 } 40 } while (i == 0); 41 if (buf[i] != '\n') { 42 errx(1, "realine: end of line not found"); 43 } 44 buf[i] = '\0'; 16 45 } 17 46 … … 25 54 int i; 26 55 fd_set rfds; 27 char gpio[ 2], current[4], tension[4];56 char gpio[3], current[4], tension[4]; 28 57 char *endp; 29 int currenti, tensioni; 30 int val; 58 int gpioi, currenti, tensioni; 59 int gpioval, currentval, tensionval; 60 int exitst = 0; 31 61 32 62 enum { … … 41 71 42 72 state = INIT; 43 currenti = tensioni = 0;73 gpioi = currenti = tensioni = 0; 44 74 45 75 if (argc != 3) { … … 87 117 } 88 118 fprintf(stderr, "tty ready\n"); 119 write(d, "M0\n", 3); 120 write(d, "M0\n", 3); 121 do { 122 readline(d, buf, sizeof(buf)); 123 if (quit) 124 goto quit; 125 } while (memcmp(buf, "OK", 2) != 0); 126 fprintf(stderr, "starting\n"); 127 write(d, "M1\n", 3); 128 89 129 alarm(atoi(argv[2])); 90 130 FD_ZERO(&rfds); … … 94 134 if (nfds > 0 && FD_ISSET(d, &rfds)) { 95 135 bytes = read(d, buf, sizeof(buf)); 136 if (bytes > sizeof(buf)) { 137 errx(1, "read %d/0x%x bytes", bytes, bytes); 138 } 96 139 if (bytes < 0 && errno != EAGAIN) { 97 140 warn("read"); 141 continue; 98 142 } 99 143 for (i = 0; i < bytes; i++) { 144 if (i > sizeof(buf)) 145 abort(); 146 if (bytes > sizeof(buf)) 147 abort(); 148 if (gpioi > sizeof(buf)) 149 abort(); 150 if (tensioni > sizeof(buf)) 151 abort(); 152 if (currenti > sizeof(buf)) 153 abort(); 100 154 switch(state) { 101 155 case INIT: 102 156 /* look for an X mark */ 103 if (buf[i] == 'X') 157 if (buf[i] == 'X') { 104 158 state = GPIO; 159 gpioi = 0; 160 } 105 161 break; 106 162 case GPIO: 107 gpio[0] = buf[i]; 108 gpio[1] = '\0'; 109 val = strtol(gpio, &endp, 16); 163 gpio[gpioi] = buf[i]; 164 gpioi++; 165 if (gpioi < 2) 166 break; 167 gpio[gpioi] = '\0'; 168 gpioval = strtol(gpio, &endp, 16); 110 169 if (*endp != '\0') { 111 170 fprintf(stderr, 112 171 "gpio error: %c\n", 113 172 *endp); 114 state = INIT;115 } else {116 state = CURRENT;117 currenti = 0;118 printf("%2d ", val);173 exitst = 1; 174 state = INIT; 175 } else { 176 state = TENSION; 177 tensioni = 0; 119 178 } 120 179 break; … … 125 184 break; 126 185 current[currenti] = '\0'; 127 val = strtol(current, &endp, 16);186 currentval = strtol(current, &endp, 16); 128 187 if (*endp != '\0') { 129 188 fprintf(stderr, 130 189 "current error: %c\n", 131 190 *endp); 132 state = INIT; 133 } else { 134 state = TENSION; 191 exitst = 1; 192 state = INIT; 193 } else { 194 state = MARK; 135 195 tensioni = 0; 136 printf("%4d ", val);137 196 } 138 197 break; … … 143 202 break; 144 203 tension[tensioni] = '\0'; 145 val = strtol(tension, &endp, 16);204 tensionval = strtol(tension, &endp, 16); 146 205 if (*endp != '\0') { 147 206 fprintf(stderr, 148 207 "tension error: %c\n", 149 208 *endp); 150 state = INIT; 151 } else { 152 state = MARK; 153 printf("%4d\n", val); 209 exitst = 1; 210 state = INIT; 211 } else { 212 state = CURRENT; 213 currenti = 0; 154 214 } 155 215 break; … … 159 219 "mark error: %c\n", 160 220 buf[i]); 161 state = INIT; 162 } else { 221 exitst = 1; 222 state = INIT; 223 } else { 224 printf("%3d ", gpioval); 225 printf("%4d ", tensionval); 226 printf("%4d\n", currentval); 163 227 state = GPIO; 228 gpioi = 0; 164 229 } 165 230 break; … … 170 235 } 171 236 } 237 quit: 238 /* stop measures */ 239 write(d, "M0\n", 3); 240 if (fflush(stdout) == EOF) { 241 warn("fflush"); 242 } 243 172 244 if (tcsetattr(d, TCSANOW, &ot) < 0) { 173 245 err(1, "restore tcsetattr"); 174 246 } 175 exit( 0);247 exit(exitst); 176 248 } 177 249
Note: See TracChangeset
for help on using the changeset viewer.