forked from liebman/esp32-gps-ntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.cpp
223 lines (188 loc) · 6.32 KB
/
Display.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
/*
* MIT License
*
* Copyright (c) 2020 Christopher B. Liebman
*
* 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 "Display.h"
#include "freertos/task.h"
#include "esp_log.h"
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif
#include "lvgl_helpers.h"
#include "LVLabel.h"
#ifndef DISPLAY_TASK_PRI
#define DISPLAY_TASK_PRI 1
#endif
#ifndef DISPLAY_LV_TICK_MS
#define DISPLAY_LV_TICK_MS 5
#endif
#ifndef DISPLAY_TASK_CORE
#define DISPLAY_TASK_CORE 0
#endif
static const char* TAG = "Display";
Display& Display::getDisplay()
{
static Display* display;
if (display == nullptr)
{
display = new Display();
}
return *display;
}
Display::Display()
{
}
bool Display::begin()
{
// there is only one Display object so this "should" be safe.
_lock = xSemaphoreCreateRecursiveMutex();
ESP_LOGI(TAG, "calling: lv_init");
lv_init();
/* Initialize SPI or I2C bus used by the drivers */
ESP_LOGI(TAG, "calling: lvgl_driver_init");
lvgl_driver_init();
/* Use double buffered when not working with monochrome displays */
static lv_color_t buf1[DISP_BUF_SIZE];
#ifndef CONFIG_LV_TFT_DISPLAY_MONOCHROME
static lv_color_t buf2[DISP_BUF_SIZE];
#endif
static lv_disp_buf_t disp_buf;
uint32_t size_in_px = DISP_BUF_SIZE;
/* Initialize the working buffer depending on the selected display */
ESP_LOGI(TAG, "calling: lv_disp_buf_init");
#if defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_IL3820 \
|| defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_JD79653A \
|| defined CONFIG_LV_TFT_DISPLAY_CONTROLLER_UC8151D
/* Actual size in pixel, not bytes and use single buffer */
size_in_px *= 8;
lv_disp_buf_init(&disp_buf, buf1, NULL, size_in_px);
#elif defined CONFIG_LV_TFT_DISPLAY_MONOCHROME
lv_disp_buf_init(&disp_buf, buf1, NULL, size_in_px);
#else
lv_disp_buf_init(&disp_buf, buf1, buf2, size_in_px);
#endif
ESP_LOGI(TAG, "calling: lv_disp_drv_init");
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.flush_cb = disp_driver_flush;
/* When using a monochrome display we need to register the callbacks:
* - rounder_cb
* - set_px_cb */
#ifdef CONFIG_LV_TFT_DISPLAY_MONOCHROME
disp_drv.rounder_cb = disp_driver_rounder;
disp_drv.set_px_cb = disp_driver_set_px;
#endif
ESP_LOGI(TAG, "calling: lv_disp_drv_register");
disp_drv.buffer = &disp_buf;
_disp = lv_disp_drv_register(&disp_drv);
/* Register an input device when enabled on the menuconfig */
#if CONFIG_LV_TOUCH_CONTROLLER != TOUCH_CONTROLLER_NONE
ESP_LOGI(TAG, "calling: lv_indev_drv_init");
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.read_cb = touch_driver_read;
indev_drv.type = LV_INDEV_TYPE_POINTER;
ESP_LOGI(TAG, "calling: lv_indev_drv_register");
lv_indev_drv_register(&indev_drv);
#endif
// quite these
esp_log_level_set("XPT2046", ESP_LOG_WARN);
esp_log_level_set("spi_master", ESP_LOG_WARN);
/* Create and start a periodic timer interrupt to call lv_tick_inc */
ESP_LOGI(TAG, "starting lv_tick_action timer");
const esp_timer_create_args_t periodic_timer_args = {
.callback = &Display::tickAction,
.arg = nullptr,
.dispatch_method = ESP_TIMER_TASK,
.name = "lv_tick_action"
};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, DISPLAY_LV_TICK_MS*1000)); // (param is microseconds)
ESP_LOGI(TAG, "Creating tabview");
_tabview = new LVTabView(nullptr);
_tabview->setAnimationTime(0);
ESP_LOGI(TAG, "Creating style for tabview");
static LVStyle style_tab_btn;
style_tab_btn.setPad(LV_STATE_DEFAULT, LV_DPX(1), LV_DPX(1), LV_DPX(1), LV_DPX(1));
_tabview->addStyle(LV_TABVIEW_PART_TAB_BTN , &style_tab_btn);
xTaskCreatePinnedToCore(task, "Display", 4096*2, NULL, DISPLAY_TASK_PRI, NULL, DISPLAY_TASK_CORE);
return true;
}
Display::~Display()
{
}
LVPage* Display::newPage(const char* name)
{
ESP_LOGI(TAG, "::newPage name='%s'", name);
LVPage* page = nullptr;
lock(-1000);
page = _tabview->addTab(name);
unlock();
return page;
}
void Display::task(void* data)
{
ESP_LOGI(TAG, "::task starting!");
while(true)
{
Display::getDisplay().lock(-1000);
lv_task_handler(); /* let the GUI do its work */
Display::getDisplay().unlock();
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void Display::tickAction(void *arg) {
(void) arg; // unused
lv_tick_inc(DISPLAY_LV_TICK_MS);
}
bool Display::lock(int timeout_ms)
{
if (!_lock)
{
ESP_LOGE(TAG, "::lock _lock not initialized!");
return false;
}
if (timeout_ms < 0)
{
while (xSemaphoreTakeRecursive( _lock, pdMS_TO_TICKS(-timeout_ms) ) != pdTRUE)
{
ESP_LOGE(TAG, "::lock failed to take lock within 1 second, retrying");
}
return true;
}
return xSemaphoreTakeRecursive( _lock, pdMS_TO_TICKS(timeout_ms) ) == pdTRUE;
}
void Display::unlock()
{
xSemaphoreGiveRecursive(_lock);
}
uint32_t Display::getIdleTime()
{
if (_disp == nullptr)
{
return 0;
}
return lv_disp_get_inactive_time(_disp) / 1000; // return in seconds
}