mcu_hi3321_watch/tjd/service/service_gsensor.c
2025-05-26 20:15:20 +08:00

220 lines
6.6 KiB
C

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2020. All rights reserved.
*
* Description: service_gsensor.c
*
* Author: liangjianfei
*
* Create: 2024-4-25
*--------------------------------------------------------------------------*/
#include "service_gsensor.h"
#include "cmsis_os2.h"
#include "common_def.h"
#include "gsensor_api.h"
#include "pm.h"
#include "rtc_api.h"
#include "securec.h"
#include "soc_osal.h"
#include "sql_fit.h"
#include "sys_config.h"
#include "sys_typedef.h"
#include "task_service_timer.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define ENABLE_PRINT_INFO 1
#define ENABLE_DEBUG 0
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_gs_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_gs_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_gs_log_e(__VA_ARGS__) // 错误信息打印一般常开
#if ENABLE_DEBUG
#define static_print_debug(...) sys_gs_log_d(__VA_ARGS__) // debug
#else
#define static_print_debug(...)
#endif
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#endif
#define REFRESH_SLEEP_TIME_HOUR 18
#define REFRESH_DAILYDATA_TIME_HOUR 00
#define GSEN_BUF_LENGTH 32
static int32_t g_flag = 0;
static gsensor_info *g_gs_driver = NULL;
static bool g_gsen_service_inited = false;
static gsensor_buffer alg_gsen_buf = {0};
// 初始化缓冲区
static bool init_gsensor_buffer(gsensor_buffer *buf, size_t capacity)
{
buf->buffer = (gsensor_xyz_info *)malloc(capacity * sizeof(gsensor_xyz_info));
if (!buf->buffer) {
static_print_error("Failed to allocate buffer");
return FALSE;
}
buf->capacity = capacity;
buf->count = 0;
return TRUE;
}
// 从缓冲区获取数据
static uint8_t get_from_gsensor_buffer(gsensor_buffer *buf, gsensor_xyz_info *output, size_t length)
{
uint8_t to_copy = (length < buf->count) ? length : buf->count;
for (uint8_t i = 0; i < to_copy; i++) {
output[i] = buf->buffer[i];
}
// 如果需要,用最后一个元素填充剩余部分
for (uint8_t i = to_copy; i < length; i++) {
output[i] = buf->buffer[to_copy-1];
}
static_print_debug("buf->count: %d", buf->count);
uint8_t valid_count = buf->count;
return valid_count;
}
// 清理缓冲区
static void free_gsensor_buffer(gsensor_buffer *buf)
{
free(buf->buffer);
buf->buffer = NULL;
buf->capacity = 0;
buf->count = 0;
}
void tjd_service_gs_add_xyz_fifo(uint8_t id,int16_t x, int16_t y, int16_t z)
{
if(alg_gsen_buf.count >= alg_gsen_buf.capacity)
{
return;
}
alg_gsen_buf.buffer[id].gsensor_x.raw_data = x;
alg_gsen_buf.buffer[id].gsensor_y.raw_data = y;
alg_gsen_buf.buffer[id].gsensor_z.raw_data = z;
alg_gsen_buf.count = id;
}
uint8_t tjd_service_gs_get_xyz_fifo(gsen_service_id_t id, gsensor_xyz_info *output, uint8_t length)
{
if (g_gsen_service_inited == false) {
static_print_error("gsen_service have not open");
return 0;
}
int valid_count = get_from_gsensor_buffer(&(alg_gsen_buf), output, length);
return valid_count;
}
uint8_t tjd_service_gs_get_fifo_count(gsen_service_id_t id) { return alg_gsen_buf.count; }
gsensor_xyz_info* tjd_service_gs_get_xyz_fifo_point(void){ return alg_gsen_buf.buffer;}
void tjd_service_gsensor_open(void)
{
if (g_gsen_service_inited == true) {
static_print_error("gsen_service has been opened");
return;
}
int ret = tjd_driver_gs_get_ops()->open();
if (ret != ERRCODE_SUCC) {
static_print_error("gsen_service open fail");
return;
}
g_gsen_service_inited = true;
// 分配内存并初始化
init_gsensor_buffer(&(alg_gsen_buf), GSEN_BUF_LENGTH);
static_print_info("gsen_service open suceess");
}
void tjd_service_gsensor_close(void)
{
if (g_gsen_service_inited == false) {
static_print_error("gsen_service have not open");
return;
}
free_gsensor_buffer(&(alg_gsen_buf));
g_gsen_service_inited = false;
}
/* -----------------------------------self test----------------------------------------- */
#define SELF_TEST_TIMER_REPEAT_TIME 320 // ms
osThreadId_t g_gsen_selftest_ThreadId;
static uint32_t selftest_timer_callback(void *para)
{
gsensor_xyz_info data;
tjd_service_gs_get_xyz_fifo(GSEN_SERVICE_MSEN, &data, 1);
static_print_info("self test gsensor raw data x: %d y: %d z: %d", data.gsensor_x.raw_data, data.gsensor_y.raw_data,
data.gsensor_z.raw_data);
return SELF_TEST_TIMER_REPEAT_TIME;
}
static void selftest_thread(void *para)
{
gsensor_xyz_info data;
while (1) {
tjd_service_gs_get_xyz_fifo(GSEN_SERVICE_MSEN, &data, 1);
static_print_info("self test gsensor raw data x: %d y: %d z: %d", data.gsensor_x.raw_data,
data.gsensor_y.raw_data, data.gsensor_z.raw_data);
osDelay(SELF_TEST_TIMER_REPEAT_TIME);
}
}
void tjd_service_gsensor_selftest_start(void)
{
if (g_gsen_service_inited == false) {
static_print_error("gsen_service have not open");
return;
}
// queue_default_info_t msg_data = {selftest_timer_callback, NULL, SELF_TEST_TIMER_REPEAT_TIME, NULL};
// int ret = osal_msg_queue_write_copy(tjd_task_service_timer_get_queue_id(), (void *)&msg_data,
// sizeof(queue_default_info_t), 0);
// if (ret != OSAL_SUCCESS) {
// static_print_error("[%s] osal_msg_queue_write_copy fail", __func__);
// }
osThreadAttr_t task_attr = {
"tjd_service_gsensor_selftest_Thread", 0, NULL, 0, NULL, 0x1000, (osPriority_t)(16), 0, 0};
g_gsen_selftest_ThreadId = osThreadNew((osThreadFunc_t)selftest_thread, NULL, &task_attr);
if (g_gsen_selftest_ThreadId == NULL) {
static_print_error("[%s:%d] create thread fail!!", __FUNCTION__, __LINE__);
return;
}
}
void tjd_service_gsensor_selftest_end(void)
{
// queue_default_info_t msg_data = {selftest_timer_callback, NULL, 0, NULL};
// int ret = osal_msg_queue_write_copy(tjd_task_service_timer_get_queue_id(), (void *)&msg_data,
// sizeof(queue_default_info_t), 0);
// if (ret != OSAL_SUCCESS) {
// static_print_error("[%s] osal_msg_queue_write_copy fail", __func__);
// }
}