azoteq.c (1043B)
1 /* 2 azoteq.c -- Azoteq IQS550 mappings 3 Copyright 2023 Valtteri Koskivuori <vkoskiv@gmail.com> 4 License: GPLv3 5 */ 6 7 #include "i2cmaster/i2cmaster.h" 8 #include "azoteq.h" 9 10 static void swap_u16(uint16_t *val) { 11 uint8_t hi = (*val & 0xff00) >> 8; 12 *val <<= 8; 13 *val |= hi; 14 } 15 16 static void swap_16(int16_t *val) { 17 uint8_t hi = (*val & 0xff00) >> 8; 18 *val <<= 8; 19 *val |= hi; 20 } 21 22 void read_azoteq_data(struct azoteq_data *data) { 23 i2c_start_wait(ADDR_SENSOR|I2C_WRITE); 24 i2c_write(0x00); 25 i2c_write(0x00); 26 i2c_rep_start(ADDR_SENSOR|I2C_READ); 27 28 for (int i = 0; i < sizeof(*data); ++i) { 29 ((unsigned char *)data)[i] = i2c_readAck(); 30 } 31 i2c_readNak(); 32 i2c_stop(); 33 34 // end cycle 35 i2c_start_wait(ADDR_SENSOR|I2C_WRITE); 36 i2c_write(0xee); 37 i2c_write(0xee); 38 i2c_write(0xff); 39 i2c_stop(); 40 41 // Sad, we have to swap byte order for 16-bit quantities 42 for (int i = 0; i < 5; ++i) { 43 swap_u16(&data->fingers[i].abs_x); 44 swap_u16(&data->fingers[i].abs_y); 45 } 46 swap_16(&data->relative_x); 47 swap_16(&data->relative_y); 48 } 49