Si115X.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <Arduino.h>
  2. #include "Si115X.h"
  3. // Si115X::Si115X() {
  4. // //empty constructor
  5. // }
  6. /**
  7. * Configures a channel at a given index
  8. */
  9. void Si115X::config_channel(uint8_t index, uint8_t *conf){
  10. int len = sizeof(conf);
  11. if(len != 4 || index > 5)
  12. return;
  13. int inc = index * len;
  14. // ADCCONFIGx:
  15. // - bits[7] - Reserved
  16. // - bits[6:5] - A/D conversion rate
  17. // - bits[4:0] - ADC MUX select (see below)
  18. // 00000 - Small IR 00001 - Medium IR 00010 - Large IR
  19. // 01011 - Visible 01101 - Large Visible
  20. param_set(Si115X::ADCCONFIG_0 + inc, conf[0]);
  21. // ADCSENSx:
  22. // - bits[7] - ADC high signal range enable
  23. // - bits[6:4] - an internal accumulation of samples
  24. // - bits[3:0] - Measurement time for 512 decimation rate
  25. param_set(Si115X::ADCSENS_0 + inc, conf[1]);
  26. // ADCPOSTx:
  27. // - bits[7] - Reserved
  28. // - bits[6] - HOSTOUTx size(0 = 16 bits, 1 = 24 bits)
  29. // - bits[5:3] - Number of bits to shift right of the output
  30. // - bits[2] - Threshold polarity
  31. // - bits[1:0] - Threshold enable
  32. param_set(Si115X::ADCPOST_0 + inc, conf[2]);
  33. // MEASCONFIGx:
  34. // - bits[7:6] - MEASCOUNTx select
  35. // - bits[5:4] - Reserved
  36. // - bits[3] - LEDx_A or LEDx_B BANK select
  37. // - bits[2:0] - LEDx enable
  38. param_set(Si115X::MEASCONFIG_0 + inc, conf[3]);
  39. }
  40. /**
  41. * Writes data over i2c
  42. */
  43. void Si115X::write_data(uint8_t addr, uint8_t *data, size_t len){
  44. Wire.beginTransmission(addr);
  45. Wire.write(data, len);
  46. Wire.endTransmission();
  47. }
  48. /**
  49. * Reads data from a register over i2c
  50. */
  51. int Si115X::read_register(uint8_t addr, uint8_t reg, int bytesOfData){
  52. int val = -1;
  53. Si115X::write_data(addr, &reg, sizeof(reg));
  54. Wire.requestFrom(addr, bytesOfData);
  55. if(Wire.available())
  56. val = Wire.read();
  57. return val;
  58. }
  59. /**
  60. * param set as shown in the datasheet
  61. */
  62. void Si115X::param_set(uint8_t loc, uint8_t val){
  63. uint8_t packet[2];
  64. int r;
  65. int cmmnd_ctr;
  66. do {
  67. cmmnd_ctr = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  68. packet[0] = Si115X::HOSTIN_0;
  69. packet[1] = val;
  70. Si115X::write_data(Si115X::DEVICE_ADDRESS, packet, sizeof(packet));
  71. packet[0] = Si115X::COMMAND;
  72. packet[1] = loc | (0B10 << 6);
  73. Si115X::write_data(Si115X::DEVICE_ADDRESS, packet, sizeof(packet));
  74. r = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  75. } while(r > cmmnd_ctr);
  76. }
  77. /**
  78. * param query as shown in the datasheet
  79. */
  80. int Si115X::param_query(uint8_t loc){
  81. int result = -1;
  82. uint8_t packet[2];
  83. int r;
  84. int cmmnd_ctr;
  85. do {
  86. cmmnd_ctr = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  87. packet[0] = Si115X::COMMAND;
  88. packet[1] = loc | (0B01 << 6);
  89. Si115X::write_data(Si115X::DEVICE_ADDRESS, packet, sizeof(packet));
  90. r = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  91. } while(r > cmmnd_ctr);
  92. result = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_1, 1);
  93. return result;
  94. }
  95. /**
  96. * Sends command to the command register
  97. */
  98. void Si115X::send_command(uint8_t code){
  99. uint8_t packet[2];
  100. int r;
  101. int cmmnd_ctr;
  102. do {
  103. cmmnd_ctr = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  104. packet[0] = Si115X::COMMAND;
  105. packet[1] = code;
  106. Si115X::write_data(Si115X::DEVICE_ADDRESS, packet, sizeof(packet));
  107. r = Si115X::read_register(Si115X::DEVICE_ADDRESS, Si115X::RESPONSE_0, 1);
  108. } while(r > cmmnd_ctr);
  109. }
  110. /**
  111. * Returns int given a byte array
  112. */
  113. int Si115X::get_int_from_bytes(uint8_t *data, size_t len){
  114. int result = 0;
  115. int shift = 8 * len;
  116. for(size_t i = 0; i < len; i++){
  117. shift -= 8;
  118. result |= ((data[i] << shift) & (0xFF << shift));
  119. }
  120. return result;
  121. }
  122. bool Si115X::Begin(bool mode){
  123. is_autonomous = mode;
  124. Wire.begin();
  125. // Wire.setClock(400000);
  126. // send_command(RESET_SW);
  127. if (ReadByte(0x00) != 0x51) {
  128. return false;
  129. }
  130. // Enable 2 channels for proximity measurement
  131. param_set(CHAN_LIST, 0B000011);
  132. // Enable Interrupt
  133. write_register(DEVICE_ADDRESS, IRQ_ENABLE, 0B000011);
  134. // Initialize LED current
  135. param_set(LED1_A, 0x3F);
  136. param_set(LED1_B, 0x3F);
  137. // Configure ADC and enable LED drive
  138. if (is_autonomous) {
  139. param_set(MEASRATE_H, 0);
  140. param_set(MEASRATE_L, 1); // 1 for a base period of 800 us
  141. param_set(MEASCOUNT_0, 1);
  142. param_set(MEASCOUNT_1, 1);
  143. param_set(THRESHOLD0_L, 0);
  144. param_set(THRESHOLD0_H, 0);
  145. uint8_t conf[4]; // ADCCONFIGx, ADCSENSx, ADCPOSTx, MEASCONFIGx
  146. conf[0] = 0B01100000; // 1x Small IR
  147. conf[1] = 0B00000010; // 48.8us Nominal Measurement time for 512 decimation rate
  148. conf[2] = 0B00000001; // 16-bits output, Interrupt when the measurement is larger than THRESHOLD0
  149. conf[3] = 0B01000001; // enable LED1A, the time between measurements is 800*MEASRATE*MEASCOUNT0 us
  150. config_channel(0, conf);
  151. conf[0] = 0B01101101; // 1x Visible
  152. conf[1] = 0B00000010; // 48.8us Nominal Measurement time for 512 decimation rate
  153. conf[2] = 0B00000001; // 16-bits output, Interrupt when the measurement is larger than THRESHOLD0
  154. conf[3] = 0B10001001; // enable LED1B, the time between measurements is 800*MEASRATE*MEASCOUNT1 us
  155. config_channel(1, conf);
  156. }
  157. else {
  158. param_set(ADCCONFIG_0, 0B01100000);
  159. param_set(MEASCONFIG_0, 0x00);
  160. param_set(ADCPOST_0, 0x00);
  161. param_set(ADCCONFIG_1, 0B01101101);
  162. param_set(MEASCONFIG_1, 0x00);
  163. param_set(ADCPOST_1, 0x00);
  164. }
  165. send_command(START);
  166. return true;
  167. }
  168. uint16_t Si115X::ReadIR(void) {
  169. if (!is_autonomous) send_command(FORCE);
  170. uint8_t data[2];
  171. data[0] = read_register(DEVICE_ADDRESS, HOSTOUT_0);
  172. data[1] = read_register(DEVICE_ADDRESS, HOSTOUT_1);
  173. return (data[0] << 8) + data[1];
  174. }
  175. uint16_t Si115X::ReadVisible(void) {
  176. if (!is_autonomous) send_command(FORCE);
  177. uint8_t data[2];
  178. data[0] = read_register(DEVICE_ADDRESS, HOSTOUT_2);
  179. data[1] = read_register(DEVICE_ADDRESS, HOSTOUT_3);
  180. return (data[0] << 8) + data[1];
  181. }
  182. uint8_t Si115X::ReadByte(uint8_t Reg) {
  183. Wire.beginTransmission(DEVICE_ADDRESS);
  184. Wire.write(Reg);
  185. Wire.endTransmission();
  186. Wire.requestFrom(DEVICE_ADDRESS, 1);
  187. return Wire.read();
  188. }