-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlv_dirver.cpp
194 lines (162 loc) · 4.25 KB
/
lv_dirver.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
#include "lv_dirver.h"
#include <TFT_eSPI.h>
#include <Ticker.h>
#include "board_def.h"
#include <lvgl.h>
#include "freertos/FreeRTOS.h"
#include <FT5206.h>
#define BACKLIGHT_CHANNEL ((uint8_t)1)
static TFT_eSPI *tft = nullptr;
static FT5206_Class *tp = nullptr;
static Ticker lvTicker1;
static Ticker lvTicker2;
static bool tpInit = false;
static TimerHandle_t touch_handle = NULL;
void touch_timer_callback( TimerHandle_t xTimer )
{
#ifdef DEBUG_DEMO
if (touch_handle) {
xTimerDelete(touch_handle, portMAX_DELAY);
touch_handle = NULL;
backlight_off();
}
#endif
}
void touch_timer_create()
{
#ifdef DEBUG_DEMO
if (!touch_handle) {
touch_handle = xTimerCreate("tp", 10000 / portTICK_PERIOD_MS, pdTRUE, (void *)0, touch_timer_callback);
xTimerStart(touch_handle, portMAX_DELAY);
}
#endif
}
static void touch_timer_reset()
{
#ifdef DEBUG_DEMO
if (touch_handle)
xTimerReset(touch_handle, portMAX_DELAY);
#endif
}
static void ex_disp_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t *color_array)
{
uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
printf("x:%d x1:%d y:%d y1:%d size:%u\n", x1, x2, y1, y2,size);
tft->setAddrWindow(x1, y1, x2, y2);
tft->pushColors((uint8_t *)color_array, size);
lv_flush_ready();
}
int tftGetScreenWidth()
{
return tft->width();
}
int tftGetScreenHeight()
{
return tft->height();
}
void display_off()
{
tft->writecommand(TFT_DISPOFF);
tft->writecommand(TFT_SLPIN);
tp->enterSleepMode();
}
void display_sleep()
{
tft->writecommand(TFT_DISPOFF);
tft->writecommand(TFT_SLPIN);
tp->enterMonitorMode();
}
void display_wakeup()
{
tft->writecommand(TFT_SLPOUT);
tft->writecommand(TFT_DISPON);
}
void display_init()
{
tft = new TFT_eSPI(LV_HOR_RES, LV_VER_RES);
tft->init();
tft->setRotation(0);
pinMode(TP_INT, INPUT);
Wire.begin(I2C_SDA, I2C_SCL);
tp = new FT5206_Class();
if (! tp->begin(Wire)) {
Serial.println("Couldn't start FT5206 touchscreen controller");
} else {
tpInit = true;
Serial.println("Capacitive touchscreen started");
}
lv_init();
/*Initialize the display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.disp_flush = ex_disp_flush; /*Used in buffered mode (LV_VDB_SIZE != 0 in lv_conf.h)*/
lv_disp_drv_register(&disp_drv);
/*Initialize the touch pad*/
lv_indev_drv_t indev_drv;
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read = [] (lv_indev_data_t *data) -> bool {
static TP_Point p;
if (!tpInit) return false;
data->state = tp->touched() ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
if (data->state == LV_INDEV_STATE_PR)
{
p = tp->getPoint();
p.x = map(p.x, 0, 320, 0, 240);
p.y = map(p.y, 0, 320, 0, 240);
touch_timer_reset();
}
/*Set the coordinates (if released use the last pressed coordinates)*/
data->point.x = p.x;
data->point.y = p.y;
return false; /*Return false because no moare to be read*/
};
lv_indev_drv_register(&indev_drv);
lvTicker1.attach_ms(1, [] {
lv_tick_inc(1);
});
lvTicker2.attach_ms(5, [] {
lv_task_handler();
});
touch_timer_create();
}
void backlight_init(void)
{
ledcAttachPin(TFT_BL, 1);
ledcSetup(BACKLIGHT_CHANNEL, 12000, 8);
}
uint8_t backlight_getLevel()
{
return ledcRead(BACKLIGHT_CHANNEL);
}
void backlight_adjust(uint8_t level)
{
ledcWrite(BACKLIGHT_CHANNEL, level);
}
void backlight_setting(unsigned char level)
{
switch (level) {
case 1:
ledcWrite(BACKLIGHT_CHANNEL, 100);
break;
case 2:
ledcWrite(BACKLIGHT_CHANNEL, 200);
break;
case 3:
ledcWrite(BACKLIGHT_CHANNEL, 255);
break;
default:
break;
}
}
bool isBacklightOn()
{
return (bool)ledcRead(BACKLIGHT_CHANNEL);
}
void backlight_off()
{
ledcWrite(BACKLIGHT_CHANNEL, 0);
}
void backlight_on()
{
ledcWrite(BACKLIGHT_CHANNEL, 200);
}