Files
luban-lite/bsp/examples_bare/test-rtp/test_rtp_draw.c
刘可亮 564e22b32f v0.7.5
2023-08-28 09:48:01 +08:00

157 lines
3.8 KiB
C

/*
* Copyright (c) 2022-2023, ArtInChip Technology Co., Ltd
*
* SPDX-License-Identifier: Apache-2.0
*
* Authors: matteo <duanmt@artinchip.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <console.h>
#include "aic_common.h"
#include "artinchip_fb.h"
#include "mpp_fb.h"
#include "hal_adcim.h"
#include "hal_rtp.h"
static struct mpp_fb *g_fb = NULL;
static struct aicfb_screeninfo g_fb_info = {0};
static struct aic_rtp_dev g_rtp_dev = {0};
static u32 g_raw = 1;
static int test_get_fb_info(void)
{
int ret = 0;
g_fb = mpp_fb_open();
if (!g_fb) {
pr_err("mpp_fb_open error!!!!\n");
return -1;
}
ret = mpp_fb_ioctl(g_fb, AICFB_GET_SCREENINFO, &g_fb_info);
if (ret < 0) {
pr_err("ioctl() failed! errno: -%d\n", -ret);
return -1;
}
pr_info("Screen width: %d, height %d\n",
g_fb_info.width, g_fb_info.height);
return ret;
}
/* Draw a grid, and each cell size: 200*200 */
static void test_draw_grid(void)
{
u32 i, j;
u8 *fb = g_fb_info.framebuffer;
memset(fb, 0, g_fb_info.smem_len);
for (i = 1; i * 200 < g_fb_info.height; i++)
memset(fb + g_fb_info.stride * (200 * i - 1), 0x30, g_fb_info.stride);
for (i = 0; i < g_fb_info.height; i++)
for (j = 1; j * 200 < g_fb_info.width; j++)
memset(fb + g_fb_info.stride * i + 200 * 4 * j - 4, 0x30, 4);
aicos_dcache_clean_invalid_range(g_fb_info.framebuffer, g_fb_info.smem_len);
}
static void test_draw_a_point(u32 cnt, struct aic_rtp_event *e)
{
u32 panel_x = 0, panel_y = 0;
u32 pos = 0;
u8 *buf = NULL;
panel_x = AIC_RTP_MAX_VAL - e->x;
panel_y = AIC_RTP_MAX_VAL - e->y;
if (g_raw) {
panel_x = (panel_x * g_fb_info.width) / AIC_RTP_MAX_VAL;
panel_y = (panel_y * g_fb_info.height) / AIC_RTP_MAX_VAL;
} else {
// TODO: translate the position with calibrate parameter
}
printf("%d: X %d/%d, Y %d/%d, Press %d\n", cnt,
panel_x, e->x, panel_y, e->y, e->pressure);
pos = panel_y * g_fb_info.stride + panel_x * (g_fb_info.bits_per_pixel / 8);
if (pos < g_fb_info.smem_len) {
buf = g_fb_info.framebuffer + pos;
memset(g_fb_info.framebuffer + pos, 0xFF, 4);
buf -= pos % CACHE_LINE_SIZE;
aicos_dcache_clean_invalid_range(buf, CACHE_LINE_SIZE);
return;
}
pr_err("Invalid position: %d\n", pos);
}
static int test_rtp_init(void)
{
if (hal_adcim_probe())
return -1;
if (hal_rtp_clk_init())
return -1;
g_rtp_dev.x_plate = 235;
g_rtp_dev.y_plate = 0;
g_rtp_dev.mode = RTP_MODE_AUTO2;
g_rtp_dev.max_press = 800;
g_rtp_dev.smp_period = 15;
g_rtp_dev.pressure_det = 1;
aicos_request_irq(RTP_IRQn, hal_rtp_isr, 0, NULL, NULL);
hal_rtp_enable(&g_rtp_dev, 1);
hal_rtp_int_enable(&g_rtp_dev, 1);
hal_rtp_auto_mode(&g_rtp_dev);
return 0;
}
static void ts_rtp_deinit(void)
{
hal_rtp_int_enable(&g_rtp_dev, 0);
hal_rtp_enable(&g_rtp_dev, 0);
}
static int cmd_test_rtp_draw(int argc, char **argv)
{
u32 ret, cnt = 0, max = 1000;
struct aic_rtp_event e = {0};
if (argc > 1)
max = atoi(argv[1]);
if (test_get_fb_info())
return -1;
if (test_rtp_init())
return -1;
test_draw_grid();
pr_info("Try to read %d points from RTP ...\n", max);
do {
memset(&e, 0, sizeof(struct aic_rtp_event));
ret = hal_rtp_ebuf_read(&g_rtp_dev.ebuf, &e);
if (ret < 0)
continue;
if (e.x > 0 || e.y > 0) {
test_draw_a_point(cnt, &e);
cnt++;
}
aicos_msleep(100);
} while (cnt < max);
if (g_fb)
mpp_fb_close(g_fb);
ts_rtp_deinit();
return 0;
}
CONSOLE_CMD(test_rtp_draw, cmd_test_rtp_draw, "RTP test example");