1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* Copyright (C) 2017 Martin Keppligner <martink@posteo.de>
*
* This file is part of tslib.
*
* ts_calibrate is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* ts_calibrate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this tool. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <SDL2/SDL.h>
#include <stdint.h>
#include <tslib.h>
void draw_line(SDL_Renderer *r, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
{
int32_t tmp;
int32_t dx = x2 - x1;
int32_t dy = y2 - y1;
if (abs(dx) < abs(dy)) {
if (y1 > y2) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
dx = -dx;
dy = -dy;
}
x1 <<= 16;
dx = (dx << 16) / dy;
while (y1 <= y2) {
SDL_RenderDrawPoint(r, x1 >> 16, y1);
y1 += dx;
y1++;
}
} else {
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
dx = -dx;
dy = -dy;
}
y1 <<= 16;
dy = dx ? (dy << 16) : 0;
while (x1 <= x2) {
SDL_RenderDrawPoint(r, x1, y1 >> 16);
y1 += dy;
x1++;
}
}
}
void draw_crosshair(SDL_Renderer *r, int32_t x, int32_t y)
{
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
draw_line(r, x - 10, y, x - 2, y);
draw_line(r, x + 2, y, x + 10, y);
draw_line(r, x, y - 10, x, y - 2);
draw_line(r, x, y + 2, x, y + 10);
SDL_SetRenderDrawColor(r, 0xff, 0xe0, 0x80, 255);
draw_line(r, x - 6, y - 9, x - 9, y - 9);
draw_line(r, x - 9, y - 8, x - 9, y - 6);
draw_line(r, x - 9, y + 6, x - 9, y + 9);
draw_line(r, x - 8, y + 9, x - 6, y + 9);
draw_line(r, x + 6, y + 9, x + 9, y + 9);
draw_line(r, x + 9, y + 8, x + 9, y + 6);
draw_line(r, x + 9, y - 6, x + 9, y - 9);
draw_line(r, x + 8, y - 9, x + 6, y - 9);
}
static int sort_by_x(const void *a, const void *b)
{
return (((struct ts_sample *)a)->x - ((struct ts_sample *)b)->x);
}
static int sort_by_y(const void *a, const void *b)
{
return (((struct ts_sample *)a)->y - ((struct ts_sample *)b)->y);
}
void getxy(struct tsdev *ts, int *x, int *y)
{
#define MAX_SAMPLES 128
struct ts_sample samp[MAX_SAMPLES];
int index, middle;
do {
if (ts_read_raw(ts, &samp[0], 1) < 0) {
perror("ts_read");
SDL_Quit();
}
} while (samp[0].pressure == 0);
/* Now collect up to MAX_SAMPLES touches into the samp array. */
index = 0;
do {
if (index < MAX_SAMPLES-1)
index++;
if (ts_read_raw(ts, &samp[index], 1) < 0) {
perror("ts_read");
SDL_Quit();
}
} while (samp[index].pressure > 0);
printf("Took %d samples...\n", index);
/*
* At this point, we have samples in indices zero to (index-1)
* which means that we have (index) number of samples. We want
* to calculate the median of the samples so that wild outliers
* don't skew the result. First off, let's assume that arrays
* are one-based instead of zero-based. If this were the case
* and index was odd, we would need sample number ((index+1)/2)
* of a sorted array; if index was even, we would need the
* average of sample number (index/2) and sample number
* ((index/2)+1). To turn this into something useful for the
* real world, we just need to subtract one off of the sample
* numbers. So for when index is odd, we need sample number
* (((index+1)/2)-1). Due to integer division truncation, we
* can simplify this to just (index/2). When index is even, we
* need the average of sample number ((index/2)-1) and sample
* number (index/2). Calculate (index/2) now and we'll handle
* the even odd stuff after we sort.
*/
middle = index/2;
if (x) {
qsort(samp, index, sizeof(struct ts_sample), sort_by_x);
if (index & 1)
*x = samp[middle].x;
else
*x = (samp[middle-1].x + samp[middle].x) / 2;
}
if (y) {
qsort(samp, index, sizeof(struct ts_sample), sort_by_y);
if (index & 1)
*y = samp[middle].y;
else
*y = (samp[middle-1].y + samp[middle].y) / 2;
}
}
|