forked from esp-cpp/espp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeed-studio-round-display.cpp
More file actions
439 lines (395 loc) · 15.5 KB
/
seeed-studio-round-display.cpp
File metadata and controls
439 lines (395 loc) · 15.5 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "seeed-studio-round-display.hpp"
using namespace espp;
SsRoundDisplay::PinConfig SsRoundDisplay::pin_config_;
SsRoundDisplay::SsRoundDisplay()
: BaseComponent("SsRoundDisplay")
, internal_i2c_({.port = internal_i2c_port,
.sda_io_num = pin_config_.sda,
.scl_io_num = pin_config_.scl,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE})
, touch_interrupt_pin_({
.gpio_num = pin_config_.touch_interrupt,
.callback =
std::bind(&SsRoundDisplay::touch_interrupt_handler, this, std::placeholders::_1),
.active_level = touch_interrupt_level,
.interrupt_type = espp::Interrupt::Type::FALLING_EDGE,
}) {
if (pin_config_ == PinConfig{}) {
logger_.error("PinConfig not set, you must call set_pin_config() before initializing the "
"SsRoundDisplay! Hardware will not work properly!");
}
}
espp::I2c &SsRoundDisplay::internal_i2c() { return internal_i2c_; }
espp::Interrupt &SsRoundDisplay::interrupts() { return interrupts_; }
////////////////////////
// Touchpad Functions //
////////////////////////
bool SsRoundDisplay::initialize_touch(const SsRoundDisplay::touch_callback_t &callback) {
if (touchpad_input_) {
logger_.warn("Touchpad already initialized, not initializing again!");
return false;
}
if (!display_) {
logger_.warn("You should call initialize_display() before initialize_touch(), otherwise lvgl "
"will not properly handle the touchpad input!");
}
logger_.info("Initializing Touch Driver");
touch_ = std::make_unique<TouchDriver>(TouchDriver::Config{
.write = std::bind(&espp::I2c::write, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.log_level = espp::Logger::Verbosity::WARN});
touchpad_input_ = std::make_shared<espp::TouchpadInput>(espp::TouchpadInput::Config{
.touchpad_read =
std::bind(&SsRoundDisplay::touchpad_read, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
.swap_xy = touch_swap_xy,
.invert_x = touch_invert_x,
.invert_y = touch_invert_y,
.log_level = espp::Logger::Verbosity::WARN});
// store the callback
touch_callback_ = callback;
// add the touch interrupt pin
interrupts_.add_interrupt(touch_interrupt_pin_);
return true;
}
void SsRoundDisplay::touch_interrupt_handler(const espp::Interrupt::Event &event) {
if (update_touch()) {
if (touch_callback_) {
touch_callback_(touchpad_data());
}
}
}
bool SsRoundDisplay::update_touch() {
// ensure the touch is initialized
if (!touch_) {
return false;
}
// get the latest data from the device
std::error_code ec;
bool new_data = touch_->update(ec);
if (ec) {
logger_.error("could not update touch driver: {}\n", ec.message());
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = {};
return false;
}
if (!new_data) {
return false;
}
// get the latest data from the touchpad
TouchpadData temp_data;
touch_->get_touch_point(&temp_data.num_touch_points, &temp_data.x, &temp_data.y);
// update the touchpad data
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
touchpad_data_ = temp_data;
return true;
}
std::shared_ptr<espp::TouchpadInput> SsRoundDisplay::touchpad_input() const {
return touchpad_input_;
}
TouchpadData SsRoundDisplay::touchpad_data() const { return touchpad_data_; }
void SsRoundDisplay::touchpad_read(uint8_t *num_touch_points, uint16_t *x, uint16_t *y,
uint8_t *btn_state) {
std::lock_guard<std::recursive_mutex> lock(touchpad_data_mutex_);
*num_touch_points = touchpad_data_.num_touch_points;
*x = touchpad_data_.x;
*y = touchpad_data_.y;
*btn_state = touchpad_data_.btn_state;
}
TouchpadData SsRoundDisplay::touchpad_convert(const TouchpadData &data) const {
TouchpadData temp_data;
temp_data.num_touch_points = data.num_touch_points;
temp_data.x = data.x;
temp_data.y = data.y;
temp_data.btn_state = data.btn_state;
if (temp_data.num_touch_points == 0) {
return temp_data;
}
if (touch_swap_xy) {
std::swap(temp_data.x, temp_data.y);
}
if (touch_invert_x) {
temp_data.x = lcd_width_ - (temp_data.x + 1);
}
if (touch_invert_y) {
temp_data.y = lcd_height_ - (temp_data.y + 1);
}
// get the orientation of the display
auto rotation = lv_display_get_rotation(lv_display_get_default());
switch (rotation) {
case LV_DISPLAY_ROTATION_0:
break;
case LV_DISPLAY_ROTATION_90:
temp_data.y = lcd_height_ - (temp_data.y + 1);
std::swap(temp_data.x, temp_data.y);
break;
case LV_DISPLAY_ROTATION_180:
temp_data.x = lcd_width_ - (temp_data.x + 1);
temp_data.y = lcd_height_ - (temp_data.y + 1);
break;
case LV_DISPLAY_ROTATION_270:
temp_data.x = lcd_width_ - (temp_data.x + 1);
std::swap(temp_data.x, temp_data.y);
break;
default:
break;
}
return temp_data;
}
////////////////////////
// Display Functions //
////////////////////////
// the user flag for the callbacks does two things:
// 1. Provides the GPIO level for the data/command pin, and
// 2. Sets some bits for other signaling (such as LVGL FLUSH)
static constexpr int FLUSH_BIT = (1 << (int)espp::display_drivers::Flags::FLUSH_BIT);
static constexpr int DC_LEVEL_BIT = (1 << (int)espp::display_drivers::Flags::DC_LEVEL_BIT);
// This function is called (in irq context!) just before a transmission starts.
// It will set the D/C line to the value indicated in the user field
// (DC_LEVEL_BIT).
//
// cppcheck-suppress constParameterCallback
static void IRAM_ATTR lcd_spi_pre_transfer_callback(spi_transaction_t *t) {
static auto lcd_dc_io = SsRoundDisplay::get_lcd_dc_gpio();
uint32_t user_flags = (uint32_t)(t->user);
bool dc_level = user_flags & DC_LEVEL_BIT;
gpio_set_level(lcd_dc_io, dc_level);
}
// This function is called (in irq context!) just after a transmission ends. It
// will indicate to lvgl that the next flush is ready to be done if the
// FLUSH_BIT is set.
//
// cppcheck-suppress constParameterCallback
static void IRAM_ATTR lcd_spi_post_transfer_callback(spi_transaction_t *t) {
uint16_t user_flags = (uint32_t)(t->user);
bool should_flush = user_flags & FLUSH_BIT;
if (should_flush) {
lv_display_t *disp = lv_display_get_default();
lv_display_flush_ready(disp);
}
}
bool SsRoundDisplay::initialize_lcd() {
if (lcd_handle_) {
logger_.warn("LCD already initialized, not initializing again!");
return false;
}
esp_err_t ret;
memset(&lcd_spi_bus_config_, 0, sizeof(lcd_spi_bus_config_));
lcd_spi_bus_config_.mosi_io_num = pin_config_.mosi;
lcd_spi_bus_config_.miso_io_num = -1;
lcd_spi_bus_config_.sclk_io_num = pin_config_.sck;
lcd_spi_bus_config_.quadwp_io_num = -1;
lcd_spi_bus_config_.quadhd_io_num = -1;
lcd_spi_bus_config_.max_transfer_sz = frame_buffer_size * sizeof(lv_color_t) + 100;
memset(&lcd_config_, 0, sizeof(lcd_config_));
lcd_config_.mode = 0;
// lcd_config_.flags = SPI_DEVICE_NO_RETURN_RESULT;
lcd_config_.clock_speed_hz = lcd_clock_speed;
lcd_config_.input_delay_ns = 0;
lcd_config_.spics_io_num = pin_config_.lcd_cs;
lcd_config_.queue_size = spi_queue_size;
lcd_config_.pre_cb = lcd_spi_pre_transfer_callback;
lcd_config_.post_cb = lcd_spi_post_transfer_callback;
// Initialize the SPI bus
ret = spi_bus_initialize(lcd_spi_num, &lcd_spi_bus_config_, SPI_DMA_CH_AUTO);
ESP_ERROR_CHECK(ret);
// Attach the LCD to the SPI bus
ret = spi_bus_add_device(lcd_spi_num, &lcd_config_, &lcd_handle_);
ESP_ERROR_CHECK(ret);
// initialize the controller
using namespace std::placeholders;
DisplayDriver::initialize(espp::display_drivers::Config{
.write_command = std::bind(&SsRoundDisplay::write_command, this, _1, _2, _3),
.lcd_send_lines = std::bind(&SsRoundDisplay::write_lcd_lines, this, _1, _2, _3, _4, _5, _6),
.reset_pin = lcd_reset_io,
.data_command_pin = pin_config_.lcd_dc,
.reset_value = reset_value,
.invert_colors = invert_colors,
.swap_color_order = swap_color_order,
.swap_xy = swap_xy,
.mirror_x = mirror_x,
.mirror_y = mirror_y});
return true;
}
bool SsRoundDisplay::initialize_display(size_t pixel_buffer_size) {
if (!lcd_handle_) {
logger_.error(
"LCD not initialized, you must call initialize_lcd() before initialize_display()!");
return false;
}
if (display_) {
logger_.warn("Display already initialized, not initializing again!");
return false;
}
// initialize the display / lvgl
using namespace std::chrono_literals;
display_ = std::make_shared<Display<Pixel>>(
Display<Pixel>::LvglConfig{.width = lcd_width_,
.height = lcd_height_,
.flush_callback = DisplayDriver::flush,
.rotation_callback = DisplayDriver::rotate,
.rotation = rotation},
Display<Pixel>::LcdConfig{.backlight_pin = pin_config_.lcd_backlight,
.backlight_on_value = backlight_value},
Display<Pixel>::DynamicMemoryConfig{
.pixel_buffer_size = pixel_buffer_size,
.double_buffered = true,
.allocation_flags = MALLOC_CAP_8BIT | MALLOC_CAP_DMA,
});
frame_buffer0_ =
(uint8_t *)heap_caps_malloc(frame_buffer_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
frame_buffer1_ =
(uint8_t *)heap_caps_malloc(frame_buffer_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
return true;
}
std::shared_ptr<espp::Display<SsRoundDisplay::Pixel>> SsRoundDisplay::display() const {
return display_;
}
void IRAM_ATTR SsRoundDisplay::lcd_wait_lines() {
spi_transaction_t *rtrans;
esp_err_t ret;
// logger_.debug("Waiting for {} queued transactions", num_queued_trans);
// Wait for all transactions to be done and get back the results.
while (num_queued_trans) {
ret = spi_device_get_trans_result(lcd_handle_, &rtrans, 10 / portTICK_PERIOD_MS);
if (ret != ESP_OK) {
logger_.error("Display: Could not get spi trans result: {} '{}'", ret, esp_err_to_name(ret));
}
num_queued_trans--;
// We could inspect rtrans now if we received any info back. The LCD is treated as write-only,
// though.
}
}
void IRAM_ATTR SsRoundDisplay::write_command(uint8_t command, std::span<const uint8_t> parameters,
uint32_t user_data) {
lcd_wait_lines();
memset(&trans[0], 0, sizeof(spi_transaction_t));
memset(&trans[1], 0, sizeof(spi_transaction_t));
trans[0].length = 8;
trans[0].user = reinterpret_cast<void *>(user_data);
trans[0].flags = SPI_TRANS_USE_TXDATA;
trans[0].tx_data[0] = command;
trans[1].length = parameters.size() * 8;
if (parameters.size() <= 4) {
// copy the data pointer to trans[1].tx_data
memcpy(trans[1].tx_data, parameters.data(), parameters.size());
trans[1].flags = SPI_TRANS_USE_TXDATA;
} else if (!parameters.empty()) {
trans[1].tx_buffer = parameters.data();
trans[1].flags = 0;
}
trans[1].user = reinterpret_cast<void *>(
user_data | (1 << static_cast<int>(display_drivers::Flags::DC_LEVEL_BIT)));
esp_err_t ret = spi_device_queue_trans(lcd_handle_, &trans[0], 10 / portTICK_PERIOD_MS);
if (ret != ESP_OK) {
logger_.error("Couldn't queue spi command trans for display: {} '{}'", ret,
esp_err_to_name(ret));
} else {
if (!parameters.empty()) {
ret = spi_device_queue_trans(lcd_handle_, &trans[1], 10 / portTICK_PERIOD_MS);
if (ret != ESP_OK) {
logger_.error("Couldn't queue spi data trans for display: {} '{}'", ret,
esp_err_to_name(ret));
} else {
++num_queued_trans;
}
}
++num_queued_trans;
}
}
void IRAM_ATTR SsRoundDisplay::write_lcd_lines(int xs, int ys, int xe, int ye, const uint8_t *data,
uint32_t user_data) {
// if we haven't waited by now, wait here...
lcd_wait_lines();
esp_err_t ret;
size_t length = (xe - xs + 1) * (ye - ys + 1) * 2;
if (length == 0) {
logger_.error("lcd_send_lines: Bad length: ({},{}) to ({},{})", xs, ys, xe, ye);
}
// initialize the spi transactions
for (int i = 0; i < 6; i++) {
memset(&trans[i], 0, sizeof(spi_transaction_t));
if ((i & 1) == 0) {
// Even transfers are commands
trans[i].length = 8;
trans[i].user = (void *)0;
} else {
// Odd transfers are data
trans[i].length = 8 * 4;
trans[i].user = (void *)DC_LEVEL_BIT;
}
trans[i].flags = SPI_TRANS_USE_TXDATA;
}
trans[0].tx_data[0] = (uint8_t)DisplayDriver::Command::caset;
trans[1].tx_data[0] = (xs) >> 8;
trans[1].tx_data[1] = (xs)&0xff;
trans[1].tx_data[2] = (xe) >> 8;
trans[1].tx_data[3] = (xe)&0xff;
trans[2].tx_data[0] = (uint8_t)DisplayDriver::Command::raset;
trans[3].tx_data[0] = (ys) >> 8;
trans[3].tx_data[1] = (ys)&0xff;
trans[3].tx_data[2] = (ye) >> 8;
trans[3].tx_data[3] = (ye)&0xff;
trans[4].tx_data[0] = (uint8_t)DisplayDriver::Command::ramwr;
trans[5].tx_buffer = data;
trans[5].length = length * 8;
// undo SPI_TRANS_USE_TXDATA flag
trans[5].flags = SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL;
// we need to keep the dc bit set, but also add our flags
trans[5].user = (void *)(DC_LEVEL_BIT | user_data);
// Queue all transactions.
for (int i = 0; i < 6; i++) {
ret = spi_device_queue_trans(lcd_handle_, &trans[i], 10 / portTICK_PERIOD_MS);
if (ret != ESP_OK) {
logger_.error("Couldn't queue spi trans for display: {} '{}'", ret, esp_err_to_name(ret));
} else {
num_queued_trans++;
}
}
// When we are here, the SPI driver is busy (in the background) getting the
// transactions sent. That happens mostly using DMA, so the CPU doesn't have
// much to do here. We're not going to wait for the transaction to finish
// because we may as well spend the time calculating the next line. When that
// is done, we can call lcd_wait_lines, which will wait for the transfers
// to be done and check their status.
}
void SsRoundDisplay::write_lcd_frame(const uint16_t xs, const uint16_t ys, const uint16_t width,
const uint16_t height, uint8_t *data) {
if (data) {
// have data, fill the area with the color data
lv_area_t area{.x1 = (lv_coord_t)(xs),
.y1 = (lv_coord_t)(ys),
.x2 = (lv_coord_t)(xs + width - 1),
.y2 = (lv_coord_t)(ys + height - 1)};
DisplayDriver::fill(nullptr, &area, data);
} else {
// don't have data, so clear the area (set to 0)
DisplayDriver::clear(xs, ys, width, height);
}
}
SsRoundDisplay::Pixel *SsRoundDisplay::vram0() const {
if (!display_) {
return nullptr;
}
return display_->vram0();
}
SsRoundDisplay::Pixel *SsRoundDisplay::vram1() const {
if (!display_) {
return nullptr;
}
return display_->vram1();
}
uint8_t *SsRoundDisplay::frame_buffer0() const { return frame_buffer0_; }
uint8_t *SsRoundDisplay::frame_buffer1() const { return frame_buffer1_; }
void SsRoundDisplay::brightness(float brightness) {
brightness = std::clamp(brightness, 0.0f, 100.0f) / 100.0f;
// display expects a value between 0 and 1
display_->set_brightness(brightness);
}
float SsRoundDisplay::brightness() const {
// display returns a value between 0 and 1
return display_->get_brightness() * 100.0f;
}