xsns_27_apds9960.ino 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /*
  2. xsns_27_apds9960.ino - Support for I2C APDS9960 Proximity Sensor for Sonoff-Tasmota
  3. Copyright (C) 2018 Shawn Hymel/Sparkfun and Theo Arends
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  15. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  16. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  17. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  19. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifdef USE_I2C
  24. #ifdef USE_APDS9960
  25. /*********************************************************************************************\
  26. * APDS9960 - Digital Proximity Ambient Light RGB and Gesture Sensor
  27. *
  28. * Source: Shawn Hymel (SparkFun Electronics)
  29. * Adaption for TASMOTA: Christian Baars
  30. *
  31. * I2C Address: 0x39
  32. \*********************************************************************************************/
  33. #define XSNS_27 27
  34. #if defined(USE_SHT) || defined(USE_VEML6070) || defined(USE_TSL2561)
  35. #warning **** Turned off conflicting drivers SHT and VEML6070 ****
  36. #ifdef USE_SHT
  37. #undef USE_SHT // SHT-Driver blocks gesture sensor
  38. #endif
  39. #ifdef USE_VEML6070
  40. #undef USE_VEML6070 // address conflict on the I2C-bus
  41. #endif
  42. #ifdef USE_TSL2561
  43. #undef USE_TSL2561 // possible address conflict on the I2C-bus
  44. #endif
  45. #endif
  46. #define APDS9960_I2C_ADDR 0x39
  47. #define APDS9960_CHIPID_1 0xAB
  48. #define APDS9960_CHIPID_2 0x9C
  49. #define APDS9930_CHIPID_1 0x12 // we will check, if someone got an incorrect sensor
  50. #define APDS9930_CHIPID_2 0x39 // there are case reports about "accidentially bought" 9930's
  51. /* Gesture parameters */
  52. #define GESTURE_THRESHOLD_OUT 10
  53. #define GESTURE_SENSITIVITY_1 50
  54. #define GESTURE_SENSITIVITY_2 20
  55. uint8_t APDS9960addr;
  56. uint8_t APDS9960type = 0;
  57. char APDS9960stype[9];
  58. char currentGesture[6];
  59. uint8_t gesture_mode = 1;
  60. volatile uint8_t recovery_loop_counter = 0; //count number of stateloops to switch the sensor off, if needed
  61. #define APDS9960_LONG_RECOVERY 50 //long pause after sensor overload in loops
  62. #define APDS9960_MAX_GESTURE_CYCLES 50 //how many FIFO-reads are allowed to prevent crash
  63. bool APDS9960_overload = false;
  64. #ifdef USE_WEBSERVER
  65. const char HTTP_APDS_9960_SNS[] PROGMEM = "%s"
  66. "{s}" "Red" "{m}%s{e}"
  67. "{s}" "Green" "{m}%s{e}"
  68. "{s}" "Blue" "{m}%s{e}"
  69. "{s}" "Ambient" "{m}%s " D_UNIT_LUX "{e}"
  70. "{s}" "CCT" "{m}%s " "K" "{e}" // calculated color temperature in Kelvin
  71. "{s}" "Proximity" "{m}%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  72. #endif // USE_WEBSERVER
  73. /*********************************************************************************************\
  74. * APDS9960
  75. *
  76. * Programmer : APDS9960 Datasheet and Sparkfun
  77. \*********************************************************************************************/
  78. /* Misc parameters */
  79. #define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads
  80. /* APDS-9960 register addresses */
  81. #define APDS9960_ENABLE 0x80
  82. #define APDS9960_ATIME 0x81
  83. #define APDS9960_WTIME 0x83
  84. #define APDS9960_AILTL 0x84
  85. #define APDS9960_AILTH 0x85
  86. #define APDS9960_AIHTL 0x86
  87. #define APDS9960_AIHTH 0x87
  88. #define APDS9960_PILT 0x89
  89. #define APDS9960_PIHT 0x8B
  90. #define APDS9960_PERS 0x8C
  91. #define APDS9960_CONFIG1 0x8D
  92. #define APDS9960_PPULSE 0x8E
  93. #define APDS9960_CONTROL 0x8F
  94. #define APDS9960_CONFIG2 0x90
  95. #define APDS9960_ID 0x92
  96. #define APDS9960_STATUS 0x93
  97. #define APDS9960_CDATAL 0x94
  98. #define APDS9960_CDATAH 0x95
  99. #define APDS9960_RDATAL 0x96
  100. #define APDS9960_RDATAH 0x97
  101. #define APDS9960_GDATAL 0x98
  102. #define APDS9960_GDATAH 0x99
  103. #define APDS9960_BDATAL 0x9A
  104. #define APDS9960_BDATAH 0x9B
  105. #define APDS9960_PDATA 0x9C
  106. #define APDS9960_POFFSET_UR 0x9D
  107. #define APDS9960_POFFSET_DL 0x9E
  108. #define APDS9960_CONFIG3 0x9F
  109. #define APDS9960_GPENTH 0xA0
  110. #define APDS9960_GEXTH 0xA1
  111. #define APDS9960_GCONF1 0xA2
  112. #define APDS9960_GCONF2 0xA3
  113. #define APDS9960_GOFFSET_U 0xA4
  114. #define APDS9960_GOFFSET_D 0xA5
  115. #define APDS9960_GOFFSET_L 0xA7
  116. #define APDS9960_GOFFSET_R 0xA9
  117. #define APDS9960_GPULSE 0xA6
  118. #define APDS9960_GCONF3 0xAA
  119. #define APDS9960_GCONF4 0xAB
  120. #define APDS9960_GFLVL 0xAE
  121. #define APDS9960_GSTATUS 0xAF
  122. #define APDS9960_IFORCE 0xE4
  123. #define APDS9960_PICLEAR 0xE5
  124. #define APDS9960_CICLEAR 0xE6
  125. #define APDS9960_AICLEAR 0xE7
  126. #define APDS9960_GFIFO_U 0xFC
  127. #define APDS9960_GFIFO_D 0xFD
  128. #define APDS9960_GFIFO_L 0xFE
  129. #define APDS9960_GFIFO_R 0xFF
  130. /* Bit fields */
  131. #define APDS9960_PON 0b00000001
  132. #define APDS9960_AEN 0b00000010
  133. #define APDS9960_PEN 0b00000100
  134. #define APDS9960_WEN 0b00001000
  135. #define APSD9960_AIEN 0b00010000
  136. #define APDS9960_PIEN 0b00100000
  137. #define APDS9960_GEN 0b01000000
  138. #define APDS9960_GVALID 0b00000001
  139. /* On/Off definitions */
  140. #define OFF 0
  141. #define ON 1
  142. /* Acceptable parameters for setMode */
  143. #define POWER 0
  144. #define AMBIENT_LIGHT 1
  145. #define PROXIMITY 2
  146. #define WAIT 3
  147. #define AMBIENT_LIGHT_INT 4
  148. #define PROXIMITY_INT 5
  149. #define GESTURE 6
  150. #define ALL 7
  151. /* LED Drive values */
  152. #define LED_DRIVE_100MA 0
  153. #define LED_DRIVE_50MA 1
  154. #define LED_DRIVE_25MA 2
  155. #define LED_DRIVE_12_5MA 3
  156. /* Proximity Gain (PGAIN) values */
  157. #define PGAIN_1X 0
  158. #define PGAIN_2X 1
  159. #define PGAIN_4X 2
  160. #define PGAIN_8X 3
  161. /* ALS Gain (AGAIN) values */
  162. #define AGAIN_1X 0
  163. #define AGAIN_4X 1
  164. #define AGAIN_16X 2
  165. #define AGAIN_64X 3
  166. /* Gesture Gain (GGAIN) values */
  167. #define GGAIN_1X 0
  168. #define GGAIN_2X 1
  169. #define GGAIN_4X 2
  170. #define GGAIN_8X 3
  171. /* LED Boost values */
  172. #define LED_BOOST_100 0
  173. #define LED_BOOST_150 1
  174. #define LED_BOOST_200 2
  175. #define LED_BOOST_300 3
  176. /* Gesture wait time values */
  177. #define GWTIME_0MS 0
  178. #define GWTIME_2_8MS 1
  179. #define GWTIME_5_6MS 2
  180. #define GWTIME_8_4MS 3
  181. #define GWTIME_14_0MS 4
  182. #define GWTIME_22_4MS 5
  183. #define GWTIME_30_8MS 6
  184. #define GWTIME_39_2MS 7
  185. /* Default values */
  186. #define DEFAULT_ATIME 0xdb // 103ms = 0xdb
  187. #define DEFAULT_WTIME 246 // 27ms
  188. #define DEFAULT_PROX_PPULSE 0x87 // 16us, 8 pulses
  189. #define DEFAULT_GESTURE_PPULSE 0x89 // 16us, 10 pulses ---89
  190. #define DEFAULT_POFFSET_UR 0 // 0 offset
  191. #define DEFAULT_POFFSET_DL 0 // 0 offset
  192. #define DEFAULT_CONFIG1 0x60 // No 12x wait (WTIME) factor
  193. #define DEFAULT_LDRIVE LED_DRIVE_100MA
  194. #define DEFAULT_PGAIN PGAIN_4X
  195. #define DEFAULT_AGAIN AGAIN_4X // we have to divide by the same facot at the end
  196. #define DEFAULT_PILT 0 // Low proximity threshold
  197. #define DEFAULT_PIHT 50 // High proximity threshold
  198. #define DEFAULT_AILT 0xFFFF // Force interrupt for calibration
  199. #define DEFAULT_AIHT 0
  200. #define DEFAULT_PERS 0x11 // 2 consecutive prox or ALS for int.
  201. #define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost
  202. #define DEFAULT_CONFIG3 0 // Enable all photodiodes, no SAI
  203. #define DEFAULT_GPENTH 40 // Threshold for entering gesture mode
  204. #define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode
  205. #define DEFAULT_GCONF1 0x40 // 4 gesture events for int., 1 for exit
  206. #define DEFAULT_GGAIN GGAIN_4X
  207. #define DEFAULT_GLDRIVE LED_DRIVE_100MA // default 100ma
  208. #define DEFAULT_GWTIME GWTIME_2_8MS // default 2_8MS
  209. #define DEFAULT_GOFFSET 0 // No offset scaling for gesture mode
  210. #define DEFAULT_GPULSE 0xC9 // 32us, 10 pulses
  211. #define DEFAULT_GCONF3 0 // All photodiodes active during gesture
  212. #define DEFAULT_GIEN 0 // Disable gesture interrupts
  213. #define ERROR 0xFF
  214. /* Direction definitions */
  215. enum {
  216. DIR_NONE,
  217. DIR_LEFT,
  218. DIR_RIGHT,
  219. DIR_UP,
  220. DIR_DOWN,
  221. DIR_ALL
  222. };
  223. /* State definitions*/
  224. enum {
  225. APDS9960_NA_STATE,
  226. APDS9960_ALL_STATE
  227. };
  228. /* Container for gesture data */
  229. typedef struct gesture_data_type {
  230. uint8_t u_data[32];
  231. uint8_t d_data[32];
  232. uint8_t l_data[32];
  233. uint8_t r_data[32];
  234. uint8_t index;
  235. uint8_t total_gestures;
  236. uint8_t in_threshold;
  237. uint8_t out_threshold;
  238. } gesture_data_type;
  239. /*Members*/
  240. gesture_data_type gesture_data_;
  241. int16_t gesture_ud_delta_ = 0;
  242. int16_t gesture_lr_delta_ = 0;
  243. int16_t gesture_ud_count_ = 0;
  244. int16_t gesture_lr_count_ = 0;
  245. int16_t gesture_state_ = 0;
  246. int16_t gesture_motion_ = DIR_NONE;
  247. typedef struct color_data_type {
  248. uint16_t a; // measured ambient
  249. uint16_t r;
  250. uint16_t g;
  251. uint16_t b;
  252. uint8_t p; // proximity
  253. uint16_t cct; // calculated color temperature
  254. uint16_t lux; // calculated illuminance - atm only from rgb
  255. } color_data_type;
  256. color_data_type color_data;
  257. uint8_t APDS9960_aTime = DEFAULT_ATIME;
  258. /*******************************************************************************
  259. * Helper functions
  260. ******************************************************************************/
  261. /**
  262. * @brief Writes a single byte to the I2C device (no register)
  263. *
  264. * @param[in] val the 1-byte value to write to the I2C device
  265. * @return True if successful write operation. False otherwise.
  266. */
  267. bool wireWriteByte(uint8_t val)
  268. {
  269. Wire.beginTransmission(APDS9960_I2C_ADDR);
  270. Wire.write(val);
  271. if( Wire.endTransmission() != 0 ) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. /**
  277. * @brief Reads a block (array) of bytes from the I2C device and register
  278. *
  279. * @param[in] reg the register to read from
  280. * @param[out] val pointer to the beginning of the data
  281. * @param[in] len number of bytes to read
  282. * @return Number of bytes read. -1 on read error.
  283. */
  284. int8_t wireReadDataBlock( uint8_t reg,
  285. uint8_t *val,
  286. uint16_t len)
  287. {
  288. unsigned char i = 0;
  289. /* Indicate which register we want to read from */
  290. if (!wireWriteByte(reg)) {
  291. return -1;
  292. }
  293. /* Read block data */
  294. Wire.requestFrom(APDS9960_I2C_ADDR, len);
  295. while (Wire.available()) {
  296. if (i >= len) {
  297. return -1;
  298. }
  299. val[i] = Wire.read();
  300. i++;
  301. }
  302. return i;
  303. }
  304. /**
  305. * Taken from the Adafruit-library
  306. * @brief Converts the raw R/G/B values to color temperature in degrees
  307. * Kelvin
  308. */
  309. void calculateColorTemperature(void)
  310. {
  311. float X, Y, Z; /* RGB to XYZ correlation */
  312. float xc, yc; /* Chromaticity co-ordinates */
  313. float n; /* McCamy's formula */
  314. float cct;
  315. /* 1. Map RGB values to their XYZ counterparts. */
  316. /* Based on 6500K fluorescent, 3000K fluorescent */
  317. /* and 60W incandescent values for a wide range. */
  318. /* Note: Y = Illuminance or lux */
  319. X = (-0.14282F * color_data.r) + (1.54924F * color_data.g) + (-0.95641F * color_data.b);
  320. Y = (-0.32466F * color_data.r) + (1.57837F * color_data.g) + (-0.73191F * color_data.b); // this is Lux ... under certain circumstances
  321. Z = (-0.68202F * color_data.r) + (0.77073F * color_data.g) + ( 0.56332F * color_data.b);
  322. /* 2. Calculate the chromaticity co-ordinates */
  323. xc = (X) / (X + Y + Z);
  324. yc = (Y) / (X + Y + Z);
  325. /* 3. Use McCamy's formula to determine the CCT */
  326. n = (xc - 0.3320F) / (0.1858F - yc);
  327. /* Calculate the final CCT */
  328. color_data.cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
  329. return;
  330. }
  331. /**
  332. * Taken from the Adafruit-Library
  333. * @brief Implements missing powf function
  334. */
  335. float powf(const float x, const float y)
  336. {
  337. return (float)(pow((double)x, (double)y));
  338. }
  339. /*******************************************************************************
  340. * Getters and setters for register values
  341. ******************************************************************************/
  342. /**
  343. * @brief Returns the lower threshold for proximity detection
  344. *
  345. * @return lower threshold
  346. */
  347. uint8_t getProxIntLowThresh(void)
  348. {
  349. uint8_t val;
  350. /* Read value from PILT register */
  351. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PILT) ;
  352. return val;
  353. }
  354. /**
  355. * @brief Sets the lower threshold for proximity detection
  356. *
  357. * @param[in] threshold the lower proximity threshold
  358. */
  359. void setProxIntLowThresh(uint8_t threshold)
  360. {
  361. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PILT, threshold);
  362. }
  363. /**
  364. * @brief Returns the high threshold for proximity detection
  365. *
  366. * @return high threshold
  367. */
  368. uint8_t getProxIntHighThresh(void)
  369. {
  370. uint8_t val;
  371. /* Read value from PIHT register */
  372. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PIHT) ;
  373. return val;
  374. }
  375. /**
  376. * @brief Sets the high threshold for proximity detection
  377. *
  378. * @param[in] threshold the high proximity threshold
  379. */
  380. void setProxIntHighThresh(uint8_t threshold)
  381. {
  382. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PIHT, threshold);
  383. }
  384. /**
  385. * @brief Returns LED drive strength for proximity and ALS
  386. *
  387. * Value LED Current
  388. * 0 100 mA
  389. * 1 50 mA
  390. * 2 25 mA
  391. * 3 12.5 mA
  392. *
  393. * @return the value of the LED drive strength. 0xFF on failure.
  394. */
  395. uint8_t getLEDDrive(void)
  396. {
  397. uint8_t val;
  398. /* Read value from CONTROL register */
  399. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ;
  400. /* Shift and mask out LED drive bits */
  401. val = (val >> 6) & 0b00000011;
  402. return val;
  403. }
  404. /**
  405. * @brief Sets the LED drive strength for proximity and ALS
  406. *
  407. * Value LED Current
  408. * 0 100 mA
  409. * 1 50 mA
  410. * 2 25 mA
  411. * 3 12.5 mA
  412. *
  413. * @param[in] drive the value (0-3) for the LED drive strength
  414. */
  415. void setLEDDrive(uint8_t drive)
  416. {
  417. uint8_t val;
  418. /* Read value from CONTROL register */
  419. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
  420. /* Set bits in register to given value */
  421. drive &= 0b00000011;
  422. drive = drive << 6;
  423. val &= 0b00111111;
  424. val |= drive;
  425. /* Write register value back into CONTROL register */
  426. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
  427. }
  428. /**
  429. * @brief Returns receiver gain for proximity detection
  430. *
  431. * Value Gain
  432. * 0 1x
  433. * 1 2x
  434. * 2 4x
  435. * 3 8x
  436. *
  437. * @return the value of the proximity gain. 0xFF on failure.
  438. */
  439. uint8_t getProximityGain(void)
  440. {
  441. uint8_t val;
  442. /* Read value from CONTROL register */
  443. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ;
  444. /* Shift and mask out PDRIVE bits */
  445. val = (val >> 2) & 0b00000011;
  446. return val;
  447. }
  448. /**
  449. * @brief Sets the receiver gain for proximity detection
  450. *
  451. * Value Gain
  452. * 0 1x
  453. * 1 2x
  454. * 2 4x
  455. * 3 8x
  456. *
  457. * @param[in] drive the value (0-3) for the gain
  458. */
  459. void setProximityGain(uint8_t drive)
  460. {
  461. uint8_t val;
  462. /* Read value from CONTROL register */
  463. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
  464. /* Set bits in register to given value */
  465. drive &= 0b00000011;
  466. drive = drive << 2;
  467. val &= 0b11110011;
  468. val |= drive;
  469. /* Write register value back into CONTROL register */
  470. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
  471. }
  472. /**
  473. * @brief Returns receiver gain for the ambient light sensor (ALS)
  474. *
  475. * Value Gain
  476. * 0 1x
  477. * 1 4x
  478. * 2 16x
  479. * 3 64x
  480. *
  481. * @return the value of the ALS gain. 0xFF on failure.
  482. */
  483. /**
  484. * @brief Sets the receiver gain for the ambient light sensor (ALS)
  485. *
  486. * Value Gain
  487. * 0 1x
  488. * 1 4x
  489. * 2 16x
  490. * 3 64x
  491. *
  492. * @param[in] drive the value (0-3) for the gain
  493. */
  494. void setAmbientLightGain(uint8_t drive)
  495. {
  496. uint8_t val;
  497. /* Read value from CONTROL register */
  498. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL);
  499. /* Set bits in register to given value */
  500. drive &= 0b00000011;
  501. val &= 0b11111100;
  502. val |= drive;
  503. /* Write register value back into CONTROL register */
  504. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val);
  505. }
  506. /**
  507. * @brief Get the current LED boost value
  508. *
  509. * Value Boost Current
  510. * 0 100%
  511. * 1 150%
  512. * 2 200%
  513. * 3 300%
  514. *
  515. * @return The LED boost value. 0xFF on failure.
  516. */
  517. uint8_t getLEDBoost(void)
  518. {
  519. uint8_t val;
  520. /* Read value from CONFIG2 register */
  521. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ;
  522. /* Shift and mask out LED_BOOST bits */
  523. val = (val >> 4) & 0b00000011;
  524. return val;
  525. }
  526. /**
  527. * @brief Sets the LED current boost value
  528. *
  529. * Value Boost Current
  530. * 0 100%
  531. * 1 150%
  532. * 2 200%
  533. * 3 300%
  534. *
  535. * @param[in] drive the value (0-3) for current boost (100-300%)
  536. */
  537. void setLEDBoost(uint8_t boost)
  538. {
  539. uint8_t val;
  540. /* Read value from CONFIG2 register */
  541. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ;
  542. /* Set bits in register to given value */
  543. boost &= 0b00000011;
  544. boost = boost << 4;
  545. val &= 0b11001111;
  546. val |= boost;
  547. /* Write register value back into CONFIG2 register */
  548. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG2, val) ;
  549. }
  550. /**
  551. * @brief Gets proximity gain compensation enable
  552. *
  553. * @return 1 if compensation is enabled. 0 if not. 0xFF on error.
  554. */
  555. uint8_t getProxGainCompEnable(void)
  556. {
  557. uint8_t val;
  558. /* Read value from CONFIG3 register */
  559. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
  560. /* Shift and mask out PCMP bits */
  561. val = (val >> 5) & 0b00000001;
  562. return val;
  563. }
  564. /**
  565. * @brief Sets the proximity gain compensation enable
  566. *
  567. * @param[in] enable 1 to enable compensation. 0 to disable compensation.
  568. */
  569. void setProxGainCompEnable(uint8_t enable)
  570. {
  571. uint8_t val;
  572. /* Read value from CONFIG3 register */
  573. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
  574. /* Set bits in register to given value */
  575. enable &= 0b00000001;
  576. enable = enable << 5;
  577. val &= 0b11011111;
  578. val |= enable;
  579. /* Write register value back into CONFIG3 register */
  580. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ;
  581. }
  582. /**
  583. * @brief Gets the current mask for enabled/disabled proximity photodiodes
  584. *
  585. * 1 = disabled, 0 = enabled
  586. * Bit Photodiode
  587. * 3 UP
  588. * 2 DOWN
  589. * 1 LEFT
  590. * 0 RIGHT
  591. *
  592. * @return Current proximity mask for photodiodes. 0xFF on error.
  593. */
  594. uint8_t getProxPhotoMask(void)
  595. {
  596. uint8_t val;
  597. /* Read value from CONFIG3 register */
  598. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
  599. /* Mask out photodiode enable mask bits */
  600. val &= 0b00001111;
  601. return val;
  602. }
  603. /**
  604. * @brief Sets the mask for enabling/disabling proximity photodiodes
  605. *
  606. * 1 = disabled, 0 = enabled
  607. * Bit Photodiode
  608. * 3 UP
  609. * 2 DOWN
  610. * 1 LEFT
  611. * 0 RIGHT
  612. *
  613. * @param[in] mask 4-bit mask value
  614. */
  615. void setProxPhotoMask(uint8_t mask)
  616. {
  617. uint8_t val;
  618. /* Read value from CONFIG3 register */
  619. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ;
  620. /* Set bits in register to given value */
  621. mask &= 0b00001111;
  622. val &= 0b11110000;
  623. val |= mask;
  624. /* Write register value back into CONFIG3 register */
  625. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ;
  626. }
  627. /**
  628. * @brief Gets the entry proximity threshold for gesture sensing
  629. *
  630. * @return Current entry proximity threshold.
  631. */
  632. uint8_t getGestureEnterThresh(void)
  633. {
  634. uint8_t val;
  635. /* Read value from GPENTH register */
  636. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GPENTH) ;
  637. return val;
  638. }
  639. /**
  640. * @brief Sets the entry proximity threshold for gesture sensing
  641. *
  642. * @param[in] threshold proximity value needed to start gesture mode
  643. */
  644. void setGestureEnterThresh(uint8_t threshold)
  645. {
  646. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold) ;
  647. }
  648. /**
  649. * @brief Gets the exit proximity threshold for gesture sensing
  650. *
  651. * @return Current exit proximity threshold.
  652. */
  653. uint8_t getGestureExitThresh(void)
  654. {
  655. uint8_t val;
  656. /* Read value from GEXTH register */
  657. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GEXTH) ;
  658. return val;
  659. }
  660. /**
  661. * @brief Sets the exit proximity threshold for gesture sensing
  662. *
  663. * @param[in] threshold proximity value needed to end gesture mode
  664. */
  665. void setGestureExitThresh(uint8_t threshold)
  666. {
  667. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold) ;
  668. }
  669. /**
  670. * @brief Gets the gain of the photodiode during gesture mode
  671. *
  672. * Value Gain
  673. * 0 1x
  674. * 1 2x
  675. * 2 4x
  676. * 3 8x
  677. *
  678. * @return the current photodiode gain. 0xFF on error.
  679. */
  680. uint8_t getGestureGain(void)
  681. {
  682. uint8_t val;
  683. /* Read value from GCONF2 register */
  684. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  685. /* Shift and mask out GGAIN bits */
  686. val = (val >> 5) & 0b00000011;
  687. return val;
  688. }
  689. /**
  690. * @brief Sets the gain of the photodiode during gesture mode
  691. *
  692. * Value Gain
  693. * 0 1x
  694. * 1 2x
  695. * 2 4x
  696. * 3 8x
  697. *
  698. * @param[in] gain the value for the photodiode gain
  699. */
  700. void setGestureGain(uint8_t gain)
  701. {
  702. uint8_t val;
  703. /* Read value from GCONF2 register */
  704. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  705. /* Set bits in register to given value */
  706. gain &= 0b00000011;
  707. gain = gain << 5;
  708. val &= 0b10011111;
  709. val |= gain;
  710. /* Write register value back into GCONF2 register */
  711. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
  712. }
  713. /**
  714. * @brief Gets the drive current of the LED during gesture mode
  715. *
  716. * Value LED Current
  717. * 0 100 mA
  718. * 1 50 mA
  719. * 2 25 mA
  720. * 3 12.5 mA
  721. *
  722. * @return the LED drive current value. 0xFF on error.
  723. */
  724. uint8_t getGestureLEDDrive(void)
  725. {
  726. uint8_t val;
  727. /* Read value from GCONF2 register */
  728. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  729. /* Shift and mask out GLDRIVE bits */
  730. val = (val >> 3) & 0b00000011;
  731. return val;
  732. }
  733. /**
  734. * @brief Sets the LED drive current during gesture mode
  735. *
  736. * Value LED Current
  737. * 0 100 mA
  738. * 1 50 mA
  739. * 2 25 mA
  740. * 3 12.5 mA
  741. *
  742. * @param[in] drive the value for the LED drive current
  743. */
  744. void setGestureLEDDrive(uint8_t drive)
  745. {
  746. uint8_t val;
  747. /* Read value from GCONF2 register */
  748. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  749. /* Set bits in register to given value */
  750. drive &= 0b00000011;
  751. drive = drive << 3;
  752. val &= 0b11100111;
  753. val |= drive;
  754. /* Write register value back into GCONF2 register */
  755. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
  756. }
  757. /**
  758. * @brief Gets the time in low power mode between gesture detections
  759. *
  760. * Value Wait time
  761. * 0 0 ms
  762. * 1 2.8 ms
  763. * 2 5.6 ms
  764. * 3 8.4 ms
  765. * 4 14.0 ms
  766. * 5 22.4 ms
  767. * 6 30.8 ms
  768. * 7 39.2 ms
  769. *
  770. * @return the current wait time between gestures. 0xFF on error.
  771. */
  772. uint8_t getGestureWaitTime(void)
  773. {
  774. uint8_t val;
  775. /* Read value from GCONF2 register */
  776. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  777. /* Mask out GWTIME bits */
  778. val &= 0b00000111;
  779. return val;
  780. }
  781. /**
  782. * @brief Sets the time in low power mode between gesture detections
  783. *
  784. * Value Wait time
  785. * 0 0 ms
  786. * 1 2.8 ms
  787. * 2 5.6 ms
  788. * 3 8.4 ms
  789. * 4 14.0 ms
  790. * 5 22.4 ms
  791. * 6 30.8 ms
  792. * 7 39.2 ms
  793. *
  794. * @param[in] the value for the wait time
  795. */
  796. void setGestureWaitTime(uint8_t time)
  797. {
  798. uint8_t val;
  799. /* Read value from GCONF2 register */
  800. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ;
  801. /* Set bits in register to given value */
  802. time &= 0b00000111;
  803. val &= 0b11111000;
  804. val |= time;
  805. /* Write register value back into GCONF2 register */
  806. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ;
  807. }
  808. /**
  809. * @brief Gets the low threshold for ambient light interrupts
  810. *
  811. * @param[out] threshold current low threshold stored on the APDS-9960
  812. */
  813. void getLightIntLowThreshold(uint16_t &threshold)
  814. {
  815. uint8_t val_byte;
  816. threshold = 0;
  817. /* Read value from ambient light low threshold, low byte register */
  818. val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AILTL) ;
  819. threshold = val_byte;
  820. /* Read value from ambient light low threshold, high byte register */
  821. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_byte) ;
  822. threshold = threshold + ((uint16_t)val_byte << 8);
  823. }
  824. /**
  825. * @brief Sets the low threshold for ambient light interrupts
  826. *
  827. * @param[in] threshold low threshold value for interrupt to trigger
  828. */
  829. void setLightIntLowThreshold(uint16_t threshold)
  830. {
  831. uint8_t val_low;
  832. uint8_t val_high;
  833. /* Break 16-bit threshold into 2 8-bit values */
  834. val_low = threshold & 0x00FF;
  835. val_high = (threshold & 0xFF00) >> 8;
  836. /* Write low byte */
  837. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTL, val_low) ;
  838. /* Write high byte */
  839. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_high) ;
  840. }
  841. /**
  842. * @brief Gets the high threshold for ambient light interrupts
  843. *
  844. * @param[out] threshold current low threshold stored on the APDS-9960
  845. */
  846. void getLightIntHighThreshold(uint16_t &threshold)
  847. {
  848. uint8_t val_byte;
  849. threshold = 0;
  850. /* Read value from ambient light high threshold, low byte register */
  851. val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AIHTL);
  852. threshold = val_byte;
  853. /* Read value from ambient light high threshold, high byte register */
  854. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTH, val_byte) ;
  855. threshold = threshold + ((uint16_t)val_byte << 8);
  856. }
  857. /**
  858. * @brief Sets the high threshold for ambient light interrupts
  859. *
  860. * @param[in] threshold high threshold value for interrupt to trigger
  861. */
  862. void setLightIntHighThreshold(uint16_t threshold)
  863. {
  864. uint8_t val_low;
  865. uint8_t val_high;
  866. /* Break 16-bit threshold into 2 8-bit values */
  867. val_low = threshold & 0x00FF;
  868. val_high = (threshold & 0xFF00) >> 8;
  869. /* Write low byte */
  870. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTL, val_low);
  871. /* Write high byte */
  872. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTH, val_high) ;
  873. }
  874. /**
  875. * @brief Gets the low threshold for proximity interrupts
  876. *
  877. * @param[out] threshold current low threshold stored on the APDS-9960
  878. */
  879. void getProximityIntLowThreshold(uint8_t &threshold)
  880. {
  881. threshold = 0;
  882. /* Read value from proximity low threshold register */
  883. threshold = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PILT);
  884. }
  885. /**
  886. * @brief Sets the low threshold for proximity interrupts
  887. *
  888. * @param[in] threshold low threshold value for interrupt to trigger
  889. */
  890. void setProximityIntLowThreshold(uint8_t threshold)
  891. {
  892. /* Write threshold value to register */
  893. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PILT, threshold) ;
  894. }
  895. /**
  896. * @brief Gets the high threshold for proximity interrupts
  897. *
  898. * @param[out] threshold current low threshold stored on the APDS-9960
  899. */
  900. void getProximityIntHighThreshold(uint8_t &threshold)
  901. {
  902. threshold = 0;
  903. /* Read value from proximity low threshold register */
  904. threshold = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PIHT) ;
  905. }
  906. /**
  907. * @brief Sets the high threshold for proximity interrupts
  908. *
  909. * @param[in] threshold high threshold value for interrupt to trigger
  910. */
  911. void setProximityIntHighThreshold(uint8_t threshold)
  912. {
  913. /* Write threshold value to register */
  914. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PIHT, threshold) ;
  915. }
  916. /**
  917. * @brief Gets if ambient light interrupts are enabled or not
  918. *
  919. * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
  920. */
  921. uint8_t getAmbientLightIntEnable(void)
  922. {
  923. uint8_t val;
  924. /* Read value from ENABLE register */
  925. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ;
  926. /* Shift and mask out AIEN bit */
  927. val = (val >> 4) & 0b00000001;
  928. return val;
  929. }
  930. /**
  931. * @brief Turns ambient light interrupts on or off
  932. *
  933. * @param[in] enable 1 to enable interrupts, 0 to turn them off
  934. */
  935. void setAmbientLightIntEnable(uint8_t enable)
  936. {
  937. uint8_t val;
  938. /* Read value from ENABLE register */
  939. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE);
  940. /* Set bits in register to given value */
  941. enable &= 0b00000001;
  942. enable = enable << 4;
  943. val &= 0b11101111;
  944. val |= enable;
  945. /* Write register value back into ENABLE register */
  946. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, val) ;
  947. }
  948. /**
  949. * @brief Gets if proximity interrupts are enabled or not
  950. *
  951. * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
  952. */
  953. uint8_t getProximityIntEnable(void)
  954. {
  955. uint8_t val;
  956. /* Read value from ENABLE register */
  957. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ;
  958. /* Shift and mask out PIEN bit */
  959. val = (val >> 5) & 0b00000001;
  960. return val;
  961. }
  962. /**
  963. * @brief Turns proximity interrupts on or off
  964. *
  965. * @param[in] enable 1 to enable interrupts, 0 to turn them off
  966. */
  967. void setProximityIntEnable(uint8_t enable)
  968. {
  969. uint8_t val;
  970. /* Read value from ENABLE register */
  971. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ;
  972. /* Set bits in register to given value */
  973. enable &= 0b00000001;
  974. enable = enable << 5;
  975. val &= 0b11011111;
  976. val |= enable;
  977. /* Write register value back into ENABLE register */
  978. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, val) ;
  979. }
  980. /**
  981. * @brief Gets if gesture interrupts are enabled or not
  982. *
  983. * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
  984. */
  985. uint8_t getGestureIntEnable(void)
  986. {
  987. uint8_t val;
  988. /* Read value from GCONF4 register */
  989. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ;
  990. /* Shift and mask out GIEN bit */
  991. val = (val >> 1) & 0b00000001;
  992. return val;
  993. }
  994. /**
  995. * @brief Turns gesture-related interrupts on or off
  996. *
  997. * @param[in] enable 1 to enable interrupts, 0 to turn them off
  998. */
  999. void setGestureIntEnable(uint8_t enable)
  1000. {
  1001. uint8_t val;
  1002. /* Read value from GCONF4 register */
  1003. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ;
  1004. /* Set bits in register to given value */
  1005. enable &= 0b00000001;
  1006. enable = enable << 1;
  1007. val &= 0b11111101;
  1008. val |= enable;
  1009. /* Write register value back into GCONF4 register */
  1010. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF4, val) ;
  1011. }
  1012. /**
  1013. * @brief Clears the ambient light interrupt
  1014. *
  1015. */
  1016. void clearAmbientLightInt(void)
  1017. {
  1018. uint8_t throwaway;
  1019. throwaway = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AICLEAR);
  1020. }
  1021. /**
  1022. * @brief Clears the proximity interrupt
  1023. *
  1024. */
  1025. void clearProximityInt(void)
  1026. {
  1027. uint8_t throwaway;
  1028. throwaway = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PICLEAR) ;
  1029. }
  1030. /**
  1031. * @brief Tells if the gesture state machine is currently running
  1032. *
  1033. * @return 1 if gesture state machine is running, 0 if not. 0xFF on error.
  1034. */
  1035. uint8_t getGestureMode(void)
  1036. {
  1037. uint8_t val;
  1038. /* Read value from GCONF4 register */
  1039. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ;
  1040. /* Mask out GMODE bit */
  1041. val &= 0b00000001;
  1042. return val;
  1043. }
  1044. /**
  1045. * @brief Tells the state machine to either enter or exit gesture state machine
  1046. *
  1047. * @param[in] mode 1 to enter gesture state machine, 0 to exit.
  1048. */
  1049. void setGestureMode(uint8_t mode)
  1050. {
  1051. uint8_t val;
  1052. /* Read value from GCONF4 register */
  1053. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ;
  1054. /* Set bits in register to given value */
  1055. mode &= 0b00000001;
  1056. val &= 0b11111110;
  1057. val |= mode;
  1058. /* Write register value back into GCONF4 register */
  1059. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF4, val) ;
  1060. }
  1061. bool APDS9960_init(void)
  1062. {
  1063. /* Set default values for ambient light and proximity registers */
  1064. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME) ;
  1065. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_WTIME, DEFAULT_WTIME) ;
  1066. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PPULSE, DEFAULT_PROX_PPULSE) ;
  1067. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR) ;
  1068. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL) ;
  1069. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG1, DEFAULT_CONFIG1) ;
  1070. setLEDDrive(DEFAULT_LDRIVE);
  1071. setProximityGain(DEFAULT_PGAIN);
  1072. setAmbientLightGain(DEFAULT_AGAIN);
  1073. setProxIntLowThresh(DEFAULT_PILT) ;
  1074. setProxIntHighThresh(DEFAULT_PIHT);
  1075. setLightIntLowThreshold(DEFAULT_AILT) ;
  1076. setLightIntHighThreshold(DEFAULT_AIHT) ;
  1077. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PERS, DEFAULT_PERS) ;
  1078. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG2, DEFAULT_CONFIG2) ;
  1079. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, DEFAULT_CONFIG3) ;
  1080. /* Set default values for gesture sense registers */
  1081. setGestureEnterThresh(DEFAULT_GPENTH);
  1082. setGestureExitThresh(DEFAULT_GEXTH) ;
  1083. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF1, DEFAULT_GCONF1) ;
  1084. setGestureGain(DEFAULT_GGAIN) ;
  1085. setGestureLEDDrive(DEFAULT_GLDRIVE) ;
  1086. setGestureWaitTime(DEFAULT_GWTIME) ;
  1087. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ;
  1088. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ;
  1089. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ;
  1090. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ;
  1091. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GPULSE, DEFAULT_GPULSE) ;
  1092. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF3, DEFAULT_GCONF3) ;
  1093. setGestureIntEnable(DEFAULT_GIEN);
  1094. disablePower(); // go to sleep
  1095. return true;
  1096. }
  1097. /*******************************************************************************
  1098. * Public methods for controlling the APDS-9960
  1099. ******************************************************************************/
  1100. /**
  1101. * @brief Reads and returns the contents of the ENABLE register
  1102. *
  1103. * @return Contents of the ENABLE register. 0xFF if error.
  1104. */
  1105. uint8_t getMode(void)
  1106. {
  1107. uint8_t enable_value;
  1108. /* Read current ENABLE register */
  1109. enable_value = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ;
  1110. return enable_value;
  1111. }
  1112. /**
  1113. * @brief Enables or disables a feature in the APDS-9960
  1114. *
  1115. * @param[in] mode which feature to enable
  1116. * @param[in] enable ON (1) or OFF (0)
  1117. */
  1118. void setMode(uint8_t mode, uint8_t enable)
  1119. {
  1120. uint8_t reg_val;
  1121. /* Read current ENABLE register */
  1122. reg_val = getMode();
  1123. /* Change bit(s) in ENABLE register */
  1124. enable = enable & 0x01;
  1125. if( mode >= 0 && mode <= 6 ) {
  1126. if (enable) {
  1127. reg_val |= (1 << mode);
  1128. } else {
  1129. reg_val &= ~(1 << mode);
  1130. }
  1131. } else if( mode == ALL ) {
  1132. if (enable) {
  1133. reg_val = 0x7F;
  1134. } else {
  1135. reg_val = 0x00;
  1136. }
  1137. }
  1138. /* Write value back to ENABLE register */
  1139. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, reg_val) ;
  1140. }
  1141. /**
  1142. * @brief Starts the light (R/G/B/Ambient) sensor on the APDS-9960
  1143. *
  1144. * no interrupts
  1145. */
  1146. void enableLightSensor(void)
  1147. {
  1148. /* Set default gain, interrupts, enable power, and enable sensor */
  1149. setAmbientLightGain(DEFAULT_AGAIN);
  1150. setAmbientLightIntEnable(0);
  1151. enablePower() ;
  1152. setMode(AMBIENT_LIGHT, 1) ;
  1153. }
  1154. /**
  1155. * @brief Ends the light sensor on the APDS-9960
  1156. *
  1157. */
  1158. void disableLightSensor(void)
  1159. {
  1160. setAmbientLightIntEnable(0) ;
  1161. setMode(AMBIENT_LIGHT, 0) ;
  1162. }
  1163. /**
  1164. * @brief Starts the proximity sensor on the APDS-9960
  1165. *
  1166. * no interrupts
  1167. */
  1168. void enableProximitySensor(void)
  1169. {
  1170. /* Set default gain, LED, interrupts, enable power, and enable sensor */
  1171. setProximityGain(DEFAULT_PGAIN);
  1172. setLEDDrive(DEFAULT_LDRIVE) ;
  1173. setProximityIntEnable(0) ;
  1174. enablePower();
  1175. setMode(PROXIMITY, 1) ;
  1176. }
  1177. /**
  1178. * @brief Ends the proximity sensor on the APDS-9960
  1179. *
  1180. */
  1181. void disableProximitySensor(void)
  1182. {
  1183. setProximityIntEnable(0) ;
  1184. setMode(PROXIMITY, 0) ;
  1185. }
  1186. /**
  1187. * @brief Starts the gesture recognition engine on the APDS-9960
  1188. *
  1189. * no interrupts
  1190. */
  1191. void enableGestureSensor(void)
  1192. {
  1193. /* Enable gesture mode
  1194. Set ENABLE to 0 (power off)
  1195. Set WTIME to 0xFF
  1196. Set AUX to LED_BOOST_300
  1197. Enable PON, WEN, PEN, GEN in ENABLE
  1198. */
  1199. resetGestureParameters();
  1200. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_WTIME, 0xFF) ;
  1201. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ;
  1202. setLEDBoost(LED_BOOST_100); // tip from jonn26 - 100 for 300 ---- 200 from Adafruit
  1203. setGestureIntEnable(0) ;
  1204. setGestureMode(1);
  1205. enablePower() ;
  1206. setMode(WAIT, 1) ;
  1207. setMode(PROXIMITY, 1) ;
  1208. setMode(GESTURE, 1);
  1209. }
  1210. /**
  1211. * @brief Ends the gesture recognition engine on the APDS-9960
  1212. *
  1213. */
  1214. void disableGestureSensor(void)
  1215. {
  1216. resetGestureParameters();
  1217. setGestureIntEnable(0) ;
  1218. setGestureMode(0) ;
  1219. setMode(GESTURE, 0) ;
  1220. }
  1221. /**
  1222. * @brief Determines if there is a gesture available for reading
  1223. *
  1224. * @return True if gesture available. False otherwise.
  1225. */
  1226. bool isGestureAvailable(void)
  1227. {
  1228. uint8_t val;
  1229. /* Read value from GSTATUS register */
  1230. val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GSTATUS) ;
  1231. /* Shift and mask out GVALID bit */
  1232. val &= APDS9960_GVALID;
  1233. /* Return true/false based on GVALID bit */
  1234. if( val == 1) {
  1235. return true;
  1236. } else {
  1237. return false;
  1238. }
  1239. }
  1240. /**
  1241. * @brief Processes a gesture event and returns best guessed gesture
  1242. *
  1243. * @return Number corresponding to gesture. -1 on error.
  1244. */
  1245. int16_t readGesture(void)
  1246. {
  1247. uint8_t fifo_level = 0;
  1248. uint8_t bytes_read = 0;
  1249. uint8_t fifo_data[128];
  1250. uint8_t gstatus;
  1251. uint16_t motion;
  1252. uint16_t i;
  1253. uint8_t gesture_loop_counter = 0; // don't loop forever later
  1254. /* Make sure that power and gesture is on and data is valid */
  1255. if( !isGestureAvailable() || !(getMode() & 0b01000001) ) {
  1256. return DIR_NONE;
  1257. }
  1258. /* Keep looping as long as gesture data is valid */
  1259. while(1) {
  1260. if (gesture_loop_counter == APDS9960_MAX_GESTURE_CYCLES){ // We will escape after a few loops
  1261. disableGestureSensor(); // stop the sensor to prevent problems with power consumption/blocking and return to the main loop
  1262. APDS9960_overload = true; // we report this as "long"-gesture
  1263. snprintf_P(log_data, sizeof(log_data), PSTR("Sensor overload"));
  1264. AddLog(LOG_LEVEL_DEBUG);
  1265. }
  1266. gesture_loop_counter += 1;
  1267. /* Wait some time to collect next batch of FIFO data */
  1268. delay(FIFO_PAUSE_TIME);
  1269. /* Get the contents of the STATUS register. Is data still valid? */
  1270. gstatus = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GSTATUS);
  1271. /* If we have valid data, read in FIFO */
  1272. if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
  1273. /* Read the current FIFO level */
  1274. fifo_level = I2cRead8(APDS9960_I2C_ADDR,APDS9960_GFLVL) ;
  1275. /* If there's stuff in the FIFO, read it into our data block */
  1276. if( fifo_level > 0) {
  1277. bytes_read = wireReadDataBlock( APDS9960_GFIFO_U,
  1278. (uint8_t*)fifo_data,
  1279. (fifo_level * 4) );
  1280. if( bytes_read == -1 ) {
  1281. return ERROR;
  1282. }
  1283. /* If at least 1 set of data, sort the data into U/D/L/R */
  1284. if( bytes_read >= 4 ) {
  1285. for( i = 0; i < bytes_read; i += 4 ) {
  1286. gesture_data_.u_data[gesture_data_.index] = \
  1287. fifo_data[i + 0];
  1288. gesture_data_.d_data[gesture_data_.index] = \
  1289. fifo_data[i + 1];
  1290. gesture_data_.l_data[gesture_data_.index] = \
  1291. fifo_data[i + 2];
  1292. gesture_data_.r_data[gesture_data_.index] = \
  1293. fifo_data[i + 3];
  1294. gesture_data_.index++;
  1295. gesture_data_.total_gestures++;
  1296. }
  1297. /* Filter and process gesture data. Decode near/far state */
  1298. if( processGestureData() ) {
  1299. if( decodeGesture() ) {
  1300. //***TODO: U-Turn Gestures
  1301. }
  1302. }
  1303. /* Reset data */
  1304. gesture_data_.index = 0;
  1305. gesture_data_.total_gestures = 0;
  1306. }
  1307. }
  1308. } else {
  1309. /* Determine best guessed gesture and clean up */
  1310. delay(FIFO_PAUSE_TIME);
  1311. decodeGesture();
  1312. motion = gesture_motion_;
  1313. resetGestureParameters();
  1314. return motion;
  1315. }
  1316. }
  1317. }
  1318. /**
  1319. * Turn the APDS-9960 on
  1320. *
  1321. */
  1322. void enablePower(void)
  1323. {
  1324. setMode(POWER, 1) ;
  1325. }
  1326. /**
  1327. * Turn the APDS-9960 off
  1328. *
  1329. */
  1330. void disablePower(void)
  1331. {
  1332. setMode(POWER, 0) ;
  1333. }
  1334. /*******************************************************************************
  1335. * Ambient light and color sensor controls
  1336. ******************************************************************************/
  1337. /**
  1338. * @brief Reads the ARGB-Data and fills color_data
  1339. *
  1340. */
  1341. void readAllColorAndProximityData(void)
  1342. {
  1343. if (I2cReadBuffer(APDS9960_I2C_ADDR, APDS9960_CDATAL, (uint8_t *) &color_data, (uint16_t)9))
  1344. {
  1345. // not absolutely shure, if this is a correct way to do this, but it is very short
  1346. // we fill the struct byte by byte
  1347. }
  1348. }
  1349. /*******************************************************************************
  1350. * High-level gesture controls
  1351. ******************************************************************************/
  1352. /**
  1353. * @brief Resets all the parameters in the gesture data member
  1354. */
  1355. void resetGestureParameters(void)
  1356. {
  1357. gesture_data_.index = 0;
  1358. gesture_data_.total_gestures = 0;
  1359. gesture_ud_delta_ = 0;
  1360. gesture_lr_delta_ = 0;
  1361. gesture_ud_count_ = 0;
  1362. gesture_lr_count_ = 0;
  1363. gesture_state_ = 0;
  1364. gesture_motion_ = DIR_NONE;
  1365. }
  1366. /**
  1367. * @brief Processes the raw gesture data to determine swipe direction
  1368. *
  1369. * @return True if near or far state seen. False otherwise.
  1370. */
  1371. bool processGestureData(void)
  1372. {
  1373. uint8_t u_first = 0;
  1374. uint8_t d_first = 0;
  1375. uint8_t l_first = 0;
  1376. uint8_t r_first = 0;
  1377. uint8_t u_last = 0;
  1378. uint8_t d_last = 0;
  1379. uint8_t l_last = 0;
  1380. uint8_t r_last = 0;
  1381. uint16_t ud_ratio_first;
  1382. uint16_t lr_ratio_first;
  1383. uint16_t ud_ratio_last;
  1384. uint16_t lr_ratio_last;
  1385. uint16_t ud_delta;
  1386. uint16_t lr_delta;
  1387. uint16_t i;
  1388. /* If we have less than 4 total gestures, that's not enough */
  1389. if( gesture_data_.total_gestures <= 4 ) {
  1390. return false;
  1391. }
  1392. /* Check to make sure our data isn't out of bounds */
  1393. if( (gesture_data_.total_gestures <= 32) && \
  1394. (gesture_data_.total_gestures > 0) ) {
  1395. /* Find the first value in U/D/L/R above the threshold */
  1396. for( i = 0; i < gesture_data_.total_gestures; i++ ) {
  1397. if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
  1398. (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
  1399. (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
  1400. (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
  1401. u_first = gesture_data_.u_data[i];
  1402. d_first = gesture_data_.d_data[i];
  1403. l_first = gesture_data_.l_data[i];
  1404. r_first = gesture_data_.r_data[i];
  1405. break;
  1406. }
  1407. }
  1408. /* If one of the _first values is 0, then there is no good data */
  1409. if( (u_first == 0) || (d_first == 0) || \
  1410. (l_first == 0) || (r_first == 0) ) {
  1411. return false;
  1412. }
  1413. /* Find the last value in U/D/L/R above the threshold */
  1414. for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
  1415. if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
  1416. (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
  1417. (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
  1418. (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
  1419. u_last = gesture_data_.u_data[i];
  1420. d_last = gesture_data_.d_data[i];
  1421. l_last = gesture_data_.l_data[i];
  1422. r_last = gesture_data_.r_data[i];
  1423. break;
  1424. }
  1425. }
  1426. }
  1427. /* Calculate the first vs. last ratio of up/down and left/right */
  1428. ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
  1429. lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
  1430. ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
  1431. lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
  1432. /* Determine the difference between the first and last ratios */
  1433. ud_delta = ud_ratio_last - ud_ratio_first;
  1434. lr_delta = lr_ratio_last - lr_ratio_first;
  1435. /* Accumulate the UD and LR delta values */
  1436. gesture_ud_delta_ += ud_delta;
  1437. gesture_lr_delta_ += lr_delta;
  1438. /* Determine U/D gesture */
  1439. if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
  1440. gesture_ud_count_ = 1;
  1441. } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
  1442. gesture_ud_count_ = -1;
  1443. } else {
  1444. gesture_ud_count_ = 0;
  1445. }
  1446. /* Determine L/R gesture */
  1447. if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
  1448. gesture_lr_count_ = 1;
  1449. } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
  1450. gesture_lr_count_ = -1;
  1451. } else {
  1452. gesture_lr_count_ = 0;
  1453. }
  1454. return false;
  1455. }
  1456. /**
  1457. * @brief Determines swipe direction or near/far state
  1458. *
  1459. * @return True if near/far event. False otherwise.
  1460. */
  1461. bool decodeGesture(void)
  1462. {
  1463. /* Determine swipe direction */
  1464. if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
  1465. gesture_motion_ = DIR_UP;
  1466. } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
  1467. gesture_motion_ = DIR_DOWN;
  1468. } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
  1469. gesture_motion_ = DIR_RIGHT;
  1470. } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
  1471. gesture_motion_ = DIR_LEFT;
  1472. } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
  1473. if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
  1474. gesture_motion_ = DIR_UP;
  1475. } else {
  1476. gesture_motion_ = DIR_RIGHT;
  1477. }
  1478. } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
  1479. if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
  1480. gesture_motion_ = DIR_DOWN;
  1481. } else {
  1482. gesture_motion_ = DIR_LEFT;
  1483. }
  1484. } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
  1485. if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
  1486. gesture_motion_ = DIR_UP;
  1487. } else {
  1488. gesture_motion_ = DIR_LEFT;
  1489. }
  1490. } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
  1491. if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
  1492. gesture_motion_ = DIR_DOWN;
  1493. } else {
  1494. gesture_motion_ = DIR_RIGHT;
  1495. }
  1496. } else {
  1497. return false;
  1498. }
  1499. return true;
  1500. }
  1501. void handleGesture(void) {
  1502. if (isGestureAvailable() ) {
  1503. switch (readGesture()) {
  1504. case DIR_UP:
  1505. snprintf_P(log_data, sizeof(log_data), PSTR("UP"));
  1506. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Up"));
  1507. break;
  1508. case DIR_DOWN:
  1509. snprintf_P(log_data, sizeof(log_data), PSTR("DOWN"));
  1510. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Down"));
  1511. break;
  1512. case DIR_LEFT:
  1513. snprintf_P(log_data, sizeof(log_data), PSTR("LEFT"));
  1514. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Left"));
  1515. break;
  1516. case DIR_RIGHT:
  1517. snprintf_P(log_data, sizeof(log_data), PSTR("RIGHT"));
  1518. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Right"));
  1519. break;
  1520. default:
  1521. if(APDS9960_overload)
  1522. {
  1523. snprintf_P(log_data, sizeof(log_data), PSTR("LONG"));
  1524. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Long"));
  1525. }
  1526. else{
  1527. snprintf_P(log_data, sizeof(log_data), PSTR("NONE"));
  1528. snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None"));
  1529. }
  1530. }
  1531. AddLog(LOG_LEVEL_DEBUG);
  1532. mqtt_data[0] = '\0';
  1533. if (MqttShowSensor()) {
  1534. MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
  1535. #ifdef USE_RULES
  1536. RulesTeleperiod(); // Allow rule based HA messages
  1537. #endif // USE_RULES
  1538. }
  1539. }
  1540. }
  1541. void APDS9960_adjustATime(void) // not really used atm
  1542. {
  1543. //readAllColorAndProximityData();
  1544. I2cValidRead16LE(&color_data.a, APDS9960_I2C_ADDR, APDS9960_CDATAL);
  1545. //disablePower();
  1546. if (color_data.a < (uint16_t)20){
  1547. APDS9960_aTime = 0x40;
  1548. }
  1549. else if (color_data.a < (uint16_t)40){
  1550. APDS9960_aTime = 0x80;
  1551. }
  1552. else if (color_data.a < (uint16_t)50){
  1553. APDS9960_aTime = DEFAULT_ATIME;
  1554. }
  1555. else if (color_data.a < (uint16_t)70){
  1556. APDS9960_aTime = 0xc0;
  1557. }
  1558. if (color_data.a < 200){
  1559. APDS9960_aTime = 0xe9;
  1560. }
  1561. /* if (color_data.a < 10000){
  1562. APDS9960_aTime = 0xF0;
  1563. }*/
  1564. else{
  1565. APDS9960_aTime = 0xff;
  1566. }
  1567. //disableLightSensor();
  1568. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, APDS9960_aTime);
  1569. enablePower();
  1570. enableLightSensor();
  1571. delay(20);
  1572. }
  1573. void APDS9960_loop(void)
  1574. {
  1575. if (recovery_loop_counter > 0){
  1576. recovery_loop_counter -= 1;
  1577. }
  1578. if (recovery_loop_counter == 1 && APDS9960_overload){ //restart sensor just before the end of recovery from long press
  1579. enableGestureSensor();
  1580. APDS9960_overload = false;
  1581. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"On\"}"));
  1582. MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); // only after the long break we report, that we are online again
  1583. gesture_mode = 1;
  1584. }
  1585. if (gesture_mode) {
  1586. if (recovery_loop_counter == 0){
  1587. handleGesture();
  1588. if (APDS9960_overload)
  1589. {
  1590. disableGestureSensor();
  1591. recovery_loop_counter = APDS9960_LONG_RECOVERY; // long pause after overload/long press - number of stateloops
  1592. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}"));
  1593. MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data);
  1594. gesture_mode = 0;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. bool APDS9960_detect(void)
  1600. {
  1601. if (APDS9960type) {
  1602. return true;
  1603. }
  1604. boolean success = false;
  1605. APDS9960type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID);
  1606. if (APDS9960type == APDS9960_CHIPID_1 || APDS9960type == APDS9960_CHIPID_2) {
  1607. strcpy_P(APDS9960stype, PSTR("APDS9960"));
  1608. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, APDS9960stype, APDS9960_I2C_ADDR);
  1609. AddLog(LOG_LEVEL_DEBUG);
  1610. if (APDS9960_init()) {
  1611. success = true;
  1612. AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "APDS9960 initialized"));
  1613. enableProximitySensor();
  1614. enableGestureSensor();
  1615. }
  1616. }
  1617. else {
  1618. if (APDS9960type == APDS9930_CHIPID_1 || APDS9960type == APDS9930_CHIPID_2) {
  1619. snprintf_P(log_data, sizeof(log_data), PSTR("APDS9930 found at address 0x%x, unsupported chip"), APDS9960_I2C_ADDR);
  1620. AddLog(LOG_LEVEL_DEBUG);
  1621. }
  1622. else{
  1623. snprintf_P(log_data, sizeof(log_data), PSTR("APDS9960 not found at address 0x%x"), APDS9960_I2C_ADDR);
  1624. AddLog(LOG_LEVEL_DEBUG);
  1625. }
  1626. }
  1627. currentGesture[0] = '\0';
  1628. return success;
  1629. }
  1630. /*********************************************************************************************\
  1631. * Presentation
  1632. \*********************************************************************************************/
  1633. void APDS9960_show(boolean json)
  1634. {
  1635. if (!APDS9960type) {
  1636. return;
  1637. }
  1638. if (!gesture_mode && !APDS9960_overload) {
  1639. char red_chr[10];
  1640. char green_chr[10];
  1641. char blue_chr[10];
  1642. char ambient_chr[10];
  1643. char cct_chr[10];
  1644. char prox_chr[10];
  1645. readAllColorAndProximityData();
  1646. sprintf (ambient_chr, "%u", color_data.a/4);
  1647. sprintf (red_chr, "%u", color_data.r);
  1648. sprintf (green_chr, "%u", color_data.g);
  1649. sprintf (blue_chr, "%u", color_data.b );
  1650. sprintf (prox_chr, "%u", color_data.p );
  1651. /* disableLightSensor();
  1652. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME); // reset to default
  1653. enableLightSensor();*/
  1654. calculateColorTemperature(); // and calculate Lux
  1655. sprintf (cct_chr, "%u", color_data.cct);
  1656. if (json) {
  1657. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"Red\":%s,\"Green\":%s,\"Blue\":%s,\"Ambient\":%s,\"CCT\":%s,\"Proximity\":%s}"),
  1658. mqtt_data, APDS9960stype, red_chr, green_chr, blue_chr, ambient_chr, cct_chr, prox_chr);
  1659. #ifdef USE_WEBSERVER
  1660. } else {
  1661. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_APDS_9960_SNS, mqtt_data, red_chr, green_chr, blue_chr, ambient_chr, cct_chr, prox_chr );
  1662. #endif // USE_WEBSERVER
  1663. }
  1664. }
  1665. else {
  1666. if (json && (currentGesture[0] != '\0' )) {
  1667. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture);
  1668. currentGesture[0] = '\0';
  1669. }
  1670. }
  1671. }
  1672. /*********************************************************************************************\
  1673. * Command Sensor27
  1674. *
  1675. * Command | Payload | Description
  1676. * ---------|---------|--------------------------
  1677. * Sensor27 | | Show current gesture mode
  1678. * Sensor27 | 0 / Off | Disable gesture mode
  1679. * Sensor27 | 1 / On | Enable gesture mode
  1680. * Sensor27 | 2 / On | Enable gesture mode with half gain
  1681. \*********************************************************************************************/
  1682. bool APDS9960CommandSensor(void)
  1683. {
  1684. boolean serviced = true;
  1685. switch (XdrvMailbox.payload) {
  1686. case 0: // Off
  1687. disableGestureSensor();
  1688. gesture_mode = 0;
  1689. enableLightSensor();
  1690. APDS9960_overload = false; // prevent unwanted re-enabling
  1691. break;
  1692. case 1: // On with default gain of 4x
  1693. if (APDS9960type) {
  1694. setGestureGain(DEFAULT_GGAIN);
  1695. setProximityGain(DEFAULT_PGAIN);
  1696. disableLightSensor();
  1697. enableGestureSensor();
  1698. gesture_mode = 1;
  1699. }
  1700. break;
  1701. case 2: // gain of 2x , needed for some models
  1702. if (APDS9960type) {
  1703. setGestureGain(GGAIN_2X);
  1704. setProximityGain(PGAIN_2X);
  1705. disableLightSensor();
  1706. enableGestureSensor();
  1707. gesture_mode = 1;
  1708. }
  1709. break;
  1710. default:
  1711. int temp_aTime = (uint8_t)XdrvMailbox.payload;
  1712. if (temp_aTime > 2 && temp_aTime < 256){
  1713. disablePower();
  1714. I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, temp_aTime);
  1715. enablePower();
  1716. enableLightSensor();
  1717. }
  1718. break;
  1719. }
  1720. snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_27, GetStateText(gesture_mode));
  1721. return serviced;
  1722. }
  1723. /*********************************************************************************************\
  1724. * Interface
  1725. \*********************************************************************************************/
  1726. boolean Xsns27(byte function)
  1727. {
  1728. boolean result = false;
  1729. if (i2c_flg) {
  1730. if (FUNC_INIT == function) {
  1731. APDS9960_detect();
  1732. } else if (APDS9960type) {
  1733. switch (function) {
  1734. case FUNC_EVERY_50_MSECOND:
  1735. APDS9960_loop();
  1736. break;
  1737. case FUNC_COMMAND:
  1738. if (XSNS_27 == XdrvMailbox.index) {
  1739. result = APDS9960CommandSensor();
  1740. }
  1741. break;
  1742. case FUNC_JSON_APPEND:
  1743. APDS9960_show(1);
  1744. break;
  1745. #ifdef USE_WEBSERVER
  1746. case FUNC_WEB_APPEND:
  1747. APDS9960_show(0);
  1748. break;
  1749. #endif // USE_WEBSERVER
  1750. }
  1751. }
  1752. }
  1753. return result;
  1754. }
  1755. #endif // USE_APDS9960
  1756. #endif // USE_I2C