-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar1021.cpp
265 lines (233 loc) · 8.18 KB
/
ar1021.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
260
261
262
263
264
265
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "ar1021.h"
namespace esphome {
namespace ar1021 {
static const char *const TAG = "ar1021.component";
static const uint8_t TOUCH_THRESHOLD = 0x02;
static const uint8_t SENSITIVITY_FILTER = 0x03;
static const uint8_t SAMPLING_FAST = 0x04;
static const uint8_t SAMPLING_SLOW = 0x05;
static const uint8_t ACCURACY_FILTER_FAST = 0x06;
static const uint8_t ACCURACY_FILTER_SLOW = 0x07;
static const uint8_t SPEED_THRESHOLD = 0x08;
static const uint8_t SLEEP_DELAY = 0x0A;
static const uint8_t PEN_UP_DELAY = 0x0B;
static const uint8_t TOUCH_MODE = 0x0C;
static const uint8_t TOUCH_OPTIONS = 0x0D;
static const uint8_t CALIBRATION_INSET = 0x0E;
static const uint8_t PEN_STATE_REPORT_DELAY = 0x0F;
static const uint8_t TOUCH_REPORT_DELAY = 0x11;
static const uint8_t GET_VERSION[4] = {0x00, 0x55, 0x01, 0x10};
static const uint8_t ENABLE_TOUCH[4] = {0x00, 0x55, 0x01, 0x12};
static const uint8_t DISABLE_TOUCH[4] = {0x00, 0x55, 0x01, 0x13};
static const uint8_t CALIBRATE_MODE[5] = {0x00, 0x55, 0x02, 0x14, 0x04};
static const uint8_t REGISTER_READ[4] = {0x00, 0x55, 0x01, 0x20};
static const uint8_t REGISTER_WRITE[4] = {0x00, 0x55, 0x01, 0x21};
static const uint8_t REGISTER_START_ADDRESS_REQUEST[4] = {0x00, 0x55, 0x01, 0x22};
static const uint8_t REGISTERS_WRITE_TO_EEPROM[4] = {0x00, 0x55, 0x01, 0x23};
static const uint8_t EEPROM_READ[4] = {0x00, 0x55, 0x01, 0x28};
static const uint8_t EEPROM_WRITE[4] = {0x00, 0x55, 0x01, 0x29};
static const uint8_t EEPROM_WRITE_TO_REGISTERS[4] = {0x00, 0x55, 0x01, 0x2B};
uint8_t calstate = 0;
static const uint8_t CALCONFIRM[4] = {0x55, 0x02, 0x00, 0x14};
#define ERROR_CHECK(err) \
if ((err) != i2c::ERROR_OK) { \
ESP_LOGE(TAG, "Failed to communicate!"); \
this->status_set_warning(); \
return; \
}
//void Store::gpio_intr(Store *store) { store->touch = true; }
float AR1021Component::get_setup_priority() const { return setup_priority::HARDWARE; }
void AR1021Component::setup() {
uint8_t buffer[4] = {0};
ESP_LOGCONFIG(TAG, "Setting up AR1021 touchscreen...");
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->interrupt_pin_->setup();
// this->store_.pin = this->interrupt_pin_->to_isr();
// this->interrupt_pin_->attach_interrupt(Store::gpio_intr, &this->store_, gpio::INTERRUPT_RISING_EDGE);
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_RISING_EDGE);
if (this->write(nullptr, 0) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to communicate!");
this->interrupt_pin_->detach_interrupt();
this->mark_failed();
return;
}
this->write(ENABLE_TOUCH, 4);
i2c::ErrorCode err;
err = this->read(buffer, 4);
ERROR_CHECK(err);
//fixup actually check success, 0x55 0x02 0x00 0x12
if (this->display_ != nullptr) {
if (this->x_raw_max_ == this->x_raw_min_) {
this->x_raw_max_ = this->display_->get_native_width();
}
if (this->y_raw_max_ == this->y_raw_min_) {
this->y_raw_max_ = this->display_->get_native_height();
}
}
//this->store_.touched = true;
}
//void AR1021Component::loop() {
void AR1021Component::update_touches() {
uint8_t buffer[4] = {0};
i2c::ErrorCode err;
std::string out = {};
// TouchPoint tp;
// bool tor;
switch (calstate) {
case 1:
this->write(DISABLE_TOUCH, 4);
err = this->read(buffer, 4);
ERROR_CHECK(err);
this->write(CALIBRATE_MODE, 5);
err = this->read(buffer, 4);
ERROR_CHECK(err);
calstate = 2;
ESP_LOGD(TAG, "start calibration");
return;
case 2:
err = this->read(buffer, 4);
ERROR_CHECK(err);
if (std::equal(std::begin(buffer), std::end(buffer), std::begin(CALCONFIRM))) {
calstate = 3;
// tp.id = 0;
// tp.state = 3;
// tp.x = 0;
// tp.y = 0;
// this->defer([this, tp]() { this->send_touch_(tp); });
this->touches_.clear();
this->was_touched_ = false;
this->first_touch_ = true;
this->add_raw_touch_position_(3, 0, 0);
ESP_LOGD(TAG, "calibration 1");
}
return;
case 3:
err = this->read(buffer, 4);
ERROR_CHECK(err);
if (std::equal(std::begin(buffer), std::end(buffer), std::begin(CALCONFIRM))) {
calstate = 4;
// tp.id = 0;
// tp.state = 4;
// tp.x = 0;
// tp.y = 0;
// this->defer([this, tp]() { this->send_touch_(tp); });
this->touches_.clear();
this->was_touched_ = false;
this->first_touch_ = true;
this->add_raw_touch_position_(4, 0, 0);
ESP_LOGD(TAG, "calibration 2");
}
return;
case 4:
err = this->read(buffer, 4);
ERROR_CHECK(err);
if (std::equal(std::begin(buffer), std::end(buffer), std::begin(CALCONFIRM))) {
calstate = 5;
// tp.id = 0;
// tp.state = 5;
// tp.x = 0;
// tp.y = 0;
// this->defer([this, tp]() { this->send_touch_(tp); });
this->touches_.clear();
this->was_touched_ = false;
this->first_touch_ = true;
this->add_raw_touch_position_(5, 0, 0);
ESP_LOGD(TAG, "calibration 3");
}
return;
case 5:
err = this->read(buffer, 4);
ERROR_CHECK(err);
if (std::equal(std::begin(buffer), std::end(buffer), std::begin(CALCONFIRM))) {
calstate = 6;
// tp.id = 0;
// tp.state = 6;
// tp.x = 0;
// tp.y = 0;
// this->defer([this, tp]() { this->send_touch_(tp); });
this->touches_.clear();
this->was_touched_ = false;
this->first_touch_ = true;
this->add_raw_touch_position_(6, 0, 0);
ESP_LOGD(TAG, "calibration 4");
}
return;
case 6:
err = this->read(buffer, 4);
ERROR_CHECK(err);
out = format_hex_pretty(buffer, 4);
ESP_LOGD(TAG, "wait completion: %s", out.c_str());
if (std::equal(std::begin(buffer), std::end(buffer), std::begin(CALCONFIRM))) {
calstate = 0;
ESP_LOGD(TAG, "calibration complete");
this->write(ENABLE_TOUCH, 4);
err = this->read(buffer, 4);
ERROR_CHECK(err);
}
return;
}
// if (!this->store_.touch)
// return;
// this->store_.touch = false;
uint8_t point = 0;
uint8_t bigbuff[5] = {0};
uint32_t sum_l = 0, sum_h = 0;
err = this->read(bigbuff, 5);
ERROR_CHECK(err);
//tp.id = bigbuff[0];
//tp.state = 0x09;
out = format_hex_pretty(bigbuff, 5);
ESP_LOGD(TAG, "raw touch: %s", out.c_str());
uint16_t y = (uint16_t) ((bigbuff[4] << 7) | (bigbuff[3]));
uint16_t x = (uint16_t) ((bigbuff[2] << 7) | (bigbuff[1]));
//tor = bigbuff[0] & 1;
y = (y * this->display_->get_native_height()) / 0xfff;
x = (x * this->display_->get_native_width()) / 0xfff;
ESP_LOGD(TAG, "touch x %d", x);
ESP_LOGD(TAG, "touch y %d", y);
ESP_LOGD(TAG, "pen %d", (bigbuff[0] - 128));
if ((bigbuff[0] - 128) == 1) {
this->touches_.clear();
this->was_touched_ = false;
this->first_touch_ = true;
this->add_raw_touch_position_(0, x, y);
}
// switch (this->rotation_) {
// case ROTATE_0_DEGREES:
// tp.y = y;
// tp.x = x;
// break;
// case ROTATE_90_DEGREES:
// tp.x = this->display_height_ - y;
// tp.y = x;
// break;
// case ROTATE_180_DEGREES:
// tp.y = this->display_height_ - y;
// tp.x = this->display_width_ - x;
// break;
// case ROTATE_270_DEGREES:
// tp.x = y;
// tp.y = this->display_width_ - x;
// break;
// }
// if (tor) {
// this->defer([this, tp]() { this->send_touch_(tp); });
// } else {
// for (auto *listener : this->touch_listeners_)
// listener->release();
// }
this->status_clear_warning();
}
void AR1021Component::dump_config() {
ESP_LOGCONFIG(TAG, "AR1021 Touchscreen:");
LOG_I2C_DEVICE(this);
LOG_PIN("Interrupt Pin: ", this->interrupt_pin_);
//this->setup();
}
void AR1021Component::calibrate() {
calstate = 1;
}
} //namespace ar1021
} //namespace esphome