static void get_calibrated_reading(MouseReading *pt) { get_raw_reading(pt); #if GINPUT_MOUSE_NEED_CALIBRATION _tsTransform(pt, &MouseConfig.caldata); #endif _tsOrientClip(pt, MouseConfig.display, !(MouseConfig.flags & FLG_CAL_RAW)); }
static void get_calibrated_reading(MouseReading *pt) { #if GINPUT_MOUSE_NEED_CALIBRATION || GDISP_NEED_CONTROL coord_t w, h; #endif get_raw_reading(pt); #if GINPUT_MOUSE_NEED_CALIBRATION || GDISP_NEED_CONTROL w = gdispGetWidth(); h = gdispGetHeight(); #endif #if GINPUT_MOUSE_NEED_CALIBRATION _tsTransform(pt, &MouseConfig.caldata); #endif #if GDISP_NEED_CONTROL switch(gdispGetOrientation()) { case GDISP_ROTATE_0: break; case GDISP_ROTATE_90: { coord_t t = pt->y; pt->y = h - 1 - pt->x; pt->x = t; } break; case GDISP_ROTATE_180: pt->x = w - 1 - pt->x; pt->y = h - 1 - pt->y; break; case GDISP_ROTATE_270: { coord_t t = pt->x; pt->x = w - 1 - pt->y; pt->y = t; } break; } #endif #if GINPUT_MOUSE_NEED_CALIBRATION if (pt->x < 0) pt->x = 0; else if (pt->x >= w) pt->x = w-1; if (pt->y < 0) pt->y = 0; else if (pt->y >= h) pt->y = h-1; #endif }
bool_t ginputCalibrateMouse(uint16_t instance) { #if !GINPUT_MOUSE_NEED_CALIBRATION (void) instance; return FALSE; #else const coord_t height = gdispGetHeight(); const coord_t width = gdispGetWidth(); const MousePoint cross[] = {{(width / 4), (height / 4)}, {(width - (width / 4)) , (height / 4)}, {(width - (width / 4)) , (height - (height / 4))}, {(width / 2), (height / 2)}}; /* Check point */ MousePoint points[GINPUT_MOUSE_CALIBRATION_POINTS]; const MousePoint *pc; MousePoint *pt; int32_t px, py; unsigned i, j; font_t font1, font2; #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 unsigned err; #endif if (instance || (MouseConfig.flags & FLG_IN_CAL)) return FALSE; font1 = gdispOpenFont(GINPUT_MOUSE_CALIBRATION_FONT); font2 = gdispOpenFont(GINPUT_MOUSE_CALIBRATION_FONT2); MouseConfig.flags |= FLG_IN_CAL; gtimerStop(&MouseTimer); MouseConfig.flags &= ~(FLG_CAL_OK|FLG_CAL_SAVED); #if GDISP_NEED_CONTROL gdispSetOrientation(GDISP_ROTATE_0); #endif #if GDISP_NEED_CLIP gdispSetClip(0, 0, width, height); #endif #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 while(1) { #endif gdispClear(Blue); gdispFillStringBox(0, 5, width, 30, GINPUT_MOUSE_CALIBRATION_TEXT, font1, White, Blue, justifyCenter); for(i = 0, pt = points, pc = cross; i < GINPUT_MOUSE_CALIBRATION_POINTS; i++, pt++, pc++) { _tsDrawCross(pc); do { /* Wait for the mouse to be pressed */ while(get_raw_reading(&MouseConfig.t), !(MouseConfig.t.buttons & GINPUT_MOUSE_BTN_LEFT)) chThdSleepMilliseconds(20); /* Average all the samples while the mouse is down */ for(px = py = 0, j = 0; chThdSleepMilliseconds(20), /* Settling time between readings */ get_raw_reading(&MouseConfig.t), (MouseConfig.t.buttons & GINPUT_MOUSE_BTN_LEFT); j++) { px += MouseConfig.t.x; py += MouseConfig.t.y; } } while(!j); pt->x = px / j; pt->y = py / j; _tsClearCross(pc); if (i >= 1 && pt->x == (pt-1)->x && pt->y == (pt-1)->y) { gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_SAME_TEXT, font2, Red, Yellow, justifyCenter); chThdSleepMilliseconds(5000); gdispFillArea(0, 35, width, 40, Blue); } } /* Apply 3 point calibration algorithm */ _tsDo3PointCalibration(cross, points, &MouseConfig.caldata); /* Verification of correctness of calibration (optional) : * See if the 4th point (Middle of the screen) coincides with the calibrated * result. If point is within +/- Squareroot(ERROR) pixel margin, then successful calibration * Else, start from the beginning. */ #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 /* Transform the co-ordinates */ MouseConfig.t.x = points[3].x; MouseConfig.t.y = points[3].y; _tsTransform(&MouseConfig.t, &MouseConfig.caldata); /* Calculate the delta */ err = (MouseConfig.t.x - cross[3].x) * (MouseConfig.t.x - cross[3].x) + (MouseConfig.t.y - cross[3].y) * (MouseConfig.t.y - cross[3].y); if (err <= GINPUT_MOUSE_MAX_CALIBRATION_ERROR * GINPUT_MOUSE_MAX_CALIBRATION_ERROR) break; gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_ERROR_TEXT, font2, Red, Yellow, justifyCenter); chThdSleepMilliseconds(5000); } #endif // Restart everything gdispCloseFont(font1); gdispCloseFont(font2); MouseConfig.flags |= FLG_CAL_OK; MouseConfig.last_buttons = 0; get_calibrated_reading(&MouseConfig.t); MouseConfig.flags &= ~FLG_IN_CAL; if ((MouseConfig.flags & FLG_INIT_DONE)) gtimerStart(&MouseTimer, MousePoll, 0, TRUE, GINPUT_MOUSE_POLL_PERIOD); // Save the calibration data (if possible) if (MouseConfig.fnsavecal) { MouseConfig.fnsavecal(instance, (const uint8_t *)&MouseConfig.caldata, sizeof(MouseConfig.caldata)); MouseConfig.flags |= FLG_CAL_SAVED; } return TRUE; #endif }