drivers/stmpe811: add runtime configurable event callback

This commit is contained in:
Alexandre Abadie 2020-07-21 22:02:10 +02:00
parent 528d884b5d
commit 1362c61abb
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
2 changed files with 20 additions and 5 deletions

View File

@ -64,7 +64,7 @@ typedef struct {
*
* @param[in] arg optional context for the callback
*/
typedef void (*touch_event_cb_t)(void *arg);
typedef void (*stmpe811_event_cb_t)(void *arg);
/**
* @brief Device initialization parameters
@ -85,6 +85,8 @@ typedef struct {
touch_dev_t *dev; /**< Pointer to the generic touch device */
#endif
stmpe811_params_t params; /**< Device parameters */
stmpe811_event_cb_t cb; /**< Configured IRQ event callback */
void *cb_arg; /**< Extra argument for the callback */
uint16_t prev_x; /**< Previous X coordinate */
uint16_t prev_y; /**< Previous Y coordinate */
} stmpe811_t;
@ -103,7 +105,7 @@ typedef struct {
* @return -STMPE811_ERR_I2C on any I2C error
*/
int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params,
touch_event_cb_t cb, void *arg);
stmpe811_event_cb_t cb, void *arg);
/**
* @brief Read the touch position

View File

@ -34,6 +34,16 @@
#define STMPE811_DEV_I2C (dev->params.i2c)
#define STMPE811_DEV_ADDR (dev->params.addr)
static void _gpio_irq(void *arg)
{
const stmpe811_t *dev = (const stmpe811_t *)arg;
assert(dev);
if (dev->cb) {
dev->cb(dev->cb_arg);
}
}
static int _soft_reset(const stmpe811_t *dev)
{
if (i2c_write_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR,
@ -66,9 +76,12 @@ static void _clear_interrupt_status(const stmpe811_t *dev)
i2c_write_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR, STMPE811_INT_STA, 0xff, 0);
}
int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params, touch_event_cb_t cb, void *arg)
int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params, stmpe811_event_cb_t cb, void *arg)
{
dev->params = *params;
dev->cb = cb;
dev->cb_arg = arg;
int ret = STMPE811_OK;
/* Acquire I2C device */
@ -160,9 +173,9 @@ int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params, touch_event
/* clear interrupt status */
_clear_interrupt_status(dev);
if (gpio_is_valid(dev->params.int_pin) && cb) {
if (gpio_is_valid(dev->params.int_pin)) {
DEBUG("[stmpe811] init: configuring touchscreen interrupt\n");
gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_FALLING, cb, arg);
gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_FALLING, _gpio_irq, dev);
/* Enable touchscreen interrupt */
ret += i2c_write_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR,