ap3216c.c 19 KB

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