APDS9930.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @file APDS-9930.h
  3. * @brief Library for the SparkFun APDS-9930 breakout board
  4. * @author Shawn Hymel (SparkFun Electronics)
  5. *
  6. * @copyright This code is public domain but you buy me a beer if you use
  7. * this and we meet someday (Beerware license).
  8. *
  9. * This library interfaces the Avago APDS-9930 to Arduino over I2C. The library
  10. * relies on the Arduino Wire (I2C) library. to use the library, instantiate an
  11. * APDS9930 object, call init(), and call the appropriate functions.
  12. */
  13. #ifndef APDS9930_H
  14. #define APDS9930_H
  15. #include <Arduino.h>
  16. /* Debug */
  17. #define DEBUG 0
  18. /* APDS-9930 I2C address */
  19. #define APDS9930_I2C_ADDR 0x39
  20. /* Command register modes */
  21. #define REPEATED_BYTE 0x80
  22. #define AUTO_INCREMENT 0xA0
  23. #define SPECIAL_FN 0xE0
  24. /* Error code for returned values */
  25. #define ERROR 0xFF
  26. /* Acceptable device IDs */
  27. #define APDS9930_ID_1 0x12
  28. #define APDS9930_ID_2 0x39
  29. /* Misc parameters */
  30. #define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads
  31. /* APDS-9930 register addresses */
  32. #define APDS9930_ENABLE 0x00
  33. #define APDS9930_ATIME 0x01
  34. #define APDS9930_PTIME 0x02
  35. #define APDS9930_WTIME 0x03
  36. #define APDS9930_AILTL 0x04
  37. #define APDS9930_AILTH 0x05
  38. #define APDS9930_AIHTL 0x06
  39. #define APDS9930_AIHTH 0x07
  40. #define APDS9930_PILTL 0x08
  41. #define APDS9930_PILTH 0x09
  42. #define APDS9930_PIHTL 0x0A
  43. #define APDS9930_PIHTH 0x0B
  44. #define APDS9930_PERS 0x0C
  45. #define APDS9930_CONFIG 0x0D
  46. #define APDS9930_PPULSE 0x0E
  47. #define APDS9930_CONTROL 0x0F
  48. #define APDS9930_ID 0x12
  49. #define APDS9930_STATUS 0x13
  50. #define APDS9930_Ch0DATAL 0x14
  51. #define APDS9930_Ch0DATAH 0x15
  52. #define APDS9930_Ch1DATAL 0x16
  53. #define APDS9930_Ch1DATAH 0x17
  54. #define APDS9930_PDATAL 0x18
  55. #define APDS9930_PDATAH 0x19
  56. #define APDS9930_POFFSET 0x1E
  57. /* Bit fields */
  58. #define APDS9930_PON 0b00000001
  59. #define APDS9930_AEN 0b00000010
  60. #define APDS9930_PEN 0b00000100
  61. #define APDS9930_WEN 0b00001000
  62. #define APSD9930_AIEN 0b00010000
  63. #define APDS9930_PIEN 0b00100000
  64. #define APDS9930_SAI 0b01000000
  65. /* On/Off definitions */
  66. #define OFF 0
  67. #define ON 1
  68. /* Acceptable parameters for setMode */
  69. #define POWER 0
  70. #define AMBIENT_LIGHT 1
  71. #define PROXIMITY 2
  72. #define WAIT 3
  73. #define AMBIENT_LIGHT_INT 4
  74. #define PROXIMITY_INT 5
  75. #define SLEEP_AFTER_INT 6
  76. #define ALL 7
  77. /* LED Drive values */
  78. #define LED_DRIVE_100MA 0
  79. #define LED_DRIVE_50MA 1
  80. #define LED_DRIVE_25MA 2
  81. #define LED_DRIVE_12_5MA 3
  82. /* Proximity Gain (PGAIN) values */
  83. #define PGAIN_1X 0
  84. #define PGAIN_2X 1
  85. #define PGAIN_4X 2
  86. #define PGAIN_8X 3
  87. /* ALS Gain (AGAIN) values */
  88. #define AGAIN_1X 0
  89. #define AGAIN_8X 1
  90. #define AGAIN_16X 2
  91. #define AGAIN_120X 3
  92. /* Interrupt clear values */
  93. #define CLEAR_PROX_INT 0xE5
  94. #define CLEAR_ALS_INT 0xE6
  95. #define CLEAR_ALL_INTS 0xE7
  96. /* Default values */
  97. #define DEFAULT_ATIME 0xED
  98. #define DEFAULT_WTIME 0xFF
  99. #define DEFAULT_PTIME 0xFF
  100. #define DEFAULT_PPULSE 0x08
  101. #define DEFAULT_POFFSET 0 // 0 offset
  102. #define DEFAULT_CONFIG 0
  103. #define DEFAULT_PDRIVE LED_DRIVE_100MA
  104. #define DEFAULT_PDIODE 2
  105. #define DEFAULT_PGAIN PGAIN_8X
  106. #define DEFAULT_AGAIN AGAIN_1X
  107. #define DEFAULT_PILT 0 // Low proximity threshold
  108. #define DEFAULT_PIHT 50 // High proximity threshold
  109. #define DEFAULT_AILT 0xFFFF // Force interrupt for calibration
  110. #define DEFAULT_AIHT 0
  111. #define DEFAULT_PERS 0x22 // 2 consecutive prox or ALS for int.
  112. /* ALS coefficients */
  113. #define DF 52
  114. #define GA 0.49
  115. #define ALS_B 1.862
  116. #define ALS_C 0.746
  117. #define ALS_D 1.291
  118. /* State definitions */
  119. enum {
  120. NOTAVAILABLE_STATE,
  121. NEAR_STATE,
  122. FAR_STATE,
  123. ALL_STATE
  124. };
  125. #ifdef _AVR_IO_H_
  126. // Do not use this alias as it's deprecated
  127. #define NA_STATE NOTAVAILABLE_STATE
  128. #endif
  129. /* APDS9930 Class */
  130. class APDS9930 {
  131. public:
  132. /* Initialization methods */
  133. APDS9930();
  134. ~APDS9930();
  135. bool init();
  136. uint8_t getMode();
  137. bool setMode(uint8_t mode, uint8_t enable);
  138. /* Turn the APDS-9930 on and off */
  139. bool enablePower();
  140. bool disablePower();
  141. /* Enable or disable specific sensors */
  142. bool enableLightSensor(bool interrupts = false);
  143. bool disableLightSensor();
  144. bool enableProximitySensor(bool interrupts = false);
  145. bool disableProximitySensor();
  146. /* LED drive strength control */
  147. uint8_t getLEDDrive();
  148. bool setLEDDrive(uint8_t drive);
  149. // uint8_t getGestureLEDDrive();
  150. // bool setGestureLEDDrive(uint8_t drive);
  151. /* Gain control */
  152. uint8_t getAmbientLightGain();
  153. bool setAmbientLightGain(uint8_t gain);
  154. uint8_t getProximityGain();
  155. bool setProximityGain(uint8_t gain);
  156. bool setProximityDiode(uint8_t drive);
  157. uint8_t getProximityDiode();
  158. /* Get and set light interrupt thresholds */
  159. bool getLightIntLowThreshold(uint16_t &threshold);
  160. bool setLightIntLowThreshold(uint16_t threshold);
  161. bool getLightIntHighThreshold(uint16_t &threshold);
  162. bool setLightIntHighThreshold(uint16_t threshold);
  163. /* Get and set interrupt enables */
  164. uint8_t getAmbientLightIntEnable();
  165. bool setAmbientLightIntEnable(uint8_t enable);
  166. uint8_t getProximityIntEnable();
  167. bool setProximityIntEnable(uint8_t enable);
  168. /* Clear interrupts */
  169. bool clearAmbientLightInt();
  170. bool clearProximityInt();
  171. bool clearAllInts();
  172. /* Proximity methods */
  173. bool readProximity(uint16_t &val);
  174. /* Ambient light methods */
  175. bool readAmbientLightLux(float &val);
  176. bool readAmbientLightLux(unsigned long &val);
  177. float floatAmbientToLux(uint16_t Ch0, uint16_t Ch1);
  178. unsigned long ulongAmbientToLux(uint16_t Ch0, uint16_t Ch1);
  179. bool readCh0Light(uint16_t &val);
  180. bool readCh1Light(uint16_t &val);
  181. //private:
  182. /* Proximity Interrupt Threshold */
  183. uint16_t getProximityIntLowThreshold();
  184. bool setProximityIntLowThreshold(uint16_t threshold);
  185. uint16_t getProximityIntHighThreshold();
  186. bool setProximityIntHighThreshold(uint16_t threshold);
  187. /* Raw I2C Commands */
  188. bool wireWriteByte(uint8_t val);
  189. bool wireWriteDataByte(uint8_t reg, uint8_t val);
  190. bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
  191. bool wireReadDataByte(uint8_t reg, uint8_t &val);
  192. int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
  193. };
  194. #endif