pat9125.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "uni_avr_rpi.h"
  2. #ifdef PAT9125
  3. #include "pat9125.h"
  4. #ifdef PAT9125_SWSPI
  5. #include "swspi.h"
  6. #endif //PAT9125_SWSPI
  7. #ifdef PAT9125_SWI2C
  8. #include "swi2c.h"
  9. #endif //PAT9125_SWI2C
  10. unsigned char pat9125_PID1 = 0;
  11. unsigned char pat9125_PID2 = 0;
  12. int pat9125_x = 0;
  13. int pat9125_y = 0;
  14. int pat9125_b = 0;
  15. int pat9125_init(unsigned char xres, unsigned char yres)
  16. {
  17. #ifdef PAT9125_SWSPI
  18. swspi_init();
  19. #endif //PAT9125_SWSPI
  20. #ifdef PAT9125_SWI2C
  21. swi2c_init(PAT9125_SWI2C_SDA, PAT9125_SWI2C_SCL, PAT9125_SWI2C_CFG);
  22. #endif //PAT9125_SWI2C
  23. pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
  24. pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
  25. if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
  26. {
  27. return 0;
  28. }
  29. pat9125_wr_reg(PAT9125_RES_X, xres);
  30. pat9125_wr_reg(PAT9125_RES_Y, yres);
  31. return 1;
  32. }
  33. int pat9125_update()
  34. {
  35. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  36. {
  37. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  38. pat9125_b = pat9125_rd_reg(PAT9125_FRAME);
  39. if (ucMotion & 0x80)
  40. {
  41. unsigned char ucXL = pat9125_rd_reg(PAT9125_DELTA_XL);
  42. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  43. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  44. int iDX = ucXL | ((ucXYH << 4) & 0xf00);
  45. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  46. if (iDX & 0x800) iDX -= 4096;
  47. if (iDY & 0x800) iDY -= 4096;
  48. pat9125_x += iDX;
  49. pat9125_y += iDY;
  50. return 1;
  51. }
  52. }
  53. return 0;
  54. }
  55. unsigned char pat9125_rd_reg(unsigned char addr)
  56. {
  57. unsigned char data = 0;
  58. #ifdef PAT9125_SWSPI
  59. swspi_start();
  60. swspi_tx(addr & 0x7f);
  61. data = swspi_rx();
  62. swspi_stop();
  63. #endif //PAT9125_SWSPI
  64. #ifdef PAT9125_SWI2C
  65. int iret = swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data);
  66. #endif //PAT9125_SWI2C
  67. return data;
  68. }
  69. void pat9125_wr_reg(unsigned char addr, unsigned char data)
  70. {
  71. #ifdef PAT9125_SWSPI
  72. swspi_start();
  73. swspi_tx(addr | 0x80);
  74. swspi_tx(data);
  75. swspi_stop();
  76. #endif //PAT9125_SWSPI
  77. #ifdef PAT9125_SWI2C
  78. int iret = swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data);
  79. #endif //PAT9125_SWI2C
  80. }
  81. #endif //PAT9125