forked from jinschoi/SphereBot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTouchscreenGrid.cpp
81 lines (69 loc) · 1.8 KB
/
TouchscreenGrid.cpp
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
/*
TouchscreenGrid.cpp - Library for touchscreen button functions.
Created by Terence F. Golla, November 9, 2022
Released into the public domain.
*/
#include "TouchscreenGrid.h"
// Constructor
TouchscreenGrid::TouchscreenGrid(unsigned int x, unsigned int y, unsigned int columns, unsigned int rows,
unsigned int boxWidth, unsigned int boxHeight, unsigned int tftWidth, unsigned int tftHeight,
unsigned int rotation)
{
_x = x;
_y = y;
_columns = columns;
_rows =rows;
_boxWidth = boxWidth;
_boxHeight = boxHeight;
_tftWidth = tftWidth;
_tftHeight = tftHeight;
_rotation = rotation;
}
int TouchscreenGrid::Column(unsigned int x, unsigned int y)
{
// Adjust for the fact that the touch coordinates do not rotate like the image.
unsigned int adjustedX;
switch (_rotation)
{
case 1:
adjustedX = _tftWidth - y;
break;
case 2:
adjustedX = x;
break;
case 3:
adjustedX = y;
break;
case 4:
adjustedX = _tftWidth - y;
break;
}
int column = ((adjustedX - _x - 1)/_boxWidth) + 1;
if (column > _columns)
column = 0;
return column;
}
int TouchscreenGrid::Row(unsigned int x, unsigned int y)
{
// Adjust for the fact that the touch coordinates do not rotate like the image.
unsigned int adjustedY;
switch (_rotation)
{
case 1:
adjustedY = x;
break;
case 2:
adjustedY = y;
break;
case 3:
adjustedY = _tftHeight - x;
break;
case 4:
adjustedY = _tftHeight - x;
break;
}
int row = ((adjustedY - _y - 1)/_boxHeight) + 1;
if (row > _rows)
row = 0;
return row;
}