ap3216c.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef __DRV_AP3216C_H__
  11. #define __DRV_AP3216C_H__
  12. #include <rtdef.h>
  13. enum ap3216c_mode_value
  14. {
  15. AP3216C_MODE_POWER_DOWN, //Power down (Default)
  16. AP3216C_MODE_ALS, //ALS function active
  17. AP3216C_MODE_PS, //PS+IR function active
  18. AP3216C_MODE_ALS_AND_PS, //ALS and PS+IR functions active
  19. AP3216C_MODE_SW_RESET, //SW reset
  20. AP3216C_MODE_ALS_ONCE, //ALS function once
  21. AP3216C_MODE_PS_ONCE, //PS+IR function once
  22. AP3216C_MODE_ALS_AND_PS_ONCE, //ALS and PS+IR functions once
  23. };
  24. enum ap3216c_int_clear_manner
  25. {
  26. AP3216C_INT_CLEAR_MANNER_BY_READING, //INT is automatically cleared by reading data registers(Default)
  27. AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE, //Software clear after writing 1 into address 0x01 each bit
  28. };
  29. enum als_range
  30. {
  31. AP3216C_ALS_RANGE_20661, //Resolution = 0.35 lux/count(default).
  32. AP3216C_ALS_RANGE_5162, //Resolution = 0.0788 lux/count.
  33. AP3216C_ALS_RANGE_1291, //Resolution = 0.0197 lux/count.
  34. AP3216C_ALS_RANGE_323, //Resolution = 0.0049 lux/count
  35. };
  36. typedef enum als_range als_range_t;
  37. enum als_gain
  38. {
  39. AP3216C_ALS_GAIN1, //detection distance *1.
  40. AP3216C_ALS_GAIN2, //detection distance *2 (default).
  41. AP3216C_ALS_GAIN4, //detection distance *4.
  42. AP3216C_ALS_GAIN8, //detection distance *8.
  43. };
  44. typedef enum als_gain als_gain_t;
  45. enum ap3216c_cmd
  46. {
  47. AP3216C_SYSTEM_MODE, //system Configuration(Default : 000)
  48. AP3216C_INT_PARAM, //INT Clear Manner(Default : 0)
  49. AP3216C_ALS_RANGE, //ALS dynamic range(Default : 00)
  50. AP3216C_ALS_PERSIST, //ALS persist(Default : 0000)
  51. AP3216C_ALS_CALIBRATION, //ALS window loss calibration(Default : 0x40)
  52. AP3216C_ALS_LOW_THRESHOLD_L, //Lower byte of low interrupt threshold for ALS(Default : 0x00)
  53. AP3216C_ALS_LOW_THRESHOLD_H, //Higher byte of low interrupt threshold for ALS(Default : 0x00)
  54. AP3216C_ALS_HIGH_THRESHOLD_L, //Lower byte of high interrupt threshold for ALS (Default : 0xFF)
  55. AP3216C_ALS_HIGH_THRESHOLD_H, //Higher byte of high interrupt threshold for ALS(Default : 0xFF)
  56. AP3216C_PS_INTEGRATED_TIME, //PS or IR Integrated time select(Default : 0000)
  57. AP3216C_PS_GAIN, //PS gain (Default : 01)
  58. AP3216C_PS_PERSIST, //Interrupt filter(Default : 01)
  59. AP3216C_PS_LED_CONTROL, //LED pulse(Default : 01)
  60. AP3216C_PS_LED_DRIVER_RATIO, //LED driver ratio(Default : 11)
  61. AP3216C_PS_INT_MODE, //PS INT Mode(Default : 0x01)
  62. AP3216C_PS_MEAN_TIME, //PS mean time(Default : 0x00)
  63. AP3216C_PS_WAITING_TIME, //PS LED Waiting(Default : 0x00)
  64. AP3216C_PS_CALIBRATION_L, //PS Calibration L(Default : 0x00)
  65. AP3216C_PS_CALIBRATION_H, //PS Calibration H(Default : 0x00)
  66. AP3216C_PS_LOW_THRESHOLD_L, //PS Low Threshold L(Default :0x00)
  67. AP3216C_PS_LOW_THRESHOLD_H, //PS Low Threshold H(Default :0x00)
  68. AP3216C_PS_HIGH_THRESHOLD_L, //PS high Threshold L(Default :0xff)
  69. AP3216C_PS_HIGH_THRESHOLD_H, //PS high Threshold H(Default :0xff)
  70. };
  71. typedef enum ap3216c_cmd ap3216c_cmd_t;
  72. #ifdef AP3216C_USING_HW_INT
  73. /* intrrupt parameters of ap3216c on ps or als */
  74. struct ap3216c_threshold
  75. {
  76. rt_uint16_t min; /* als 16 bits, ps 10 bits available(0-1 bit and 8-15 bit ) */
  77. rt_uint16_t max; /* als 16 bits, ps 10 bits available(0-1 bit and 8-15 bit ) */
  78. rt_uint8_t noises_time; /* filter special noises trigger interrupt */
  79. };
  80. typedef struct ap3216c_threshold ap3216c_threshold_t;
  81. typedef void (*ap3216c_int_cb)(void *args);
  82. #endif /* AP3216C_USING_HW_INT */
  83. struct ap3216c_device
  84. {
  85. struct rt_i2c_bus_device *i2c;
  86. #ifdef AP3216C_USING_HW_INT
  87. ap3216c_int_cb als_int_cb;
  88. ap3216c_int_cb ps_int_cb;
  89. #endif /* AP3216C_USING_HW_INT */
  90. rt_mutex_t lock;
  91. };
  92. typedef struct ap3216c_device *ap3216c_device_t;
  93. #ifdef AP3216C_USING_HW_INT
  94. /**
  95. * This function initializes ps interrupt with callback function
  96. *
  97. * @param dev the name of ap3216c device
  98. * @param enabled enable or disenable ps interrupt
  99. * @param threshold threshold and filtering times of ps threshold
  100. *
  101. * @param int_cb callback funtion is defined by user.
  102. */
  103. void ap3216c_int_ps_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb);
  104. /**
  105. * This function initializes als interrupt with callback function
  106. *
  107. * @param dev the name of ap3216c device
  108. * @param enabled enable or disenable als interrupt
  109. * @param threshold threshold and filtering times of als threshold
  110. *
  111. * @param int_cb callback funtion is defined by user.
  112. */
  113. void ap3216c_int_als_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb);
  114. #endif /* AP3216C_USING_HW_INT */
  115. /**
  116. * This function initializes ap3216c registered device driver
  117. *
  118. * @param dev the name of ap3216c device
  119. *
  120. * @return the ap3216c device.
  121. */
  122. ap3216c_device_t ap3216c_init(const char *i2c_bus_name);
  123. /**
  124. * This function releases memory and deletes mutex lock
  125. *
  126. * @param dev the pointer of device driver structure.
  127. */
  128. void ap3216c_deinit(ap3216c_device_t dev);
  129. rt_err_t ap3216c_reset_sensor(ap3216c_device_t dev);
  130. /**
  131. * This function reads temperature by ap3216c sensor measurement
  132. *
  133. * @param dev the pointer of device driver structure
  134. *
  135. * @return the ambient light converted to float data.
  136. */
  137. float ap3216c_read_ambient_light(ap3216c_device_t dev);
  138. /**
  139. * This function reads temperature by ap3216c sensor measurement
  140. *
  141. * @param dev the pointer of device driver structure
  142. *
  143. * @return the proximity data.
  144. */
  145. uint16_t ap3216c_read_ps_data(ap3216c_device_t dev);
  146. /**
  147. * This function sets parameter of ap3216c sensor
  148. *
  149. * @param dev the pointer of device driver structure
  150. * @param cmd the parameter cmd of device
  151. * @param value for setting value in cmd register
  152. *
  153. * @return the setting parameter status,RT_EOK reprensents setting successfully.
  154. */
  155. rt_err_t ap3216c_set_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t value);
  156. /**
  157. * This function gets parameter of ap3216c sensor
  158. *
  159. * @param dev the pointer of device driver structure
  160. * @param cmd the parameter cmd of device
  161. * @param value to get value in cmd register
  162. *
  163. * @return the getting parameter status,RT_EOK reprensents getting successfully.
  164. */
  165. rt_err_t ap3216c_get_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t *value);
  166. #endif /*__DRV_AP3216C_H__ */