SparkFun_APDS9960.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @file SparkFun_APDS-9960.h
  3. * @brief Library for the SparkFun APDS-9960 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-9960 to Arduino over I2C. The library
  10. * relies on the Arduino Wire (I2C) library. to use the library, instantiate an
  11. * APDS9960 object, call init(), and call the appropriate functions.
  12. */
  13. #ifndef SparkFun_APDS9960_H
  14. #define SparkFun_APDS9960_H
  15. #include <Arduino.h>
  16. /* Debug */
  17. #define DEBUG 0
  18. /* APDS-9960 I2C address */
  19. #define APDS9960_I2C_ADDR 0x39
  20. /* Gesture parameters */
  21. #define GESTURE_THRESHOLD_OUT 10
  22. #define GESTURE_SENSITIVITY_1 50
  23. #define GESTURE_SENSITIVITY_2 20
  24. /* Error code for returned values */
  25. #define ERROR 0xFF
  26. /* Acceptable device IDs */
  27. #define APDS9960_ID_1 0xAB
  28. #define APDS9960_ID_2 0x9C
  29. #define APDS9960_ID_3 0xA8
  30. /* Misc parameters */
  31. #define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads
  32. /* APDS-9960 register addresses */
  33. #define APDS9960_ENABLE 0x80
  34. #define APDS9960_ATIME 0x81
  35. #define APDS9960_WTIME 0x83
  36. #define APDS9960_AILTL 0x84
  37. #define APDS9960_AILTH 0x85
  38. #define APDS9960_AIHTL 0x86
  39. #define APDS9960_AIHTH 0x87
  40. #define APDS9960_PILT 0x89
  41. #define APDS9960_PIHT 0x8B
  42. #define APDS9960_PERS 0x8C
  43. #define APDS9960_CONFIG1 0x8D
  44. #define APDS9960_PPULSE 0x8E
  45. #define APDS9960_CONTROL 0x8F
  46. #define APDS9960_CONFIG2 0x90
  47. #define APDS9960_ID 0x92
  48. #define APDS9960_STATUS 0x93
  49. #define APDS9960_CDATAL 0x94
  50. #define APDS9960_CDATAH 0x95
  51. #define APDS9960_RDATAL 0x96
  52. #define APDS9960_RDATAH 0x97
  53. #define APDS9960_GDATAL 0x98
  54. #define APDS9960_GDATAH 0x99
  55. #define APDS9960_BDATAL 0x9A
  56. #define APDS9960_BDATAH 0x9B
  57. #define APDS9960_PDATA 0x9C
  58. #define APDS9960_POFFSET_UR 0x9D
  59. #define APDS9960_POFFSET_DL 0x9E
  60. #define APDS9960_CONFIG3 0x9F
  61. #define APDS9960_GPENTH 0xA0
  62. #define APDS9960_GEXTH 0xA1
  63. #define APDS9960_GCONF1 0xA2
  64. #define APDS9960_GCONF2 0xA3
  65. #define APDS9960_GOFFSET_U 0xA4
  66. #define APDS9960_GOFFSET_D 0xA5
  67. #define APDS9960_GOFFSET_L 0xA7
  68. #define APDS9960_GOFFSET_R 0xA9
  69. #define APDS9960_GPULSE 0xA6
  70. #define APDS9960_GCONF3 0xAA
  71. #define APDS9960_GCONF4 0xAB
  72. #define APDS9960_GFLVL 0xAE
  73. #define APDS9960_GSTATUS 0xAF
  74. #define APDS9960_IFORCE 0xE4
  75. #define APDS9960_PICLEAR 0xE5
  76. #define APDS9960_CICLEAR 0xE6
  77. #define APDS9960_AICLEAR 0xE7
  78. #define APDS9960_GFIFO_U 0xFC
  79. #define APDS9960_GFIFO_D 0xFD
  80. #define APDS9960_GFIFO_L 0xFE
  81. #define APDS9960_GFIFO_R 0xFF
  82. /* Bit fields */
  83. #define APDS9960_PON 0b00000001
  84. #define APDS9960_AEN 0b00000010
  85. #define APDS9960_PEN 0b00000100
  86. #define APDS9960_WEN 0b00001000
  87. #define APSD9960_AIEN 0b00010000
  88. #define APDS9960_PIEN 0b00100000
  89. #define APDS9960_GEN 0b01000000
  90. #define APDS9960_GVALID 0b00000001
  91. /* On/Off definitions */
  92. #define OFF 0
  93. #define ON 1
  94. /* Acceptable parameters for setMode */
  95. #define POWER 0
  96. #define AMBIENT_LIGHT 1
  97. #define PROXIMITY 2
  98. #define WAIT 3
  99. #define AMBIENT_LIGHT_INT 4
  100. #define PROXIMITY_INT 5
  101. #define GESTURE 6
  102. #define ALL 7
  103. /* LED Drive values */
  104. #define LED_DRIVE_100MA 0
  105. #define LED_DRIVE_50MA 1
  106. #define LED_DRIVE_25MA 2
  107. #define LED_DRIVE_12_5MA 3
  108. /* Proximity Gain (PGAIN) values */
  109. #define PGAIN_1X 0
  110. #define PGAIN_2X 1
  111. #define PGAIN_4X 2
  112. #define PGAIN_8X 3
  113. /* ALS Gain (AGAIN) values */
  114. #define AGAIN_1X 0
  115. #define AGAIN_4X 1
  116. #define AGAIN_16X 2
  117. #define AGAIN_64X 3
  118. /* Gesture Gain (GGAIN) values */
  119. #define GGAIN_1X 0
  120. #define GGAIN_2X 1
  121. #define GGAIN_4X 2
  122. #define GGAIN_8X 3
  123. /* LED Boost values */
  124. #define LED_BOOST_100 0
  125. #define LED_BOOST_150 1
  126. #define LED_BOOST_200 2
  127. #define LED_BOOST_300 3
  128. /* Gesture wait time values */
  129. #define GWTIME_0MS 0
  130. #define GWTIME_2_8MS 1
  131. #define GWTIME_5_6MS 2
  132. #define GWTIME_8_4MS 3
  133. #define GWTIME_14_0MS 4
  134. #define GWTIME_22_4MS 5
  135. #define GWTIME_30_8MS 6
  136. #define GWTIME_39_2MS 7
  137. /* Default values */
  138. #define DEFAULT_ATIME 219 // 103ms
  139. #define DEFAULT_WTIME 246 // 27ms
  140. #define DEFAULT_PROX_PPULSE 0x87 // 16us, 8 pulses
  141. #define DEFAULT_GESTURE_PPULSE 0x89 // 16us, 10 pulses
  142. #define DEFAULT_POFFSET_UR 0 // 0 offset
  143. #define DEFAULT_POFFSET_DL 0 // 0 offset
  144. #define DEFAULT_CONFIG1 0x60 // No 12x wait (WTIME) factor
  145. #define DEFAULT_LDRIVE LED_DRIVE_100MA
  146. #define DEFAULT_PGAIN PGAIN_4X
  147. #define DEFAULT_AGAIN AGAIN_4X
  148. #define DEFAULT_PILT 0 // Low proximity threshold
  149. #define DEFAULT_PIHT 50 // High proximity threshold
  150. #define DEFAULT_AILT 0xFFFF // Force interrupt for calibration
  151. #define DEFAULT_AIHT 0
  152. #define DEFAULT_PERS 0x11 // 2 consecutive prox or ALS for int.
  153. #define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost
  154. #define DEFAULT_CONFIG3 0 // Enable all photodiodes, no SAI
  155. #define DEFAULT_GPENTH 40 // Threshold for entering gesture mode
  156. #define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode
  157. #define DEFAULT_GCONF1 0x40 // 4 gesture events for int., 1 for exit
  158. #define DEFAULT_GGAIN GGAIN_4X
  159. #define DEFAULT_GLDRIVE LED_DRIVE_100MA
  160. #define DEFAULT_GWTIME GWTIME_2_8MS
  161. #define DEFAULT_GOFFSET 0 // No offset scaling for gesture mode
  162. #define DEFAULT_GPULSE 0xC9 // 32us, 10 pulses
  163. #define DEFAULT_GCONF3 0 // All photodiodes active during gesture
  164. #define DEFAULT_GIEN 0 // Disable gesture interrupts
  165. /* Direction definitions */
  166. enum {
  167. DIR_NONE,
  168. DIR_LEFT,
  169. DIR_RIGHT,
  170. DIR_UP,
  171. DIR_DOWN,
  172. DIR_NEAR,
  173. DIR_FAR,
  174. DIR_ALL
  175. };
  176. /* State definitions */
  177. enum {
  178. NA_STATE,
  179. NEAR_STATE,
  180. FAR_STATE,
  181. ALL_STATE
  182. };
  183. /* Container for gesture data */
  184. typedef struct gesture_data_type {
  185. uint8_t u_data[32];
  186. uint8_t d_data[32];
  187. uint8_t l_data[32];
  188. uint8_t r_data[32];
  189. uint8_t index;
  190. uint8_t total_gestures;
  191. uint8_t in_threshold;
  192. uint8_t out_threshold;
  193. } gesture_data_type;
  194. /* APDS9960 Class */
  195. class SparkFun_APDS9960 {
  196. public:
  197. /* Initialization methods */
  198. SparkFun_APDS9960();
  199. ~SparkFun_APDS9960();
  200. bool init();
  201. uint8_t getMode();
  202. bool setMode(uint8_t mode, uint8_t enable);
  203. /* Turn the APDS-9960 on and off */
  204. bool enablePower();
  205. bool disablePower();
  206. /* Enable or disable specific sensors */
  207. bool enableLightSensor(bool interrupts = false);
  208. bool disableLightSensor();
  209. bool enableProximitySensor(bool interrupts = false);
  210. bool disableProximitySensor();
  211. bool enableGestureSensor(bool interrupts = true);
  212. bool disableGestureSensor();
  213. /* LED drive strength control */
  214. uint8_t getLEDDrive();
  215. bool setLEDDrive(uint8_t drive);
  216. uint8_t getGestureLEDDrive();
  217. bool setGestureLEDDrive(uint8_t drive);
  218. /* Gain control */
  219. uint8_t getAmbientLightGain();
  220. bool setAmbientLightGain(uint8_t gain);
  221. uint8_t getProximityGain();
  222. bool setProximityGain(uint8_t gain);
  223. uint8_t getGestureGain();
  224. bool setGestureGain(uint8_t gain);
  225. /* Get and set light interrupt thresholds */
  226. bool getLightIntLowThreshold(uint16_t &threshold);
  227. bool setLightIntLowThreshold(uint16_t threshold);
  228. bool getLightIntHighThreshold(uint16_t &threshold);
  229. bool setLightIntHighThreshold(uint16_t threshold);
  230. /* Get and set proximity interrupt thresholds */
  231. bool getProximityIntLowThreshold(uint8_t &threshold);
  232. bool setProximityIntLowThreshold(uint8_t threshold);
  233. bool getProximityIntHighThreshold(uint8_t &threshold);
  234. bool setProximityIntHighThreshold(uint8_t threshold);
  235. /* Get and set interrupt enables */
  236. uint8_t getAmbientLightIntEnable();
  237. bool setAmbientLightIntEnable(uint8_t enable);
  238. uint8_t getProximityIntEnable();
  239. bool setProximityIntEnable(uint8_t enable);
  240. uint8_t getGestureIntEnable();
  241. bool setGestureIntEnable(uint8_t enable);
  242. /* Clear interrupts */
  243. bool clearAmbientLightInt();
  244. bool clearProximityInt();
  245. /* Ambient light methods */
  246. bool readAmbientLight(uint16_t &val);
  247. bool readRedLight(uint16_t &val);
  248. bool readGreenLight(uint16_t &val);
  249. bool readBlueLight(uint16_t &val);
  250. /* Proximity methods */
  251. bool readProximity(uint8_t &val);
  252. /* Gesture methods */
  253. bool isGestureAvailable();
  254. int readGesture();
  255. private:
  256. /* Gesture processing */
  257. void resetGestureParameters();
  258. bool processGestureData();
  259. bool decodeGesture();
  260. /* Proximity Interrupt Threshold */
  261. uint8_t getProxIntLowThresh();
  262. bool setProxIntLowThresh(uint8_t threshold);
  263. uint8_t getProxIntHighThresh();
  264. bool setProxIntHighThresh(uint8_t threshold);
  265. /* LED Boost Control */
  266. uint8_t getLEDBoost();
  267. bool setLEDBoost(uint8_t boost);
  268. /* Proximity photodiode select */
  269. uint8_t getProxGainCompEnable();
  270. bool setProxGainCompEnable(uint8_t enable);
  271. uint8_t getProxPhotoMask();
  272. bool setProxPhotoMask(uint8_t mask);
  273. /* Gesture threshold control */
  274. uint8_t getGestureEnterThresh();
  275. bool setGestureEnterThresh(uint8_t threshold);
  276. uint8_t getGestureExitThresh();
  277. bool setGestureExitThresh(uint8_t threshold);
  278. /* Gesture LED, gain, and time control */
  279. uint8_t getGestureWaitTime();
  280. bool setGestureWaitTime(uint8_t time);
  281. /* Gesture mode */
  282. uint8_t getGestureMode();
  283. bool setGestureMode(uint8_t mode);
  284. /* Raw I2C Commands */
  285. bool wireWriteByte(uint8_t val);
  286. bool wireWriteDataByte(uint8_t reg, uint8_t val);
  287. bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
  288. bool wireReadDataByte(uint8_t reg, uint8_t &val);
  289. int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
  290. /* Members */
  291. gesture_data_type gesture_data_;
  292. int gesture_ud_delta_;
  293. int gesture_lr_delta_;
  294. int gesture_ud_count_;
  295. int gesture_lr_count_;
  296. int gesture_near_count_;
  297. int gesture_far_count_;
  298. int gesture_state_;
  299. int gesture_motion_;
  300. };
  301. #endif