pat9125.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "pat9125.h"
  2. #include "swspi.h"
  3. #ifdef SWSPI_RPI
  4. // #include <bcm2835.h>
  5. #define DELAY(delay) usleep(delay)
  6. #endif //SWSPI_RPI
  7. #ifdef SWSPI_AVR
  8. #include "Arduino.h"
  9. #define DELAY(delay) delayMicroseconds(delay)
  10. #endif //SWSPI_AVR
  11. unsigned char ucPID1 = 0;
  12. unsigned char ucPID2 = 0;
  13. int pat9125_x = 0;
  14. int pat9125_y = 0;
  15. int pat9125_init(unsigned char xres, unsigned char yres)
  16. {
  17. swspi_init();
  18. ucPID1 = pat9125_rd_reg(PAT9125_PID1);
  19. ucPID2 = pat9125_rd_reg(PAT9125_PID2);
  20. if ((ucPID1 != 0x31) || (ucPID2 != 0x91))
  21. {
  22. return 0;
  23. }
  24. pat9125_wr_reg(PAT9125_RES_X, xres);
  25. pat9125_wr_reg(PAT9125_RES_Y, yres);
  26. return 1;
  27. }
  28. int pat9125_update()
  29. {
  30. if ((ucPID1 == 0x31) && (ucPID2 == 0x91))
  31. {
  32. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  33. if (ucMotion & 0x80)
  34. {
  35. int iDX = pat9125_rd_reg(PAT9125_DELTA_XL);
  36. int iDY = pat9125_rd_reg(PAT9125_DELTA_YL);
  37. if (iDX >= 0x80) iDX = iDX - 256;
  38. if (iDY >= 0x80) iDY = iDY - 256;
  39. pat9125_x += iDX;
  40. pat9125_y += iDY;
  41. return 1;
  42. }
  43. }
  44. return 0;
  45. }
  46. unsigned char pat9125_rd_reg(unsigned char addr)
  47. {
  48. swspi_start();
  49. DELAY(100);
  50. swspi_tx(addr & 0x7f);
  51. DELAY(100);
  52. unsigned char data = swspi_rx();
  53. swspi_stop();
  54. DELAY(100);
  55. return data;
  56. }
  57. void pat9125_wr_reg(unsigned char addr, unsigned char data)
  58. {
  59. swspi_start();
  60. DELAY(100);
  61. swspi_tx(addr | 0x80);
  62. DELAY(100);
  63. swspi_tx(data);
  64. swspi_stop();
  65. DELAY(100);
  66. }