1e41f4b71Sopenharmony_ci# Touchscreen 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci### Functions 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe touchscreen driver powers on its integrated circuit (IC), initializes hardware pins, registers interrupts, configures the communication (I2C or SPI) interface, sets input configurations, and downloads and updates firmware. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciThe touchscreen driver is developed based on the OpenHarmony input driver model, which applies basic APIs of the operating system abstraction layer (OSAL) and platform interface layer on the OpenHarmony Hardware Driver Foundation [(HDF)](driver-overview-foundation.md). Common APIs include the bus communication APIs and OS native APIs (such as memory, lock, thread, and timer APIs). The OSAL and platform APIs shield the differences of underlying hardware. This allows the use of the touchscreen driver across platforms and OSs. In this regard, you can develop the touchscreen driver only once and deploy it on multiple devices. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci### Working Principles 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciThe input driver model is developed based on the HDF and APIs of the platform and OSAL. It provides hardware driver capabilities through the input Hardware Driver Interface (HDI) for upper-layer input services to control the touchscreen. The following figure shows the architecture of the input driver model. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci**Figure 1** Input driver model 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciThe input driver model consists of the following: 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci- Input Device Manager: provides APIs for input device drivers to register and deregister input devices and manages the input device list in a unified manner. 23e41f4b71Sopenharmony_ci- Common input drivers: provide common APIs that are applicable to different input devices (such as the common driver APIs for touchscreens). The APIs can be used to initialize board-specific hardware, handle hardware interrupts, and register input devices with the Input Device Manager. 24e41f4b71Sopenharmony_ci- Input chip drivers: provide differentiated APIs for the drivers form different vendors. You can use these APIs to develop your drivers with minimum modification. 25e41f4b71Sopenharmony_ci- Event Hub: provides a unified channel for different input devices to report input events. 26e41f4b71Sopenharmony_ci- HDF input config: parses and manages the board-specific and private configuration of input devices.<br>The input driver model provides configuration files to help you quickly develop your drivers. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## How to Develop 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### When to Use 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciThe input module provides APIs for powering on the touchscreen driver IC, configuring and initializing hardware pins, registering interrupts, configuring the communication (I2C or SPI) interface, setting input configurations, and downloading and updating firmware. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### Available APIs 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci#### Hardware Interfaces 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciThe hardware interfaces for touchscreens can be classified into the following types based on the pin attributes: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- Power interfaces 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- I/O control interfaces 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci- Communication interfaces 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Figure 2** Common touchscreen pins 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ciThe interfaces shown in the preceding figure are described as follows: 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci1. **Power interfaces** 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci - **LDO_1P8**: 1.8 V digital circuit 56e41f4b71Sopenharmony_ci - **LDO_3P3**: 3.3 V analog circuit 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci If the touchscreen driver and ICD driver have its own IC, the touchscreen driver IC requires 1.8 V and 3.3 V power supplies. If the touchscreen driver and LCD driver have an integrated IC, you only need to care about the 1.8 V power supply for the touchscreen. The 3.3 V power supply required can be provided by the LCD VSP power (typically 5.5 V) in the driver IC. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci2. **I/O control interfaces** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci - **RESET**: pin used to reset the driver IC on the host when the kernel is put into hibernation or waken up. 63e41f4b71Sopenharmony_ci - **INT**: interrupt pin, which must be set to the input pull-up state during driver initialization. After detecting an external touch signal, the driver triggers an interrupt by operating the interrupt pin. Then, the driver reads the touch reporting data in an interrupt handler. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci3. **Communication interfaces** 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci - I2C: I2C is used if a small amount of data is reported by the touchscreen. For details about the I2C protocol and related operation APIs, see [I2C](../driver/driver-platform-i2c-des.md). 68e41f4b71Sopenharmony_ci - SPI: SPI is used if a large amount of data is reported by the touchscreen. For details about the SPI protocol and related operation APIs, see [SPI](../driver/driver-platform-spi-des.md). 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci#### Software Interfaces 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciThe HDI driver APIs provided for the input service can be classified into the input manager module, input reporter module, and input controller module. The following tables describe the available APIs. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci- input_manager.h 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci | API | Description | 77e41f4b71Sopenharmony_ci | ------------------------------------------------------------------------------------- | -------------------| 78e41f4b71Sopenharmony_ci | int32_t (*OpenInputDevice)(uint32_t devIndex); | Opens an input device. | 79e41f4b71Sopenharmony_ci | int32_t (*CloseInputDevice)(uint32_t devIndex); | Closes an input device. | 80e41f4b71Sopenharmony_ci | int32_t (*GetInputDevice)(uint32_t devIndex, DeviceInfo **devInfo); | Obtains information about an input device.| 81e41f4b71Sopenharmony_ci | int32_t (*GetInputDeviceList)(uint32_t *devNum, DeviceInfo **devList, uint32_t size); | Obtains the input device list.| 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci- input_reporter.h 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci | API | Description | 86e41f4b71Sopenharmony_ci | ----------------------------------------------------------------------------------- | ------------------ | 87e41f4b71Sopenharmony_ci | int32_t (*RegisterReportCallback)(uint32_t devIndex, InputReportEventCb *callback); | Registers a callback for an input device.| 88e41f4b71Sopenharmony_ci | int32_t (*UnregisterReportCallback)(uint32_t devIndex); | Unregisters the callback for an input device.| 89e41f4b71Sopenharmony_ci | void (*ReportEventPkgCallback)(const EventPackage **pkgs, uint32_t count); | Called to report input event data. | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci- input_controller.h 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci | API | Description | 94e41f4b71Sopenharmony_ci | --------------------------------------------------------------------------------------------------- |--------------- | 95e41f4b71Sopenharmony_ci | int32_t (*SetPowerStatus)(uint32_t devIndex, uint32_t status); | Sets the power status. | 96e41f4b71Sopenharmony_ci | int32_t (*GetPowerStatus)(uint32_t devIndex, uint32_t *status); | Obtains the power status. | 97e41f4b71Sopenharmony_ci | int32_t (*GetDeviceType)(uint32_t devIndex, uint32_t *deviceType); | Obtains the device type. | 98e41f4b71Sopenharmony_ci | int32_t (*GetChipInfo)(uint32_t devIndex, char *chipInfo, uint32_t length); | Obtains the chip information of a device.| 99e41f4b71Sopenharmony_ci | int32_t (*GetVendorName)(uint32_t devIndex, char *vendorName, uint32_t length); | Obtains the module vendor name of a device. | 100e41f4b71Sopenharmony_ci | int32_t (*GetChipName)(uint32_t devIndex, char *chipName, uint32_t length); | Obtains the driver chip name of a device. | 101e41f4b71Sopenharmony_ci | int32_t (*SetGestureMode)(uint32_t devIndex, uint32_t gestureMode); | Sets the gesture mode. | 102e41f4b71Sopenharmony_ci | int32_t (*RunCapacitanceTest)(uint32_t devIndex, uint32_t testType, char *result, uint32_t length); | Performs a capacitance test.| 103e41f4b71Sopenharmony_ci | int32_t (*RunExtraCommand)(uint32_t devIndex, InputExtraCmd *cmd); | Executes the specified command. | 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ciFor more information, see [input](https://gitee.com/openharmony/drivers_peripheral/tree/master/input). 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci### Development Procedure 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciThe load process of the input driver model (for the touchscreen driver) is as follows: 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci1. The device configuration, including the driver loading priority, board-specific hardware information, and private data, is complete. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci2. The HDF driver loads the input device manager driver to create and initialize the device manager. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci3. The HDF loads the platform driver to parse the board-specific configuration, initialize the hardware, and provide the API for registering the touchscreen. 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci4. The HDF loads the touchscreen driver to instantiate the touchscreen device, parse the private data, and implement the differentiated APIs for the platform. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci5. The instantiated touchscreen device registers with the platform driver to bind the device and the driver and complete the device initialization, including interrupt registration and device power-on and power-off. 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci6. The instantiated input device registers with the input device manager for unified management. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciThe development process of the touchscreen driver is as follows: 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci1. Configure device information. <br>The input driver is developed based on the HDF. The HDF loads and starts the driver in a unified manner. You need to configure the driver information, such as whether to load the driver and the loading priority, in the configuration file. Then, the HDF starts the registered driver modules one by one. For details about the driver configuration, see [HDF Driver Development Process](driver-hdf-manage.md). 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci2. Configure board-specific information and touchscreen private information.<br>Configure the I/O pin functions. For example, set registers for the I2C pins on the board for the touchscreen to enable I2C communication. 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci3. Implement device-specific APIs.<br>Based on the communication interfaces designed for the board, use the pin operation APIs provided by the platform interface layer to configure the corresponding reset pin, interrupt pin, and power operations. For details about GPIO operations, see [GPIO](../driver/driver-platform-gpio-des.md). 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci### Development Example 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciThe following example describes how to develop the touchscreen driver for an RK3568 development board. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci1. Configure device information. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci Configure the modules of the input driver model in **vendor/hihope/rk3568/hdf_config/khdf/device_info/device_info.hcs**. For details, see [HDF Driver Development Process](driver-hdf-manage.md). The HDF loads modules of the input model in sequence based on the configuration information. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci ```c 142e41f4b71Sopenharmony_ci input :: host { 143e41f4b71Sopenharmony_ci hostName = "input_host"; 144e41f4b71Sopenharmony_ci priority = 100; 145e41f4b71Sopenharmony_ci device_input_manager :: device { 146e41f4b71Sopenharmony_ci device0 :: deviceNode { 147e41f4b71Sopenharmony_ci policy = 2; // The driver provides services externally. 148e41f4b71Sopenharmony_ci priority = 100; // Loading priority. In the input model, the manager module has the highest priority. 149e41f4b71Sopenharmony_ci preload = 0; // Whether to load the driver. The value 0 means to load the driver; 1 means the opposite. 150e41f4b71Sopenharmony_ci permission = 0660; 151e41f4b71Sopenharmony_ci moduleName = "HDF_INPUT_MANAGER"; 152e41f4b71Sopenharmony_ci serviceName = "input_dev_manager"; 153e41f4b71Sopenharmony_ci deviceMatchAttr = ""; 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci device_hdf_touch :: device { 157e41f4b71Sopenharmony_ci device0 :: deviceNode { 158e41f4b71Sopenharmony_ci policy = 2; 159e41f4b71Sopenharmony_ci priority = 120; 160e41f4b71Sopenharmony_ci preload = 0; 161e41f4b71Sopenharmony_ci permission = 0660; 162e41f4b71Sopenharmony_ci moduleName = "HDF_TOUCH"; 163e41f4b71Sopenharmony_ci serviceName = "event1"; 164e41f4b71Sopenharmony_ci deviceMatchAttr = "touch_device1"; 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci device_touch_chip :: device { 169e41f4b71Sopenharmony_ci device0 :: deviceNode { 170e41f4b71Sopenharmony_ci policy = 0; 171e41f4b71Sopenharmony_ci priority = 130; 172e41f4b71Sopenharmony_ci preload = 0; 173e41f4b71Sopenharmony_ci permission = 0660; 174e41f4b71Sopenharmony_ci moduleName = "HDF_TOUCH_SAMPLE"; 175e41f4b71Sopenharmony_ci serviceName = "hdf_touch_sample_service"; 176e41f4b71Sopenharmony_ci deviceMatchAttr = "zsj_sample_5p5"; 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci ``` 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci2. Configure board-specific and private data for the touchscreen. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci Configure the data in **vendor/hihope/rk3568/hdf_config/khdf/input/input_config.hcs**. The following is an example. You can modify the configuration as required. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci ```c 187e41f4b71Sopenharmony_ci root { 188e41f4b71Sopenharmony_ci input_config { 189e41f4b71Sopenharmony_ci touchConfig { 190e41f4b71Sopenharmony_ci touch0 { 191e41f4b71Sopenharmony_ci boardConfig { 192e41f4b71Sopenharmony_ci match_attr = "touch_device1"; 193e41f4b71Sopenharmony_ci inputAttr { 194e41f4b71Sopenharmony_ci inputType = 0; // 0 indicates touchscreen. 195e41f4b71Sopenharmony_ci solutionX = 480; 196e41f4b71Sopenharmony_ci solutionY = 960; 197e41f4b71Sopenharmony_ci devName = "main_touch"; // Device name. 198e41f4b71Sopenharmony_ci } 199e41f4b71Sopenharmony_ci busConfig { 200e41f4b71Sopenharmony_ci busType = 0; // 0 indicates I2C. 201e41f4b71Sopenharmony_ci busNum = 6; 202e41f4b71Sopenharmony_ci clkGpio = 86; 203e41f4b71Sopenharmony_ci dataGpio = 87; 204e41f4b71Sopenharmony_ci i2cClkIomux = [0x114f0048, 0x403]; // Register of the I2C_CLK pin. 205e41f4b71Sopenharmony_ci i2cDataIomux = [0x114f004c, 0x403]; // Register of the I2C_DATA pin. 206e41f4b71Sopenharmony_ci } 207e41f4b71Sopenharmony_ci pinConfig { 208e41f4b71Sopenharmony_ci rstGpio = 3; 209e41f4b71Sopenharmony_ci intGpio = 4; 210e41f4b71Sopenharmony_ci rstRegCfg = [0x112f0094, 0x400]; // Register of the reset pin. 211e41f4b71Sopenharmony_ci intRegCfg = [0x112f0098, 0x400]; // Register of the interrupt pin. 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci powerConfig { 214e41f4b71Sopenharmony_ci vccType = 2; // The value 1 stands for LDO, 2 for GPIO, and 3 for PMIC. 215e41f4b71Sopenharmony_ci vccNum = 20; // Set the GPIO number to 20. 216e41f4b71Sopenharmony_ci vccValue = 1800; // Set the voltage amplitude to 1800 mV. 217e41f4b71Sopenharmony_ci vciType = 1; 218e41f4b71Sopenharmony_ci vciNum = 12; 219e41f4b71Sopenharmony_ci vciValue = 3300; 220e41f4b71Sopenharmony_ci } 221e41f4b71Sopenharmony_ci featureConfig { 222e41f4b71Sopenharmony_ci capacitanceTest = 0; 223e41f4b71Sopenharmony_ci gestureMode = 0; 224e41f4b71Sopenharmony_ci gloverMOde = 0; 225e41f4b71Sopenharmony_ci coverMode = 0; 226e41f4b71Sopenharmony_ci chargerMode = 0; 227e41f4b71Sopenharmony_ci knuckleMode = 0; 228e41f4b71Sopenharmony_ci } 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci chipConfig { 231e41f4b71Sopenharmony_ci template touchChip { 232e41f4b71Sopenharmony_ci match_attr = ""; 233e41f4b71Sopenharmony_ci chipName = "sample"; 234e41f4b71Sopenharmony_ci vendorName = "zsj"; 235e41f4b71Sopenharmony_ci chipInfo = "AAAA11222"; // The first four characters indicate the product name. The fifth and sixth characters indicate the IC model. The last three characters indicate the model number. 236e41f4b71Sopenharmony_ci busType = 0; 237e41f4b71Sopenharmony_ci deviceAddr = 0x5D; 238e41f4b71Sopenharmony_ci irqFlag = 2; // The value 1 means to trigger an interrupt on the rising edge, 2 means to trigger an interrupt on the falling edge, 4 means to trigger an interrupt by the high level, and 8 means to trigger an interrupt by the low level. 239e41f4b71Sopenharmony_ci maxSpeed = 400; 240e41f4b71Sopenharmony_ci chipVersion = 0; 241e41f4b71Sopenharmony_ci powerSequence { 242e41f4b71Sopenharmony_ci /* Description of the power-on sequence: 243e41f4b71Sopenharmony_ci [type, status, direction, delay] 244e41f4b71Sopenharmony_ci <type> 0 stands for null; 1 for VCC power (1.8 V); 2 for VCI power (3.3 V); 3 for reset pin; 4 for interrupt pin. 245e41f4b71Sopenharmony_ci <status> 0 stands for power-off or pull-down; 1 for power-on or pull-up; 2 for no operation. 246e41f4b71Sopenharmony_ci <dir> 0 stands for input; 1 for output; 2 for no operation. 247e41f4b71Sopenharmony_ci <delay> indicates the delay, in milliseconds. For example, 20 indicates 20 ms delay. 248e41f4b71Sopenharmony_ci */ 249e41f4b71Sopenharmony_ci powerOnSeq = [4, 0, 1, 0, 250e41f4b71Sopenharmony_ci 3, 0, 1, 10, 251e41f4b71Sopenharmony_ci 3, 1, 2, 60, 252e41f4b71Sopenharmony_ci 4, 2, 0, 0]; 253e41f4b71Sopenharmony_ci suspendSeq = [3, 0, 2, 10]; 254e41f4b71Sopenharmony_ci resumeSeq = [3, 1, 2, 10]; 255e41f4b71Sopenharmony_ci powerOffSeq = [3, 0, 2, 10, 256e41f4b71Sopenharmony_ci 1, 0, 2, 20]; 257e41f4b71Sopenharmony_ci } 258e41f4b71Sopenharmony_ci } 259e41f4b71Sopenharmony_ci chip0 :: touchChip { 260e41f4b71Sopenharmony_ci match_attr = "zsj_sample_5p5"; 261e41f4b71Sopenharmony_ci chipInfo = "ZIDN45100"; 262e41f4b71Sopenharmony_ci chipVersion = 0; 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci } 265e41f4b71Sopenharmony_ci } 266e41f4b71Sopenharmony_ci } 267e41f4b71Sopenharmony_ci } 268e41f4b71Sopenharmony_ci } 269e41f4b71Sopenharmony_ci ``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci3. Add the touchscreen driver. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci Implement the touchscreen-specific APIs in **drivers/hdf_core/framework/model/input/driver/touchscreen/touch_gt911.c**. 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci The following uses the APIs for obtaining and parsing device data as an example. You can implement the related APIs to match your development. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci ```c 278e41f4b71Sopenharmony_ci /* Parse the touch reporting data read from the touchscreen into coordinates. */ 279e41f4b71Sopenharmony_ci static void ParsePointData(ChipDevice *device, FrameData *frame, uint8_t *buf, uint8_t pointNum) 280e41f4b71Sopenharmony_ci { 281e41f4b71Sopenharmony_ci int32_t resX = device->driver->boardCfg->attr.resolutionX; 282e41f4b71Sopenharmony_ci int32_t resY = device->driver->boardCfg->attr.resolutionY; 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci for (int32_t i = 0; i < pointNum; i++) { 285e41f4b71Sopenharmony_ci frame->fingers[i].y = (buf[GT_POINT_SIZE * i + GT_X_LOW] & ONE_BYTE_MASK) | 286e41f4b71Sopenharmony_ci ((buf[GT_POINT_SIZE * i + GT_X_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET); 287e41f4b71Sopenharmony_ci frame->fingers[i].x = (buf[GT_POINT_SIZE * i + GT_Y_LOW] & ONE_BYTE_MASK) | 288e41f4b71Sopenharmony_ci ((buf[GT_POINT_SIZE * i + GT_Y_HIGH] & ONE_BYTE_MASK) << ONE_BYTE_OFFSET); 289e41f4b71Sopenharmony_ci frame->fingers[i].valid = true; 290e41f4b71Sopenharmony_ci } 291e41f4b71Sopenharmony_ci } 292e41f4b71Sopenharmony_ci /* Obtain the touch reporting data from the device. */ 293e41f4b71Sopenharmony_ci static int32_t ChipDataHandle(ChipDevice *device) 294e41f4b71Sopenharmony_ci { 295e41f4b71Sopenharmony_ci int32_t ret; 296e41f4b71Sopenharmony_ci uint8_t touchStatus = 0; 297e41f4b71Sopenharmony_ci uint8_t pointNum; 298e41f4b71Sopenharmony_ci uint8_t buf[GT_POINT_SIZE * MAX_SUPPORT_POINT] = {0}; 299e41f4b71Sopenharmony_ci InputI2cClient *i2cClient = &device->driver->i2cClient; 300e41f4b71Sopenharmony_ci uint8_t reg[GT_ADDR_LEN] = {0}; 301e41f4b71Sopenharmony_ci FrameData *frame = &device->driver->frameData; 302e41f4b71Sopenharmony_ci reg[0] = (GT_BUF_STATE_ADDR >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK; 303e41f4b71Sopenharmony_ci reg[1] = GT_BUF_STATE_ADDR & ONE_BYTE_MASK; 304e41f4b71Sopenharmony_ci ret = InputI2cRead(i2cClient, reg, GT_ADDR_LEN, &touchStatus, 1); 305e41f4b71Sopenharmony_ci if (ret < 0 || touchStatus == GT_EVENT_INVALID) { 306e41f4b71Sopenharmony_ci return HDF_FAILURE; 307e41f4b71Sopenharmony_ci } 308e41f4b71Sopenharmony_ci OsalMutexLock(&device->driver->mutex); 309e41f4b71Sopenharmony_ci (void)memset_s(frame, sizeof(FrameData), 0, sizeof(FrameData)); 310e41f4b71Sopenharmony_ci if (touchStatus == GT_EVENT_UP) { 311e41f4b71Sopenharmony_ci frame->realPointNum = 0; 312e41f4b71Sopenharmony_ci frame->definedEvent = TOUCH_UP; 313e41f4b71Sopenharmony_ci goto exit; 314e41f4b71Sopenharmony_ci } 315e41f4b71Sopenharmony_ci reg[0] = (GT_X_LOW_BYTE_BASE >> ONE_BYTE_OFFSET) & ONE_BYTE_MASK; 316e41f4b71Sopenharmony_ci reg[1] = GT_X_LOW_BYTE_BASE & ONE_BYTE_MASK; 317e41f4b71Sopenharmony_ci pointNum = touchStatus & GT_FINGER_NUM_MASK; 318e41f4b71Sopenharmony_ci if (pointNum <= 0 || pointNum > MAX_SUPPORT_POINT) { 319e41f4b71Sopenharmony_ci HDF_LOGE("%s: pointNum is invalid, %d", __func__, pointNum); 320e41f4b71Sopenharmony_ci (void)ChipCleanBuffer(i2cClient); 321e41f4b71Sopenharmony_ci OsalMutexUnlock(&device->driver->mutex); 322e41f4b71Sopenharmony_ci return HDF_FAILURE; 323e41f4b71Sopenharmony_ci } 324e41f4b71Sopenharmony_ci frame->realPointNum = pointNum; 325e41f4b71Sopenharmony_ci frame->definedEvent = TOUCH_DOWN; 326e41f4b71Sopenharmony_ci /* Read the touch reporting data from the register. */ 327e41f4b71Sopenharmony_ci (void)InputI2cRead(i2cClient, reg, GT_ADDR_LEN, buf, GT_POINT_SIZE * pointNum); 328e41f4b71Sopenharmony_ci /* Parse the touch reporting data. */ 329e41f4b71Sopenharmony_ci ParsePointData(device, frame, buf, pointNum); 330e41f4b71Sopenharmony_ci exit: 331e41f4b71Sopenharmony_ci OsalMutexUnlock(&device->driver->mutex); 332e41f4b71Sopenharmony_ci if (ChipCleanBuffer(i2cClient) != HDF_SUCCESS) { 333e41f4b71Sopenharmony_ci return HDF_FAILURE; 334e41f4b71Sopenharmony_ci } 335e41f4b71Sopenharmony_ci return HDF_SUCCESS; 336e41f4b71Sopenharmony_ci } 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci static struct TouchChipOps g_sampleChipOps = { 339e41f4b71Sopenharmony_ci .Init = ChipInit, 340e41f4b71Sopenharmony_ci .Detect = ChipDetect, 341e41f4b71Sopenharmony_ci .Resume = ChipResume, 342e41f4b71Sopenharmony_ci .Suspend = ChipSuspend, 343e41f4b71Sopenharmony_ci .DataHandle = ChipDataHandle, 344e41f4b71Sopenharmony_ci }; 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci static TouchChipCfg *ChipConfigInstance(struct HdfDeviceObject *device) 347e41f4b71Sopenharmony_ci { 348e41f4b71Sopenharmony_ci TouchChipCfg *chipCfg = (TouchChipCfg *)OsalMemAlloc(sizeof(TouchChipCfg)); 349e41f4b71Sopenharmony_ci if (chipCfg == NULL) { 350e41f4b71Sopenharmony_ci HDF_LOGE("%s: instance chip config failed", __func__); 351e41f4b71Sopenharmony_ci return NULL; 352e41f4b71Sopenharmony_ci } 353e41f4b71Sopenharmony_ci (void)memset_s(chipCfg, sizeof(TouchChipCfg), 0, sizeof(TouchChipCfg)); 354e41f4b71Sopenharmony_ci /* Parse the touchscreen private configuration. */ 355e41f4b71Sopenharmony_ci if (ParseTouchChipConfig(device->property, chipCfg) != HDF_SUCCESS) { 356e41f4b71Sopenharmony_ci HDF_LOGE("%s: parse chip config failed", __func__); 357e41f4b71Sopenharmony_ci OsalMemFree(chipCfg); 358e41f4b71Sopenharmony_ci chipCfg = NULL; 359e41f4b71Sopenharmony_ci } 360e41f4b71Sopenharmony_ci return chipCfg; 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci static ChipDevice *ChipDeviceInstance(void) 364e41f4b71Sopenharmony_ci { 365e41f4b71Sopenharmony_ci ChipDevice *chipDev = (ChipDevice *)OsalMemAlloc(sizeof(ChipDevice)); 366e41f4b71Sopenharmony_ci if (chipDev == NULL) { 367e41f4b71Sopenharmony_ci HDF_LOGE("%s: instance chip device failed", __func__); 368e41f4b71Sopenharmony_ci return NULL; 369e41f4b71Sopenharmony_ci } 370e41f4b71Sopenharmony_ci (void)memset_s(chipDev, sizeof(ChipDevice), 0, sizeof(ChipDevice)); 371e41f4b71Sopenharmony_ci return chipDev; 372e41f4b71Sopenharmony_ci } 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci static void FreeChipConfig(TouchChipCfg *config) 375e41f4b71Sopenharmony_ci { 376e41f4b71Sopenharmony_ci if (config->pwrSeq.pwrOn.buf != NULL) { 377e41f4b71Sopenharmony_ci OsalMemFree(config->pwrSeq.pwrOn.buf); 378e41f4b71Sopenharmony_ci } 379e41f4b71Sopenharmony_ci if (config->pwrSeq.pwrOff.buf != NULL) { 380e41f4b71Sopenharmony_ci OsalMemFree(config->pwrSeq.pwrOff.buf); 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci OsalMemFree(config); 383e41f4b71Sopenharmony_ci } 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci static int32_t HdfSampleChipInit(struct HdfDeviceObject *device) 386e41f4b71Sopenharmony_ci { 387e41f4b71Sopenharmony_ci TouchChipCfg *chipCfg = NULL; 388e41f4b71Sopenharmony_ci ChipDevice *chipDev = NULL; 389e41f4b71Sopenharmony_ci HDF_LOGE("%s: enter", __func__); 390e41f4b71Sopenharmony_ci if (device == NULL) { 391e41f4b71Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 392e41f4b71Sopenharmony_ci } 393e41f4b71Sopenharmony_ci /* Parse the touchscreen private configuration. */ 394e41f4b71Sopenharmony_ci chipCfg = ChipConfigInstance(device); 395e41f4b71Sopenharmony_ci if (chipCfg == NULL) { 396e41f4b71Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 397e41f4b71Sopenharmony_ci } 398e41f4b71Sopenharmony_ci /* Instantiate the touchscreen device. */ 399e41f4b71Sopenharmony_ci chipDev = ChipDeviceInstance(); 400e41f4b71Sopenharmony_ci if (chipDev == NULL) { 401e41f4b71Sopenharmony_ci goto freeCfg; 402e41f4b71Sopenharmony_ci } 403e41f4b71Sopenharmony_ci chipDev->chipCfg = chipCfg; 404e41f4b71Sopenharmony_ci chipDev->ops = &g_sampleChipOps; 405e41f4b71Sopenharmony_ci chipDev->chipName = chipCfg->chipName; 406e41f4b71Sopenharmony_ci chipDev->vendorName = chipCfg->vendorName; 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci /* Register the touchscreen device with the platform driver. */ 409e41f4b71Sopenharmony_ci if (RegisterChipDevice(chipDev) != HDF_SUCCESS) { 410e41f4b71Sopenharmony_ci goto freeDev; 411e41f4b71Sopenharmony_ci } 412e41f4b71Sopenharmony_ci HDF_LOGI("%s: exit succ, chipName = %s", __func__, chipCfg->chipName); 413e41f4b71Sopenharmony_ci return HDF_SUCCESS; 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci freeDev: 416e41f4b71Sopenharmony_ci OsalMemFree(chipDev); 417e41f4b71Sopenharmony_ci freeCfg: 418e41f4b71Sopenharmony_ci FreeChipConfig(chipCfg); 419e41f4b71Sopenharmony_ci return HDF_FAILURE; 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci struct HdfDriverEntry g_touchSampleChipEntry = { 423e41f4b71Sopenharmony_ci .moduleVersion = 1, 424e41f4b71Sopenharmony_ci .moduleName = "HDF_TOUCH_SAMPLE", 425e41f4b71Sopenharmony_ci .Init = HdfSampleChipInit, 426e41f4b71Sopenharmony_ci }; 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci HDF_INIT(g_touchSampleChipEntry); 429e41f4b71Sopenharmony_ci ``` 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci4. Call the Input HDI APIs. 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci The following sample code shows how an upper-layer input system service calls Input HDI APIs. 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci ```c 436e41f4b71Sopenharmony_ci #include "input_manager.h" 437e41f4b71Sopenharmony_ci #define DEV_INDEX 1 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci IInputInterface *g_inputInterface; 440e41f4b71Sopenharmony_ci InputReportEventCb g_callback; 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci /* Define the callback for data reporting. */ 443e41f4b71Sopenharmony_ci static void ReportEventPkgCallback(const EventPackage **pkgs, uint32_t count) 444e41f4b71Sopenharmony_ci { 445e41f4b71Sopenharmony_ci if (pkgs == NULL || count > MAX_PKG_NUM) { 446e41f4b71Sopenharmony_ci return; 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci for (uint32_t i = 0; i < count; i++) { 449e41f4b71Sopenharmony_ci HDF_LOGI("%s: pkgs[%d] = 0x%x, 0x%x, %d", __func__, i, pkgs[i]->type, pkgs[i]->code, pkgs[i]->value); 450e41f4b71Sopenharmony_ci } 451e41f4b71Sopenharmony_ci } 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci int InputServiceSample(void) 454e41f4b71Sopenharmony_ci { 455e41f4b71Sopenharmony_ci uint32_t devType = INIT_DEFAULT_VALUE; 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci /* Obtain the input driver APIs. */ 458e41f4b71Sopenharmony_ci int ret = GetInputInterface(&g_inputInterface); 459e41f4b71Sopenharmony_ci if (ret != INPUT_SUCCESS) { 460e41f4b71Sopenharmony_ci HDF_LOGE("%s: get input interfaces failed, ret = %d", __func__, ret); 461e41f4b71Sopenharmony_ci return ret; 462e41f4b71Sopenharmony_ci } 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR); 465e41f4b71Sopenharmony_ci INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR); 466e41f4b71Sopenharmony_ci /* Open an input device. */ 467e41f4b71Sopenharmony_ci ret = g_inputInterface->iInputManager->OpenInputDevice(DEV_INDEX); 468e41f4b71Sopenharmony_ci if (ret) { 469e41f4b71Sopenharmony_ci HDF_LOGE("%s: open input device failed, ret = %d", __func__, ret); 470e41f4b71Sopenharmony_ci return ret; 471e41f4b71Sopenharmony_ci } 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputController, INPUT_NULL_PTR); 474e41f4b71Sopenharmony_ci /* Obtain the type of the input device. */ 475e41f4b71Sopenharmony_ci ret = g_inputInterface->iInputController->GetDeviceType(DEV_INDEX, &devType); 476e41f4b71Sopenharmony_ci if (ret) { 477e41f4b71Sopenharmony_ci HDF_LOGE("%s: get device type failed, ret: %d", __FUNCTION__, ret); 478e41f4b71Sopenharmony_ci return ret; 479e41f4b71Sopenharmony_ci } 480e41f4b71Sopenharmony_ci HDF_LOGI("%s: device1's type is %u\n", __FUNCTION__, devType); 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci /* Register the data reporting callback for the input device. */ 483e41f4b71Sopenharmony_ci g_callback.ReportEventPkgCallback = ReportEventPkgCallback; 484e41f4b71Sopenharmony_ci INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputReporter, INPUT_NULL_PTR); 485e41f4b71Sopenharmony_ci ret = g_inputInterface->iInputReporter->RegisterReportCallback(DEV_INDEX, &g_callback); 486e41f4b71Sopenharmony_ci if (ret) { 487e41f4b71Sopenharmony_ci HDF_LOGE("%s: register callback failed, ret: %d", __FUNCTION__, ret); 488e41f4b71Sopenharmony_ci return ret; 489e41f4b71Sopenharmony_ci } 490e41f4b71Sopenharmony_ci HDF_LOGI("%s: wait 10s for testing, pls touch the panel now", __FUNCTION__); 491e41f4b71Sopenharmony_ci OsalMSleep(KEEP_ALIVE_TIME_MS); 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci /* Unregister the callback for the input device. */ 494e41f4b71Sopenharmony_ci ret = g_inputInterface->iInputReporter->UnregisterReportCallback(DEV_INDEX); 495e41f4b71Sopenharmony_ci if (ret) { 496e41f4b71Sopenharmony_ci HDF_LOGE("%s: unregister callback failed, ret: %d", __FUNCTION__, ret); 497e41f4b71Sopenharmony_ci return ret; 498e41f4b71Sopenharmony_ci } 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci /* Close the input device. */ 501e41f4b71Sopenharmony_ci ret = g_inputInterface->iInputManager->CloseInputDevice(DEV_INDEX); 502e41f4b71Sopenharmony_ci if (ret) { 503e41f4b71Sopenharmony_ci HDF_LOGE("%s: close device failed, ret: %d", __FUNCTION__, ret); 504e41f4b71Sopenharmony_ci return ret; 505e41f4b71Sopenharmony_ci } 506e41f4b71Sopenharmony_ci return 0; 507e41f4b71Sopenharmony_ci } 508e41f4b71Sopenharmony_ci ``` 509