LibDriver PMW3901MB
Loading...
Searching...
No Matches
driver_pmw3901mb_interrupt.c
Go to the documentation of this file.
1
36
38
39static pmw3901mb_handle_t gs_handle;
40static void (*gs_irq)(pmw3901mb_motion_t *motion, float delta_x, float delta_y) = NULL;
41
50uint8_t pmw3901mb_interrupt_irq_handler(float height_m)
51{
52 uint8_t res;
53 float delta_x;
54 float delta_y;
55 pmw3901mb_motion_t motion;
56
57 /* burst read */
58 res = pmw3901mb_burst_read(&gs_handle, &motion);
59 if (res != 0)
60 {
61 pmw3901mb_interface_debug_print("pmw3901mb: burst read failed.\n");
62 (void)pmw3901mb_set_motion(&gs_handle, 0x00);
63
64 return 1;
65 }
66
67 /* check the result */
68 if (motion.is_valid == 1)
69 {
70 /* convert the delta x */
71 res = pmw3901mb_delta_raw_to_delta_cm(&gs_handle, motion.delta_x, height_m, (float *)&delta_x);
72 if (res != 0)
73 {
74 pmw3901mb_interface_debug_print("pmw3901mb: delta raw to delta cm failed.\n");
75 (void)pmw3901mb_set_motion(&gs_handle, 0x00);
76
77 return 1;
78 }
79
80 /* convert the delta y */
81 res = pmw3901mb_delta_raw_to_delta_cm(&gs_handle, motion.delta_y, height_m, (float *)&delta_y);
82 if (res != 0)
83 {
84 pmw3901mb_interface_debug_print("pmw3901mb: delta raw to delta cm failed.\n");
85 (void)pmw3901mb_set_motion(&gs_handle, 0x00);
86
87 return 1;
88 }
89
90 /* run the callback */
91 if (gs_irq != NULL)
92 {
93 gs_irq(&motion, delta_x, delta_y);
94 }
95 }
96
97 /* clear the interrupt flag */
98 res = pmw3901mb_set_motion(&gs_handle, 0x00);
99 if (res != 0)
100 {
101 pmw3901mb_interface_debug_print("pmw3901mb: set motion failed.\n");
102
103 return 1;
104 }
105
106 return 0;
107}
108
117uint8_t pmw3901mb_interrupt_init(void (*callback)(pmw3901mb_motion_t *motion, float delta_x, float delta_y))
118{
119 uint8_t res;
120
121 /* link interface function */
132
133 /* init pmw3901mb */
134 res = pmw3901mb_init(&gs_handle);
135 if (res != 0)
136 {
137 pmw3901mb_interface_debug_print("pmw3901mb: init failed.\n");
138
139 return 1;
140 }
141
142 /* chip power up */
143 res = pmw3901mb_power_up(&gs_handle);
144 if (res != 0)
145 {
146 pmw3901mb_interface_debug_print("pmw3901mb: power up failed.\n");
147 (void)pmw3901mb_deinit(&gs_handle);
148
149 return 1;
150 }
151
152 /* set optimum performance */
153 res = pmw3901mb_set_optimum_performance(&gs_handle);
154 if (res != 0)
155 {
156 pmw3901mb_interface_debug_print("pmw3901mb: set optimum performance failed.\n");
157 (void)pmw3901mb_deinit(&gs_handle);
158
159 return 1;
160 }
161
162 /* set callback */
163 gs_irq = callback;
164
165 return 0;
166}
167
176{
177 gs_irq = NULL;
178
179 if (pmw3901mb_deinit(&gs_handle) != 0)
180 {
181 return 1;
182 }
183 else
184 {
185 return 0;
186 }
187}
driver pmw3901mb interrupt header file
uint8_t pmw3901mb_init(pmw3901mb_handle_t *handle)
initialize the chip
struct pmw3901mb_handle_s pmw3901mb_handle_t
pmw3901mb handle structure definition
uint8_t pmw3901mb_set_motion(pmw3901mb_handle_t *handle, uint8_t motion)
set the motion
uint8_t pmw3901mb_set_optimum_performance(pmw3901mb_handle_t *handle)
set the optimum performance
uint8_t pmw3901mb_power_up(pmw3901mb_handle_t *handle)
power up the chip
struct pmw3901mb_motion_s pmw3901mb_motion_t
pmw3901mb motion structure definition
uint8_t pmw3901mb_delta_raw_to_delta_cm(pmw3901mb_handle_t *handle, int16_t raw, float height_m, float *cm)
convert the delta raw to the delta cm
uint8_t pmw3901mb_deinit(pmw3901mb_handle_t *handle)
close the chip
uint8_t pmw3901mb_burst_read(pmw3901mb_handle_t *handle, pmw3901mb_motion_t *motion)
burst read data
uint8_t pmw3901mb_interrupt_deinit(void)
interrupt example deinit
uint8_t pmw3901mb_interrupt_init(void(*callback)(pmw3901mb_motion_t *motion, float delta_x, float delta_y))
interrupt example init
uint8_t pmw3901mb_interrupt_irq_handler(float height_m)
interrupt irq
void pmw3901mb_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pmw3901mb_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus write
uint8_t pmw3901mb_interface_spi_deinit(void)
interface spi bus deinit
uint8_t pmw3901mb_interface_reset_gpio_write(uint8_t data)
interface reset gpio write
uint8_t pmw3901mb_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus read
uint8_t pmw3901mb_interface_spi_init(void)
interface spi bus init
uint8_t pmw3901mb_interface_reset_gpio_deinit(void)
interface reset gpio deinit
void pmw3901mb_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t pmw3901mb_interface_reset_gpio_init(void)
interface reset gpio init