RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for Touch:

Data Structures

struct  rt_touch_info
 
struct  rt_touch_config
 
struct  rt_touch_device
 
struct  rt_touch_data
 
struct  rt_touch_ops
 

Functions

int rt_hw_touch_register (rt_touch_t touch, const char *name, rt_uint32_t flag, void *data)
 
void rt_hw_touch_isr (rt_touch_t touch)
 

Detailed Description

Touch driver api.

Example

#include <rtthread.h>
#include "rtdevice.h"
static rt_thread_t gt9147_thread = RT_NULL;
static rt_sem_t gt9147_sem = RT_NULL;
static rt_device_t dev = RT_NULL;
static struct rt_touch_data *read_data;
// 读取数据线程入口函数
static void gt9147_entry(void *parameter)
{
struct rt_touch_data *read_data;
read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * 5);
while (1)
{
// 请求信号量
rt_sem_take(gt9147_sem, RT_WAITING_FOREVER);
// 读取五个点的触摸信息
if (rt_device_read(dev, 0, read_data, 5) == 5)
{
for (rt_uint8_t i = 0; i < 5; i++)
{
if (read_data[i].event == RT_TOUCH_EVENT_DOWN || read_data[i].event == RT_TOUCH_EVENT_MOVE)
{
rt_kprintf("%d %d %d %d %d\n",
read_data[i].track_id,
read_data[i].x_coordinate,
read_data[i].y_coordinate,
read_data[i].timestamp,
read_data[i].width);
}
}
}
// 打开中断
rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
}
}
// 接收回调函数
static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
{
// 关闭中断
rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
// 释放信号量
rt_sem_release(gt9147_sem);
return 0;
}
static int gt9147_sample(void)
{
// 查找 Touch 设备
dev = rt_device_find("touch");
if (dev == RT_NULL)
{
rt_kprintf("can't find device:%s\n", "touch");
return -1;
}
// 以中断的方式打开设备
if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
rt_kprintf("open device failed!");
return -1;
}
// 设置接收回调
rt_device_set_rx_indicate(dev, rx_callback);
// 创建信号量
gt9147_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO);
if (gt9147_sem == RT_NULL)
{
rt_kprintf("create dynamic semaphore failed.\n");
return -1;
}
// 创建读取数据线程
gt9147_thread = rt_thread_create("thread1",
gt9147_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);
// 启动线程
if (gt9147_thread != RT_NULL)
rt_thread_startup(gt9147_thread);
return 0;
}
MSH_CMD_EXPORT(gt9147_sample, gt9147 sample);
#define RT_DEVICE_FLAG_INT_RX
Definition: rtdef.h:1297
#define RT_IPC_FLAG_PRIO
Definition: rtdef.h:973
#define RT_WAITING_FOREVER
Definition: rtdef.h:980
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
Definition: kservice.c:807
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
Definition: thread.c:393
rt_thread_t rt_thread_create(const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick)
This function will create a thread object and allocate thread object memory. and stack.
Definition: thread.c:523
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
Creating a semaphore object.
Definition: ipc.c:467
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
Definition: ipc.c:695
Definition: rtdef.h:1370
Definition: rtdef.h:1003
Definition: rtdef.h:852
Touch data.
Definition: dev_touch.h:203

Function Documentation

◆ rt_hw_touch_register()

int rt_hw_touch_register ( rt_touch_t  touch,
const char *  name,
rt_uint32_t  flag,
void *  data 
)

register a touch device

Parameters
touchthe touch device
namethe name of touch device
flagthe flag of touch device
datathe user data of touch device
Returns
rt_err_t error code

◆ rt_hw_touch_isr()

void rt_hw_touch_isr ( rt_touch_t  touch)

Touch irq handle.

Parameters
touchthe touch device
Note
If you doesn't use pin device. you must call this function in your touch irq callback