123 lines
4.1 KiB
C
123 lines
4.1 KiB
C
#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 xlog(format, ...) printf("[%s] " format, __func__, ##__VA_ARGS__)
|
||
|
||
|
||
void send_data_to_ble_client(const u8* data, u16 length);
|
||
|
||
// --- 任务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;
|
||
|
||
|
||
// 采集传感器数据并存入环形缓冲区的任务
|
||
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(¤t_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");
|
||
}
|
||
}
|
||
|
||
// 从环形缓冲区读取数据并发送
|
||
void send_sensor_data_task(void) {
|
||
|
||
//查询开关机状态
|
||
u8 temp[5]={0xBB,0xBE,0x02,0x00,0x00};
|
||
printf("xtell_ble_send\n");
|
||
send_data_to_ble_client(&temp,5);
|
||
}
|
||
|
||
void create_process(u16* pid, const char* name, void *priv, void (*func)(void *priv), u32 msec){
|
||
xlog("Create Task: %s, pid=%d\n", name, *pid);
|
||
if (*pid != 0) return;
|
||
*pid = sys_timer_add(priv, func, msec);
|
||
xlog("Task %s is created, pid=%d\n", name, *pid);
|
||
}
|
||
|
||
void rcsp_adv_fill_mac_addr(u8 *mac_addr_buf) //by xtell
|
||
{
|
||
swapX(bt_get_mac_addr(), mac_addr_buf, 6);
|
||
}
|
||
|
||
void xtell_main(void){
|
||
|
||
u8 mac_data[6];
|
||
|
||
soft_iic_init(0);
|
||
extern u8 LIS2DH12_Config(void);
|
||
LIS2DH12_Config();
|
||
|
||
rcsp_adv_fill_mac_addr(mac_data); //读取MAC地址
|
||
xlog("xtell BT mac data:%x:%x:%x:%x:%x:%x",mac_data[0],mac_data[1],mac_data[2],mac_data[3],mac_data[4],mac_data[5]);
|
||
|
||
|
||
|
||
//开经典蓝牙
|
||
// user_send_cmd_prepare(USER_CTRL_WRITE_SCAN_ENABLE, 0, NULL); //打开蓝牙可发现,已连接时不能操作
|
||
// delay_2ms(50);
|
||
// user_send_cmd_prepare(USER_CTRL_WRITE_CONN_ENABLE, 0, NULL); //打开蓝牙可连接
|
||
// delay_2ms(50);
|
||
// connect_last_device_from_vm(); //自动回连上一个设备
|
||
|
||
|
||
// 初始化环形缓冲区
|
||
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, 200);
|
||
}
|