1 #include <twr_cy8cmbr3102.h>
3 #define _TWR_CY8CMBR3102_START_INTERVAL 1500
4 #define _TWR_CY8CMBR3102_SCAN_INTERVAL 100
5 #define _TWR_CY8CMBR3102_SCAN_INTERVAL_IS_TOUCH 1000
7 static const uint8_t _twr_cy8cmbr3102_default_setting[] = {
8 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9 0x00, 0x00, 0x00, 0x00, 0x32, 0x7F, 0x00, 0x00,
10 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
12 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80,
13 0x05, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00,
14 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00,
15 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x54,
18 0x00, 0x37, 0x01, 0x00, 0x00, 0x0A, 0x00, 0x00,
19 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x0E
26 static void _twr_cy8cmbr3102_task(
void *param);
30 memset(
self, 0,
sizeof(*
self));
32 self->_i2c_channel = i2c_channel;
33 self->_i2c_address = i2c_address;
37 self->_scan_interval = _TWR_CY8CMBR3102_SCAN_INTERVAL;
39 self->_task_id_task =
twr_scheduler_register(_twr_cy8cmbr3102_task,
self, _TWR_CY8CMBR3102_START_INTERVAL);
46 self->_event_handler = event_handler;
47 self->_event_param = event_param;
52 self->_scan_interval = scan_interval;
64 uint8_t prox_stat = 0;
71 *is_touch = (prox_stat & 0x01) != 0;
77 static void _twr_cy8cmbr3102_task(
void *param)
85 case TWR_CY8CMBR3102_STATE_ERROR:
87 if (self->_event_handler != NULL)
92 self->_state = TWR_CY8CMBR3102_STATE_INITIALIZE;
98 case TWR_CY8CMBR3102_STATE_INITIALIZE:
103 transfer.
buffer = (uint8_t *)_twr_cy8cmbr3102_default_setting;
104 transfer.
length =
sizeof(_twr_cy8cmbr3102_default_setting);
108 if (self->_error_cnt++ < 2)
115 self->_error_cnt = 0;
117 self->_state = TWR_CY8CMBR3102_STATE_ERROR;
122 self->_state = TWR_CY8CMBR3102_STATE_CALC_CONFIG_CRC;
128 case TWR_CY8CMBR3102_STATE_CALC_CONFIG_CRC:
132 self->_state = TWR_CY8CMBR3102_STATE_ERROR;
137 self->_state = TWR_CY8CMBR3102_STATE_SELF_RESET;
143 case TWR_CY8CMBR3102_STATE_SELF_RESET:
147 self->_state = TWR_CY8CMBR3102_STATE_ERROR;
151 self->_state = TWR_CY8CMBR3102_STATE_READ;
157 case TWR_CY8CMBR3102_STATE_READ:
163 if (self->_error_cnt++ < 2)
170 self->_error_cnt = 0;
172 self->_state = TWR_CY8CMBR3102_STATE_ERROR;
179 if (self->_event_handler != NULL)
195 self->_state = TWR_CY8CMBR3102_STATE_ERROR;
twr_cy8cmbr3102_event_t
Callback events.
bool twr_cy8cmbr3102_is_touch(twr_cy8cmbr3102_t *self, bool *is_touch)
Is touch.
bool twr_cy8cmbr3102_get_proximity(twr_cy8cmbr3102_t *self, uint16_t value)
Get proximity (Capacitive sensor difference count signal.)
void twr_cy8cmbr3102_set_scan_interval(twr_cy8cmbr3102_t *self, twr_tick_t scan_interval)
Set scan interval.
void twr_cy8cmbr3102_set_event_handler(twr_cy8cmbr3102_t *self, void(*event_handler)(twr_cy8cmbr3102_t *, twr_cy8cmbr3102_event_t, void *), void *event_param)
Set callback function.
bool twr_cy8cmbr3102_init(twr_cy8cmbr3102_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize CY8CMBR3102.
struct twr_cy8cmbr3102_t twr_cy8cmbr3102_t
TCA9534A instance.
@ TWR_CY8CMBR3102_EVENT_TOUCH
Update event.
@ TWR_CY8CMBR3102_EVENT_ERROR
Error event.
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_write(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory write to I2C channel.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
bool twr_i2c_memory_read_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t *data)
Memory read 2 bytes from I2C channel.
bool twr_i2c_memory_read_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.
I2C memory transfer parameters.
uint32_t memory_address
8-bit I2C memory address (it can be extended to 16-bit format if OR-ed with TWR_I2C_MEMORY_ADDRESS_16...
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.
void * buffer
Pointer to buffer which is being written or read.