#include <lvgl.h>
#include <lv_examples.h>
#include "SPI.h"
#include "AmebaILI9341.h"
#include <Adafruit_FT6206.h>
#define TFT_RESET 5
#define TFT_DC 9
#define TFT_CS 10
AmebaILI9341 tft = AmebaILI9341(TFT_CS, TFT_DC, TFT_RESET);
#define ILI9341_SPI_FREQUENCY 50000000
Adafruit_FT6206 ts = Adafruit_FT6206();
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10];
#if USE_LV_LOG != 0
/* Serial debugging */
void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
{
Serial.printf("%s@%d->%s\r\n", file, line, dsc);
Serial.flush();
}
#endif
/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.setAddress(area->x1, area->y1, area->x2, area->y2);
//tft.pushColors(&color_p->full, w * h, true);
uint16_t *pImg = &color_p->full;
uint32_t mcu_pixels = w * h;
digitalWrite(TFT_CS, LOW);
// push all the image block pixels to the screen
digitalWrite(TFT_DC, HIGH);
while (mcu_pixels--) {
uint16_t pixel = *pImg++;
SPI.transfer(pixel >> 8);
SPI.transfer(pixel & 0xFF);
}
digitalWrite(TFT_CS, HIGH);
lv_disp_flush_ready(disp);
}
/*Read the touchpad*/
bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
{
bool touched = ts.touched();
if (!touched) {
data->state = LV_INDEV_STATE_REL;
} else {
data->state = LV_INDEV_STATE_PR;
TS_Point p = ts.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
int y = tft.getHeight() - p.x;
int x = p.y;
/*Set the coordinates*/
data->point.x = x;
data->point.y = y;
Serial.print("Data x");
Serial.println(x);
Serial.print("Data y");
Serial.println(y);
}
return false; /*Return `false` because we are not buffering and no more data to read*/
}
void setup()
{
Serial.begin(115200); /* prepare for possible serial debug */
SPI.setDefaultFrequency(ILI9341_SPI_FREQUENCY);
lv_init();
#if USE_LV_LOG != 0
lv_log_register_print_cb(my_print); /* register print function for debugging */
#endif
tft.begin(); /* TFT init */
tft.setRotation(1); /* Landscape orientation */
if (!ts.begin(40)) {
Serial.println("Unable to start touchscreen.");
}
lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
/*Initialize the display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 320;
disp_drv.ver_res = 240;
disp_drv.flush_cb = my_disp_flush;
disp_drv.buffer = &disp_buf;
lv_disp_drv_register(&disp_drv);
/*Initialize the (dummy) input device driver*/
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
//Try an example from the lv_examples repository
//https://github.com/lvgl/lv_examples
lv_demo_widgets();
}
void loop()
{
lv_task_handler(); /* let the GUI do its work */
delay(5);
}
Comments
Please log in or sign up to comment.