LCD屏顯示實現(xiàn)觸摸功能
驅(qū)動320* 240 ST7789的SPI屏幕進行內(nèi)容顯示,顯示文字和觸控屏的基本使用;參考了:驅(qū)動模型中定義了可供上層應(yīng)用調(diào)用的關(guān)鍵接口,如 讀/寫framebuffer 、開/關(guān)觸控動作的操作畫面 、獲取屏幕設(shè)備信息 、 設(shè)置亮度/對比度/像素格式/方向等。
下圖為觸控屏的四個觸控開/關(guān)四個電機的“按鍵”
圖一(上電開機后顯示)
圖二(1號電機工作)
圖三(2號電機工作)
圖四(3號電機工作)
圖五(4號電機工作)
圖六(等待操作的狀態(tài))
顯示觸控屏的常用API接口 :
void display_get_capabilities(const struct device *dev, struct display_capabilities *capabilities)
參數(shù)說明
圖七
顯示屏設(shè)備樹配置
&csk6002_9s_nano_pinctrl{
...
/* 顯示屏SPI接口配置 */
pinctrl_spi0_sclk_default: spi0_sclk_default {
pinctrls = < &pinmuxa 15 6 >;
};
pinctrl_spi0_mosi_default: spi0_mosi_default {
pinctrls = < &pinmuxa 10 6 >;
};
pinctrl_spi0_miso_default: spi0_miso_default {
pinctrls = < &pinmuxa 17 6 >;
};
pinctrl_spi0_cs_default: spi0_cs_default {
pinctrls = < &pinmuxa 12 6 >;
};
};
觸摸屏設(shè)備樹配置
用到引腳:i2c0_scl(pb2)、i2c0_sda(pb3)。
在 csk6011a_nano.overlay中完成外設(shè)接口的配置,具體實現(xiàn)如下:
在app/boards/目錄下的csk6002_9s_nano.overlay文件并添加如下設(shè)備樹配置:
圖八
&csk6002_9s_nano_pinctrl{
// ...
/* 觸摸屏I2C接口配置 */
pinctrl_i2c0_scl_default: i2c0_scl_default{
pinctrls = <&pinmuxb 2 8>;
};
pinctrl_i2c0_sda_default: i2c0_sda_default{
pinctrls = <&pinmuxb 3 8>;
};
};
&i2c0 {
status = "okay";
pinctrl-0 = <&pinctrl_i2c0_scl_default &pinctrl_i2c0_sda_default>;
pinctrl-names = "default";
ft5336@0 {
compatible = "focaltech,ft5336";
reg = <0x38>;
label = "FT5336";
status = "okay";
int-gpios = <&gpioa 3 0>;
};
};
日志串口設(shè)備樹配置
本示例中將SDK默認的日志串口(GPIOA_2、GPIOA_3)中的GPIOA_3復用為觸控屏的int使能腳,因此將日志輸出串口配置為GPIOb_10 、GPIOb_11,具體如下:
&csk6002_9s_nano_pinctrl{
/* 日志串口配置 */
pinctrl_uart0_rx_default: uart0_rx_default{
pinctrls = <&pinmuxb 10 2>;
};
pinctrl_uart0_tx_default: uart0_tx_default{
pinctrls = <&pinmuxb 11 2>;
};
...
};
觸摸屏組件配置
在prj.conf文件中打開觸摸屏功能配置:
觸摸配置
CONFIG_KSCAN=y
I2C功能配置
CONFIG_I2C=y
觸摸屏屏驅(qū)動配置
CONFIG_KSCAN_BL6XXX=y
顯示屏組件配置
在prj.conf文件中打開顯示屏功能配置:
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_LOG=y
GPIO功能配置
CONFIG_GPIO=y
顯示功能配置
CONFIG_DISPLAY=y
spi功能配置
CONFIG_SPI=y
ST7789V顯示屏驅(qū)動配置
CONFIG_ST7789V=y
LCD屏顯示實現(xiàn)
void main(void)
{
size_t x;
size_t y;
size_t rect_w;
size_t rect_h;
size_t h_step;
size_t scale;
size_t grey_count;
uint8_t *buf;
int32_t grey_scale_sleep;
const struct device *display_dev;
struct display_capabilities capabilities;
struct display_buffer_descriptor buf_desc;
size_t buf_size = 0;
fill_buffer fill_buffer_fnc = NULL;
LOG_INF("Display sample for %s", DISPLAY_DEV_NAME);
/* kscan初始化 */
kscan_init();
/* 獲取display設(shè)備實例 */
display_dev = device_get_binding(DISPLAY_DEV_NAME);
if (display_dev == NULL) {
LOG_ERR("Device %s not found. Aborting sample.",
DISPLAY_DEV_NAME);
RETURN_FROM_MAIN(1);
}
/* 獲取顯示功能 */
display_get_capabilities(display_dev, &capabilities);
if (capabilities.screen_info & SCREEN_INFO_MONO_VTILED) {
rect_w = 32;
rect_h = 10;
} else {
rect_w = 2;
rect_h = 1;
}
h_step = rect_h;
scale = (capabilities.x_resolution / 8) / rect_h;
rect_w *= scale;
rect_h *= scale;
if (capabilities.screen_info & SCREEN_INFO_EPD) {
grey_scale_sleep = 10000;
} else {
grey_scale_sleep = 100;
}
buf_size = rect_w * rect_h;
if (buf_size < (capabilities.x_resolution * h_step)) {
buf_size = capabilities.x_resolution * h_step;
}
/* 色塊配置 */
switch (capabilities.current_pixel_format) {
case PIXEL_FORMAT_ARGB_8888:
fill_buffer_fnc = fill_buffer_argb8888;
buf_size *= 4;
break;
case PIXEL_FORMAT_RGB_888:
fill_buffer_fnc = fill_buffer_rgb888;
buf_size *= 3;
break;
case PIXEL_FORMAT_RGB_565:
fill_buffer_fnc = fill_buffer_rgb565;
buf_size *= 2;
break;
case PIXEL_FORMAT_BGR_565:
fill_buffer_fnc = fill_buffer_bgr565;
buf_size *= 2;
break;
case PIXEL_FORMAT_MONO01:
case PIXEL_FORMAT_MONO10:
fill_buffer_fnc = fill_buffer_mono;
buf_size /= 8;
break;
default:
LOG_ERR("Unsupported pixel format. Aborting sample.");
RETURN_FROM_MAIN(1);
}
buf = k_malloc(buf_size);
if (buf == NULL) {
LOG_ERR("Could not allocate memory. Aborting sample.");
RETURN_FROM_MAIN(1);
}
(void)memset(buf, 0xFFu, buf_size);
buf_desc.buf_size = buf_size;
buf_desc.pitch = capabilities.x_resolution;
buf_desc.width = capabilities.x_resolution;
buf_desc.height = h_step;
/*整屏填充白色背景*/
for (int idx = 0; idx < capabilities.y_resolution; idx += h_step) {
display_write(display_dev, 0, idx, &buf_desc, buf);
}
/*左上角填充紅色塊*/
fill_buffer_fnc(TOP_LEFT, 0, buf, buf_size);
x = 0;
y = 0;
display_write(display_dev, x, y, &buf_desc, buf);
/*右上角填充綠色塊*/
fill_buffer_fnc(TOP_RIGHT, 0, buf, buf_size);
x = capabilities.x_resolution - rect_w;
y = 0;
display_write(display_dev, x, y, &buf_desc, buf);
/*右下角填充藍色塊*/
fill_buffer_fnc(BOTTOM_RIGHT, 0, buf, buf_size);
x = capabilities.x_resolution - rect_w;
y = capabilities.y_resolution - rect_h;
display_write(display_dev, x, y, &buf_desc, buf);
/* 關(guān)閉顯示消隱 */
display_blanking_off(display_dev);
grey_count = 0;
x = 0;
y = capabilities.y_resolution - rect_h;
/*左下角灰色動態(tài)色塊*/
while (1) {
fill_buffer_fnc(BOTTOM_LEFT, grey_count, buf, buf_size);
display_write(display_dev, x, y, &buf_desc, buf);
++grey_count;
k_msleep(grey_scale_sleep);
}
}
觸摸屏功能實現(xiàn)
Zephyr具備kscan(keyboard scan matrix)驅(qū)動模型,其驅(qū)動程序用于檢測矩陣鍵盤或帶有按鈕的設(shè)備中的按鍵(用戶觸碰觸摸屏本質(zhì)上是生成一個行列坐標)。查閱zephyr官網(wǎng)Display Interface。
圖九
注冊callback函數(shù)
/* 觸摸回調(diào)函數(shù),打印坐標 */
static void k_callback(const struct device *dev, uint32_t row, uint32_t col,
bool pressed)
{
ARG_UNUSED(dev);
printk("row = %u col = %u, pressed:%s\\n", row, col, pressed ? "TRUE" : "FLASE");
}
void kscan_init(void)
{
/* 獲取kscan設(shè)備實例 */
const struct device *kscan_dev = device_get_binding(DISPLAY_KSAN_DEV_NAME);
if (!device_is_ready(kscan_dev)) {
LOG_ERR("kscan device %s not ready", kscan_dev->name);
return;
}
/* 注冊回調(diào),在k_callback中可看到對應(yīng)的坐標與狀態(tài)printk輸出操作 */
kscan_config(kscan_dev, k_callback);
/* 使能回調(diào) */
kscan_enable_callback(kscan_dev);
}
void main(void)
{
kscan_init();
...
}
編譯的指令是:
lisa zep build
燒錄的指令是:
lisa zep flash
結(jié)果
觸控屏顯示了四個觸控開關(guān)符,分別觸摸對應(yīng)顯示在(LED1符 LED2符 LED3符 LED4符的下面觸控開關(guān))。四個功能(GPIO) PWM 控制電機運行,后面補上(高發(fā)性的疫情過后,回公司上班補上,焊接上電機驅(qū)動板)。我們開發(fā)了一個PC上位機軟件(視覺圖分析用)。GC032A攝像頭上傳的圖像以后可以在觸控屏顯(一個靜態(tài)圖)。
圖十
審核編輯:劉清
-
SPI
+關(guān)注
關(guān)注
17文章
1701瀏覽量
91345 -
LCD觸摸屏
+關(guān)注
關(guān)注
0文章
11瀏覽量
6684 -
回調(diào)函數(shù)
+關(guān)注
關(guān)注
0文章
87瀏覽量
11543 -
控制電機
+關(guān)注
關(guān)注
0文章
237瀏覽量
18087
發(fā)布評論請先 登錄
相關(guān)推薦
評論