Files
99_7018_lmx/apps/earphone/xtell_Sensor/send_data.c
2025-10-31 17:43:12 +08:00

129 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "system/includes.h"
#include "btstack/btstack_task.h"
#include "app_config.h"
#include "app_action.h"
#include "asm/pwm_led.h"
#include "tone_player.h"
#include "ui_manage.h"
#include "gpio.h"
#include "app_main.h"
#include "asm/charge.h"
#include "update.h"
#include "app_power_manage.h"
#include "audio_config.h"
#include "app_charge.h"
#include "bt_profile_cfg.h"
#include "dev_manager/dev_manager.h"
#include "update_loader_download.h"
#include "LIS2DH12.h"
#include "circle_buffer.h"
#include "circle_buffer.h"
#include "btstack/avctp_user.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//宏定义
#define ENABLE_XLOG 1
#ifdef xlog
#undef xlog
#endif
#if ENABLE_XLOG
#define xlog(format, ...) printf("[XT:%s] " format, __func__, ##__VA_ARGS__)
#else
#define xlog(format, ...) ((void)0)
#endif
//
///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//START -- 函数定义
void send_data_to_ble_client(const u8* data, u16 length);
//END -- 函数定义
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//START -- 变量定义
// --- 任务ID ---
static u16 xtell_i2c_test_id;
static u16 collect_data_id;
static u16 send_data_id;
// --- 环形缓冲区 ---
#define SENSOR_DATA_BUFFER_SIZE 512
static u8 sensor_data_buffer[SENSOR_DATA_BUFFER_SIZE];
static circle_buffer_t sensor_cb;
//END -- 变量定义
//////////////////////////////////////////////////////////////////////////////////////////////////
// 采集传感器数据并存入环形缓冲区的任务
void collect_and_buffer_sensor_data_task(void) {
motion_data_t current_motion;
axis_info_xtell total_accel;
axis_info_xtell linear_accel;
char data_string[256];
// 从驱动获取最新的运动数据
get_motion_data(&current_motion);
total_accel = get_current_accel_mss();
linear_accel = get_linear_accel_mss();
// 将浮点数据转换为整数乘以100以保留两位小数进行格式化
int len = snprintf(data_string, sizeof(data_string),
"T:%d,%d,%d;L:%d,%d,%d;V:%d,%d,%d;D:%d,%d,%d\n",
(int)(total_accel.x * 100), (int)(total_accel.y * 100), (int)(total_accel.z * 100),
(int)(linear_accel.x * 100), (int)(linear_accel.y * 100), (int)(linear_accel.z * 100),
(int)(current_motion.velocity.x * 100), (int)(current_motion.velocity.y * 100), (int)(current_motion.velocity.z * 100),
(int)(current_motion.distance.x * 100), (int)(current_motion.distance.y * 100), (int)(current_motion.distance.z * 100));
// 写入环形缓冲区
u16 written = circle_buffer_write(&sensor_cb, (u8*)data_string, len);
if (written < len) {
xlog("The circular buffer is full!\n");
}
}
// 定义数组大小
#define ARRAY_SIZE (178)
// 在 main 函数外部声明为全局变量,它将被存储在静态数据区
unsigned char global_data_array[ARRAY_SIZE];
// 从环形缓冲区读取数据并发送
void send_sensor_data_task(void) {
// printf("xtell_ble_send\n");
send_data_to_ble_client(&global_data_array,ARRAY_SIZE);
}
extern void create_process(u16* pid, const char* name, void *priv, void (*func)(void *priv), u32 msec);
void rcsp_adv_fill_mac_addr(u8 *mac_addr_buf) //by xtell
{
swapX(bt_get_mac_addr(), mac_addr_buf, 6);
}
void xtell_task_create(void){
xlog("xtell_task_create\n");
//写入测试数据
for (int i = 0; i < ARRAY_SIZE; i++) { //ARRAY_SIZE字节的数组
global_data_array[i] = i % 256;
}
// 初始化环形缓冲区
circle_buffer_init(&sensor_cb, sensor_data_buffer, SENSOR_DATA_BUFFER_SIZE);
// 创建一个定时器每200ms调用一次核心计算任务
// create_process(&xtell_i2c_test_id, "xtell_i2c_test", NULL, xtell_i2c_test, (u32)(SAMPLING_PERIOD_S * 1000));
// 创建一个定时器每1000ms采集一次数据
// create_process(&collect_data_id, "collect_data", NULL, collect_and_buffer_sensor_data_task, 1000);
// 创建一个定时器每200ms尝试发送一次数据
create_process(&send_data_id, "send_data", NULL, send_sensor_data_task, 1);
}