|
| 1 | +/* |
| 2 | +* Copyright © 2021 Sturnus Inc. |
| 3 | +
|
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 5 | +* software and associated documentation files (the “Software”), to deal in the Software |
| 6 | +* without restriction, including without limitation the rights to use, copy, modify, merge, |
| 7 | +* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 8 | +* to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +* |
| 10 | +* The above copyright notice and this permission notice shall be included in all copies or |
| 11 | +* substantial portions of the Software. |
| 12 | +* |
| 13 | +* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 14 | +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 15 | +* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 16 | +* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 17 | +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | +* SOFTWARE. |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <esp_log.h> |
| 22 | +#include <driver/i2c.h> |
| 23 | +#ifdef LV_LVGL_H_INCLUDE_SIMPLE |
| 24 | +#include <lvgl.h> |
| 25 | +#else |
| 26 | +#include <lvgl/lvgl.h> |
| 27 | +#endif |
| 28 | +#include "gt911.h" |
| 29 | +#include "tp_i2c.h" |
| 30 | +#include "../lvgl_i2c_conf.h" |
| 31 | + |
| 32 | +#define TAG "GT911" |
| 33 | + |
| 34 | +gt911_status_t gt911_status; |
| 35 | + |
| 36 | +//TODO: handle multibyte read and refactor to just one read transaction |
| 37 | +esp_err_t gt911_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) { |
| 38 | + i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); |
| 39 | + |
| 40 | + i2c_master_start(i2c_cmd); |
| 41 | + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); |
| 42 | + i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); |
| 43 | + i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); |
| 44 | + |
| 45 | + i2c_master_start(i2c_cmd); |
| 46 | + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true); |
| 47 | + |
| 48 | + i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK); |
| 49 | + i2c_master_stop(i2c_cmd); |
| 50 | + esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); |
| 51 | + i2c_cmd_link_delete(i2c_cmd); |
| 52 | + return ret; |
| 53 | +} |
| 54 | + |
| 55 | +esp_err_t gt911_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) { |
| 56 | + i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); |
| 57 | + |
| 58 | + i2c_master_start(i2c_cmd); |
| 59 | + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); |
| 60 | + i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); |
| 61 | + i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); |
| 62 | + i2c_master_write_byte(i2c_cmd, data, I2C_MASTER_ACK); |
| 63 | + i2c_master_stop(i2c_cmd); |
| 64 | + esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); |
| 65 | + i2c_cmd_link_delete(i2c_cmd); |
| 66 | + return ret; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Initialize for GT911 communication via I2C |
| 71 | + * @param dev_addr: Device address on communication Bus (I2C slave address of GT911). |
| 72 | + * @retval None |
| 73 | + */ |
| 74 | +void gt911_init(uint8_t dev_addr) { |
| 75 | + if (!gt911_status.inited) { |
| 76 | + gt911_status.i2c_dev_addr = dev_addr; |
| 77 | + uint8_t data_buf; |
| 78 | + esp_err_t ret; |
| 79 | + |
| 80 | + ESP_LOGI(TAG, "Checking for GT911 Touch Controller"); |
| 81 | + if ((ret = gt911_i2c_read(dev_addr, GT911_PRODUCT_ID1, &data_buf, 1) != ESP_OK)) { |
| 82 | + ESP_LOGE(TAG, "Error reading from device: %s", |
| 83 | + esp_err_to_name(ret)); // Only show error the first time |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Read 4 bytes for Product ID in ASCII |
| 88 | + for (int i = 0; i < GT911_PRODUCT_ID_LEN; i++) { |
| 89 | + gt911_i2c_read(dev_addr, (GT911_PRODUCT_ID1 + i), (uint8_t *)&(gt911_status.product_id[i]), 1); |
| 90 | + } |
| 91 | + ESP_LOGI(TAG, "\tProduct ID: %s", gt911_status.product_id); |
| 92 | + |
| 93 | + gt911_i2c_read(dev_addr, GT911_VENDOR_ID, &data_buf, 1); |
| 94 | + ESP_LOGI(TAG, "\tVendor ID: 0x%02x", data_buf); |
| 95 | + |
| 96 | + gt911_i2c_read(dev_addr, GT911_X_COORD_RES_L, &data_buf, 1); |
| 97 | + gt911_status.max_x_coord = data_buf; |
| 98 | + gt911_i2c_read(dev_addr, GT911_X_COORD_RES_H, &data_buf, 1); |
| 99 | + gt911_status.max_x_coord |= ((uint16_t)data_buf << 8); |
| 100 | + ESP_LOGI(TAG, "\tX Resolution: %d", gt911_status.max_x_coord); |
| 101 | + |
| 102 | + gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_L, &data_buf, 1); |
| 103 | + gt911_status.max_y_coord = data_buf; |
| 104 | + gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_H, &data_buf, 1); |
| 105 | + gt911_status.max_y_coord |= ((uint16_t)data_buf << 8); |
| 106 | + ESP_LOGI(TAG, "\tY Resolution: %d", gt911_status.max_y_coord); |
| 107 | + gt911_status.inited = true; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Get the touch screen X and Y positions values. Ignores multi touch |
| 113 | + * @param drv: |
| 114 | + * @param data: Store data here |
| 115 | + * @retval Always false |
| 116 | + */ |
| 117 | +bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data) { |
| 118 | + uint8_t touch_pnt_cnt; // Number of detected touch points |
| 119 | + static int16_t last_x = 0; // 12bit pixel value |
| 120 | + static int16_t last_y = 0; // 12bit pixel value |
| 121 | + uint8_t data_buf; |
| 122 | + uint8_t status_reg; |
| 123 | + |
| 124 | + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_STATUS_REG, &status_reg, 1); |
| 125 | +// ESP_LOGI(TAG, "\tstatus: 0x%02x", status_reg); |
| 126 | + touch_pnt_cnt = status_reg & 0x0F; |
| 127 | + if ((status_reg & 0x80) || (touch_pnt_cnt < 6)) { |
| 128 | + //Reset Status Reg Value |
| 129 | + gt911_i2c_write8(gt911_status.i2c_dev_addr, GT911_STATUS_REG, 0x00); |
| 130 | + } |
| 131 | + if (touch_pnt_cnt != 1) { // ignore no touch & multi touch |
| 132 | + data->point.x = last_x; |
| 133 | + data->point.y = last_y; |
| 134 | + data->state = LV_INDEV_STATE_REL; |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | +// gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_TRACK_ID1, &data_buf, 1); |
| 139 | +// ESP_LOGI(TAG, "\ttrack_id: %d", data_buf); |
| 140 | + |
| 141 | + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_L, &data_buf, 1); |
| 142 | + last_x = data_buf; |
| 143 | + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_H, &data_buf, 1); |
| 144 | + last_x |= ((uint16_t)data_buf << 8); |
| 145 | + |
| 146 | + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_L, &data_buf, 1); |
| 147 | + last_y = data_buf; |
| 148 | + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_H, &data_buf, 1); |
| 149 | + last_y |= ((uint16_t)data_buf << 8); |
| 150 | + |
| 151 | +#if CONFIG_LV_GT911_INVERT_X |
| 152 | + last_x = gt911_status.max_x_coord - last_x; |
| 153 | +#endif |
| 154 | +#if CONFIG_LV_GT911_INVERT_Y |
| 155 | + last_y = gt911_status.max_y_coord - last_y; |
| 156 | +#endif |
| 157 | +#if CONFIG_LV_GT911_SWAPXY |
| 158 | + int16_t swap_buf = last_x; |
| 159 | + last_x = last_y; |
| 160 | + last_y = swap_buf; |
| 161 | +#endif |
| 162 | + data->point.x = last_x; |
| 163 | + data->point.y = last_y; |
| 164 | + data->state = LV_INDEV_STATE_PR; |
| 165 | + ESP_LOGI(TAG, "X=%u Y=%u", data->point.x, data->point.y); |
| 166 | + ESP_LOGV(TAG, "X=%u Y=%u", data->point.x, data->point.y); |
| 167 | + return false; |
| 168 | +} |
0 commit comments