Files
99_7018_lmx/apps/earphone/xtell_Sensor/send_data.c

212 lines
6.8 KiB
C
Raw Normal View History

2025-10-29 16:24:16 +08:00
#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"
2025-10-30 11:33:38 +08:00
#include "circle_buffer.h"
#include "circle_buffer.h"
#include "btstack/avctp_user.h"
2025-11-03 18:48:15 +08:00
#include "calculate/skiing_tracker.h"
2025-10-31 09:40:15 +08:00
///////////////////////////////////////////////////////////////////////////////////////////////////
//宏定义
#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
//
///////////////////////////////////////////////////////////////////////////////////////////////////
2025-10-29 16:24:16 +08:00
2025-10-31 10:38:17 +08:00
//////////////////////////////////////////////////////////////////////////////////////////////////
//START -- 函数定义
2025-10-30 16:14:14 +08:00
void send_data_to_ble_client(const u8* data, u16 length);
2025-11-03 18:48:15 +08:00
extern void create_process(u16* pid, const char* name, void *priv, void (*func)(void *priv), u32 msec);
2025-10-31 10:38:17 +08:00
//END -- 函数定义
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//START -- 变量定义
2025-10-30 16:14:14 +08:00
2025-10-30 11:33:38 +08:00
// --- 任务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;
2025-11-03 18:48:15 +08:00
//--- test ---
2025-11-04 14:40:55 +08:00
// 全局变量
u16 gsensor_id=0;
u16 test_id=0;
2025-10-31 10:38:17 +08:00
//END -- 变量定义
//////////////////////////////////////////////////////////////////////////////////////////////////
2025-10-30 11:33:38 +08:00
2025-10-29 16:24:16 +08:00
2025-11-04 19:20:58 +08:00
// /**
// * @brief 向匿名上位机发送数据帧
// * @param function_id 功能码 (例如 0x01, 0x03)
// * @param data 指向 int16_t 数据数组的指针 (例如加速度、欧拉角)
// * @param data_len int16_t 数据的个数 (例如发送6轴数据时为6发送3个欧拉角时为3)
// * @param status_byte 附加的状态字节 (例如 SHOCK_STA 或 FUSION_STA)
// */
// void send_data_anotc(uint8_t function_id, int16_t* data, uint8_t data_len, uint8_t status_byte) {
// // 定义一个足够大的缓冲区来构建数据帧
// // 最大长度(ID 0x01): 1(HEAD)+1(D_ADDR)+1(ID)+1(LEN)+13(DATA)+1(SC)+1(AC) = 19字节
// uint8_t buffer[32];
// uint8_t data_payload_len = data_len * sizeof(int16_t) + sizeof(uint8_t);
// // 1. 填充帧头和地址
// buffer[0] = 0xAA; // 帧头 HEAD
// buffer[1] = 0xFF; // 目标地址 D_ADDR
// // 2. 填充功能码和数据长度
// buffer[2] = function_id;
// buffer[3] = data_payload_len;
// // 3. 填充数据内容 (DATA)
// // 首先使用 memcpy 拷贝主要的 int16_t 数组数据
// // &buffer[4] 是数据区的起始地址
// memcpy(&buffer[4], (uint8_t*)data, data_len * sizeof(int16_t));
// // 然后在数据区末尾填充状态字节
// buffer[4 + data_len * sizeof(int16_t)] = status_byte;
// // 4. 计算校验和 (SC 和 AC)
// uint8_t sum_check = 0;
// uint8_t add_check = 0;
// // SC: 和校验 (从帧头到数据区最后一个字节)
// for (int i = 0; i < 4 + data_payload_len; ++i) {
// sum_check += buffer[i];
// }
// // 将SC填充到缓冲区
// buffer[4 + data_payload_len] = sum_check;
// // AC: 附加校验 (从帧头到SC)
// for (int i = 0; i < 4 + data_payload_len + 1; ++i) {
// add_check += buffer[i];
// }
// // 将AC填充到缓冲区
// buffer[4 + data_payload_len + 1] = add_check;
// // 5. 发送整个数据帧
// uint16_t frame_length = 4 + data_payload_len + 2;
// // Serial_Send_Buffer(buffer, frame_length);
// for (int i = 0; i < frame_length; ++i) {
// // 使用 %c 格式化字符来发送单个字节的原始值
// printf("%c", buffer[i]);
// }
// printf("\n");
// }
void ble_send_data(signed short *acc_gyro_input, float *Angle_output){
char buffer[50]; //一次最多发送50字节
u8 len = 0;
//AA FF 01 六轴数据 EE
//TO DO
send_data_to_ble_client(&buffer,len);
//AA FF 02 欧若拉角数据 EE
// TO DO
send_data_to_ble_client(&buffer,len);
}
2025-11-03 18:48:15 +08:00
// 从环形缓冲区读取数据并发送
void send_sensor_data_task(void) {
// printf("xtell_ble_send\n");
}
2025-10-29 16:24:16 +08:00
2025-11-04 14:40:55 +08:00
void test(){
signed short acc_data_buf[3] = {0};
signed short gyr_data_buf[3] = {0};
signed short acc_gyro_input[6] = {0};
float Angle_output[3] = {0};
extern void SL_SC7U22_RawData_Read(signed short * acc_data_buf,signed short * gyr_data_buf);
SL_SC7U22_RawData_Read(acc_data_buf,gyr_data_buf);
acc_gyro_input[0] = acc_data_buf[0];
acc_gyro_input[1] = acc_data_buf[1];
acc_gyro_input[2] = acc_data_buf[2];
acc_gyro_input[3] = gyr_data_buf[0];
acc_gyro_input[4] = gyr_data_buf[1];
acc_gyro_input[5] = gyr_data_buf[2];
// extern unsigned char SL_SC7U22_Angle_Output(unsigned char calibration_en, signed short *acc_gyro_input, float *Angle_output, unsigned char yaw_rst);
// SL_SC7U22_Angle_Output(0,acc_gyro_input,Angle_output,1);
static int count = 0;
if(count >=100){ //1s打印1次
count = 0;
xlog("ACC_X:%d, ACC_Y:%d, ACC_Z:%d, GYR_X:%.d, GYR_Y:%d, GYR_Z:%d",
acc_gyro_input[0],acc_gyro_input[1],acc_gyro_input[2],acc_gyro_input[3],acc_gyro_input[4],acc_gyro_input[5]
);
// xlog("Pitch:%.2f, Roll:%.2f, Yaw:%.2f\n",
// Angle_output[0],Angle_output[1],Angle_output[2]
// );
2025-11-03 18:48:15 +08:00
}
2025-11-04 14:40:55 +08:00
count++;
2025-10-29 16:24:16 +08:00
2025-11-04 14:40:55 +08:00
void sensor_processing_task(signed short * acc_data_buf,signed short * gyr_data_buf);
sensor_processing_task(acc_data_buf, gyr_data_buf);
}
void gsensor_test(){
2025-11-03 18:48:15 +08:00
2025-11-04 14:40:55 +08:00
sys_timer_del(gsensor_id);
2025-11-03 18:48:15 +08:00
2025-11-04 14:40:55 +08:00
}
2025-11-03 18:48:15 +08:00
void xtell_task_create(void){
2025-10-29 16:24:16 +08:00
2025-11-03 18:48:15 +08:00
// int ret = hw_iic_init(0);
// xlog("hw_iic_init result:%d\n",ret);
// //初始化传感器
// SL_SC7U22_Config();
2025-10-29 16:24:16 +08:00
2025-11-03 18:48:15 +08:00
soft_iic_init(0);
gpio_set_direction(IO_PORTE_05,0); //设置PE5 输出模式
gpio_set_pull_up(IO_PORTE_05,1);
gpio_direction_output(IO_PORTE_05,1);
2025-10-31 16:58:39 +08:00
2025-11-03 18:48:15 +08:00
SL_SC7U22_Config();
// extern u8 LIS2DH12_Config(void);
// LIS2DH12_Config();
2025-10-30 11:33:38 +08:00
2025-10-30 18:30:40 +08:00
xlog("xtell_task_create\n");
2025-10-30 11:33:38 +08:00
// 初始化环形缓冲区
circle_buffer_init(&sensor_cb, sensor_data_buffer, SENSOR_DATA_BUFFER_SIZE);
2025-11-03 18:48:15 +08:00
//初始化滑雪追踪器
2025-11-04 14:40:55 +08:00
// SkiingTracker_Init(&skiing_data);
2025-11-03 18:48:15 +08:00
xlog("SkiingTracker_Init\n");
2025-10-30 11:33:38 +08:00
2025-11-04 14:40:55 +08:00
// create_process(&gsensor_id, "gsensor",NULL, gsensor_test, 1000);
create_process(&test_id, "test",NULL, test, 10);
2025-10-30 11:33:38 +08:00
}