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_gt9xx.h"
12 #include "hpm_touch.h"
13 
14 gt9xx_context_t gt9xx = {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     gt9xx_touch_data_t touch_data = {0};
20     uint8_t num, i;
21     uint16_t tmp;
22 
23     stat = gt9xx_read_touch_data(&gt9xx, &touch_data);
24     if (stat != status_success) {
25         printf("gt9xx read data failed\n");
26         return stat;
27     }
28     /* the buffer status is ready*/
29     if (GT9XX_GET_STATUS_BUFFER_STAT(touch_data.status) == 1) {
30         num = GT9XX_GET_STATUS_NUM_OF_POINTS(touch_data.status);
31         *num_of_points = num;
32         if (num > 0 && num <= GT9XX_MAX_TOUCH_POINTS) {
33             for (i = 0; i < num; i++) {
34                 points[i].x = (touch_data.points[i].x_h & 0xF) << 8 | touch_data.points[i].x_l;
35                 points[i].y = (touch_data.points[i].y_h & 0xF) << 8 | touch_data.points[i].y_l;
36 
37                 if (gt9xx.reverse_x)
38                     points[i].x = gt9xx.abs_x_max - points[i].x;
39 
40                 if (gt9xx.reverse_y)
41                     points[i].y = gt9xx.abs_y_max - points[i].y;
42 
43                 if (gt9xx.exchange_xy) {
44                     tmp = points[i].x;
45                     points[i].x = points[i].y;
46                     points[i].y = tmp;
47                 }
48             }
49         } else {
50             stat = status_touch_points_over_number;
51         }
52         gt9xx_write_register(&gt9xx, GT9XX_STATUS, 0);
53     } else {
54         stat = status_touch_buffer_no_ready;
55     }
56 
57     return stat;
58 }
59 
pull_int_pin(bool high)60 void pull_int_pin(bool high)
61 {
62     gpio_set_pin_output(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN);
63     gpio_write_pin(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN, high);
64 }
65 
float_int_pin(void)66 void float_int_pin(void)
67 {
68     gpio_set_pin_input(BOARD_CAP_INTR_GPIO, BOARD_CAP_INTR_GPIO_INDEX, BOARD_CAP_INTR_GPIO_PIN);
69 }
70 
touch_init(I2C_Type * i2c_ptr)71 hpm_stat_t touch_init(I2C_Type *i2c_ptr)
72 {
73     hpm_stat_t stat = status_success;
74 
75     gt9xx.ptr = i2c_ptr;
76 
77     stat = gt9xx_init(&gt9xx, BOARD_LCD_WIDTH, BOARD_LCD_HEIGHT);
78     if (stat != status_success) {
79         return stat;
80     }
81     gt9xx_write_register(&gt9xx, GT9XX_CMD, GT9XX_CMD_READ_COORD_STAT);
82 
83     return stat;
84 }
85 
touch_config(bool exchange_xy,bool reverse_x,bool reverse_y)86 hpm_stat_t touch_config(bool exchange_xy, bool reverse_x, bool reverse_y)
87 {
88     gt9xx.exchange_xy = exchange_xy;
89     gt9xx.reverse_x = reverse_x;
90     gt9xx.reverse_y = reverse_y;
91 
92     return status_success;
93 }
94