1 /*
2 * Copyright (c) 2021 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #include "board.h"
9 #include "hpm_touch.h"
10 #include "hpm_gpio_drv.h"
11 #include "hpm_gt911.h"
12 #include "hpm_touch.h"
13
14 gt911_context_t gt911 = {0};
15
touch_get_data(touch_point_t * points,uint8_t * num_of_points)16 hpm_stat_t touch_get_data(touch_point_t *points, uint8_t *num_of_points)
17 {
18 hpm_stat_t stat = status_success;
19 gt911_touch_data_t touch_data = {0};
20 uint8_t num, i;
21
22 stat = gt911_read_touch_data(>911, &touch_data);
23 if (stat != status_success) {
24 printf("gt911 read data failed\n");
25 return stat;
26 }
27 /* the buffer status is ready*/
28 if (GT911_GET_STATUS_BUFFER_STAT(touch_data.status) == 1) {
29 num = GT911_GET_STATUS_NUM_OF_POINTS(touch_data.status);
30 *num_of_points = num;
31 if (num > 0 && num <= GT911_MAX_TOUCH_POINTS) {
32 for (i = 0; i < num; i++) {
33 points[i].x = (touch_data.points[i].x_h & 0xF) << 8 | touch_data.points[i].x_l;
34 points[i].y = (touch_data.points[i].y_h & 0xF) << 8 | touch_data.points[i].y_l;
35 }
36 } else {
37 stat = status_touch_points_over_number;
38 }
39 } else {
40 stat = status_touch_buffer_no_ready;
41 }
42 gt911_write_register(>911, GT911_STATUS, 0);
43 return stat;
44 }
45
pull_int_pin(bool high)46 void pull_int_pin(bool high)
47 {
48 gpio_set_pin_output(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN);
49 gpio_write_pin(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN, high);
50 }
51
float_int_pin(void)52 void float_int_pin(void)
53 {
54 gpio_set_pin_input(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN);
55 }
56
touch_init(I2C_Type * i2c_ptr)57 hpm_stat_t touch_init(I2C_Type *i2c_ptr)
58 {
59 hpm_stat_t stat = status_success;
60
61 gt911.ptr = i2c_ptr;
62
63 stat = gt911_init(>911, BOARD_LCD_WIDTH, BOARD_LCD_HEIGHT);
64 if (stat != status_success) {
65 return stat;
66 }
67 gt911_write_register(>911, GT911_CMD, GT911_CMD_READ_COORD_STAT);
68
69 return stat;
70 }
71