#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 #include #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 "./sensor/SC7U22.h" #include "./buffer/circle_buffer.h" #include "btstack/avctp_user.h" #include "calculate/skiing_tracker.h" #include "xtell.h" #include "./ano/ano_protocol.h" #include "./sensor/MMC56.h" #include "./sensor/BMP280.h" #include "./sensor/AK8963.h" #include "asm/rtc.h" #include "system/timer.h" #include "adv_time_stamp_setting.h" #include "btstack/le/le_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 #define SENSOR_DATA_BUFFER_SIZE 200 // 定义缓冲区可以存储XXX个sensor_data_t元素 #define MPU_FIFO_INTERVAL 4 //隔多久读取六轴fifo,单位10ms #define MPU_FIFO_LEN 16 //(MPU_FIFO_INTERVAL*10/2.5) //400hz采用频率,那每40ms的时间,fifo就有16组六轴数据 // /////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// //START -- 函数定义 void send_data_to_ble_client(const u8* data, u16 length); extern void create_process(u16* pid, const char* name, void *priv, void (*func)(void *priv), u32 msec); extern void close_process(u16* pid,char* name); void BLE_send_fuc(void); //END -- 函数定义 ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// //START -- 变量定义 static u32 timer_offset_ms = 0; typedef struct{ signed short SC7U22_data[6]; //12字节 int mmc5603nj_buffer[3]; //12字节 int16_t temperature; //2 uint32_t pressure; //4 }sensor_package_t __attribute__((packed)); typedef struct{ uint8_t checkout_1; uint8_t checkout_2; uint8_t index; sensor_package_t sensor_package[MPU_FIFO_LEN];//一次蓝牙发送MPU_FIFO_LEN组传感器数据 }ble_send_data_t; //一次蓝牙发送的数据内容 // --- 环形缓冲区 --- static circle_buffer_t g_ble_send_cb; // 环形缓冲区管理结构体 static ble_send_data_t g_sensor_data_storage[SENSOR_DATA_BUFFER_SIZE]; //缓冲区 static OS_SEM receiver_ready_sem; // 用于启动同步的信号量 //END -- 变量定义 ////////////////////////////////////////////////////////////////////////////////////////////////// // 重置计时器 void reset_ms_timer(void) { timer_offset_ms = sys_timer_get_ms(); xlog("Timer has been reset.\n"); } // 获取从上次重置后经过的毫秒数 u32 get_ms_timer(void) { return sys_timer_get_ms() - timer_offset_ms; } /** * @brief 传感器采集任务 * */ void sensor_collect_task(void){ ble_send_data_t send_data; mmc5603nj_mag_data_t mmc5603nj_buffer; float temperature = 0; float pressure = 0; int interval = 0; signed short accx_buf[100]; signed short accy_buf[100]; signed short accz_buf[100]; signed short gyrx_buf[100]; signed short gyry_buf[100]; signed short gyrz_buf[100]; int fifo_num = 0; u8 index = 1; while(1){//4组地磁数据、16组六轴数据、1组气压计数据 interval++; mmc5603nj_read_mag_data(&mmc5603nj_buffer); for(int i = (interval-1)*4; i < interval*4; i++){ send_data.sensor_package[i].mmc5603nj_buffer[0] = (int32_t)(mmc5603nj_buffer.x * 1000.0f); send_data.sensor_package[i].mmc5603nj_buffer[1] = (int32_t)(mmc5603nj_buffer.y * 1000.0f); send_data.sensor_package[i].mmc5603nj_buffer[2] = (int32_t)(mmc5603nj_buffer.z * 1000.0f); } // xlog("MAG x: %.2f,y: %.2f,z: %.2f\n",mmc5603nj_buffer.x,mmc5603nj_buffer.y,mmc5603nj_buffer.z); if(interval >= 4){ interval = 0; memset(&accx_buf, 0, sizeof(accx_buf)); memset(&accy_buf, 0, sizeof(accy_buf)); memset(&accz_buf, 0, sizeof(accz_buf)); memset(&gyrx_buf, 0, sizeof(gyrx_buf)); memset(&gyry_buf, 0, sizeof(gyry_buf)); memset(&gyrz_buf, 0, sizeof(gyrz_buf)); fifo_num = SL_SC7U22_FIFO_Read(accx_buf,accy_buf,accz_buf,gyrx_buf,gyry_buf,gyrz_buf); //40ms一次性读取内置fifo的数据 bmp280_read_data(&temperature, &pressure);//每40ms读取一次 for(int i = 0;i= 0xFF) index = 1; // xlog("=====================%d============================\n",get_ms_timer()); } os_time_dly(1); //10ms为单位 } } static int random = 0; void data_log(uint8_t* data){ // 检查数据包头部 if (data[0] != 0xBE || data[1] != 0xBB) { printf("Error: Invalid data packet header.\n"); return; } // 解析包索引 uint8_t index = data[2]; printf("--- Parsing Data Packet Index: %d ---\n", index); uint8_t* p = &data[3]; // 指向数据负载的起始位置 // 循环解析16组数据 for (int i = 0; i < MPU_FIFO_LEN; i++) { // 1. 解析六轴传感器数据 (12 bytes) int16_t imu_raw[6]; for (int j = 0; j < 6; j++) { // 小端模式: 低字节在前, 高字节在后 imu_raw[j] = (int16_t)(((uint16_t)p[1] << 8) | (uint16_t)p[0]); p += 2; } float acc_g[3]; float gyr_dps[3]; acc_g[0] = (float)imu_raw[0] / 2048.0f; acc_g[1] = (float)imu_raw[1] / 2048.0f; acc_g[2] = (float)imu_raw[2] / 2048.0f; gyr_dps[0] = (float)imu_raw[3] * 0.061f; gyr_dps[1] = (float)imu_raw[4] * 0.061f; gyr_dps[2] = (float)imu_raw[5] * 0.061f; // 2. 解析地磁传感器数据 (12 bytes) int32_t mag_raw[3]; for (int j = 0; j < 3; j++) { // 小端模式 mag_raw[j] = (int32_t)(((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) | ((uint32_t)p[1] << 8) | (uint32_t)p[0]); p += 4; } float mag_gauss[3]; mag_gauss[0] = (float)mag_raw[0] / 1000.0f; mag_gauss[1] = (float)mag_raw[1] / 1000.0f; mag_gauss[2] = (float)mag_raw[2] / 1000.0f; // 3. 解析温度数据 (2 bytes) int16_t temp_raw = (int16_t)(((uint16_t)p[1] << 8) | (uint16_t)p[0]); p += 2; float temperature = (float)temp_raw / 1000.0f; // 4. 解析气压数据 (4 bytes) uint32_t press_raw = (uint32_t)(((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) | ((uint32_t)p[1] << 8) | (uint32_t)p[0]); p += 4; float pressure = (float)press_raw / 1000.0f; // 打印解析后的数据 // if(i % 4 == 0){ printf("Package[%d]:====================\n", i); // printf(" ACC(g): x=%.3f, y=%.3f, z=%.3f\n", acc_g[0], acc_g[1], acc_g[2]); // printf(" GYR(dps):x=%.3f, y=%.3f, z=%.3f\n", gyr_dps[0], gyr_dps[1], gyr_dps[2]); printf(" MAG(Gs): x=%.3f, y=%.3f, z=%.3f\n", mag_gauss[0], mag_gauss[1], mag_gauss[2]); printf(" TEMP(C): %.3f, PRESS(Pa): %.3f\n", temperature, pressure); // } } printf("--- End of Packet ---\n\n"); } /** * @brief ble数据发送函数 * */ void BLE_send_fuc(void){ ble_send_data_t send_data; uint8_t send_buffer[483]; while(1){ os_sem_pend(&receiver_ready_sem, 0); //阻塞等待 circle_buffer_read(&g_ble_send_cb, &send_data); // 逐字节打包数据到 send_buffer, 采用小端模式 uint8_t *p = send_buffer; *p++ = send_data.checkout_1; *p++ = send_data.checkout_2; *p++ = send_data.index; for (int i = 0; i < MPU_FIFO_LEN; i++) { sensor_package_t *pkg = &send_data.sensor_package[i]; // 1. 打包六轴数据 (6 * int16_t) for (int j = 0; j < 6; j++) { *p++ = (uint8_t)(pkg->SC7U22_data[j] & 0xFF); *p++ = (uint8_t)((pkg->SC7U22_data[j] >> 8) & 0xFF); } // 2. 打包地磁数据 (3 * int32_t) for (int j = 0; j < 3; j++) { *p++ = (uint8_t)(pkg->mmc5603nj_buffer[j] & 0xFF); *p++ = (uint8_t)((pkg->mmc5603nj_buffer[j] >> 8) & 0xFF); *p++ = (uint8_t)((pkg->mmc5603nj_buffer[j] >> 16) & 0xFF); *p++ = (uint8_t)((pkg->mmc5603nj_buffer[j] >> 24) & 0xFF); } // 3. 打包温度数据 (int16_t) *p++ = (uint8_t)(pkg->temperature & 0xFF); *p++ = (uint8_t)((pkg->temperature >> 8) & 0xFF); // 4. 打包气压数据 (uint32_t) *p++ = (uint8_t)(pkg->pressure & 0xFF); *p++ = (uint8_t)((pkg->pressure >> 8) & 0xFF); *p++ = (uint8_t)((pkg->pressure >> 16) & 0xFF); *p++ = (uint8_t)((pkg->pressure >> 24) & 0xFF); #if 0 float acc_g[3]; float gyr_dps[3]; acc_g[0] = (float)send_data.sensor_package[i].SC7U22_data[0] / 2048.0f; acc_g[1] = (float)send_data.sensor_package[i].SC7U22_data[1] / 2048.0f; acc_g[2] = (float)send_data.sensor_package[i].SC7U22_data[2] / 2048.0f; gyr_dps[0] = (float)send_data.sensor_package[i].SC7U22_data[3] * 0.061f; gyr_dps[1] = (float)send_data.sensor_package[i].SC7U22_data[4] * 0.061f; gyr_dps[2] = (float)send_data.sensor_package[i].SC7U22_data[5] * 0.061f; printf(" ACC(g): x=%.3f, y=%.3f, z=%.3f\n", acc_g[0], acc_g[1], acc_g[2]); printf(" GYR(dps):x=%.3f, y=%.3f, z=%.3f\n", gyr_dps[0], gyr_dps[1], gyr_dps[2]); #endif } // send_data_to_ble_client(send_buffer, 3 + sizeof(send_data.sensor_package)); // 发送数据 data_log(send_buffer); } } // ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------ /** * @brief 开始采集传感器数据并通过ble发送 * */ void start_clloct(void){ os_task_create(sensor_collect_task,NULL,5,1024,32,"sensor_collect_task"); os_task_create(BLE_send_fuc,NULL,5,1024,32,"BLE_send_fuc"); } /** * @brief 停止采集和ble发送 * */ void stop_clloct(void){ os_task_del_req("sensor_collect_task"); os_task_del_req("BLE_send_fuc"); } /** * @brief 初始化,在app_main.c的app_main函数被调用 * */ void xtell_task_create(void){ #if TCFG_GSENOR_USER_IIC_TYPE int ret = hw_iic_init(0); xlog("init iic result:%d\n", ret); //返回0成功 #else int ret = soft_iic_init(0); int num_chars_written = snprintf(log_buffer_1, sizeof(log_buffer_1),"init iic: %d\n", ret); #endif // MPU9250_Mag_Init(); //iic总线设备扫描 // extern void i2c_scanner_probe(void); // i2c_scanner_probe(); xlog("xtell_task_create\n"); circle_buffer_init(&g_ble_send_cb, g_sensor_data_storage, SENSOR_DATA_BUFFER_SIZE, sizeof(ble_send_data_t)); os_sem_create(&receiver_ready_sem, 0); } ////////////////////////////////////////////////////////////////////////////// //test // #define BUFF_LEN 500 static signed char acc_data_buf[BUFF_LEN] = {0}; // 1. 定义一个全局的信号量 static OS_SEM ble_send_sem; void on_ble_can_send(void) { os_sem_post(&ble_send_sem); } int j = 0; void data_send_task(void){ while(1){ // 等待“发送许可”信号量,如果没许可,任务会在此阻塞 // os_sem_pend(&ble_send_sem, 1); // static signed char acc_data_buf[60] = { // 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22 // }; acc_data_buf[1] = (uint8_t)(j & 0xFF); acc_data_buf[2] = (uint8_t)(j>>8 & 0xFF); acc_data_buf[3] = (uint8_t)(j>>16 & 0xFF); acc_data_buf[4] = (uint8_t)(j>>24 & 0xFF); acc_data_buf[0] = 0xAA; send_data_to_ble_client(&acc_data_buf, 500); // acc_data_buf[0] = 0xBB; // send_data_to_ble_client(&acc_data_buf, 500); // acc_data_buf[0] = 0xCC; // send_data_to_ble_client(&acc_data_buf, 500); // acc_data_buf[0] = 0xDD; // send_data_to_ble_client(&acc_data_buf, 500); // static signed char acc_data_buf[20] = { // 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // }; // send_data_to_ble_client(&acc_data_buf, 20); // send_data_to_ble_client(&acc_data_buf, 20); // send_data_to_ble_client(&acc_data_buf, 20); // xlog("===========time:%d\n",get_ms_timer()); j++; os_time_dly(4); //10ms单位 } } static u16 gtest_id = 0; void test_func(void){ // a. 初始化信号量,初始值为0 // os_sem_create(&ble_send_sem, 0); // b. 注册回调函数,让协议栈知道在准备好时该调用谁 // struct ble_server_operation_t *ble_ops; // ble_get_server_operation_table(&ble_ops); // ble_ops->regist_wakeup_send(NULL, on_ble_can_send); for(int i = 0;i