mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-12 09:17:42 +03:00
ESP32 | LVGL8 | Ep 1. Demos with LILY PI
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
/////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
ESP32 | LVGL8 | Ep 1. LVGL Demos with LILY PI
|
||||
Video Tutorial: https://youtu.be/kRTFc2vY5A8
|
||||
Created by Eric N. (ThatProject)
|
||||
*/
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <lvgl.h>
|
||||
#include <lv_demo.h>
|
||||
|
||||
#define LGFX_USE_V1
|
||||
#include <LovyanGFX.hpp>
|
||||
#include <FT6236.h>
|
||||
#define SDA_FT6236 21
|
||||
#define SCL_FT6236 22
|
||||
FT6236 ts = FT6236();
|
||||
|
||||
class LGFX : public lgfx::LGFX_Device
|
||||
{
|
||||
lgfx::Panel_ST7796 _panel_instance;
|
||||
lgfx::Bus_SPI _bus_instance; // SPIバスのインスタンス
|
||||
lgfx::Light_PWM _light_instance;
|
||||
|
||||
public:
|
||||
LGFX(void)
|
||||
{
|
||||
{ // バス制御の設定を行います。
|
||||
auto cfg = _bus_instance.config(); // バス設定用の構造体を取得します。
|
||||
cfg.spi_host = VSPI_HOST; // 使用するSPIを選択 (VSPI_HOST or HSPI_HOST)
|
||||
cfg.spi_mode = 0; // SPI通信モードを設定 (0 ~ 3)
|
||||
cfg.freq_write = 40000000; // 送信時のSPIクロック (最大80MHz, 80MHzを整数で割った値に丸められます)
|
||||
cfg.freq_read = 16000000; // 受信時のSPIクロック
|
||||
cfg.spi_3wire = false; // 受信をMOSIピンで行う場合はtrueを設定
|
||||
cfg.use_lock = true; // トランザクションロックを使用する場合はtrueを設定
|
||||
cfg.dma_channel = 1; // Set the DMA channel (1 or 2. 0=disable) 使用するDMAチャンネルを設定 (0=DMA不使用)
|
||||
cfg.pin_sclk = 18; // SPIのSCLKピン番号を設定
|
||||
cfg.pin_mosi = 19; // SPIのMOSIピン番号を設定
|
||||
cfg.pin_miso = 23; // SPIのMISOピン番号を設定 (-1 = disable)
|
||||
cfg.pin_dc = 27; // SPIのD/Cピン番号を設定 (-1 = disable)
|
||||
_bus_instance.config(cfg); // 設定値をバスに反映します。
|
||||
_panel_instance.setBus(&_bus_instance); // バスをパネルにセットします。
|
||||
}
|
||||
|
||||
{ // 表示パネル制御の設定を行います。
|
||||
auto cfg = _panel_instance.config(); // 表示パネル設定用の構造体を取得します。
|
||||
cfg.pin_cs = 5; // CSが接続されているピン番号 (-1 = disable)
|
||||
cfg.pin_rst = -1; // RSTが接続されているピン番号 (-1 = disable)
|
||||
cfg.pin_busy = -1; // BUSYが接続されているピン番号 (-1 = disable)
|
||||
cfg.memory_width = 320; // ドライバICがサポートしている最大の幅
|
||||
cfg.memory_height = 480; // ドライバICがサポートしている最大の高さ
|
||||
cfg.panel_width = 320; // 実際に表示可能な幅
|
||||
cfg.panel_height = 480; // 実際に表示可能な高さ
|
||||
cfg.offset_x = 0; // パネルのX方向オフセット量
|
||||
cfg.offset_y = 0; // パネルのY方向オフセット量
|
||||
cfg.offset_rotation = 0; // 回転方向の値のオフセット 0~7 (4~7は上下反転)
|
||||
cfg.dummy_read_pixel = 8; // ピクセル読出し前のダミーリードのビット数
|
||||
cfg.dummy_read_bits = 1; // ピクセル以外のデータ読出し前のダミーリードのビット数
|
||||
cfg.readable = true; // データ読出しが可能な場合 trueに設定
|
||||
cfg.invert = false; // パネルの明暗が反転してしまう場合 trueに設定
|
||||
cfg.rgb_order = false; // パネルの赤と青が入れ替わってしまう場合 trueに設定
|
||||
cfg.dlen_16bit = false; // データ長を16bit単位で送信するパネルの場合 trueに設定
|
||||
cfg.bus_shared = true; // SDカードとバスを共有している場合 trueに設定(drawJpgFile等でバス制御を行います)
|
||||
|
||||
_panel_instance.config(cfg);
|
||||
}
|
||||
|
||||
{ // バックライト制御の設定を行います。(必要なければ削除)
|
||||
auto cfg = _light_instance.config(); // バックライト設定用の構造体を取得します。
|
||||
|
||||
cfg.pin_bl = 12; // バックライトが接続されているピン番号
|
||||
cfg.invert = false; // バックライトの輝度を反転させる場合 true
|
||||
cfg.freq = 44100; // バックライトのPWM周波数
|
||||
cfg.pwm_channel = 7; // 使用するPWMのチャンネル番号
|
||||
|
||||
_light_instance.config(cfg);
|
||||
_panel_instance.setLight(&_light_instance); // バックライトをパネルにセットします。
|
||||
}
|
||||
|
||||
setPanel(&_panel_instance); // 使用するパネルをセットします。
|
||||
}
|
||||
};
|
||||
|
||||
LGFX tft;
|
||||
|
||||
/*Change to your screen resolution*/
|
||||
static const uint32_t screenWidth = 480;
|
||||
static const uint32_t screenHeight = 320;
|
||||
static lv_disp_draw_buf_t draw_buf;
|
||||
static lv_color_t buf[ screenWidth * 10 ];
|
||||
|
||||
/* 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.startWrite();
|
||||
tft.setAddrWindow( area->x1, area->y1, w, h );
|
||||
//tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
|
||||
tft.writePixels((lgfx::rgb565_t *)&color_p->full, w * h);
|
||||
tft.endWrite();
|
||||
|
||||
lv_disp_flush_ready( disp );
|
||||
}
|
||||
|
||||
/*Read the touchpad*/
|
||||
void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
|
||||
{
|
||||
if(ts.touched()){
|
||||
data->state = LV_INDEV_STATE_PR;
|
||||
TS_Point p = ts.getPoint();
|
||||
data->point.x = p.y;
|
||||
data->point.y = tft.height() - p.x;
|
||||
|
||||
Serial.printf("x-%d,y-%d\n", data->point.x, data->point.y);
|
||||
}else{
|
||||
data->state = LV_INDEV_STATE_REL;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
tft.begin();
|
||||
tft.setRotation(1);
|
||||
tft.setBrightness(255);
|
||||
|
||||
if(!ts.begin(40, SDA_FT6236, SCL_FT6236)){
|
||||
Serial.println("Unable to start the capacitive touch Screen.");
|
||||
}
|
||||
|
||||
|
||||
lv_init();
|
||||
lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );
|
||||
|
||||
/*Initialize the display*/
|
||||
static lv_disp_drv_t disp_drv;
|
||||
lv_disp_drv_init(&disp_drv);
|
||||
|
||||
/*Change the following line to your display resolution*/
|
||||
disp_drv.hor_res = screenWidth;
|
||||
disp_drv.ver_res = screenHeight;
|
||||
disp_drv.flush_cb = my_disp_flush;
|
||||
disp_drv.draw_buf = &draw_buf;
|
||||
lv_disp_drv_register(&disp_drv);
|
||||
|
||||
/*Initialize the (dummy) input device driver*/
|
||||
static 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);
|
||||
|
||||
//lv_example_get_started_1();
|
||||
//lv_demo_music();
|
||||
lv_demo_widgets();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
lv_timer_handler(); /* let the GUI do its work */
|
||||
delay( 5 );
|
||||
}
|
||||
|
||||
static void btn_event_cb(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * btn = lv_event_get_target(e);
|
||||
if(code == LV_EVENT_CLICKED) {
|
||||
static uint8_t cnt = 0;
|
||||
cnt++;
|
||||
|
||||
/*Get the first child of the button which is the label and change its text*/
|
||||
lv_obj_t * label = lv_obj_get_child(btn, 0);
|
||||
lv_label_set_text_fmt(label, "Button: %d", cnt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a button with a label and react on click event.
|
||||
*/
|
||||
void lv_example_get_started_1(void)
|
||||
{
|
||||
lv_obj_t * btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
|
||||
lv_obj_set_size(btn, 120, 50); /*Set its size*/
|
||||
lv_obj_align(btn, LV_ALIGN_CENTER, 0,0);
|
||||
lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
|
||||
|
||||
lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/
|
||||
lv_label_set_text(label, "Button"); /*Set the labels text*/
|
||||
lv_obj_center(label);
|
||||
}
|
||||
Reference in New Issue
Block a user