sensor_algo.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*******************************************************************************
  2. * @file sensor_algo.h
  3. * @author MEMS Application Team
  4. * @version V1.0
  5. * @date 2021-5-25
  6. * @brief sensor algorithm api
  7. *******************************************************************************/
  8. #ifndef _SENSOR_ALGO_H
  9. #define _SENSOR_ALGO_H
  10. /******************************************************************************/
  11. //includes
  12. /******************************************************************************/
  13. #include <stdint.h>
  14. #include <stdarg.h>
  15. /******************************************************************************/
  16. //constants
  17. /******************************************************************************/
  18. /* Input sensor id */
  19. typedef enum {
  20. IN_ACC = 0,
  21. IN_GYRO,
  22. IN_MAG,
  23. IN_BARO,
  24. IN_TEMP,
  25. IN_HEARTRATE,
  26. IN_GNSS,
  27. IN_OFFBODY,
  28. } sensor_in_e;
  29. /* Output sensor id */
  30. typedef enum {
  31. RAW_ACCEL = 0,
  32. RAW_GYRO,
  33. RAW_MAG,
  34. RAW_BARO,
  35. RAW_TEMP,
  36. RAW_HEARTRATE,
  37. RAW_GNSS,
  38. RAW_OFFBODY,
  39. ALGO_SEDENTARY,
  40. ALGO_HANDUP,
  41. ALGO_SLEEP,
  42. REQ_SENSOR,
  43. ALGO_ACTTYPEDETECT,
  44. ALGO_SHAKE,
  45. ALGO_ACTIVITY_OUTPUT,
  46. ALGO_ANY_MOTION,
  47. ALGO_NO_MOTION,
  48. ALGO_FALLING,
  49. ALGO_ORIENTATION,
  50. } sensor_out_e;
  51. /* Activity mode */
  52. typedef enum {
  53. NORMAL = 1001, // acc + baro
  54. TREADMILL = 1002, // acc + hr
  55. OUTDOOR_RUNNING = 1003, // acc + hr + gnss
  56. OUTDOOR_BIKING = 3001, // acc + baro + hr +gnss
  57. MAIN_EXTRA = 100, // main extra info
  58. BIKING_EXTRA = 120, // biking extra info
  59. } activity_mode_e;
  60. /* Control id */
  61. typedef enum {
  62. SCL_LOG = 0,
  63. SCL_USER_INFO = 1,
  64. SCL_DATE_TIME = 2,
  65. SCL_SEDENTARY = 3,
  66. SCL_PEDO_RESET = 4,
  67. SCL_REQ_SLEEPING_DATA = 5,
  68. SCL_LOW_POWER_MODE = 6,
  69. SCL_SET_ACTIVITY_MODE = 7,
  70. SCL_GET_CHIP_INFO = 8,
  71. SCL_ACTIVITY_CONFIG = 9,
  72. SCL_LIB_DEBUG = 10,
  73. SCL_PEDO_CONFIG = 11,
  74. SCL_GET_LIB_INFO = 12,
  75. SCL_SWIM_CONFIG = 13,
  76. SCL_BIKING_CONFIG = 14,
  77. SCL_ACTIVITY_PAUSE = 15,
  78. SCL_HAND_UPDOWN_CONFIG = 16,
  79. SCL_CHIP_VENDOR_CONFIG = 17,
  80. SCL_SLEEP_CONFIG = 18,
  81. SCL_ALGO_PROC_CONFIG = 100,
  82. SCL_INPUT_SENSOR_CONFIG = 101,
  83. } sensor_ctl_e;
  84. /******************************************************************************/
  85. //typedef
  86. /******************************************************************************/
  87. #ifdef __CC_ARM /* ARM Compiler */
  88. #pragma anon_unions
  89. #endif
  90. /* Input sensor data */
  91. typedef struct {
  92. uint32_t id;
  93. union {
  94. float fData[16];
  95. double dData[10];
  96. };
  97. } sensor_raw_t;
  98. /* Ouput sensor event */
  99. typedef struct {
  100. uint32_t id;
  101. uint32_t index;
  102. union {
  103. float fData[16];
  104. double dData[8];
  105. int iData[16];
  106. };
  107. uint64_t timestamp_ns;
  108. } sensor_evt_t;
  109. /* Sensor control data */
  110. typedef struct {
  111. union {
  112. int iData[16];
  113. float fData[16];
  114. double dData[8];
  115. };
  116. } sensor_ctl_t;
  117. /* Sensor date time */
  118. typedef struct {
  119. uint8_t month;
  120. uint8_t day;
  121. uint8_t hour;
  122. uint8_t minute;
  123. } sensor_datetime_t;
  124. /* Sensor algorithm result */
  125. typedef struct {
  126. /* orientation values are in degrees */
  127. float orientation;
  128. /* pressure in hectopascal (hPa) */
  129. float pressure;
  130. /* altitude in meter (m) */
  131. float altitude;
  132. /* temperature is in degrees centigrade (Celsius) */
  133. float temperature;
  134. /* heart reate in ppm */
  135. float heart_rate;
  136. /* gnss data */
  137. struct gnss_s {
  138. float latitude; // degrees
  139. float longitude; // degrees
  140. float altitude; // meters
  141. float bearing; // heading in degrees
  142. float speed; // m/s
  143. } gnss;
  144. /* on-body/off-body status */
  145. uint32_t onbody; //1:on-body 0:off-body
  146. /* sedentary status */
  147. uint32_t sedentary; //1:enter 2:exit 3:remider
  148. /* handup status */
  149. uint32_t handup; //1:hand-up 2:hand-down
  150. /* sleeping data */
  151. struct sleeping_s {
  152. // status 0:enter 1:shallow 2:deep 3:awake 4:exit 12:rapid eye movement
  153. uint32_t status;
  154. // time [month-day-hour-minute]
  155. sensor_datetime_t time_enter;
  156. sensor_datetime_t time_shallow;
  157. sensor_datetime_t time_deep;
  158. sensor_datetime_t time_awake;
  159. sensor_datetime_t time_rem;
  160. sensor_datetime_t time_exit;
  161. // duration in minute
  162. uint32_t duration_total;
  163. uint32_t duration_shallow;
  164. uint32_t duration_deep;
  165. uint32_t duration_awake;
  166. uint32_t duration_rem;
  167. } sleeping;
  168. /* activity mode */
  169. uint32_t activity_mode;
  170. /* activity detect type */
  171. uint32_t activity_detect;
  172. /* pedometer data */
  173. struct pedometer_s {
  174. // 1:static 2:walk 4:upstairs 5:downstairs 6:run 7:bike 8:vehicle
  175. uint32_t activity_type;
  176. float total_steps; // steps
  177. float total_distance; // meters
  178. float total_calories; // Kcal
  179. float cur_step_freq; // steps/minute
  180. float avg_step_freq; // steps/minute
  181. float max_step_freq; // steps/minute
  182. float cur_step_len; // meters
  183. float avg_step_len; // meters
  184. float cur_pace; // minutes/Km
  185. float avg_pace; // minutes/Km
  186. float max_pace; // minutes/Km
  187. float slope; // percentage
  188. float elevation; // meters
  189. float elevation_up; // meters
  190. float elevation_down; // meters
  191. float floors_up; // floors
  192. float floors_down; // floors
  193. float vo2max; // ml/kg/min
  194. float hr_intensity; //
  195. float hr_zone; //
  196. } pedometer;
  197. /* biking data */
  198. struct biking_s {
  199. float latitude; // degrees
  200. float longitude; // degrees
  201. float distance; // meters
  202. float calories; // Kcal
  203. float cur_speed; // km/hour
  204. float avg_speed; // km/hour
  205. float max_speed; // km/hour
  206. float slope; // percentage
  207. float elevation; // meters
  208. float vo2max; // ml/kg/min
  209. float hr_intensity; //
  210. float hr_zone; //
  211. } biking;
  212. } sensor_res_t;
  213. /* Sensor event handler */
  214. typedef void (*sensor_handler_t)(sensor_evt_t *evt);
  215. /* Sensor os api */
  216. typedef struct {
  217. /* Printf */
  218. int (*dbgOutput)(const char *fmt, ...);
  219. /* Get current timestamp */
  220. uint64_t (*get_timestamp_ns)(void);
  221. /* User handler */
  222. void (*user_handler)(sensor_evt_t *evt);
  223. /* I2C write func */
  224. int (*i2c_write)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len);
  225. /* I2C read func */
  226. int (*i2c_read)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len);
  227. } sensor_os_api_t;
  228. /* Sensor algo api */
  229. typedef struct {
  230. /* Init sensor algo */
  231. int (*init)(const sensor_os_api_t *api);
  232. /* Get os api */
  233. sensor_os_api_t* (*get_os_api)(void);
  234. /* Set os api */
  235. int (*set_os_api)(const sensor_os_api_t *api);
  236. /* Control sensor algo */
  237. int (*control)(uint32_t ctl_id, sensor_ctl_t *ctl);
  238. /* Enable sensor id */
  239. int (*enable)(uint32_t id, uint32_t func);
  240. /* Disable sensor id */
  241. int (*disable)(uint32_t id, uint32_t func);
  242. /* Input sensor raw data */
  243. void (*input)(sensor_raw_t* dat);
  244. /* Input sensor fifo init */
  245. void (*input_fifo_init)(int num);
  246. /* Input sensor fifo start */
  247. int (*input_fifo_start)(uint32_t id, uint64_t timestamp, uint64_t delta);
  248. /* Input sensor fifo end */
  249. void (*input_fifo_end)(uint32_t id);
  250. /* Get the duration time of the next event */
  251. int64_t (*get_next_duration)(void);
  252. /* Read sensor data and output through sensor call-back function */
  253. int (*process)(uint32_t id);
  254. /* Read sensor algorithm result */
  255. sensor_res_t* (*get_result)(void);
  256. /* Dump sensor algorithum event */
  257. void (*dump_event)(sensor_evt_t *evt);
  258. /* Dump sensor algorithum result */
  259. void (*dump_result)(sensor_res_t *res);
  260. } sensor_algo_api_t;
  261. /******************************************************************************/
  262. //function
  263. /******************************************************************************/
  264. /* Init sensor algo */
  265. extern int sensor_algo_init(const sensor_os_api_t *api);
  266. /* Get os api */
  267. extern sensor_os_api_t* sensor_algo_get_os_api(void);
  268. /* Set os api */
  269. extern int sensor_algo_set_os_api(const sensor_os_api_t *api);
  270. /* Control sensor algo */
  271. extern int sensor_algo_control(uint32_t ctl_id, sensor_ctl_t *ctl);
  272. /* Enable sensor id */
  273. extern int sensor_algo_enable(uint32_t id, uint32_t func);
  274. /* Disable sensor id */
  275. extern int sensor_algo_disable(uint32_t id, uint32_t func);
  276. /* Input sensor raw data */
  277. extern void sensor_algo_input(sensor_raw_t* dat);
  278. /* Input sensor fifo init */
  279. extern void sensor_algo_input_fifo_init(int num);
  280. /* Input sensor fifo start */
  281. extern int sensor_algo_input_fifo_start(uint32_t id, uint64_t timestamp, uint64_t delta);
  282. /* Input sensor fifo end */
  283. extern void sensor_algo_input_fifo_end(uint32_t id);
  284. /* Get the duration time of the next event */
  285. extern int64_t sensor_algo_get_next_duration(void);
  286. /* Read sensor data and output through sensor call-back function */
  287. extern int sensor_algo_process(uint32_t id);
  288. /* Read sensor algorithm result */
  289. extern sensor_res_t* sensor_algo_get_result(void);
  290. /* Dump sensor algorithum event */
  291. extern void sensor_algo_dump_event(sensor_evt_t *evt);
  292. /* Dump sensor algorithum result */
  293. extern void sensor_algo_dump_result(sensor_res_t *res);
  294. /******************************************************************************/
  295. //api
  296. /******************************************************************************/
  297. /* sensor rom variable */
  298. extern char __sensor_algo_start[];
  299. extern char __sensor_algo_end[];
  300. #define __sensor_algo_size (__sensor_algo_end - __sensor_algo_start)
  301. extern char __sensor_algo_sleep_start[];
  302. extern char __sensor_algo_sleep_end[];
  303. #define __sensor_algo_sleep_size (__sensor_algo_sleep_end - __sensor_algo_sleep_start)
  304. /* sensor algo api */
  305. #define p_sensor_algo_api ((const sensor_algo_api_t*)__sensor_algo_start)
  306. #define p_sensor_algo_sleep_api ((const sensor_algo_api_t*)&__kernel_ram_start)
  307. #endif /* _SENSOR_ALGO_H */