pat9125.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifdef PAT9125_HWI2C
  11. #include <Wire.h>
  12. #endif //PAT9125_HWI2C
  13. unsigned char pat9125_PID1 = 0;
  14. unsigned char pat9125_PID2 = 0;
  15. int pat9125_x = 0;
  16. int pat9125_y = 0;
  17. int pat9125_b = 0;
  18. int pat9125_init(unsigned char xres, unsigned char yres)
  19. {
  20. #ifdef PAT9125_SWSPI
  21. swspi_init();
  22. #endif //PAT9125_SWSPI
  23. #ifdef PAT9125_SWI2C
  24. swi2c_init(PAT9125_SWI2C_SDA, PAT9125_SWI2C_SCL, PAT9125_SWI2C_CFG);
  25. #endif //PAT9125_SWI2C
  26. #ifdef PAT9125_HWI2C
  27. Wire.begin();
  28. #endif //PAT9125_HWI2C
  29. pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
  30. pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
  31. if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
  32. {
  33. return 0;
  34. }
  35. pat9125_wr_reg(PAT9125_RES_X, xres);
  36. pat9125_wr_reg(PAT9125_RES_Y, yres);
  37. return 1;
  38. }
  39. int pat9125_update()
  40. {
  41. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  42. {
  43. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  44. pat9125_b = pat9125_rd_reg(PAT9125_FRAME);
  45. if (ucMotion & 0x80)
  46. {
  47. unsigned char ucXL = pat9125_rd_reg(PAT9125_DELTA_XL);
  48. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  49. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  50. int iDX = ucXL | ((ucXYH << 4) & 0xf00);
  51. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  52. if (iDX & 0x800) iDX -= 4096;
  53. if (iDY & 0x800) iDY -= 4096;
  54. pat9125_x += iDX;
  55. pat9125_y += iDY;
  56. return 1;
  57. }
  58. }
  59. return 0;
  60. }
  61. unsigned char pat9125_rd_reg(unsigned char addr)
  62. {
  63. unsigned char data = 0;
  64. #ifdef PAT9125_SWSPI
  65. swspi_start();
  66. swspi_tx(addr & 0x7f);
  67. data = swspi_rx();
  68. swspi_stop();
  69. #endif //PAT9125_SWSPI
  70. #ifdef PAT9125_SWI2C
  71. int iret = swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data);
  72. #endif //PAT9125_SWI2C
  73. #ifdef PAT9125_HWI2C
  74. Wire.beginTransmission(PAT9125_I2C_ADDR);
  75. Wire.write(addr);
  76. Wire.endTransmission();
  77. if (Wire.requestFrom(PAT9125_I2C_ADDR, 1) == 1)
  78. // if (Wire.available())
  79. data = Wire.read();
  80. #endif //PAT9125_HWI2C
  81. return data;
  82. }
  83. void pat9125_wr_reg(unsigned char addr, unsigned char data)
  84. {
  85. #ifdef PAT9125_SWSPI
  86. swspi_start();
  87. swspi_tx(addr | 0x80);
  88. swspi_tx(data);
  89. swspi_stop();
  90. #endif //PAT9125_SWSPI
  91. #ifdef PAT9125_SWI2C
  92. int iret = swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data);
  93. #endif //PAT9125_SWI2C
  94. #ifdef PAT9125_HWI2C
  95. Wire.beginTransmission(PAT9125_I2C_ADDR);
  96. Wire.write(addr);
  97. Wire.write(data);
  98. Wire.endTransmission();
  99. #endif //PAT9125_HWI2C
  100. }
  101. #endif //PAT9125