Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,29 @@ bool Calibrator::finish(int width, int height)
// based on old_axys: inversion/swapping is relative to the old axis
XYinfo new_axis(old_axys);


// calculate average of clicks
float x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
float x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
float y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
float y_max = (clicked.y[LL] + clicked.y[LR])/2.0;

// Should x and y be swapped?
if (abs(clicked.x[UL] - clicked.x[UR]) < abs(clicked.y[UL] - clicked.y[UR])) {
new_axis.swap_xy = !new_axis.swap_xy;
std::swap(x_min, y_min);
std::swap(x_max, y_max);
// Calculate average of clicks.
// Set min and max values depending on the sequence of the clicked coordinates.
// If first two clicks have different x coordinates, we have a 0 or 180
// degrees rotation else we have a 70 or 270 degrees rotation.
float x_min;
float x_max;
float y_min;
float y_max;
if(abs(clicked.x[UL] - clicked.x[LL]) < abs(clicked.x[UL] - clicked.x[UR])) {
// rotation of 0 or 180 degrees
x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
y_max = (clicked.y[LL] + clicked.y[LR])/2.0;
} else {
// rotation of 90 or 270 degreees
x_min = (clicked.x[UL] + clicked.x[UR])/2.0;
x_max = (clicked.x[LL] + clicked.x[LR])/2.0;
y_min = (clicked.y[UL] + clicked.y[LL])/2.0;
y_max = (clicked.y[UR] + clicked.y[LR])/2.0;

// axes are swapped on 90 or 270 degrees rotation
new_axis.swap_xy = !old_axys.swap_xy;
}

// the screen was divided in num_blocks blocks, and the touch points were at
Expand All @@ -185,6 +196,11 @@ bool Calibrator::finish(int width, int height)
y_min = scaleAxis(y_min, old_axys.y.max, old_axys.y.min, height, 0);
y_max = scaleAxis(y_max, old_axys.y.max, old_axys.y.min, height, 0);

// If we swapped axes also swap x and y values
if(new_axis.swap_xy != old_axys.swap_xy) {
std::swap(x_min, y_min);
std::swap(x_max, y_max);
}

// round and put in new_axis struct
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
Expand Down
46 changes: 30 additions & 16 deletions src/calibrator/Evdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,30 @@ bool CalibratorEvdev::finish(int width, int height)
// based on old_axys: inversion/swapping is relative to the old axis
XYinfo new_axis(old_axys);


// calculate average of clicks
float x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
float x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
float y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
float y_max = (clicked.y[LL] + clicked.y[LR])/2.0;

// Calculate average of clicks.
// Set min and max values depending on the sequence of the clicked coordinates.
// If first two clicks have different x coordinates, we have a 0 or 180
// degrees rotation else we have a 70 or 270 degrees rotation.
float x_min;
float x_max;
float y_min;
float y_max;
if(abs(clicked.x[UL] - clicked.x[LL]) < abs(clicked.x[UL] - clicked.x[UR])) {
// rotation of 0 or 180 degrees
x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
y_max = (clicked.y[LL] + clicked.y[LR])/2.0;
} else {
// rotation of 90 or 270 degreees
x_min = (clicked.x[UL] + clicked.x[UR])/2.0;
x_max = (clicked.x[LL] + clicked.x[LR])/2.0;
y_min = (clicked.y[UL] + clicked.y[LL])/2.0;
y_max = (clicked.y[UR] + clicked.y[LR])/2.0;

// axes are swapped on 90 or 270 degrees rotation
new_axis.swap_xy = !old_axys.swap_xy;
}

// When evdev detects an invert_X/Y option,
// it performs the following *crazy* code just before returning
Expand All @@ -217,14 +234,6 @@ bool CalibratorEvdev::finish(int width, int height)
}
// end of evdev inversion crazyness


// Should x and y be swapped?
if (abs(clicked.x[UL] - clicked.x[UR]) < abs(clicked.y[UL] - clicked.y[UR])) {
new_axis.swap_xy = !new_axis.swap_xy;
std::swap(x_min, y_min);
std::swap(x_max, y_max);
}

// the screen was divided in num_blocks blocks, and the touch points were at
// one block away from the true edges of the screen.
const float block_x = width/(float)num_blocks;
Expand All @@ -238,7 +247,7 @@ bool CalibratorEvdev::finish(int width, int height)
float scale_y = (y_max - y_min)/(height - 2*block_y);
y_min -= block_y * scale_y;
y_max += block_y * scale_y;

// now, undo the transformations done by the X server, to obtain the true 'raw' value in X.
// The raw value was scaled from old_axis to the device min/max, and from the device min/max
// to the screen min/max
Expand All @@ -248,6 +257,11 @@ bool CalibratorEvdev::finish(int width, int height)
y_min = scaleAxis(y_min, old_axys.y.max, old_axys.y.min, height, 0);
y_max = scaleAxis(y_max, old_axys.y.max, old_axys.y.min, height, 0);

// If we swapped axes also swap x and y values
if(new_axis.swap_xy != old_axys.swap_xy) {
std::swap(x_min, y_min);
std::swap(x_max, y_max);
}

// round and put in new_axis struct
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
Expand Down
21 changes: 17 additions & 4 deletions src/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ int main() {
printf("Old axis: "); old_axis.print();

for (unsigned c=0; c != raw_coords.size(); c++) {

for (unsigned s=0; s<=1; s++) {
XYinfo raw(raw_coords[c]);
//printf("Raw: "); raw.print();

Expand All @@ -84,10 +86,19 @@ int main() {
//printf("\tClicked: "); clicked.print();

// emulate screen clicks
calib->add_click(clicked.x.min, clicked.y.min);
calib->add_click(clicked.x.max, clicked.y.min);
calib->add_click(clicked.x.min, clicked.y.max);
calib->add_click(clicked.x.max, clicked.y.max);
// Depending on the sequence of clicks the first changed coordinate can
// be either x or y so we emulate both possible sequences
if(!s) {
calib->add_click(clicked.x.min, clicked.y.min);
calib->add_click(clicked.x.max, clicked.y.min);
calib->add_click(clicked.x.min, clicked.y.max);
calib->add_click(clicked.x.max, clicked.y.max);
} else {
calib->add_click(clicked.x.min, clicked.y.min);
calib->add_click(clicked.x.min, clicked.y.max);
calib->add_click(clicked.x.max, clicked.y.min);
calib->add_click(clicked.x.max, clicked.y.max);
}
calib->finish(width, height);

// test result
Expand All @@ -112,6 +123,8 @@ int main() {
printf("%i", maxdiff);
} // loop over raw_coords

} // loop over sequences

printf(". OK\n");
} // loop over old_axes

Expand Down