LibDriver PMW3901MB
Loading...
Searching...
No Matches
driver_pmw3901mb.c
Go to the documentation of this file.
1
36
37#include "driver_pmw3901mb.h"
38
42#define CHIP_NAME "PixArt Imaging PMW3901MB"
43#define MANUFACTURER_NAME "PixArt Imaging"
44#define SUPPLY_VOLTAGE_MIN 1.8f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 70.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define PMW3901MB_REG_PRODUCT_ID 0x00
55#define PMW3901MB_REG_REVISION_ID 0x01
56#define PMW3901MB_REG_MOTION 0x02
57#define PMW3901MB_REG_DELTA_X_L 0x03
58#define PMW3901MB_REG_DELTA_X_H 0x04
59#define PMW3901MB_REG_DELTA_Y_L 0x05
60#define PMW3901MB_REG_DELTA_Y_H 0x06
61#define PMW3901MB_REG_SQUAL 0x07
62#define PMW3901MB_REG_RAW_DATA_SUM 0x08
63#define PMW3901MB_REG_MAXIMUM_RAW_DATA 0x09
64#define PMW3901MB_REG_MINIMUM_RAW_DATA 0x0A
65#define PMW3901MB_REG_SHUTTER_LOWER 0x0B
66#define PMW3901MB_REG_SHUTTER_UPPER 0x0C
67#define PMW3901MB_REG_OBSERVATION 0x15
68#define PMW3901MB_REG_MOTION_BURST 0x16
69#define PMW3901MB_REG_POWER_UP_RESET 0x3A
70#define PMW3901MB_REG_SHUTDOWN 0x3B
71#define PMW3901MB_REG_RAW_DATA_GRAB 0x58
72#define PMW3901MB_REG_RAW_DATA_GRAB_STATUS 0x59
73#define PMW3901MB_REG_INVERSE_PRODUCT_ID 0x5F
74
86static uint8_t a_pmw3901mb_spi_read(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
87{
88 if (handle->spi_read(reg, buf, len) != 0) /* spi read */
89 {
90 return 1; /* return error */
91 }
92 else
93 {
94 return 0; /* success return 0 */
95 }
96}
97
109static uint8_t a_pmw3901mb_spi_write(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
110{
111 if (handle->spi_write(0x80 | reg, buf, len) != 0) /* spi write */
112 {
113 return 1; /* return error */
114 }
115 else
116 {
117 return 0; /* success return 0 */
118 }
119}
120
134{
135 uint8_t id;
136
137 if (handle == NULL) /* check handle */
138 {
139 return 2; /* return error */
140 }
141 if (handle->debug_print == NULL) /* check debug_print */
142 {
143 return 3; /* return error */
144 }
145 if (handle->spi_init == NULL) /* check spi_init */
146 {
147 handle->debug_print("pmw3901mb: spi_init is null.\n"); /* spi_init is null */
148
149 return 3; /* return error */
150 }
151 if (handle->spi_deinit == NULL) /* check spi_deinit */
152 {
153 handle->debug_print("pmw3901mb: spi_deinit is null.\n"); /* spi_deinit is null */
154
155 return 3; /* return error */
156 }
157 if (handle->spi_read == NULL) /* check spi_read */
158 {
159 handle->debug_print("pmw3901mb: spi_read is null.\n"); /* spi_read is null */
160
161 return 3; /* return error */
162 }
163 if (handle->spi_write == NULL) /* check spi_write */
164 {
165 handle->debug_print("pmw3901mb: spi_write is null.\n"); /* spi_write is null */
166
167 return 3; /* return error */
168 }
169 if (handle->reset_gpio_init == NULL) /* check reset_gpio_init */
170 {
171 handle->debug_print("pmw3901mb: reset_gpio_init is null.\n"); /* reset_gpio_init is null */
172
173 return 3; /* return error */
174 }
175 if (handle->reset_gpio_deinit == NULL) /* check reset_gpio_deinit */
176 {
177 handle->debug_print("pmw3901mb: reset_gpio_deinit is null.\n"); /* reset_gpio_deinit is null */
178
179 return 3; /* return error */
180 }
181 if (handle->reset_gpio_write == NULL) /* check reset_gpio_write */
182 {
183 handle->debug_print("pmw3901mb: reset_gpio_write is null.\n"); /* reset_gpio_write is null */
184
185 return 3; /* return error */
186 }
187 if (handle->delay_ms == NULL) /* check delay_ms */
188 {
189 handle->debug_print("pmw3901mb: delay_ms is null.\n"); /* delay_ms is null */
190
191 return 3; /* return error */
192 }
193
194 if (handle->spi_init() != 0) /* initialize spi bus */
195 {
196 handle->debug_print("pmw3901mb: spi init failed.\n"); /* spi init failed */
197
198 return 1; /* return error */
199 }
200 if (handle->reset_gpio_init() != 0) /* initialize gpio */
201 {
202 handle->debug_print("pmw3901mb: gpio init failed.\n"); /* gpio init failed */
203 (void)handle->spi_deinit(); /* spi deinit */
204
205 return 1; /* return error */
206 }
207 if (handle->reset_gpio_write(0) != 0) /* write 0 */
208 {
209 handle->debug_print("pmw3901mb: reset failed.\n"); /* reset failed */
210 (void)handle->spi_deinit(); /* spi deinit */
211 (void)handle->reset_gpio_deinit(); /* reset gpio deinit */
212
213 return 5; /* return error */
214 }
215 handle->delay_ms(10); /* delay 10 ms */
216 if (handle->reset_gpio_write(1) != 0) /* write 1 */
217 {
218 handle->debug_print("pmw3901mb: reset failed.\n"); /* reset failed */
219 (void)handle->spi_deinit(); /* spi deinit */
220 (void)handle->reset_gpio_deinit(); /* reset gpio deinit */
221
222 return 5; /* return error */
223 }
224 handle->delay_ms(10); /* delay 10 ms */
225 if (a_pmw3901mb_spi_read(handle, PMW3901MB_REG_PRODUCT_ID, (uint8_t *)&id, 1) != 0) /* get product id */
226 {
227 handle->debug_print("pmw3901mb: get product id failed.\n"); /* get product id failed */
228 (void)handle->spi_deinit(); /* spi deinit */
229 (void)handle->reset_gpio_deinit(); /* reset gpio deinit */
230
231 return 4; /* return error */
232 }
233 if (id != 0x49) /* check id */
234 {
235 handle->debug_print("pmw3901mb: id is invalid.\n"); /* id is invalid */
236 (void)handle->spi_deinit(); /* spi deinit */
237 (void)handle->reset_gpio_deinit(); /* reset gpio deinit */
238
239 return 4; /* return error */
240 }
241 handle->inited = 1; /* flag finish initialization */
242
243 return 0; /* success return 0 */
244}
245
258{
259 uint8_t res;
260 uint8_t cmd;
261
262 if (handle == NULL) /* check handle */
263 {
264 return 2; /* return error */
265 }
266 if (handle->inited != 1) /* check handle initialization */
267 {
268 return 3; /* return error */
269 }
270
271 cmd = 0xB6; /* shutdown command */
272 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_SHUTDOWN, (uint8_t *)&cmd, 1); /* set shutdown reset */
273 if (res != 0) /* check result */
274 {
275 handle->debug_print("pmw3901mb: power down failed.\n"); /* power down failed */
276
277 return 4; /* return error */
278 }
279 res = handle->spi_deinit(); /* spi deinit */
280 if (res != 0) /* check result */
281 {
282 handle->debug_print("pmw3901mb: spi deinit failed.\n"); /* spi deinit failed */
283
284 return 1; /* return error */
285 }
286 res = handle->reset_gpio_deinit(); /* reset gpio deinit */
287 if (res != 0) /* check result */
288 {
289 handle->debug_print("pmw3901mb: gpio deinit failed.\n"); /* gpio deinit failed */
290
291 return 1; /* return error */
292 }
293 handle->inited = 0; /* flag close */
294
295 return 0; /* success return 0 */
296}
297
309{
310 uint8_t res;
311 uint8_t cmd;
312
313 if (handle == NULL) /* check handle */
314 {
315 return 2; /* return error */
316 }
317 if (handle->inited != 1) /* check handle initialization */
318 {
319 return 3; /* return error */
320 }
321
322 res = handle->reset_gpio_write(0); /* write 0 */
323 if (res != 0) /* check result */
324 {
325 handle->debug_print("pmw3901mb: set gpio failed.\n"); /* set gpio failed */
326
327 return 1; /* return error */
328 }
329 handle->delay_ms(10); /* delay 10 ms */
330 res = handle->reset_gpio_write(1); /* write 1 */
331 if (res != 0) /* check result */
332 {
333 handle->debug_print("pmw3901mb: set gpio failed.\n"); /* set gpio failed */
334
335 return 1; /* return error */
336 }
337 handle->delay_ms(10); /* delay 10 ms */
338 cmd = 0x5A; /* power up command */
339 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_POWER_UP_RESET, (uint8_t *)&cmd, 1); /* set power up reset */
340 if (res != 0) /* check result */
341 {
342 handle->debug_print("pmw3901mb: set power up reset failed.\n"); /* set power up reset failed */
343
344 return 1; /* return error */
345 }
346 handle->delay_ms(10); /* delay 10 ms */
347
348 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MOTION, (uint8_t *)&cmd, 1); /* get command */
349 if (res != 0) /* check result */
350 {
351 handle->debug_print("pmw3901mb: get command failed.\n"); /* get command failed */
352
353 return 1; /* return error */
354 }
355 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_X_L, (uint8_t *)&cmd, 1); /* get command */
356 if (res != 0) /* check result */
357 {
358 handle->debug_print("pmw3901mb: get command failed.\n"); /* get command failed */
359
360 return 1; /* return error */
361 }
362 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_X_H, (uint8_t *)&cmd, 1); /* get command */
363 if (res != 0) /* check result */
364 {
365 handle->debug_print("pmw3901mb: get command failed.\n"); /* get command failed */
366
367 return 1; /* return error */
368 }
369 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_Y_L, (uint8_t *)&cmd, 1); /* get command */
370 if (res != 0) /* check result */
371 {
372 handle->debug_print("pmw3901mb: get command failed.\n"); /* get command failed */
373
374 return 1; /* return error */
375 }
376 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_Y_H, (uint8_t *)&cmd, 1); /* get command */
377 if (res != 0) /* check result */
378 {
379 handle->debug_print("pmw3901mb: get command failed.\n"); /* get command failed */
380
381 return 1; /* return error */
382 }
383
384 return 0; /* success return 0 */
385}
386
399{
400 uint8_t res;
401
402 if (handle == NULL) /* check handle */
403 {
404 return 2; /* return error */
405 }
406 if (handle->inited != 1) /* check handle initialization */
407 {
408 return 3; /* return error */
409 }
410
411 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MOTION_BURST, (uint8_t *)motion->raw, 12); /* burst read */
412 if (res != 0) /* check result */
413 {
414 handle->debug_print("pmw3901mb: burst read failed.\n"); /* burst read failed */
415
416 return 1; /* return error */
417 }
418 if ((motion->raw[0] & (1 << 7)) != 0) /* check motion flag */
419 {
420 if ((motion->raw[6] < 0x19) || (motion->raw[10] == 0x1F)) /* check data */
421 {
422 motion->is_valid = 2; /* set invalid */
423
424 return 0; /* success return 0 */
425 }
426 motion->delta_x = (int16_t)(((uint16_t)motion->raw[3] << 8) | motion->raw[2]); /* set delta_x */
427 motion->delta_y = (int16_t)(((uint16_t)motion->raw[5] << 8) | motion->raw[4]); /* set delta_y */
428 motion->observation = motion->raw[1] & 0x3F; /* set observation */
429 motion->raw_average = motion->raw[7]; /* set raw average */
430 motion->raw_max = motion->raw[8]; /* set raw max */
431 motion->raw_min = motion->raw[9]; /* set raw min */
432 motion->shutter = (((((uint16_t)motion->raw[10] & 0x1F) << 8)) | motion->raw[11]); /* set shutter */
433 motion->surface_quality = motion->raw[6] * 4; /* set surface quality */
434
435 motion->is_valid = 1; /* set valid */
436 }
437 else
438 {
439 motion->is_valid = 0; /* set invalid */
440 }
441
442 return 0; /* success return 0 */
443}
444
456{
457 uint8_t res;
458 uint8_t cmd;
459
460 if (handle == NULL) /* check handle */
461 {
462 return 2; /* return error */
463 }
464 if (handle->inited != 1) /* check handle initialization */
465 {
466 return 3; /* return error */
467 }
468
469 cmd = 0x07; /* set the command */
470 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
471 if (res != 0) /* check result */
472 {
473 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
474
475 return 1; /* return error */
476 }
477 cmd = 0x1D; /* set the command */
478 res = a_pmw3901mb_spi_write(handle, 0x41, (uint8_t *)&cmd, 1); /* sent the command */
479 if (res != 0) /* check result */
480 {
481 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
482
483 return 1; /* return error */
484 }
485 cmd = 0x00; /* set the command */
486 res = a_pmw3901mb_spi_write(handle, 0x4C, (uint8_t *)&cmd, 1); /* sent the command */
487 if (res != 0) /* check result */
488 {
489 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
490
491 return 1; /* return error */
492 }
493 cmd = 0x08; /* set the command */
494 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
495 if (res != 0) /* check result */
496 {
497 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
498
499 return 1; /* return error */
500 }
501 cmd = 0x38; /* set the command */
502 res = a_pmw3901mb_spi_write(handle, 0x6A, (uint8_t *)&cmd, 1); /* sent the command */
503 if (res != 0) /* check result */
504 {
505 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
506
507 return 1; /* return error */
508 }
509 cmd = 0x00; /* set the command */
510 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
511 if (res != 0) /* check result */
512 {
513 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
514
515 return 1; /* return error */
516 }
517 cmd = 0x04; /* set the command */
518 res = a_pmw3901mb_spi_write(handle, 0x55, (uint8_t *)&cmd, 1); /* sent the command */
519 if (res != 0) /* check result */
520 {
521 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
522
523 return 1; /* return error */
524 }
525 cmd = 0x80; /* set the command */
526 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
527 if (res != 0) /* check result */
528 {
529 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
530
531 return 1; /* return error */
532 }
533 cmd = 0x11; /* set the command */
534 res = a_pmw3901mb_spi_write(handle, 0x4D, (uint8_t *)&cmd, 1); /* sent the command */
535 if (res != 0) /* check result */
536 {
537 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
538
539 return 1; /* return error */
540 }
541
542 return 0; /* success return 0 */
543}
544
556{
557 uint8_t res;
558 uint8_t cmd;
559
560 if (handle == NULL) /* check handle */
561 {
562 return 2; /* return error */
563 }
564 if (handle->inited != 1) /* check handle initialization */
565 {
566 return 3; /* return error */
567 }
568
569 cmd = 0x00; /* set the command */
570 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
571 if (res != 0) /* check result */
572 {
573 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
574
575 return 1; /* return error */
576 }
577 cmd = 0x11; /* set the command */
578 res = a_pmw3901mb_spi_write(handle, 0x4D, (uint8_t *)&cmd, 1); /* sent the command */
579 if (res != 0) /* check result */
580 {
581 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
582
583 return 1; /* return error */
584 }
585 cmd = 0x80; /* set the command */
586 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
587 if (res != 0) /* check result */
588 {
589 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
590
591 return 1; /* return error */
592 }
593 cmd = 0x80; /* set the command */
594 res = a_pmw3901mb_spi_write(handle, 0x55, (uint8_t *)&cmd, 1); /* sent the command */
595 if (res != 0) /* check result */
596 {
597 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
598
599 return 1; /* return error */
600 }
601 cmd = 0x08; /* set the command */
602 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
603 if (res != 0) /* check result */
604 {
605 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
606
607 return 1; /* return error */
608 }
609 cmd = 0x18; /* set the command */
610 res = a_pmw3901mb_spi_write(handle, 0x6A, (uint8_t *)&cmd, 1); /* sent the command */
611 if (res != 0) /* check result */
612 {
613 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
614
615 return 1; /* return error */
616 }
617 cmd = 0x07; /* set the command */
618 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
619 if (res != 0) /* check result */
620 {
621 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
622
623 return 1; /* return error */
624 }
625 cmd = 0x0D; /* set the command */
626 res = a_pmw3901mb_spi_write(handle, 0x41, (uint8_t *)&cmd, 1); /* sent the command */
627 if (res != 0) /* check result */
628 {
629 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
630
631 return 1; /* return error */
632 }
633 cmd = 0x80; /* set the command */
634 res = a_pmw3901mb_spi_write(handle, 0x4C, (uint8_t *)&cmd, 1); /* sent the command */
635 if (res != 0) /* check result */
636 {
637 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
638
639 return 1; /* return error */
640 }
641 cmd = 0x00; /* set the command */
642 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
643 if (res != 0) /* check result */
644 {
645 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
646
647 return 1; /* return error */
648 }
649
650 return 0; /* success return 0 */
651}
652
668uint8_t pmw3901mb_get_frame(pmw3901mb_handle_t *handle, uint8_t frame[35][35])
669{
670 uint8_t res;
671 uint8_t cmd;
672 uint8_t i, j;
673 uint32_t retry_times;
674
675 if (handle == NULL) /* check handle */
676 {
677 return 2; /* return error */
678 }
679 if (handle->inited != 1) /* check handle initialization */
680 {
681 return 3; /* return error */
682 }
683
684 cmd = 0x00; /* set the command */
685 res = a_pmw3901mb_spi_write(handle, 0x70, (uint8_t *)&cmd, 1); /* sent the command */
686 if (res != 0) /* check result */
687 {
688 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
689
690 return 1; /* return error */
691 }
692 cmd = 0xFF; /* set the command */
693 res = a_pmw3901mb_spi_write(handle, 0x58, (uint8_t *)&cmd, 1); /* sent the command */
694 if (res != 0) /* check result */
695 {
696 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
697
698 return 1; /* return error */
699 }
700
701 retry_times = 10; /* set retry times */
702
703 start:
704
705 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_GRAB_STATUS, (uint8_t *)&cmd, 1); /* read grab status */
706 if (res != 0) /* check result */
707 {
708 handle->debug_print("pmw3901mb: read grab status failed.\n"); /* read grab status failed */
709
710 return 1; /* return error */
711 }
712 if ((cmd & (1 << 7)) && (cmd & (1 << 6)))
713 {
714 for (i = 0; i < 35; i++) /* 35 times */
715 {
716 for (j = 0; j < 35; j++) /* 35 times */
717 {
718 retry_times = 10; /* set retry times */
719
720 step1:
721
722 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_GRAB, (uint8_t *)&cmd, 1); /* read grab data */
723 if (res != 0) /* check result */
724 {
725 handle->debug_print("pmw3901mb: read grab data failed.\n"); /* read grab data failed */
726
727 return 1; /* return error */
728 }
729 if ((cmd & (1 << 6)) != 0) /* check flag */
730 {
731 frame[i][j] = 0; /* set 0 */
732 frame[i][j] |= (cmd & 0x3F) << 2; /* upper 6 bits */
733 }
734 else
735 {
736 retry_times--; /* retry times-- */
737 if (retry_times != 0) /* check retry times */
738 {
739 handle->delay_ms(10); /* delay 10 ms */
740
741 goto step1; /* goto step1 */
742 }
743 else
744 {
745 handle->debug_print("pmw3901mb: read timeout.\n"); /* read timeout */
746
747 return 4; /* return error */
748 }
749 }
750 retry_times = 10; /* set 10 times */
751
752 step2:
753
754 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_GRAB, (uint8_t *)&cmd, 1); /* read grab data */
755 if (res != 0) /* check result */
756 {
757 handle->debug_print("pmw3901mb: read grab data failed.\n"); /* read grab data failed */
758
759 return 1; /* return error */
760 }
761 if ((cmd & (2 << 6)) != 0) /* check flag */
762 {
763 cmd = (cmd >> 2) & 0x3; /* get 2 bits */
764 frame[i][j] = frame[i][j] | cmd; /* set the frame */
765 }
766 else
767 {
768 retry_times--; /* retry times-- */
769 if (retry_times != 0) /* check retry times */
770 {
771 handle->delay_ms(10); /* delay 10 ms */
772
773 goto step2; /* goto step2 */
774 }
775 else
776 {
777 handle->debug_print("pmw3901mb: read timeout.\n"); /* read timeout */
778
779 return 4; /* return error */
780 }
781 }
782 }
783 }
784
785 return 0; /* success return 0 */
786 }
787 else
788 {
789 retry_times--; /* retry times-- */
790 if (retry_times != 0)
791 {
792 handle->delay_ms(10); /* delay 10 ms */
793
794 goto start; /* goto start */
795 }
796 else
797 {
798 handle->debug_print("pmw3901mb: read timeout.\n"); /* read timeout */
799
800 return 4; /* return error */
801 }
802 }
803}
804
816{
817 uint8_t res;
818 uint8_t cmd;
819 uint8_t retry_times;
820
821 if (handle == NULL) /* check handle */
822 {
823 return 2; /* return error */
824 }
825 if (handle->inited != 1) /* check handle initialization */
826 {
827 return 3; /* return error */
828 }
829
830 cmd = 0x00; /* set the command */
831 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
832 if (res != 0) /* check result */
833 {
834 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
835
836 return 1; /* return error */
837 }
838 cmd = 0x01; /* set the command */
839 res = a_pmw3901mb_spi_write(handle, 0x55, (uint8_t *)&cmd, 1); /* sent the command */
840 if (res != 0) /* check result */
841 {
842 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
843
844 return 1; /* return error */
845 }
846 cmd = 0x07; /* set the command */
847 res = a_pmw3901mb_spi_write(handle, 0x50, (uint8_t *)&cmd, 1); /* sent the command */
848 if (res != 0) /* check result */
849 {
850 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
851
852 return 1; /* return error */
853 }
854 cmd = 0x0E; /* set the command */
855 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
856 if (res != 0) /* check result */
857 {
858 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
859
860 return 1; /* return error */
861 }
862
863 retry_times = 3; /* set retry times 3 */
864
865 start:
866
867 cmd = 0x10; /* set the command */
868 res = a_pmw3901mb_spi_write(handle, 0x43, (uint8_t *)&cmd, 1); /* sent the command */
869 if (res != 0) /* check result */
870 {
871 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
872
873 return 1; /* return error */
874 }
875 res = a_pmw3901mb_spi_read(handle, 0x47, (uint8_t *)&cmd, 1); /* read */
876 if (res != 0) /* check result */
877 {
878 handle->debug_print("pmw3901mb: read failed.\n"); /* read failed */
879
880 return 1; /* return error */
881 }
882 if (cmd != 0x08) /* check the result */
883 {
884 if (retry_times != 0) /* check retry times */
885 {
886 retry_times--; /* retry times-- */
887 handle->delay_ms(100); /* delay 100 ms */
888
889 goto start; /* goto start */
890 }
891 else
892 {
893 handle->debug_print("pmw3901mb: set failed.\n"); /* set failed */
894
895 return 1; /* return error */
896 }
897 }
898 res = a_pmw3901mb_spi_read(handle, 0x67, (uint8_t *)&cmd, 1); /* read */
899 if (res != 0) /* check result */
900 {
901 handle->debug_print("pmw3901mb: read failed.\n"); /* read failed */
902
903 return 1; /* return error */
904 }
905 if ((cmd & (1 << 7)) != 0)
906 {
907 cmd = 0x04; /* set the command */
908 res = a_pmw3901mb_spi_write(handle, 0x48, (uint8_t *)&cmd, 1); /* sent the command */
909 if (res != 0) /* check result */
910 {
911 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
912
913 return 1; /* return error */
914 }
915 }
916 else
917 {
918 cmd = 0x02; /* set the command */
919 res = a_pmw3901mb_spi_write(handle, 0x48, (uint8_t *)&cmd, 1); /* sent the command */
920 if (res != 0) /* check result */
921 {
922 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
923
924 return 1; /* return error */
925 }
926 }
927 cmd = 0x00; /* set the command */
928 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
929 if (res != 0) /* check result */
930 {
931 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
932
933 return 1; /* return error */
934 }
935 cmd = 0x7B; /* set the command */
936 res = a_pmw3901mb_spi_write(handle, 0x51, (uint8_t *)&cmd, 1); /* sent the command */
937 if (res != 0) /* check result */
938 {
939 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
940
941 return 1; /* return error */
942 }
943 cmd = 0x00; /* set the command */
944 res = a_pmw3901mb_spi_write(handle, 0x50, (uint8_t *)&cmd, 1); /* sent the command */
945 if (res != 0) /* check result */
946 {
947 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
948
949 return 1; /* return error */
950 }
951 cmd = 0x00; /* set the command */
952 res = a_pmw3901mb_spi_write(handle, 0x55, (uint8_t *)&cmd, 1); /* sent the command */
953 if (res != 0) /* check result */
954 {
955 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
956
957 return 1; /* return error */
958 }
959 cmd = 0x0E; /* set the command */
960 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
961 if (res != 0) /* check result */
962 {
963 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
964
965 return 1; /* return error */
966 }
967 res = a_pmw3901mb_spi_read(handle, 0x73, (uint8_t *)&cmd, 1); /* read */
968 if (res != 0) /* check result */
969 {
970 handle->debug_print("pmw3901mb: read failed.\n"); /* read failed */
971
972 return 1; /* return error */
973 }
974 if (cmd == 0x00) /* check status */
975 {
976 uint8_t c1;
977 uint8_t c2;
978
979 res = a_pmw3901mb_spi_read(handle, 0x70, (uint8_t *)&c1, 1); /* read */
980 if (res != 0) /* check result */
981 {
982 handle->debug_print("pmw3901mb: read failed.\n"); /* read failed */
983
984 return 1; /* return error */
985 }
986 if (c1 <= 28) /* check c1 */
987 {
988 c1 = c1 + 14; /* c1 = c1 + 14 */
989 }
990 else
991 {
992 c1 = c1 + 11; /* c1 = c1 + 11 */
993 }
994 if (c1 > 0x3F) /* check c1 */
995 {
996 c1 = 0x3F; /* max 0x3F */
997 }
998 res = a_pmw3901mb_spi_read(handle, 0x71, (uint8_t *)&c2, 1); /* read */
999 if (res != 0) /* check result */
1000 {
1001 handle->debug_print("pmw3901mb: read failed.\n"); /* read failed */
1002
1003 return 1; /* return error */
1004 }
1005 c2 = (uint8_t)(((uint32_t)c2 * 45) / 100); /* get c2 */
1006 cmd = 0x00; /* set the command */
1007 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1008 if (res != 0) /* check result */
1009 {
1010 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1011
1012 return 1; /* return error */
1013 }
1014 cmd = 0xAD; /* set the command */
1015 res = a_pmw3901mb_spi_write(handle, 0x61, (uint8_t *)&cmd, 1); /* sent the command */
1016 if (res != 0) /* check result */
1017 {
1018 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1019
1020 return 1; /* return error */
1021 }
1022 cmd = 0x70; /* set the command */
1023 res = a_pmw3901mb_spi_write(handle, 0x51, (uint8_t *)&cmd, 1); /* sent the command */
1024 if (res != 0) /* check result */
1025 {
1026 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1027
1028 return 1; /* return error */
1029 }
1030 cmd = 0x0E; /* set the command */
1031 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1032 if (res != 0) /* check result */
1033 {
1034 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1035
1036 return 1; /* return error */
1037 }
1038 cmd = c1; /* set the command */
1039 res = a_pmw3901mb_spi_write(handle, 0x70, (uint8_t *)&cmd, 1); /* sent the command */
1040 if (res != 0) /* check result */
1041 {
1042 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1043
1044 return 1; /* return error */
1045 }
1046 cmd = c2; /* set the command */
1047 res = a_pmw3901mb_spi_write(handle, 0x71, (uint8_t *)&cmd, 1); /* sent the command */
1048 if (res != 0) /* check result */
1049 {
1050 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1051
1052 return 1; /* return error */
1053 }
1054 }
1055
1056 cmd = 0x00; /* set the command */
1057 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1058 if (res != 0) /* check result */
1059 {
1060 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1061
1062 return 1; /* return error */
1063 }
1064 cmd = 0xAD; /* set the command */
1065 res = a_pmw3901mb_spi_write(handle, 0x61, (uint8_t *)&cmd, 1); /* sent the command */
1066 if (res != 0) /* check result */
1067 {
1068 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1069
1070 return 1; /* return error */
1071 }
1072 cmd = 0x03; /* set the command */
1073 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1074 if (res != 0) /* check result */
1075 {
1076 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1077
1078 return 1; /* return error */
1079 }
1080 cmd = 0x00; /* set the command */
1081 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
1082 if (res != 0) /* check result */
1083 {
1084 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1085
1086 return 1; /* return error */
1087 }
1088 cmd = 0x05; /* set the command */
1089 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1090 if (res != 0) /* check result */
1091 {
1092 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1093
1094 return 1; /* return error */
1095 }
1096 cmd = 0xB3; /* set the command */
1097 res = a_pmw3901mb_spi_write(handle, 0x41, (uint8_t *)&cmd, 1); /* sent the command */
1098 if (res != 0) /* check result */
1099 {
1100 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1101
1102 return 1; /* return error */
1103 }
1104 cmd = 0xF1; /* set the command */
1105 res = a_pmw3901mb_spi_write(handle, 0x43, (uint8_t *)&cmd, 1); /* sent the command */
1106 if (res != 0) /* check result */
1107 {
1108 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1109
1110 return 1; /* return error */
1111 }
1112 cmd = 0x14; /* set the command */
1113 res = a_pmw3901mb_spi_write(handle, 0x45, (uint8_t *)&cmd, 1); /* sent the command */
1114 if (res != 0) /* check result */
1115 {
1116 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1117
1118 return 1; /* return error */
1119 }
1120 cmd = 0x32; /* set the command */
1121 res = a_pmw3901mb_spi_write(handle, 0x5B, (uint8_t *)&cmd, 1); /* sent the command */
1122 if (res != 0) /* check result */
1123 {
1124 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1125
1126 return 1; /* return error */
1127 }
1128 cmd = 0x34; /* set the command */
1129 res = a_pmw3901mb_spi_write(handle, 0x5F, (uint8_t *)&cmd, 1); /* sent the command */
1130 if (res != 0) /* check result */
1131 {
1132 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1133
1134 return 1; /* return error */
1135 }
1136 cmd = 0x08; /* set the command */
1137 res = a_pmw3901mb_spi_write(handle, 0x7B, (uint8_t *)&cmd, 1); /* sent the command */
1138 if (res != 0) /* check result */
1139 {
1140 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1141
1142 return 1; /* return error */
1143 }
1144 cmd = 0x06; /* set the command */
1145 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1146 if (res != 0) /* check result */
1147 {
1148 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1149
1150 return 1; /* return error */
1151 }
1152 cmd = 0x1B; /* set the command */
1153 res = a_pmw3901mb_spi_write(handle, 0x44, (uint8_t *)&cmd, 1); /* sent the command */
1154 if (res != 0) /* check result */
1155 {
1156 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1157
1158 return 1; /* return error */
1159 }
1160 cmd = 0xBF; /* set the command */
1161 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
1162 if (res != 0) /* check result */
1163 {
1164 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1165
1166 return 1; /* return error */
1167 }
1168 cmd = 0x3F; /* set the command */
1169 res = a_pmw3901mb_spi_write(handle, 0x4E, (uint8_t *)&cmd, 1); /* sent the command */
1170 if (res != 0) /* check result */
1171 {
1172 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1173
1174 return 1; /* return error */
1175 }
1176 cmd = 0x08; /* set the command */
1177 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1178 if (res != 0) /* check result */
1179 {
1180 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1181
1182 return 1; /* return error */
1183 }
1184 cmd = 0x20; /* set the command */
1185 res = a_pmw3901mb_spi_write(handle, 0x65, (uint8_t *)&cmd, 1); /* sent the command */
1186 if (res != 0) /* check result */
1187 {
1188 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1189
1190 return 1; /* return error */
1191 }
1192 cmd = 0x18; /* set the command */
1193 res = a_pmw3901mb_spi_write(handle, 0x6A, (uint8_t *)&cmd, 1); /* sent the command */
1194 if (res != 0) /* check result */
1195 {
1196 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1197
1198 return 1; /* return error */
1199 }
1200 cmd = 0x09; /* set the command */
1201 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1202 if (res != 0) /* check result */
1203 {
1204 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1205
1206 return 1; /* return error */
1207 }
1208 cmd = 0xAF; /* set the command */
1209 res = a_pmw3901mb_spi_write(handle, 0x4F, (uint8_t *)&cmd, 1); /* sent the command */
1210 if (res != 0) /* check result */
1211 {
1212 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1213
1214 return 1; /* return error */
1215 }
1216 cmd = 0x40; /* set the command */
1217 res = a_pmw3901mb_spi_write(handle, 0x5F, (uint8_t *)&cmd, 1); /* sent the command */
1218 if (res != 0) /* check result */
1219 {
1220 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1221
1222 return 1; /* return error */
1223 }
1224 cmd = 0x80; /* set the command */
1225 res = a_pmw3901mb_spi_write(handle, 0x48, (uint8_t *)&cmd, 1); /* sent the command */
1226 if (res != 0) /* check result */
1227 {
1228 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1229
1230 return 1; /* return error */
1231 }
1232 cmd = 0x80; /* set the command */
1233 res = a_pmw3901mb_spi_write(handle, 0x49, (uint8_t *)&cmd, 1); /* sent the command */
1234 if (res != 0) /* check result */
1235 {
1236 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1237
1238 return 1; /* return error */
1239 }
1240 cmd = 0x77; /* set the command */
1241 res = a_pmw3901mb_spi_write(handle, 0x57, (uint8_t *)&cmd, 1); /* sent the command */
1242 if (res != 0) /* check result */
1243 {
1244 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1245
1246 return 1; /* return error */
1247 }
1248 cmd = 0x78; /* set the command */
1249 res = a_pmw3901mb_spi_write(handle, 0x60, (uint8_t *)&cmd, 1); /* sent the command */
1250 if (res != 0) /* check result */
1251 {
1252 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1253
1254 return 1; /* return error */
1255 }
1256 cmd = 0x78; /* set the command */
1257 res = a_pmw3901mb_spi_write(handle, 0x61, (uint8_t *)&cmd, 1); /* sent the command */
1258 if (res != 0) /* check result */
1259 {
1260 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1261
1262 return 1; /* return error */
1263 }
1264 cmd = 0x08; /* set the command */
1265 res = a_pmw3901mb_spi_write(handle, 0x62, (uint8_t *)&cmd, 1); /* sent the command */
1266 if (res != 0) /* check result */
1267 {
1268 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1269
1270 return 1; /* return error */
1271 }
1272 cmd = 0x50; /* set the command */
1273 res = a_pmw3901mb_spi_write(handle, 0x63, (uint8_t *)&cmd, 1); /* sent the command */
1274 if (res != 0) /* check result */
1275 {
1276 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1277
1278 return 1; /* return error */
1279 }
1280 cmd = 0x0A; /* set the command */
1281 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1282 if (res != 0) /* check result */
1283 {
1284 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1285
1286 return 1; /* return error */
1287 }
1288 cmd = 0x60; /* set the command */
1289 res = a_pmw3901mb_spi_write(handle, 0x45, (uint8_t *)&cmd, 1); /* sent the command */
1290 if (res != 0) /* check result */
1291 {
1292 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1293
1294 return 1; /* return error */
1295 }
1296 cmd = 0x00; /* set the command */
1297 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1298 if (res != 0) /* check result */
1299 {
1300 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1301
1302 return 1; /* return error */
1303 }
1304 cmd = 0x11; /* set the command */
1305 res = a_pmw3901mb_spi_write(handle, 0x4D, (uint8_t *)&cmd, 1); /* sent the command */
1306 if (res != 0) /* check result */
1307 {
1308 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1309
1310 return 1; /* return error */
1311 }
1312 cmd = 0x80; /* set the command */
1313 res = a_pmw3901mb_spi_write(handle, 0x55, (uint8_t *)&cmd, 1); /* sent the command */
1314 if (res != 0) /* check result */
1315 {
1316 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1317
1318 return 1; /* return error */
1319 }
1320 cmd = 0x1F; /* set the command */
1321 res = a_pmw3901mb_spi_write(handle, 0x74, (uint8_t *)&cmd, 1); /* sent the command */
1322 if (res != 0) /* check result */
1323 {
1324 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1325
1326 return 1; /* return error */
1327 }
1328 cmd = 0x1F; /* set the command */
1329 res = a_pmw3901mb_spi_write(handle, 0x75, (uint8_t *)&cmd, 1); /* sent the command */
1330 if (res != 0) /* check result */
1331 {
1332 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1333
1334 return 1; /* return error */
1335 }
1336 cmd = 0x78; /* set the command */
1337 res = a_pmw3901mb_spi_write(handle, 0x4A, (uint8_t *)&cmd, 1); /* sent the command */
1338 if (res != 0) /* check result */
1339 {
1340 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1341
1342 return 1; /* return error */
1343 }
1344 cmd = 0x78; /* set the command */
1345 res = a_pmw3901mb_spi_write(handle, 0x4B, (uint8_t *)&cmd, 1); /* sent the command */
1346 if (res != 0) /* check result */
1347 {
1348 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1349
1350 return 1; /* return error */
1351 }
1352 cmd = 0x08; /* set the command */
1353 res = a_pmw3901mb_spi_write(handle, 0x44, (uint8_t *)&cmd, 1); /* sent the command */
1354 if (res != 0) /* check result */
1355 {
1356 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1357
1358 return 1; /* return error */
1359 }
1360 cmd = 0x50; /* set the command */
1361 res = a_pmw3901mb_spi_write(handle, 0x45, (uint8_t *)&cmd, 1); /* sent the command */
1362 if (res != 0) /* check result */
1363 {
1364 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1365
1366 return 1; /* return error */
1367 }
1368 cmd = 0xFF; /* set the command */
1369 res = a_pmw3901mb_spi_write(handle, 0x64, (uint8_t *)&cmd, 1); /* sent the command */
1370 if (res != 0) /* check result */
1371 {
1372 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1373
1374 return 1; /* return error */
1375 }
1376 cmd = 0x1F; /* set the command */
1377 res = a_pmw3901mb_spi_write(handle, 0x65, (uint8_t *)&cmd, 1); /* sent the command */
1378 if (res != 0) /* check result */
1379 {
1380 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1381
1382 return 1; /* return error */
1383 }
1384 cmd = 0x14; /* set the command */
1385 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1386 if (res != 0) /* check result */
1387 {
1388 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1389
1390 return 1; /* return error */
1391 }
1392 cmd = 0x67; /* set the command */
1393 res = a_pmw3901mb_spi_write(handle, 0x65, (uint8_t *)&cmd, 1); /* sent the command */
1394 if (res != 0) /* check result */
1395 {
1396 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1397
1398 return 1; /* return error */
1399 }
1400 cmd = 0x08; /* set the command */
1401 res = a_pmw3901mb_spi_write(handle, 0x66, (uint8_t *)&cmd, 1); /* sent the command */
1402 if (res != 0) /* check result */
1403 {
1404 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1405
1406 return 1; /* return error */
1407 }
1408 cmd = 0x70; /* set the command */
1409 res = a_pmw3901mb_spi_write(handle, 0x63, (uint8_t *)&cmd, 1); /* sent the command */
1410 if (res != 0) /* check result */
1411 {
1412 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1413
1414 return 1; /* return error */
1415 }
1416 cmd = 0x15; /* set the command */
1417 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1418 if (res != 0) /* check result */
1419 {
1420 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1421
1422 return 1; /* return error */
1423 }
1424 cmd = 0x48; /* set the command */
1425 res = a_pmw3901mb_spi_write(handle, 0x48, (uint8_t *)&cmd, 1); /* sent the command */
1426 if (res != 0) /* check result */
1427 {
1428 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1429
1430 return 1; /* return error */
1431 }
1432 cmd = 0x07; /* set the command */
1433 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1434 if (res != 0) /* check result */
1435 {
1436 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1437
1438 return 1; /* return error */
1439 }
1440 cmd = 0x0D; /* set the command */
1441 res = a_pmw3901mb_spi_write(handle, 0x41, (uint8_t *)&cmd, 1); /* sent the command */
1442 if (res != 0) /* check result */
1443 {
1444 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1445
1446 return 1; /* return error */
1447 }
1448 cmd = 0x14; /* set the command */
1449 res = a_pmw3901mb_spi_write(handle, 0x43, (uint8_t *)&cmd, 1); /* sent the command */
1450 if (res != 0) /* check result */
1451 {
1452 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1453
1454 return 1; /* return error */
1455 }
1456 cmd = 0x0E; /* set the command */
1457 res = a_pmw3901mb_spi_write(handle, 0x4B, (uint8_t *)&cmd, 1); /* sent the command */
1458 if (res != 0) /* check result */
1459 {
1460 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1461
1462 return 1; /* return error */
1463 }
1464 cmd = 0x0F; /* set the command */
1465 res = a_pmw3901mb_spi_write(handle, 0x45, (uint8_t *)&cmd, 1); /* sent the command */
1466 if (res != 0) /* check result */
1467 {
1468 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1469
1470 return 1; /* return error */
1471 }
1472 cmd = 0x42; /* set the command */
1473 res = a_pmw3901mb_spi_write(handle, 0x44, (uint8_t *)&cmd, 1); /* sent the command */
1474 if (res != 0) /* check result */
1475 {
1476 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1477
1478 return 1; /* return error */
1479 }
1480 cmd = 0x80; /* set the command */
1481 res = a_pmw3901mb_spi_write(handle, 0x4C, (uint8_t *)&cmd, 1); /* sent the command */
1482 if (res != 0) /* check result */
1483 {
1484 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1485
1486 return 1; /* return error */
1487 }
1488 cmd = 0x10; /* set the command */
1489 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1490 if (res != 0) /* check result */
1491 {
1492 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1493
1494 return 1; /* return error */
1495 }
1496 cmd = 0x02; /* set the command */
1497 res = a_pmw3901mb_spi_write(handle, 0x5B, (uint8_t *)&cmd, 1); /* sent the command */
1498 if (res != 0) /* check result */
1499 {
1500 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1501
1502 return 1; /* return error */
1503 }
1504 cmd = 0x07; /* set the command */
1505 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1506 if (res != 0) /* check result */
1507 {
1508 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1509
1510 return 1; /* return error */
1511 }
1512 cmd = 0x41; /* set the command */
1513 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
1514 if (res != 0) /* check result */
1515 {
1516 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1517
1518 return 1; /* return error */
1519 }
1520 cmd = 0x00; /* set the command */
1521 res = a_pmw3901mb_spi_write(handle, 0x70, (uint8_t *)&cmd, 1); /* sent the command */
1522 if (res != 0) /* check result */
1523 {
1524 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1525
1526 return 1; /* return error */
1527 }
1528 handle->delay_ms(10); /* delay 10 ms */
1529 cmd = 0x44; /* set the command */
1530 res = a_pmw3901mb_spi_write(handle, 0x32, (uint8_t *)&cmd, 1); /* sent the command */
1531 if (res != 0) /* check result */
1532 {
1533 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1534
1535 return 1; /* return error */
1536 }
1537 cmd = 0x07; /* set the command */
1538 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1539 if (res != 0) /* check result */
1540 {
1541 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1542
1543 return 1; /* return error */
1544 }
1545 cmd = 0x40; /* set the command */
1546 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
1547 if (res != 0) /* check result */
1548 {
1549 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1550
1551 return 1; /* return error */
1552 }
1553 cmd = 0x06; /* set the command */
1554 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1555 if (res != 0) /* check result */
1556 {
1557 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1558
1559 return 1; /* return error */
1560 }
1561 cmd = 0xF0; /* set the command */
1562 res = a_pmw3901mb_spi_write(handle, 0x62, (uint8_t *)&cmd, 1); /* sent the command */
1563 if (res != 0) /* check result */
1564 {
1565 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1566
1567 return 1; /* return error */
1568 }
1569 cmd = 0x00; /* set the command */
1570 res = a_pmw3901mb_spi_write(handle, 0x63, (uint8_t *)&cmd, 1); /* sent the command */
1571 if (res != 0) /* check result */
1572 {
1573 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1574
1575 return 1; /* return error */
1576 }
1577 cmd = 0x0D; /* set the command */
1578 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1579 if (res != 0) /* check result */
1580 {
1581 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1582
1583 return 1; /* return error */
1584 }
1585 cmd = 0xC0; /* set the command */
1586 res = a_pmw3901mb_spi_write(handle, 0x48, (uint8_t *)&cmd, 1); /* sent the command */
1587 if (res != 0) /* check result */
1588 {
1589 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1590
1591 return 1; /* return error */
1592 }
1593 cmd = 0xD5; /* set the command */
1594 res = a_pmw3901mb_spi_write(handle, 0x6F, (uint8_t *)&cmd, 1); /* sent the command */
1595 if (res != 0) /* check result */
1596 {
1597 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1598
1599 return 1; /* return error */
1600 }
1601 cmd = 0x00; /* set the command */
1602 res = a_pmw3901mb_spi_write(handle, 0x7F, (uint8_t *)&cmd, 1); /* sent the command */
1603 if (res != 0) /* check result */
1604 {
1605 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1606
1607 return 1; /* return error */
1608 }
1609 cmd = 0xA0; /* set the command */
1610 res = a_pmw3901mb_spi_write(handle, 0x5B, (uint8_t *)&cmd, 1); /* sent the command */
1611 if (res != 0) /* check result */
1612 {
1613 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1614
1615 return 1; /* return error */
1616 }
1617 cmd = 0xA8; /* set the command */
1618 res = a_pmw3901mb_spi_write(handle, 0x4E, (uint8_t *)&cmd, 1); /* sent the command */
1619 if (res != 0) /* check result */
1620 {
1621 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1622
1623 return 1; /* return error */
1624 }
1625 cmd = 0x50; /* set the command */
1626 res = a_pmw3901mb_spi_write(handle, 0x5A, (uint8_t *)&cmd, 1); /* sent the command */
1627 if (res != 0) /* check result */
1628 {
1629 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1630
1631 return 1; /* return error */
1632 }
1633 cmd = 0x80; /* set the command */
1634 res = a_pmw3901mb_spi_write(handle, 0x40, (uint8_t *)&cmd, 1); /* sent the command */
1635 if (res != 0) /* check result */
1636 {
1637 handle->debug_print("pmw3901mb: sent the command failed.\n"); /* sent the command failed */
1638
1639 return 1; /* return error */
1640 }
1641
1642 return 0; /* success return 0 */
1643}
1644
1657{
1658 uint8_t res;
1659
1660 if (handle == NULL) /* check handle */
1661 {
1662 return 2; /* return error */
1663 }
1664 if (handle->inited != 1) /* check handle initialization */
1665 {
1666 return 3; /* return error */
1667 }
1668
1669 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_PRODUCT_ID, (uint8_t *)id, 1); /* get product id */
1670 if (res != 0) /* check result */
1671 {
1672 handle->debug_print("pmw3901mb: get product id failed.\n"); /* get product id failed */
1673
1674 return 1; /* return error */
1675 }
1676
1677 return 0; /* success return 0 */
1678}
1679
1692{
1693 uint8_t res;
1694
1695 if (handle == NULL) /* check handle */
1696 {
1697 return 2; /* return error */
1698 }
1699 if (handle->inited != 1) /* check handle initialization */
1700 {
1701 return 3; /* return error */
1702 }
1703
1704 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_INVERSE_PRODUCT_ID, (uint8_t *)id, 1); /* get inverse product id */
1705 if (res != 0) /* check result */
1706 {
1707 handle->debug_print("pmw3901mb: get inverse product id failed.\n"); /* get inverse product id failed */
1708
1709 return 1; /* return error */
1710 }
1711
1712 return 0; /* success return 0 */
1713}
1714
1727{
1728 uint8_t res;
1729
1730 if (handle == NULL) /* check handle */
1731 {
1732 return 2; /* return error */
1733 }
1734 if (handle->inited != 1) /* check handle initialization */
1735 {
1736 return 3; /* return error */
1737 }
1738
1739 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_REVISION_ID, (uint8_t *)id, 1); /* get revision id */
1740 if (res != 0) /* check result */
1741 {
1742 handle->debug_print("pmw3901mb: get revision id failed.\n"); /* get revision id failed */
1743
1744 return 1; /* return error */
1745 }
1746
1747 return 0; /* success return 0 */
1748}
1749
1761{
1762 uint8_t res;
1763 uint8_t cmd;
1764
1765 if (handle == NULL) /* check handle */
1766 {
1767 return 2; /* return error */
1768 }
1769 if (handle->inited != 1) /* check handle initialization */
1770 {
1771 return 3; /* return error */
1772 }
1773
1774 cmd = 0x5A; /* power up command */
1775 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_POWER_UP_RESET, (uint8_t *)&cmd, 1); /* set power up reset */
1776 if (res != 0) /* check result */
1777 {
1778 handle->debug_print("pmw3901mb: set power up reset failed.\n"); /* set power up reset failed */
1779
1780 return 1; /* return error */
1781 }
1782
1783 return 0; /* success return 0 */
1784}
1785
1797{
1798 uint8_t res;
1799 uint8_t cmd;
1800
1801 if (handle == NULL) /* check handle */
1802 {
1803 return 2; /* return error */
1804 }
1805 if (handle->inited != 1) /* check handle initialization */
1806 {
1807 return 3; /* return error */
1808 }
1809
1810 cmd = 0xB6; /* shutdown command */
1811 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_SHUTDOWN, (uint8_t *)&cmd, 1); /* set shutdown reset */
1812 if (res != 0) /* check result */
1813 {
1814 handle->debug_print("pmw3901mb: set shutdown failed.\n"); /* set shutdown failed */
1815
1816 return 1; /* return error */
1817 }
1818
1819 return 0; /* success return 0 */
1820}
1821
1833uint8_t pmw3901mb_get_motion(pmw3901mb_handle_t *handle, uint8_t *motion)
1834{
1835 uint8_t res;
1836
1837 if (handle == NULL) /* check handle */
1838 {
1839 return 2; /* return error */
1840 }
1841 if (handle->inited != 1) /* check handle initialization */
1842 {
1843 return 3; /* return error */
1844 }
1845
1846 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MOTION, (uint8_t *)motion, 1); /* get motion */
1847 if (res != 0) /* check result */
1848 {
1849 handle->debug_print("pmw3901mb: get motion failed.\n"); /* get motion failed */
1850
1851 return 1; /* return error */
1852 }
1853
1854 return 0; /* success return 0 */
1855}
1856
1868uint8_t pmw3901mb_set_motion(pmw3901mb_handle_t *handle, uint8_t motion)
1869{
1870 uint8_t res;
1871
1872 if (handle == NULL) /* check handle */
1873 {
1874 return 2; /* return error */
1875 }
1876 if (handle->inited != 1) /* check handle initialization */
1877 {
1878 return 3; /* return error */
1879 }
1880
1881 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_MOTION, (uint8_t *)&motion, 1); /* set motion */
1882 if (res != 0) /* check result */
1883 {
1884 handle->debug_print("pmw3901mb: set motion failed.\n"); /* set motion failed */
1885
1886 return 1; /* return error */
1887 }
1888
1889 return 0; /* success return 0 */
1890}
1891
1903uint8_t pmw3901mb_get_delta_x(pmw3901mb_handle_t *handle, int16_t *delta)
1904{
1905 uint8_t res;
1906 uint8_t msb;
1907 uint8_t lsb;
1908
1909 if (handle == NULL) /* check handle */
1910 {
1911 return 2; /* return error */
1912 }
1913 if (handle->inited != 1) /* check handle initialization */
1914 {
1915 return 3; /* return error */
1916 }
1917
1918 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_X_L, (uint8_t *)&lsb, 1); /* get delta xl */
1919 if (res != 0) /* check result */
1920 {
1921 handle->debug_print("pmw3901mb: get delta xl failed.\n"); /* get delta xl failed */
1922
1923 return 1; /* return error */
1924 }
1925 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_X_H, (uint8_t *)&msb, 1); /* get delta xh */
1926 if (res != 0) /* check result */
1927 {
1928 handle->debug_print("pmw3901mb: get delta xh failed.\n"); /* get delta xh failed */
1929
1930 return 1; /* return error */
1931 }
1932 *delta = (int16_t)(((uint16_t)(msb) << 8) | lsb); /* get delta */
1933
1934 return 0; /* success return 0 */
1935}
1936
1948uint8_t pmw3901mb_get_delta_y(pmw3901mb_handle_t *handle, int16_t *delta)
1949{
1950 uint8_t res;
1951 uint8_t msb;
1952 uint8_t lsb;
1953
1954 if (handle == NULL) /* check handle */
1955 {
1956 return 2; /* return error */
1957 }
1958 if (handle->inited != 1) /* check handle initialization */
1959 {
1960 return 3; /* return error */
1961 }
1962
1963 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_Y_L, (uint8_t *)&lsb, 1); /* get delta yl */
1964 if (res != 0) /* check result */
1965 {
1966 handle->debug_print("pmw3901mb: get delta yl failed.\n"); /* get delta yl failed */
1967
1968 return 1; /* return error */
1969 }
1970 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_DELTA_Y_H, (uint8_t *)&msb, 1); /* get delta yh */
1971 if (res != 0) /* check result */
1972 {
1973 handle->debug_print("pmw3901mb: get delta yh failed.\n"); /* get delta yh failed */
1974
1975 return 1; /* return error */
1976 }
1977 *delta = (int16_t)(((uint16_t)(msb) << 8) | lsb); /* get delta */
1978
1979 return 0; /* success return 0 */
1980}
1981
1994uint8_t pmw3901mb_get_motion_burst(pmw3901mb_handle_t *handle, uint8_t *burst, uint8_t len)
1995{
1996 uint8_t res;
1997
1998 if (handle == NULL) /* check handle */
1999 {
2000 return 2; /* return error */
2001 }
2002 if (handle->inited != 1) /* check handle initialization */
2003 {
2004 return 3; /* return error */
2005 }
2006
2007 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MOTION_BURST, (uint8_t *)burst, len); /* get motion burst */
2008 if (res != 0) /* check result */
2009 {
2010 handle->debug_print("pmw3901mb: get motion burst failed.\n"); /* get motion burst failed */
2011
2012 return 1; /* return error */
2013 }
2014
2015 return 0; /* success return 0 */
2016}
2017
2029uint8_t pmw3901mb_get_squal(pmw3901mb_handle_t *handle, uint8_t *squal)
2030{
2031 uint8_t res;
2032
2033 if (handle == NULL) /* check handle */
2034 {
2035 return 2; /* return error */
2036 }
2037 if (handle->inited != 1) /* check handle initialization */
2038 {
2039 return 3; /* return error */
2040 }
2041
2042 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_SQUAL, (uint8_t *)squal, 1); /* get squal */
2043 if (res != 0) /* check result */
2044 {
2045 handle->debug_print("pmw3901mb: get squal failed.\n"); /* get squal failed */
2046
2047 return 1; /* return error */
2048 }
2049
2050 return 0; /* success return 0 */
2051}
2052
2065{
2066 uint8_t res;
2067
2068 if (handle == NULL) /* check handle */
2069 {
2070 return 2; /* return error */
2071 }
2072 if (handle->inited != 1) /* check handle initialization */
2073 {
2074 return 3; /* return error */
2075 }
2076
2077 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_SUM, (uint8_t *)sum, 1); /* get sum */
2078 if (res != 0) /* check result */
2079 {
2080 handle->debug_print("pmw3901mb: get sum failed.\n"); /* get sum failed */
2081
2082 return 1; /* return error */
2083 }
2084
2085 return 0; /* success return 0 */
2086}
2087
2100{
2101 uint8_t res;
2102
2103 if (handle == NULL) /* check handle */
2104 {
2105 return 2; /* return error */
2106 }
2107 if (handle->inited != 1) /* check handle initialization */
2108 {
2109 return 3; /* return error */
2110 }
2111
2112 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MAXIMUM_RAW_DATA, (uint8_t *)max, 1); /* get maximum raw data */
2113 if (res != 0) /* check result */
2114 {
2115 handle->debug_print("pmw3901mb: get maximum raw data failed.\n"); /* get maximum raw data failed */
2116
2117 return 1; /* return error */
2118 }
2119
2120 return 0; /* success return 0 */
2121}
2122
2135{
2136 uint8_t res;
2137
2138 if (handle == NULL) /* check handle */
2139 {
2140 return 2; /* return error */
2141 }
2142 if (handle->inited != 1) /* check handle initialization */
2143 {
2144 return 3; /* return error */
2145 }
2146
2147 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_MINIMUM_RAW_DATA, (uint8_t *)min, 1); /* get minimum raw data */
2148 if (res != 0) /* check result */
2149 {
2150 handle->debug_print("pmw3901mb: get minimum raw data failed.\n"); /* get minimum raw data failed */
2151
2152 return 1; /* return error */
2153 }
2154
2155 return 0; /* success return 0 */
2156}
2157
2169uint8_t pmw3901mb_get_shutter(pmw3901mb_handle_t *handle, uint16_t *shutter)
2170{
2171 uint8_t res;
2172 uint8_t msb;
2173 uint8_t lsb;
2174
2175 if (handle == NULL) /* check handle */
2176 {
2177 return 2; /* return error */
2178 }
2179 if (handle->inited != 1) /* check handle initialization */
2180 {
2181 return 3; /* return error */
2182 }
2183
2184 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_SHUTTER_UPPER, (uint8_t *)&msb, 1); /* get shutter upper */
2185 if (res != 0) /* check result */
2186 {
2187 handle->debug_print("pmw3901mb: get shutter upper failed.\n"); /* get shutter upper failed */
2188
2189 return 1; /* return error */
2190 }
2191 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_SHUTTER_LOWER, (uint8_t *)&lsb, 1); /* get shutter lower */
2192 if (res != 0) /* check result */
2193 {
2194 handle->debug_print("pmw3901mb: get shutter lower failed.\n"); /* get shutter lower failed */
2195
2196 return 1; /* return error */
2197 }
2198 *shutter = (uint16_t)(((uint16_t)(msb & 0x1F) << 8) | lsb); /* get shutter */
2199
2200 return 0; /* success return 0 */
2201}
2202
2214uint8_t pmw3901mb_get_observation(pmw3901mb_handle_t *handle, uint8_t *observation)
2215{
2216 uint8_t res;
2217
2218 if (handle == NULL) /* check handle */
2219 {
2220 return 2; /* return error */
2221 }
2222 if (handle->inited != 1) /* check handle initialization */
2223 {
2224 return 3; /* return error */
2225 }
2226
2227 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_OBSERVATION, (uint8_t *)observation, 1); /* get observation data */
2228 if (res != 0) /* check result */
2229 {
2230 handle->debug_print("pmw3901mb: get observation failed.\n"); /* get observation failed */
2231
2232 return 1; /* return error */
2233 }
2234 *observation = (*observation) & 0x3F; /* set the observation */
2235
2236 return 0; /* success return 0 */
2237}
2238
2250uint8_t pmw3901mb_set_observation(pmw3901mb_handle_t *handle, uint8_t observation)
2251{
2252 uint8_t res;
2253
2254 if (handle == NULL) /* check handle */
2255 {
2256 return 2; /* return error */
2257 }
2258 if (handle->inited != 1) /* check handle initialization */
2259 {
2260 return 3; /* return error */
2261 }
2262
2263 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_OBSERVATION, (uint8_t *)&observation, 1); /* set observation data */
2264 if (res != 0) /* check result */
2265 {
2266 handle->debug_print("pmw3901mb: set observation failed.\n"); /* set observation failed */
2267
2268 return 1; /* return error */
2269 }
2270
2271 return 0; /* success return 0 */
2272}
2273
2286uint8_t pmw3901mb_get_raw_data_grab(pmw3901mb_handle_t *handle, uint8_t *grab, uint16_t len)
2287{
2288 uint8_t res;
2289 uint16_t i;
2290
2291 if (handle == NULL) /* check handle */
2292 {
2293 return 2; /* return error */
2294 }
2295 if (handle->inited != 1) /* check handle initialization */
2296 {
2297 return 3; /* return error */
2298 }
2299
2300 for (i = 0; i < len; i++) /* length times */
2301 {
2302 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_GRAB, (uint8_t *)&grab[i], 1); /* get raw data grab data */
2303 if (res != 0) /* check result */
2304 {
2305 handle->debug_print("pmw3901mb: get raw data grab failed.\n"); /* get raw data grab failed */
2306
2307 return 1; /* return error */
2308 }
2309 }
2310
2311 return 0; /* success return 0 */
2312}
2313
2326uint8_t pmw3901mb_set_raw_data_grab(pmw3901mb_handle_t *handle, uint8_t *grab, uint16_t len)
2327{
2328 uint8_t res;
2329 uint16_t i;
2330
2331 if (handle == NULL) /* check handle */
2332 {
2333 return 2; /* return error */
2334 }
2335 if (handle->inited != 1) /* check handle initialization */
2336 {
2337 return 3; /* return error */
2338 }
2339
2340 for (i = 0; i < len; i++) /* length times */
2341 {
2342 res = a_pmw3901mb_spi_write(handle, PMW3901MB_REG_RAW_DATA_GRAB, (uint8_t *)&grab[i], 1); /* set raw data grab */
2343 if (res != 0) /* check result */
2344 {
2345 handle->debug_print("pmw3901mb: set raw data grab failed.\n"); /* set raw data grab failed */
2346
2347 return 1; /* return error */
2348 }
2349 }
2350
2351 return 0; /* success return 0 */
2352}
2353
2366{
2367 uint8_t res;
2368
2369 if (handle == NULL) /* check handle */
2370 {
2371 return 2; /* return error */
2372 }
2373 if (handle->inited != 1) /* check handle initialization */
2374 {
2375 return 3; /* return error */
2376 }
2377
2378 res = a_pmw3901mb_spi_read(handle, PMW3901MB_REG_RAW_DATA_GRAB_STATUS, (uint8_t *)status, 1); /* get raw data grab status */
2379 if (res != 0) /* check result */
2380 {
2381 handle->debug_print("pmw3901mb: get raw data grab status failed.\n"); /* get raw data grab status failed */
2382
2383 return 1; /* return error */
2384 }
2385
2386 return 0; /* success return 0 */
2387}
2388
2402uint8_t pmw3901mb_delta_raw_to_delta_cm(pmw3901mb_handle_t *handle, int16_t raw, float height_m, float *cm)
2403{
2404 if (handle == NULL) /* check handle */
2405 {
2406 return 2; /* return error */
2407 }
2408 if (handle->inited != 1) /* check handle initialization */
2409 {
2410 return 3; /* return error */
2411 }
2412
2413 *cm = (float)(raw) * height_m / 11.914f * 2.54f; /* convert */
2414
2415 return 0; /* success return 0 */
2416}
2417
2431uint8_t pmw3901mb_set_reg(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2432{
2433 if (handle == NULL) /* check handle */
2434 {
2435 return 2; /* return error */
2436 }
2437 if (handle->inited != 1) /* check handle initialization */
2438 {
2439 return 3; /* return error */
2440 }
2441
2442 return a_pmw3901mb_spi_write(handle, reg, buf, len); /* write data */
2443}
2444
2458uint8_t pmw3901mb_get_reg(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2459{
2460 if (handle == NULL) /* check handle */
2461 {
2462 return 2; /* return error */
2463 }
2464 if (handle->inited != 1) /* check handle initialization */
2465 {
2466 return 3; /* return error */
2467 }
2468
2469 return a_pmw3901mb_spi_read(handle, reg, buf, len); /* read data */
2470}
2471
2481{
2482 if (info == NULL) /* check handle */
2483 {
2484 return 2; /* return error */
2485 }
2486
2487 memset(info, 0, sizeof(pmw3901mb_info_t)); /* initialize pmw3901mb info structure */
2488 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2489 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2490 strncpy(info->interface, "SPI", 8); /* copy interface name */
2491 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2492 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2493 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2494 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2495 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2496 info->driver_version = DRIVER_VERSION; /* set driver version */
2497
2498 return 0; /* success return 0 */
2499}
#define PMW3901MB_REG_MINIMUM_RAW_DATA
#define PMW3901MB_REG_SHUTTER_LOWER
#define MAX_CURRENT
#define PMW3901MB_REG_DELTA_X_H
#define PMW3901MB_REG_INVERSE_PRODUCT_ID
#define PMW3901MB_REG_DELTA_Y_L
#define SUPPLY_VOLTAGE_MAX
#define PMW3901MB_REG_MOTION
#define PMW3901MB_REG_OBSERVATION
#define PMW3901MB_REG_RAW_DATA_GRAB
#define PMW3901MB_REG_REVISION_ID
#define PMW3901MB_REG_SHUTTER_UPPER
#define TEMPERATURE_MAX
#define PMW3901MB_REG_PRODUCT_ID
chip register definition
#define PMW3901MB_REG_RAW_DATA_SUM
#define MANUFACTURER_NAME
#define PMW3901MB_REG_RAW_DATA_GRAB_STATUS
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define PMW3901MB_REG_MOTION_BURST
#define PMW3901MB_REG_DELTA_X_L
#define PMW3901MB_REG_DELTA_Y_H
#define CHIP_NAME
chip information definition
#define PMW3901MB_REG_POWER_UP_RESET
#define DRIVER_VERSION
#define PMW3901MB_REG_SHUTDOWN
#define PMW3901MB_REG_SQUAL
#define PMW3901MB_REG_MAXIMUM_RAW_DATA
driver pmw3901mb header file
uint8_t pmw3901mb_init(pmw3901mb_handle_t *handle)
initialize the chip
uint8_t pmw3901mb_set_raw_data_grab(pmw3901mb_handle_t *handle, uint8_t *grab, uint16_t len)
set the raw data grab
uint8_t pmw3901mb_stop_frame_capture(pmw3901mb_handle_t *handle)
stop frame capture
uint8_t pmw3901mb_get_min_raw_data(pmw3901mb_handle_t *handle, uint8_t *min)
get the minimum raw data value
struct pmw3901mb_handle_s pmw3901mb_handle_t
pmw3901mb handle structure definition
uint8_t pmw3901mb_get_shutter(pmw3901mb_handle_t *handle, uint16_t *shutter)
get the shutter
uint8_t pmw3901mb_get_motion_burst(pmw3901mb_handle_t *handle, uint8_t *burst, uint8_t len)
get the motion burst
uint8_t pmw3901mb_set_observation(pmw3901mb_handle_t *handle, uint8_t observation)
set the observation
uint8_t pmw3901mb_set_motion(pmw3901mb_handle_t *handle, uint8_t motion)
set the motion
uint8_t pmw3901mb_get_motion(pmw3901mb_handle_t *handle, uint8_t *motion)
get the motion
struct pmw3901mb_info_s pmw3901mb_info_t
pmw3901mb information structure definition
uint8_t pmw3901mb_get_squal(pmw3901mb_handle_t *handle, uint8_t *squal)
get the surface quality
uint8_t pmw3901mb_get_delta_y(pmw3901mb_handle_t *handle, int16_t *delta)
get the delta y
uint8_t pmw3901mb_get_delta_x(pmw3901mb_handle_t *handle, int16_t *delta)
get the delta x
uint8_t pmw3901mb_get_frame(pmw3901mb_handle_t *handle, uint8_t frame[35][35])
get the frame
uint8_t pmw3901mb_get_max_raw_data(pmw3901mb_handle_t *handle, uint8_t *max)
get the maximum raw data value
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
uint8_t pmw3901mb_shutdown(pmw3901mb_handle_t *handle)
shutdown the chip
uint8_t pmw3901mb_info(pmw3901mb_info_t *info)
get chip's information
uint8_t pmw3901mb_get_raw_data_sum(pmw3901mb_handle_t *handle, uint8_t *sum)
get the average raw data value
struct pmw3901mb_motion_s pmw3901mb_motion_t
pmw3901mb motion structure definition
uint8_t pmw3901mb_get_product_id(pmw3901mb_handle_t *handle, uint8_t *id)
get the product id
uint8_t pmw3901mb_get_inverse_product_id(pmw3901mb_handle_t *handle, uint8_t *id)
get the inverse product id
uint8_t pmw3901mb_get_raw_data_grab(pmw3901mb_handle_t *handle, uint8_t *grab, uint16_t len)
get the raw data grab
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_get_observation(pmw3901mb_handle_t *handle, uint8_t *observation)
get the observation
uint8_t pmw3901mb_deinit(pmw3901mb_handle_t *handle)
close the chip
uint8_t pmw3901mb_get_raw_data_grab_status(pmw3901mb_handle_t *handle, uint8_t *status)
get the raw data grab status
uint8_t pmw3901mb_reset(pmw3901mb_handle_t *handle)
reset the chip
uint8_t pmw3901mb_burst_read(pmw3901mb_handle_t *handle, pmw3901mb_motion_t *motion)
burst read data
uint8_t pmw3901mb_start_frame_capture(pmw3901mb_handle_t *handle)
start frame capture
uint8_t pmw3901mb_get_revision_id(pmw3901mb_handle_t *handle, uint8_t *id)
get the revision id
uint8_t pmw3901mb_get_reg(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t pmw3901mb_set_reg(pmw3901mb_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read)(uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* reset_gpio_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* reset_gpio_init)(void)
uint8_t(* reset_gpio_write)(uint8_t value)