diff --git a/cngt/cngt.c b/cngt/cngt.c index 74dfac9..0011ac6 100644 --- a/cngt/cngt.c +++ b/cngt/cngt.c @@ -25,7 +25,7 @@ static double pos_x[MAX_POS]; static double pos_y[MAX_POS]; static double z0, height; -static double x, y, z; +static double cx, cy, cz; #define UNITS(mm) ((mm)*40.0) @@ -33,20 +33,21 @@ static double x, y, z; static void move(void) { - serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", UNITS(z), UNITS(x), UNITS(y)); + serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", + UNITS(cz), UNITS(cx), UNITS(cy)); } static void up(void) { - z = z0+height; + cz = z0+height; move(); } static void down(void) { - z = z0; + cz = z0; move(); } @@ -62,22 +63,22 @@ static int do_key(char c) break; case 'h': up(); - x -= STEP; + cx -= STEP; move(); break; case 'j': up(); - y -= STEP; + cy -= STEP; move(); break; case 'k': up(); - y += STEP; + cy += STEP; move(); break; case 'l': up(); - x += STEP; + cx += STEP; move(); break; case 'q': @@ -86,14 +87,54 @@ static int do_key(char c) if (c < '0' || c > '9') break; up(); - x = pos_x[c-'0']; - y = pos_y[c-'0']; + cx = pos_x[c-'0']; + cy = pos_y[c-'0']; move(); } return 0; } +static void gp_minmax(const char *name, + double *xa, double *ya, double *xb, double *yb) +{ + FILE *file; + char buf[1024]; + double x, y, z; + int n; + int first = 1; + + file = fopen(name, "r"); + if (!file) { + perror(name); + exit(1); + } + while (fgets(buf, sizeof(buf), file)) { + if (*buf == '#') + continue; + n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z); + switch (n) { + case 3: + /* fall through */ + case 2: + if (first || x < *xa) + *xa = x; + if (first || x > *xb) + *xb = x; + if (first || y < *ya) + *ya = y; + if (first || y > *yb) + *yb = y; + first = 0; + break; + default: + break; + } + } + fclose(file); +} + + static void usage(const char *name) { fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name); @@ -103,27 +144,39 @@ static void usage(const char *name) int main(int argc, char **argv) { + double xa = 0, ya = 0, xb = 0, yb = 0; int i; char c; if (argc < 4) usage(*argv); - if (!(argc & 1)) - usage(*argv); z0 = atof(argv[1]); height = atof(argv[2]); - for (i = 3; i != argc; i += 2) { - pos_x[(i-3)/2] = atof(argv[i]); - pos_y[(i-3)/2] = atof(argv[i+1]); + + if (argc & 1) { + for (i = 3; i != argc; i += 2) { + pos_x[(i-3)/2] = atof(argv[i]); + pos_y[(i-3)/2] = atof(argv[i+1]); + } + } else { + if (argc != 4) + usage(*argv); + gp_minmax(argv[3], &xa, &ya, &xb, &yb); + pos_x[1] = pos_x[4] = pos_x[7] = xa; + pos_x[2] = pos_x[5] = pos_x[8] = (xa+xb)/2; + pos_x[3] = pos_x[6] = pos_x[9] = xb; + pos_y[1] = pos_y[2] = pos_y[3] = ya; + pos_y[4] = pos_y[5] = pos_y[6] = (ya+yb)/2; + pos_y[7] = pos_y[8] = pos_y[9] = yb; } serial_open("/dev/ttyUSB0"); serial_printf("\nIN;!MC0\n"); - x = pos_x[0]; - y = pos_y[0]; - z = z0+height; + cx = pos_x[0]; + cy = pos_y[0]; + cz = z0+height; move(); while ((c = getkey()))