📄 cal_ui.c
字号:
** CalPt_t *newpts = Array of points from the user** Returns: Nothing (void)**\*******************************************************************************/static voidnxcal_normalizePts(int npts, CalPt_t * ctrlpts, CalPt_t * newpts){ AxisMode_t eAxMode = amNONE; /* No axis rotation/flipping required */ CalPt_t tmppt1, /* Temporary point */ tmppt2; /* Tmp pt #2 */ /* ** Determine the type of axis flipping/rotation (if any) that may be ** required on this device: ** ** Map of the pts on the device: ** ** +-------------+ ** |0 | ** | 1 | ** | | ** | | ** | 4 | ** | | ** | | ** | 2 | ** | 3| ** +-------------+ */ /* Adjust the array based on type of rotation */ switch (eAxMode) { case amXFLIP: break; case amYFLIP: break; case amXYFLIP: break; } /* end of switch */ return;} /* end of nxcal_normalizePts() */#endif/*******************************************************************************\**** Function: int nxcal_setStoredPts()** Desc: Stores the current calibrated points into the file specified by** the calfile parameter** Accepts: int npts = The number of pts stored in pts array** CalPt_t *cpts = The array of control pts to store** CalPt_t *dpts = The array of data pts to store** char *calfile = File to write data out to** Returns: int; 0 on successful write of data, -1 otherwise**\*******************************************************************************/static intnxcal_setStoredPts(int npts, CalPt_t * cpts, CalPt_t * dpts, char *calfile){ int i, /* Loop iterator */ retval = -1; /* Return value */ FILE *outfp; /* Output file */ if (calfile == NULL || (outfp = fopen(calfile, "w")) == NULL) return (retval); for (i = 0; i < npts; i++) { char buf[20 + 1]; /* Data */ int dta_len; /* Length of data */ /* Get the length if the data */ snprintf(buf, sizeof(buf), "C%d,%dD%d,%d", cpts[i].x, cpts[i].y, dpts[i].x, dpts[i].y); dta_len = strlen(buf); if (fprintf(outfp, "%s\n", buf) != dta_len + 1) break; } /* end of for */ if (i == npts) retval = 0; fclose(outfp); return (retval);} /* end of nxcal_setStoredPts() *//******************************************************************************\**** Function: int nxcal_ValidatePt()** Desc: Calculates the final points based on opposing points in the** array. Right now, for this initial version, it only recognizes** calibration data with 3 or 5 points.** Accepts: int npts = Number of points in array** CalPt_t *dpts = Array of data points** Returns: int; 0 = Actual point was within calculated range** -1 = Actual point was outside of calculated range**\******************************************************************************/static intnxcal_ValidatePt(int npts, CalPt_t * dpts){ int deltax, /* Change in x */ deltay, /* Change in y */ retval = -1; /* Return value, default to fail */ double fudgex, /* Fudge factor in x */ fudgey; /* Fudge factor in y */ CalPt_t validpt1, /* Validation point #1 */ validpt2; /* Validation point #2 */ /* ** NOTE: This is an initial attempt at calculating a "good calibration", things may need ** to be adjusted depending on how loose/tight the definition of "good calibration" is ** defined. */ switch (npts) { case 3: /* Need to check the points to make sure they are not the same value, or around the same value */ deltax = abs(dpts[0].x - dpts[1].x); deltay = abs(dpts[0].y - dpts[1].y); if (deltax < 100 || deltay < 50) break; validpt1.x = (dpts[0].x + dpts[1].x) / 2; validpt1.y = (dpts[0].y + dpts[1].y) / 2; fudgex = abs(validpt1.x - (validpt1.x * 0.85)); fudgey = abs(validpt1.y - (validpt1.y * 0.85)); printf("Sanity check: v1 = (%d,%d) fx = %f fy = %f pt = (%d,%d)\n", validpt1.x, validpt1.y, fudgex, fudgey, dpts[2].x, dpts[2].y); if (abs(dpts[2].x - validpt1.x) <= fudgex && abs(dpts[2].y - validpt1.y) <= fudgey) { retval = 0; } /* end of if */ break; case 5: /* Need to check the points to make sure they are not the same value, or around the same value */ deltax = abs(dpts[0].x - dpts[3].x); deltay = abs(dpts[0].y - dpts[3].y); if (deltax < 100 || deltay < 50) break; /* Check point pairs are indices (0,3) and (1,2) */ validpt1.x = (dpts[0].x + dpts[3].x) / 2; validpt1.y = (dpts[0].y + dpts[3].y) / 2; validpt2.x = (dpts[1].x + dpts[2].x) / 2; validpt2.y = (dpts[1].y + dpts[2].y) / 2; fudgex = abs(((validpt1.x + validpt2.x) / 2) - ((validpt1.x + validpt2.x) / 2) * 0.96); fudgey = abs(((validpt1.y + validpt2.y) / 2) - ((validpt1.y + validpt2.y) / 2) * 0.96); printf ("Sanity Check: v1 = (%d,%d), v2 = (%d,%d), fx=%f, fy=%f, pt=(%d,%d).\n", validpt1.x, validpt1.y, validpt2.x, validpt2.y, fudgex, fudgey, dpts[4].x, dpts[4].y); /* ** 10/29/01 -- JMW ** Modified to be within range of both midpoint values, not just one, to avoid ** a crappy reading on the first point and still succeed */ if ((abs(dpts[4].x - validpt1.x) <= fudgex && abs(dpts[4].y - validpt1.y) <= fudgey) && (abs(dpts[4].x - validpt2.x) <= fudgex && abs(dpts[4].y - validpt2.y) <= fudgey)) {/* printf("Calibration would have succeeded!!!!\n"); */ retval++; }#if 0 /* Compare the values, as long as they are in line with at least one of the checkpoints... */ if (abs(dpts[4].x - validpt1.x) <= fudgex && abs(dpts[4].y - validpt1.y) <= fudgey) retval++; if (abs(dpts[4].x - validpt2.x) <= fudgex && abs(dpts[4].y - validpt2.y) <= fudgey) retval++;#endif break; default: break; } /* end of switch */ printf("returning %d from nxcal_ValidatePt()\n", retval); return ((retval >= 0 ? 0 : -1));} /* end of nxcal_ValidatePt() *//*-----------------------------------------------------------------------------*\**** Extern function definitions**\*-----------------------------------------------------------------------------*//*******************************************************************************\**** Function: int nxcal()** Desc: Starting point of the calibration (i.e. sets up the GUI, controls** interaction with platform specific stuff, etc)** Accepts: int flags = Determines how this function is to operate, where** NXCAL_NONINT_MODE = Non interactive mode, needs to init graphics** and allow reading of coordinate from file** char *calfile = Path to the calibration file to either retreive** data from or to store data into.** Returns: int; 0 on success, -1 on error (errno *should* be set)**\*******************************************************************************/intnxcal(int flags, char *calfile){ int good_cal = 0, /* Good calibration flag */ i = 0, /* Loop iterator */ npts, /* Number of points */ rc, /* return code */ retval = 0; /* Return value */ CalPt_t *ctrl_ptdata = NULL, /* Data for the control points */ draw_pt, /* Draw point */ *new_ptdata; /* Data for the new points (read from device) */ GR_GC_ID gc; /* Filled rectangle */ /* ** Determine if the data is stored in a file, and read it/process it ** without doing the GUI and exit. */ if ((flags & NXCAL_NONINT_MODE) && !nxcal_getStoredPts(&npts, &ctrl_ptdata, &new_ptdata, calfile)) { /* Pixlib call to set the calibration */ printf("Retrieved data from %s, using stored coordinates!\n", calfile); retval = pix_cal_Calibrate(CAL_MAX_CTRL_PTS, ctrl_ptdata, new_ptdata); if (retval == PIXLIB_STUB_VAL) retval = 0; free(ctrl_ptdata); free(new_ptdata); if (npts > 0 && retval == 0) return (retval); } /* end of if */ /* ** Set up window */ if ((retval = nxcal_grInit(flags)) < 0) return (retval); nxcal_drawText("Touch the center of"); nxcal_drawText("each cross to calibrate"); nxcal_drawText("the screen"); /* ** Determine the set of points to process */ if ((rc = pix_cal_GetCtrlPts(&npts, &ctrl_ptdata, si.vs_width, si.vs_height, si.bpp)) == -1) { return (-1); } /* end of if */ else if (rc == PIXLIB_STUB_VAL) { ctrl_ptdata = (CalPt_t *) calloc((npts = 5), sizeof(CalPt_t)); if (ctrl_ptdata == NULL) return (-1); nxcal_getCtrlPts(npts, ctrl_ptdata); } /* end of else if */ /* Allocate memory for the data points */ if ((new_ptdata = (CalPt_t *) calloc(npts, sizeof(CalPt_t))) == NULL) { free(ctrl_ptdata); return (-1); } /* end of if */ /* ** Get individual points based on the control point positions */ while (!good_cal) { for (i = 0; i < npts; i++) { /* ** Draw point */ if (pix_cal_GetDrawPt(&ctrl_ptdata[i], &draw_pt) == -1) { printf("pix_cal_GetDrawPt() failed.\n"); memcpy(&draw_pt, &ctrl_ptdata[i], sizeof(CalPt_t)); } nxcal_drawPt(&draw_pt, 0); /* ** Get point data from touch screen (pixlib call) */ if ((rc = pix_cal_GetDataPt(&new_ptdata[i])) == -1) { /* There is an error, abandon */ retval = -1; break; } /* end of if */ else if (rc == PIXLIB_STUB_VAL) { /* ** This function was "stubbed" out, so wait around for ** a mouse up event. */ int brk_flg = 0; /* Break flag */ GR_EVENT event; /* Event */ GR_EVENT_MASK new_mask; /* New mask */ GR_WINDOW_INFO winf; /* Window info */ /* Get the current event mask and add the button up event to it */ GrGetWindowInfo(cal_win, &winf); new_mask = winf.eventmask | GR_EVENT_MASK_BUTTON_UP; GrSelectEvents(cal_win, new_mask); while (!brk_flg) { GrGetNextEvent(&event); switch (event.type) { case GR_EVENT_TYPE_BUTTON_UP: /* Fill this in, just for the heck of it */ new_ptdata[i].x = event.button.x; new_ptdata[i].y = event.button.y; brk_flg = 1; break; } /* end of switch */ } /* end of while */ /* Restore the original mask */ new_mask = winf.eventmask; GrSelectEvents(cal_win, new_mask); } /* end of else if */ } /* end of for */ /* ** Check the last point against pts (0,3) and (1,2) for consistancy) */ if (nxcal_ValidatePt(npts, new_ptdata) == 0) good_cal = 1; } /* end of for */ if (i >= npts) { /* ** TODO: Handle any rotational differences *//* nxcal_normalizePts(npts, ctrl_ptdata, new_ptdata); */ /* ** Calibrate it (pixlib call) */ retval = pix_cal_Calibrate(npts, ctrl_ptdata, new_ptdata); if (retval == PIXLIB_STUB_VAL) retval = 0; /* ** Store the data into a file */ nxcal_setStoredPts(CAL_MAX_CTRL_PTS, ctrl_ptdata, new_ptdata, calfile); /* ** Close window (and destroy all resources) */ /* Clear the window to the background color */ gc = GrNewGC(); GrSetGCBackground(gc, NXCAL_BACKGROUND); GrSetGCForeground(gc, NXCAL_BACKGROUND); GrSetGCMode(gc, GR_MODE_SET); GrFillRect(cal_win, gc, 1, 1, si.vs_width - 2, si.vs_height - 2); GrDestroyWindow(cal_win); /* Reset some of the static's back to original value */ cal_win = (GR_WINDOW_ID) - 1; g_text_x = START_TEXT_Y; last.x = last.y = -1;#if 0 if (flags & NXCAL_NONINT_MODE) GrClose();#endif } /* end of if */ /* Free the dynamically allocated memory */ if (ctrl_ptdata) free(ctrl_ptdata); if (new_ptdata) free(new_ptdata); return (retval);} /* end of nxcal */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -