forked from fbiego/CST816S
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCST816S.cpp
More file actions
201 lines (178 loc) · 4.46 KB
/
CST816S.cpp
File metadata and controls
201 lines (178 loc) · 4.46 KB
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
/*
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;
}
/*!
@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 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;
}