-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCST816S.cpp
259 lines (230 loc) · 5.98 KB
/
CST816S.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
MIT License
Copyright (c) 2021 Felix Biego
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "Arduino.h"
#include <Wire.h>
#include <FunctionalInterrupt.h>
#include "CST816S.h"
/*!
@brief Constructor for CST816S
@param sda
i2c data pin
@param scl
i2c clock pin
@param rst
touch reset pin
@param irq
touch interrupt pin
*/
CST816S::CST816S(int sda, int scl, int rst, int irq) {
_sda = sda;
_scl = scl;
_rst = rst;
_irq = irq;
}
/*!
@brief read touch data
*/
void CST816S::read_touch() {
byte data_raw[8];
i2c_read(CST816S_ADDRESS, 0x01, data_raw, 6);
data.gestureID = data_raw[0];
data.points = data_raw[1];
data.event = data_raw[2] >> 6;
data.x = ((data_raw[2] & 0xF) << 8) + data_raw[3];
data.y = ((data_raw[4] & 0xF) << 8) + data_raw[5];
}
/*!
@brief handle interrupts
*/
void IRAM_ATTR CST816S::handleISR(void) {
_event_available = true;
// Call user callback if it has been set
if (userISR != nullptr) {
userISR();
}
}
/*!
@brief enable double click
*/
void CST816S::enable_double_click(void) {
byte enableDoubleTap = 0x01; // Set EnDClick (bit 0) to enable double-tap
i2c_write(CST816S_ADDRESS, 0xEC, &enableDoubleTap, 1);
}
/*!
@brief Disable auto sleep mode
*/
void CST816S::disable_auto_sleep(void)
{
byte disableAutoSleep = 0xFE; // Non-zero value disables auto sleep
i2c_write(CST816S_ADDRESS, 0xFE, &disableAutoSleep, 1);
}
/*!
@brief Enable auto sleep mode
*/
void CST816S::enable_auto_sleep(void)
{
byte enableAutoSleep = 0x00; // 0 value enables auto sleep
i2c_write(CST816S_ADDRESS, 0xFE, &enableAutoSleep, 1);
}
/*!
@brief Set the auto sleep time
@param seconds Time in seconds (1-255) before entering standby mode after inactivity
*/
void CST816S::set_auto_sleep_time(int seconds)
{
if (seconds < 1)
{
seconds = 1; // Enforce minimum value of 1 second
}
else if (seconds > 255)
{
seconds = 255; // Enforce maximum value of 255 seconds
}
byte sleepTime = static_cast<byte>(seconds); // Convert int to byte
i2c_write(CST816S_ADDRESS, 0xF9, &sleepTime, 1);
}
/*!
@brief initialize the touch screen
@param interrupt
type of interrupt FALLING, RISING..
*/
void CST816S::begin(int interrupt) {
Wire.begin(_sda, _scl);
pinMode(_irq, INPUT);
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH );
delay(50);
digitalWrite(_rst, LOW);
delay(5);
digitalWrite(_rst, HIGH );
delay(50);
i2c_read(CST816S_ADDRESS, 0x15, &data.version, 1);
delay(5);
i2c_read(CST816S_ADDRESS, 0xA7, data.versionInfo, 3);
attachInterrupt(_irq, std::bind(&CST816S::handleISR, this), interrupt);
}
/*!
@brief Attaches a user-defined callback function to be triggered on an interrupt event from the CST816S touch controller.
@param callback A function to be called when an interrupt event occurs, must have no parameters and return void.
*/
void CST816S::attachUserInterrupt(std::function<void(void)> callback)
{
userISR = callback;
}
/*!
@brief check for a touch event
*/
bool CST816S::available() {
if (_event_available) {
read_touch();
_event_available = false;
return true;
}
return false;
}
/*!
@brief put the touch screen in standby mode
*/
void CST816S::sleep() {
digitalWrite(_rst, LOW);
delay(5);
digitalWrite(_rst, HIGH );
delay(50);
byte standby_value = 0x03;
i2c_write(CST816S_ADDRESS, 0xA5, &standby_value, 1);
}
/*!
@brief get the gesture event name
*/
String CST816S::gesture() {
switch (data.gestureID) {
case NONE:
return "NONE";
break;
case SWIPE_DOWN:
return "SWIPE DOWN";
break;
case SWIPE_UP:
return "SWIPE UP";
break;
case SWIPE_LEFT:
return "SWIPE LEFT";
break;
case SWIPE_RIGHT:
return "SWIPE RIGHT";
break;
case SINGLE_CLICK:
return "SINGLE CLICK";
break;
case DOUBLE_CLICK:
return "DOUBLE CLICK";
break;
case LONG_PRESS:
return "LONG PRESS";
break;
default:
return "UNKNOWN";
break;
}
}
/*!
@brief read data from i2c
@param addr
i2c device address
@param reg_addr
device register address
@param reg_data
array to copy the read data
@param length
length of data
*/
uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length)
{
Wire.beginTransmission(addr);
Wire.write(reg_addr);
if ( Wire.endTransmission(true))return -1;
Wire.requestFrom(addr, length, true);
for (int i = 0; i < length; i++) {
*reg_data++ = Wire.read();
}
return 0;
}
/*!
@brief write data to i2c
@brief read data from i2c
@param addr
i2c device address
@param reg_addr
device register address
@param reg_data
data to be sent
@param length
length of data
*/
uint8_t CST816S::i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length)
{
Wire.beginTransmission(addr);
Wire.write(reg_addr);
for (int i = 0; i < length; i++) {
Wire.write(*reg_data++);
}
if ( Wire.endTransmission(true))return -1;
return 0;
}