ap3216c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-20 Ernest Chen the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include <finsh.h>
  14. #include <string.h>
  15. #include "ap3216c.h"
  16. #ifdef PKG_USING_AP3216C
  17. #define DBG_ENABLE
  18. #define DBG_SECTION_NAME "ap3216c"
  19. #define DBG_LEVEL DBG_LOG
  20. #define DBG_COLOR
  21. #include <rtdbg.h>
  22. //System Register
  23. #define AP3216C_SYS_CONFIGURATION_REG 0x00 //default
  24. #define AP3216C_SYS_INT_STATUS_REG 0x01
  25. #define AP3216C_SYS_INT_CLEAR_MANNER_REG 0x02
  26. #define AP3216C_IR_DATA_L_REG 0x0A
  27. #define AP3216C_IR_DATA_H_REG 0x0B
  28. #define AP3216C_ALS_DATA_L_REG 0x0C
  29. #define AP3216C_ALS_DATA_H_REG 0x0D
  30. #define AP3216C_PS_DATA_L_REG 0x0E
  31. #define AP3216C_PS_DATA_H_REG 0x0F
  32. //ALS Register
  33. #define AP3216C_ALS_CONFIGURATION_REG 0x10 //range 5:4,persist 3:0
  34. #define AP3216C_ALS_CALIBRATION_REG 0x19
  35. #define AP3216C_ALS_THRESHOLD_LOW_L_REG 0x1A //bit 7:0
  36. #define AP3216C_ALS_THRESHOLD_LOW_H_REG 0x1B //bit 15:8
  37. #define AP3216C_ALS_THRESHOLD_HIGH_L_REG 0x1C //bit 7:0
  38. #define AP3216C_ALS_THRESHOLD_HIGH_H_REG 0x1D //bit 15:8
  39. //PS Register
  40. #define AP3216C_PS_CONFIGURATION_REG 0x20
  41. #define AP3216C_PS_LED_DRIVER_REG 0x21
  42. #define AP3216C_PS_INT_FORM_REG 0x22
  43. #define AP3216C_PS_MEAN_TIME_REG 0x23
  44. #define AP3216C_PS_LED_WAITING_TIME_REG 0x24
  45. #define AP3216C_PS_CALIBRATION_L_REG 0x28
  46. #define AP3216C_PS_CALIBRATION_H_REG 0x29
  47. #define AP3216C_PS_THRESHOLD_LOW_L_REG 0x2A //bit 1:0
  48. #define AP3216C_PS_THRESHOLD_LOW_H_REG 0x2B //bit 9:2
  49. #define AP3216C_PS_THRESHOLD_HIGH_L_REG 0x2C //bit 1:0
  50. #define AP3216C_PS_THRESHOLD_HIGH_H_REG 0x2D //bit 9:2
  51. #define AP3216C_ADDR 0x1e /*0x3c=0x1e<<1*/
  52. static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t data)
  53. {
  54. struct rt_i2c_msg msgs;
  55. rt_uint8_t temp[2];
  56. temp[0] = reg;
  57. temp[1] = data;
  58. msgs.addr = AP3216C_ADDR;
  59. msgs.flags = RT_I2C_WR;
  60. msgs.buf = temp;
  61. msgs.len = 2;
  62. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  63. {
  64. return RT_EOK;
  65. }
  66. else
  67. {
  68. LOG_E("Writing command error");
  69. return -RT_ERROR;
  70. }
  71. }
  72. static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t len, rt_uint8_t *buf)
  73. {
  74. struct rt_i2c_msg msgs[2];
  75. msgs[0].addr = AP3216C_ADDR;
  76. msgs[0].flags = RT_I2C_WR;
  77. msgs[0].buf = &reg;
  78. msgs[0].len = 1;
  79. msgs[1].addr = AP3216C_ADDR;
  80. msgs[1].flags = RT_I2C_RD;
  81. msgs[1].buf = buf;
  82. msgs[1].len = len;
  83. if (rt_i2c_transfer(bus, msgs, 2) == 2)
  84. {
  85. return RT_EOK;
  86. }
  87. else
  88. {
  89. LOG_E("Reading command error");
  90. return -RT_ERROR;
  91. }
  92. }
  93. rt_err_t ap3216c_reset_sensor(ap3216c_device_t dev)
  94. {
  95. if (dev == RT_NULL)
  96. {
  97. return -RT_EINVAL;
  98. }
  99. write_reg(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, AP3216C_MODE_SW_RESET); //reset
  100. rt_thread_mdelay(15); /* need to wait at least 10ms */
  101. return RT_EOK;
  102. }
  103. /**
  104. * This function is convenient to getting data except including high and low data for this sensor.
  105. * note:after reading lower register first,reading higher add one.
  106. */
  107. static rt_uint32_t read_low_and_high(ap3216c_device_t dev, rt_uint8_t reg, rt_uint8_t len)
  108. {
  109. rt_uint32_t data;
  110. rt_uint8_t buf = 0;
  111. read_regs(dev->i2c, reg, len, &buf); //low
  112. data = buf;
  113. read_regs(dev->i2c, reg + 1, len, &buf); //high
  114. data = data + (buf << len * 8);
  115. return data;
  116. }
  117. #ifdef AP3216C_USING_HW_INT
  118. /**
  119. * This function is only used to set threshold without filtering times
  120. *
  121. * @param dev the name of ap3216c device
  122. * @param cmd first register , and other cmd count by it.
  123. * @param threshold threshold and filtering times of als threshold
  124. */
  125. static void set_threshold(ap3216c_device_t dev, ap3216c_cmd_t cmd, ap3216c_threshold_t threshold)
  126. {
  127. ap3216c_set_param(dev, cmd, (threshold.min & 0xff));
  128. ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 1), (threshold.min >> 8));
  129. ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 2), (threshold.max & 0xff));
  130. ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 3), threshold.max >> 8);
  131. }
  132. static void ap3216c_hw_interrupt(void *args)
  133. {
  134. ap3216c_device_t dev = (ap3216c_device_t)args;
  135. if (dev->als_int_cb)
  136. {
  137. dev->als_int_cb(dev->als_int_cb);
  138. }
  139. if (dev->ps_int_cb)
  140. {
  141. dev->ps_int_cb(dev->ps_int_cb);
  142. }
  143. }
  144. static void ap3216c_int_init(ap3216c_device_t dev)
  145. {
  146. RT_ASSERT(dev);
  147. rt_pin_mode(AP3216C_INT_PIN, PIN_MODE_INPUT_PULLUP);
  148. rt_pin_attach_irq(AP3216C_INT_PIN, PIN_IRQ_MODE_FALLING, ap3216c_hw_interrupt, (void *)dev);
  149. rt_pin_irq_enable(AP3216C_INT_PIN, PIN_IRQ_ENABLE);
  150. }
  151. /**
  152. * This function registers als interrupt with callback function
  153. *
  154. * @param dev the name of ap3216c device
  155. * @param enabled enable or disenable als interrupt
  156. * @param threshold threshold and filtering times of als threshold
  157. *
  158. * @param int_cb callback funtion is defined by user.
  159. */
  160. void ap3216c_int_als_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb)
  161. {
  162. RT_ASSERT(dev);
  163. if (enabled)
  164. {
  165. dev->als_int_cb = int_cb;
  166. set_threshold(dev, AP3216C_ALS_LOW_THRESHOLD_L, threshold);
  167. }
  168. else
  169. {
  170. dev->als_int_cb = RT_NULL;
  171. }
  172. }
  173. /**
  174. * This function registers ps interrupt with callback function
  175. *
  176. * @param dev the name of ap3216c device
  177. * @param enabled enable or disenable ps interrupt
  178. * @param threshold threshold and filtering times of ps threshold
  179. *
  180. * @param int_cb callback funtion is defined by user.
  181. */
  182. void ap3216c_int_ps_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb)
  183. {
  184. RT_ASSERT(dev);
  185. if (enabled)
  186. {
  187. dev->ps_int_cb = int_cb;
  188. set_threshold(dev, AP3216C_PS_LOW_THRESHOLD_L, threshold);
  189. }
  190. else
  191. {
  192. dev->ps_int_cb = RT_NULL;
  193. }
  194. }
  195. #endif /* AP3216C_USING_HW_INT */
  196. /**
  197. * This function initializes ap3216c registered device driver
  198. *
  199. * @param dev the name of ap3216c device
  200. *
  201. * @return the ap3216c device.
  202. */
  203. ap3216c_device_t ap3216c_init(const char *i2c_bus_name)
  204. {
  205. ap3216c_device_t dev;
  206. RT_ASSERT(i2c_bus_name);
  207. dev = rt_calloc(1, sizeof(struct ap3216c_device));
  208. if (dev == RT_NULL)
  209. {
  210. LOG_E("Can't allocate memory for ap3216c device on '%s' ", i2c_bus_name);
  211. rt_free(dev);
  212. return RT_NULL;
  213. }
  214. dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
  215. if (dev->i2c == RT_NULL)
  216. {
  217. LOG_E("Can't find ap3216c device on '%s'", i2c_bus_name);
  218. rt_free(dev);
  219. return RT_NULL;
  220. }
  221. dev->lock = rt_mutex_create("mutex_ap3216c", RT_IPC_FLAG_FIFO);
  222. if (dev->lock == RT_NULL)
  223. {
  224. LOG_E("Can't create mutex for ap3216c device on '%s'", i2c_bus_name);
  225. rt_free(dev);
  226. return RT_NULL;
  227. }
  228. /* reset ap3216c */
  229. ap3216c_reset_sensor(dev);
  230. ap3216c_set_param(dev, AP3216C_SYSTEM_MODE, AP3216C_MODE_ALS_AND_PS);
  231. #ifdef AP3216C_USING_HW_INT
  232. /* init interrupt mode */
  233. ap3216c_int_init(dev);
  234. #endif /* AP3216C_USING_HW_INT */
  235. return dev;
  236. }
  237. /**
  238. * This function releases memory and deletes mutex lock
  239. *
  240. * @param dev the pointer of device driver structure
  241. */
  242. void ap3216c_deinit(ap3216c_device_t dev)
  243. {
  244. RT_ASSERT(dev);
  245. rt_mutex_delete(dev->lock);
  246. rt_free(dev);
  247. }
  248. /**
  249. * This function reads temperature by ap3216c sensor measurement
  250. *
  251. * @param dev the pointer of device driver structure
  252. *
  253. * @return the ambient light converted to float data.
  254. */
  255. float ap3216c_read_ambient_light(ap3216c_device_t dev)
  256. {
  257. float brightness = 0.0; // default error data
  258. rt_err_t result;
  259. rt_uint32_t read_data;
  260. rt_uint8_t temp;
  261. RT_ASSERT(dev);
  262. result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
  263. if (result == RT_EOK)
  264. {
  265. read_data = read_low_and_high(dev, AP3216C_ALS_DATA_L_REG, 1);
  266. ap3216c_get_param(dev, AP3216C_ALS_RANGE, &temp);
  267. if (temp == AP3216C_ALS_RANGE_20661)
  268. {
  269. brightness = 0.35 * read_data; //sensor ambient light converse to reality
  270. }
  271. else if (temp == AP3216C_ALS_RANGE_5162)
  272. {
  273. brightness = 0.0788 * read_data; //sensor ambient light converse to reality
  274. }
  275. else if (temp == AP3216C_ALS_RANGE_1291)
  276. {
  277. brightness = 0.0197 * read_data; //sensor ambient light converse to reality
  278. }
  279. else if (temp == AP3216C_ALS_RANGE_323)
  280. {
  281. brightness = 0.0049 * read_data; //sensor ambient light converse to reality
  282. }
  283. else
  284. {
  285. LOG_E("Failed to get range of ap3216c");
  286. }
  287. }
  288. else
  289. {
  290. LOG_E("Failed to reading ambient light");
  291. }
  292. rt_mutex_release(dev->lock);
  293. return brightness;
  294. }
  295. /**
  296. * This function reads temperature by ap3216c sensor measurement
  297. *
  298. * @param dev the pointer of device driver structure
  299. *
  300. * @return the proximity data.
  301. */
  302. uint16_t ap3216c_read_ps_data(ap3216c_device_t dev)
  303. {
  304. rt_uint16_t proximity = 0;
  305. rt_err_t result;
  306. RT_ASSERT(dev);
  307. result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
  308. if (result == RT_EOK)
  309. {
  310. rt_uint32_t read_data;
  311. read_data = read_low_and_high(dev, AP3216C_PS_DATA_L_REG, 1); //read two data
  312. if (1 == ((read_data >> 6) & 0x01 || (read_data >> 14) & 0x01))
  313. {
  314. LOG_I("The data of PS is invalid for high intensive IR light ");
  315. }
  316. proximity = (read_data & 0x000f) + (((read_data >> 8) & 0x3f) << 4); //sensor proximity converse to reality
  317. }
  318. else
  319. {
  320. LOG_E("Failed to reading ps data ");
  321. }
  322. rt_mutex_release(dev->lock);
  323. return proximity;
  324. }
  325. /**
  326. * This function sets parameter of ap3216c sensor
  327. *
  328. * @param dev the pointer of device driver structure
  329. * @param cmd the parameter cmd of device
  330. * @param value for setting value in cmd register
  331. *
  332. * @return the setting parameter status,RT_EOK reprensents setting successfully.
  333. */
  334. rt_err_t ap3216c_set_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t value)
  335. {
  336. RT_ASSERT(dev);
  337. switch (cmd)
  338. {
  339. case AP3216C_SYSTEM_MODE:
  340. {
  341. if (value > AP3216C_MODE_ALS_AND_PS_ONCE)
  342. {
  343. LOG_E("Setting system mode parameter is wrong !");
  344. return -RT_ERROR;
  345. }
  346. /* default 000,power down */
  347. write_reg(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, value);
  348. break;
  349. }
  350. case AP3216C_INT_PARAM:
  351. {
  352. if (value > AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE)
  353. {
  354. LOG_E("Setting int parameter is wrong !");
  355. return -RT_ERROR;
  356. }
  357. write_reg(dev->i2c, AP3216C_SYS_INT_CLEAR_MANNER_REG, value);
  358. break;
  359. }
  360. case AP3216C_ALS_RANGE:
  361. {
  362. rt_uint8_t args;
  363. if (!(value == AP3216C_ALS_RANGE_20661 || value == AP3216C_ALS_RANGE_5162 || value == AP3216C_ALS_RANGE_1291 || value == AP3216C_ALS_RANGE_323))
  364. {
  365. LOG_E("Setting als dynamic range is wrong, please refer als_range");
  366. return -RT_ERROR;
  367. }
  368. read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, &args);
  369. args &= 0xcf;
  370. args |= value << 4;
  371. write_reg(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, args);
  372. break;
  373. }
  374. case AP3216C_ALS_PERSIST:
  375. {
  376. rt_uint8_t args = 0;
  377. if (value > 0x0f)
  378. {
  379. LOG_E("Setting als persist overflows ");
  380. return -RT_ERROR;
  381. }
  382. read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, &args);
  383. args &= 0xf0;
  384. args |= value;
  385. write_reg(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, args);
  386. break;
  387. }
  388. case AP3216C_ALS_LOW_THRESHOLD_L:
  389. {
  390. write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_L_REG, value);
  391. break;
  392. }
  393. case AP3216C_ALS_LOW_THRESHOLD_H:
  394. {
  395. write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_H_REG, value);
  396. break;
  397. }
  398. case AP3216C_ALS_HIGH_THRESHOLD_L:
  399. {
  400. write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_L_REG, value);
  401. break;
  402. }
  403. case AP3216C_ALS_HIGH_THRESHOLD_H:
  404. {
  405. write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_H_REG, value);
  406. break;
  407. }
  408. case AP3216C_PS_GAIN:
  409. {
  410. rt_uint8_t args = 0;
  411. if (value > 0x3)
  412. {
  413. LOG_E("Setting ps again overflows ");
  414. return -RT_ERROR;
  415. }
  416. read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &args);
  417. args &= 0xf3;
  418. args |= value;
  419. write_reg(dev->i2c, AP3216C_PS_CONFIGURATION_REG, args);
  420. break;
  421. }
  422. case AP3216C_PS_PERSIST:
  423. {
  424. rt_uint8_t args = 0;
  425. if (value > 0x3)
  426. {
  427. LOG_E("Setting ps persist overflows ");
  428. return -RT_ERROR;
  429. }
  430. read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &args);
  431. args &= 0xfc;
  432. args |= value;
  433. write_reg(dev->i2c, AP3216C_PS_CONFIGURATION_REG, args);
  434. break;
  435. }
  436. case AP3216C_PS_LOW_THRESHOLD_L:
  437. {
  438. if (value > 0x3)
  439. {
  440. LOG_E("Setting ps low threshold of low bit is wrong !");
  441. return -RT_ERROR;
  442. }
  443. write_reg(dev->i2c, AP3216C_PS_THRESHOLD_LOW_L_REG, value);
  444. break;
  445. }
  446. case AP3216C_PS_LOW_THRESHOLD_H:
  447. {
  448. write_reg(dev->i2c, AP3216C_PS_THRESHOLD_LOW_H_REG, value);
  449. break;
  450. }
  451. case AP3216C_PS_HIGH_THRESHOLD_L:
  452. {
  453. if (value > 0x3)
  454. {
  455. LOG_E("Setting ps high threshold of low bit is wrong !");
  456. return -RT_ERROR;
  457. }
  458. write_reg(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_L_REG, value);
  459. break;
  460. }
  461. case AP3216C_PS_HIGH_THRESHOLD_H:
  462. {
  463. write_reg(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_H_REG, value);
  464. break;
  465. }
  466. default:
  467. {
  468. return -RT_ERROR;
  469. }
  470. }
  471. return RT_EOK;
  472. }
  473. /**
  474. * This function gets parameter of ap3216c sensor
  475. *
  476. * @param dev the pointer of device driver structure
  477. * @param cmd the parameter cmd of device
  478. * @param value to get value in cmd register
  479. *
  480. * @return the getting parameter status,RT_EOK reprensents getting successfully.
  481. */
  482. rt_err_t ap3216c_get_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t *value)
  483. {
  484. RT_ASSERT(dev);
  485. switch (cmd)
  486. {
  487. case AP3216C_SYSTEM_MODE:
  488. {
  489. read_regs(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, 1, value);
  490. if (*value > AP3216C_MODE_ALS_AND_PS_ONCE)
  491. {
  492. LOG_E("Getting system mode parameter is wrong !");
  493. return -RT_ERROR;
  494. }
  495. break;
  496. }
  497. case AP3216C_INT_PARAM:
  498. {
  499. read_regs(dev->i2c, AP3216C_SYS_INT_CLEAR_MANNER_REG, 1, value);
  500. if (*value > AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE)
  501. {
  502. LOG_E("Getting int parameter is wrong !");
  503. return -RT_ERROR;
  504. }
  505. break;
  506. }
  507. case AP3216C_ALS_RANGE:
  508. {
  509. rt_uint8_t temp;
  510. read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, value);
  511. temp = (*value & 0xff) >> 4;
  512. if (!(temp == AP3216C_ALS_RANGE_20661 || temp == AP3216C_ALS_RANGE_5162 || temp == AP3216C_ALS_RANGE_1291 || temp == AP3216C_ALS_RANGE_323))
  513. {
  514. LOG_E("Getting als dynamic range is wrong, please refer als_range");
  515. return -RT_ERROR;
  516. }
  517. *value = temp;
  518. break;
  519. }
  520. case AP3216C_ALS_PERSIST:
  521. {
  522. rt_uint8_t temp;
  523. read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, value);
  524. temp = *value & 0x0f;
  525. if (temp > 0x0f)
  526. {
  527. LOG_E("Getting als persist is wrong, please refer als_range");
  528. return -RT_ERROR;
  529. }
  530. *value = temp;
  531. break;
  532. }
  533. case AP3216C_ALS_LOW_THRESHOLD_L:
  534. {
  535. read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_L_REG, 1, value);
  536. break;
  537. }
  538. case AP3216C_ALS_LOW_THRESHOLD_H:
  539. {
  540. read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_H_REG, 1, value);
  541. break;
  542. }
  543. case AP3216C_ALS_HIGH_THRESHOLD_L:
  544. {
  545. read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_L_REG, 1, value);
  546. break;
  547. }
  548. case AP3216C_ALS_HIGH_THRESHOLD_H:
  549. {
  550. read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_H_REG, 1, value);
  551. break;
  552. }
  553. case AP3216C_PS_GAIN:
  554. {
  555. rt_uint8_t temp;
  556. read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &temp);
  557. *value = (temp & 0xc) >> 2;
  558. break;
  559. }
  560. case AP3216C_PS_PERSIST:
  561. {
  562. rt_uint8_t temp;
  563. read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &temp);
  564. *value = temp & 0x3;
  565. break;
  566. }
  567. case AP3216C_PS_LOW_THRESHOLD_L:
  568. {
  569. read_regs(dev->i2c, AP3216C_PS_THRESHOLD_LOW_L_REG, 1, value);
  570. if ((*value & 0xff) > 0x3)
  571. {
  572. LOG_E("Getting ps low threshold of low bit is wrong !");
  573. return -RT_ERROR;
  574. }
  575. break;
  576. }
  577. case AP3216C_PS_LOW_THRESHOLD_H:
  578. {
  579. read_regs(dev->i2c, AP3216C_PS_THRESHOLD_LOW_H_REG, 1, value);
  580. break;
  581. }
  582. case AP3216C_PS_HIGH_THRESHOLD_L:
  583. {
  584. read_regs(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_L_REG, 1, value);
  585. if ((*value & 0xff) > 3)
  586. {
  587. LOG_E("Getting ps high threshold of low bit is wrong !");
  588. return -RT_ERROR;
  589. }
  590. break;
  591. }
  592. case AP3216C_PS_HIGH_THRESHOLD_H:
  593. {
  594. read_regs(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_H_REG, 1, value);
  595. break;
  596. }
  597. default:
  598. {
  599. return -RT_ERROR;
  600. }
  601. }
  602. return RT_EOK;
  603. }
  604. void ap3216c(int argc, char *argv[])
  605. {
  606. static ap3216c_device_t dev = RT_NULL;
  607. if (argc > 1)
  608. {
  609. if (!strcmp(argv[1], "probe"))
  610. {
  611. if (argc > 2)
  612. {
  613. /* initialize the sensor when first probe */
  614. if (!dev || strcmp(dev->i2c->parent.parent.name, argv[2]))
  615. {
  616. /* deinit the old device */
  617. if (dev)
  618. {
  619. ap3216c_deinit(dev);
  620. }
  621. dev = ap3216c_init(argv[2]);
  622. }
  623. }
  624. else
  625. {
  626. rt_kprintf("ap3216c probe <dev_name> - probe sensor by given name\n");
  627. }
  628. }
  629. else if (!strcmp(argv[1], "read"))
  630. {
  631. if (dev)
  632. {
  633. rt_uint16_t ps_data;
  634. float brightness;
  635. /* read the sensor */
  636. ps_data = ap3216c_read_ps_data(dev);
  637. if (ps_data == 0)
  638. {
  639. rt_kprintf("object is not proximity of sensor \n");
  640. }
  641. else
  642. {
  643. rt_kprintf("ap3216c read current ps data : %d\n", ps_data);
  644. }
  645. brightness = ap3216c_read_ambient_light(dev);
  646. rt_kprintf("ap3216c measure current brightness: %d.%d(lux) \n", (int)brightness, ((int)(10 * brightness) % 10));
  647. }
  648. else
  649. {
  650. rt_kprintf("Please using 'ap3216c probe <dev_name>' first\n");
  651. }
  652. }
  653. else
  654. {
  655. rt_kprintf("Unknown command. Please enter 'ap3216c' for help\n");
  656. }
  657. }
  658. else
  659. {
  660. rt_kprintf("Usage:\n");
  661. rt_kprintf("ap3216c probe <dev_name> - probe sensor by given name\n");
  662. rt_kprintf("ap3216c read - read sensor ap3216c data\n");
  663. }
  664. }
  665. MSH_CMD_EXPORT(ap3216c, ap3216c sensor function);
  666. #endif /* PKG_USING_AP3216C */