-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchScreen.cpp
185 lines (143 loc) · 3.89 KB
/
TouchScreen.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* TouchScreen.cpp
*
* Created on: 29.03.2020
* Author: rom3
*/
#include "Arduino.h"
#include "TouchScreen.h"
#define NUMSAMPLES 3 //.kbv
#if defined(__STM32F1__) || defined(ESP32) //Maple core
#define ADC_ADJUST >>2
#else
#define ADC_ADJUST
#endif
TSPoint_kbv::TSPoint_kbv(void) {
x = y = z = 0;
}
TSPoint_kbv::TSPoint_kbv(int16_t x0, int16_t y0, int16_t z0) {
x = x0;
y = y0;
z = z0;
}
bool TSPoint_kbv::operator==(TSPoint_kbv p1) {
return ((p1.x == x) && (p1.y == y) && (p1.z == z));
}
bool TSPoint_kbv::operator!=(TSPoint_kbv p1) {
return ((p1.x != x) || (p1.y != y) || (p1.z != z));
}
static void insert_sort(int array[], uint8_t size) {
uint8_t j;
int save;
for (int i = 1; i < size; i++) {
save = array[i];
for (j = i; j >= 1 && save < array[j - 1]; j--)
array[j] = array[j - 1];
array[j] = save;
}
}
TSPoint_kbv TouchScreen_kbv::getPoint(void) {
int x, y, z;
int samples[NUMSAMPLES];
uint8_t i;
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);
pinMode(_xp, OUTPUT);
pinMode(_xm, OUTPUT);
digitalWrite(_xp, HIGH);
digitalWrite(_xm, LOW);
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(_yp) ADC_ADJUST;
}
insert_sort(samples, NUMSAMPLES);
x = (1023 - samples[NUMSAMPLES / 2]); //choose median
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW); //.kbv for Due
pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW); //.kbv for Due
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(_xm) ADC_ADJUST;
}
insert_sort(samples, NUMSAMPLES);
y = (1023 - samples[NUMSAMPLES / 2]);
// Set X+ to ground
pinMode(_xp, OUTPUT);
digitalWrite(_xp, LOW);
// Set Y- to VCC
pinMode(_ym, OUTPUT); //.kbv
digitalWrite(_ym, HIGH);
// Hi-Z X- and Y+
digitalWrite(_xm, LOW); //.kbv
pinMode(_xm, INPUT); //.kbv
digitalWrite(_yp, LOW);
pinMode(_yp, INPUT);
int z1 = analogRead(_xm) ADC_ADJUST;
int z2 = analogRead(_yp) ADC_ADJUST;
z = (1023 - (z2 - z1));
pinMode(_yp, OUTPUT); //restore shared pins
pinMode(_xm, OUTPUT);
digitalWrite(_yp, HIGH); //because TFT control pins
digitalWrite(_xm, HIGH);
return TSPoint_kbv(x, y, z); //XM, YP still in ANALOG mode
}
TouchScreen_kbv::TouchScreen_kbv(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) {
_yp = yp;
_xm = xm;
_ym = ym;
_xp = xp;
_rxplate = 0;
pressureThreshhold = 10;
}
TouchScreen_kbv::TouchScreen_kbv(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym,
uint16_t rxplate) {
_yp = yp;
_xm = xm;
_ym = ym;
_xp = xp;
_rxplate = rxplate;
pressureThreshhold = 10;
}
int TouchScreen_kbv::readTouchX(void) {
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);
pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);
return (1023 - (analogRead(_yp)) ADC_ADJUST);
}
int TouchScreen_kbv::readTouchY(void) {
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW);
pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);
return (1023 - (analogRead(_xm)) ADC_ADJUST);
}
uint16_t TouchScreen_kbv::pressure(void) {
// Set X+ to ground
pinMode(_xp, OUTPUT);
digitalWrite(_xp, LOW);
// Set Y- to VCC
pinMode(_ym, OUTPUT);
digitalWrite(_ym, HIGH);
// Hi-Z X- and Y+
digitalWrite(_xm, LOW);
pinMode(_xm, INPUT);
digitalWrite(_yp, LOW);
pinMode(_yp, INPUT);
int z1 = analogRead(_xm) ADC_ADJUST;
int z2 = analogRead(_yp) ADC_ADJUST;
return (1023 - (z2 - z1));
}