pat9125.cpp 3.4 KB

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