pat9125.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. unsigned char pat9125_xres = 0;
  16. unsigned char pat9125_yres = 0;
  17. int pat9125_x = 0;
  18. int pat9125_y = 0;
  19. unsigned char pat9125_b = 0;
  20. unsigned char pat9125_s = 0;
  21. int pat9125_init(unsigned char xres, unsigned char yres)
  22. {
  23. #ifdef PAT9125_SWSPI
  24. swspi_init();
  25. #endif //PAT9125_SWSPI
  26. #ifdef PAT9125_SWI2C
  27. swi2c_init(PAT9125_SWI2C_SDA, PAT9125_SWI2C_SCL, PAT9125_SWI2C_CFG);
  28. #endif //PAT9125_SWI2C
  29. #ifdef PAT9125_HWI2C
  30. Wire.begin();
  31. #endif //PAT9125_HWI2C
  32. pat9125_xres = xres;
  33. pat9125_yres = yres;
  34. pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
  35. pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
  36. if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
  37. {
  38. return 0;
  39. }
  40. pat9125_wr_reg(PAT9125_RES_X, pat9125_xres);
  41. pat9125_wr_reg(PAT9125_RES_Y, pat9125_yres);
  42. // pat9125_wr_reg(PAT9125_ORIENTATION, 0x04 | (xinv?0x08:0) | (yinv?0x10:0)); //!? direction switching does not work
  43. return 1;
  44. }
  45. int pat9125_update()
  46. {
  47. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  48. {
  49. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  50. pat9125_b = pat9125_rd_reg(PAT9125_FRAME);
  51. pat9125_s = pat9125_rd_reg(PAT9125_SHUTTER);
  52. if (ucMotion & 0x80)
  53. {
  54. unsigned char ucXL = pat9125_rd_reg(PAT9125_DELTA_XL);
  55. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  56. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  57. int iDX = ucXL | ((ucXYH << 4) & 0xf00);
  58. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  59. if (iDX & 0x800) iDX -= 4096;
  60. if (iDY & 0x800) iDY -= 4096;
  61. pat9125_x += iDX;
  62. pat9125_y -= iDY; //negative number, because direction switching does not work
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }
  68. int pat9125_update_y()
  69. {
  70. if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
  71. {
  72. unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
  73. if (ucMotion & 0x80)
  74. {
  75. unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
  76. unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
  77. int iDY = ucYL | ((ucXYH << 8) & 0xf00);
  78. if (iDY & 0x800) iDY -= 4096;
  79. pat9125_y -= iDY; //negative number, because direction switching does not work
  80. return 1;
  81. }
  82. }
  83. return 0;
  84. }
  85. unsigned char pat9125_rd_reg(unsigned char addr)
  86. {
  87. unsigned char data = 0;
  88. #ifdef PAT9125_SWSPI
  89. swspi_start();
  90. swspi_tx(addr & 0x7f);
  91. data = swspi_rx();
  92. swspi_stop();
  93. #endif //PAT9125_SWSPI
  94. #ifdef PAT9125_SWI2C
  95. int iret = swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data);
  96. #endif //PAT9125_SWI2C
  97. #ifdef PAT9125_HWI2C
  98. Wire.beginTransmission(PAT9125_I2C_ADDR);
  99. Wire.write(addr);
  100. Wire.endTransmission();
  101. if (Wire.requestFrom(PAT9125_I2C_ADDR, 1) == 1)
  102. // if (Wire.available())
  103. data = Wire.read();
  104. #endif //PAT9125_HWI2C
  105. return data;
  106. }
  107. void pat9125_wr_reg(unsigned char addr, unsigned char data)
  108. {
  109. #ifdef PAT9125_SWSPI
  110. swspi_start();
  111. swspi_tx(addr | 0x80);
  112. swspi_tx(data);
  113. swspi_stop();
  114. #endif //PAT9125_SWSPI
  115. #ifdef PAT9125_SWI2C
  116. int iret = swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data);
  117. #endif //PAT9125_SWI2C
  118. #ifdef PAT9125_HWI2C
  119. Wire.beginTransmission(PAT9125_I2C_ADDR);
  120. Wire.write(addr);
  121. Wire.write(data);
  122. Wire.endTransmission();
  123. #endif //PAT9125_HWI2C
  124. }
  125. #endif //PAT9125