在逗猫棒的基础上实现蓝牙转USB功能,目前功能已经实现,还需裁剪掉之前逗猫棒的业务程序
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
#include "system/includes.h"
|
||||
#include "app_config.h"
|
||||
#include "asm/pwm_led.h"
|
||||
#include "tone_player.h"
|
||||
#include "ui_manage.h"
|
||||
#include "app_main.h"
|
||||
#include "app_task.h"
|
||||
#include "asm/charge.h"
|
||||
#include "app_power_manage.h"
|
||||
#include "app_charge.h"
|
||||
#include "user_cfg.h"
|
||||
#include "audio.h"
|
||||
#include "vm.h"
|
||||
#include "bleproto.h"
|
||||
#include "bleproto_api.h"
|
||||
#define LOG_TAG_CONST APP
|
||||
#define LOG_TAG "[APP]"
|
||||
#define LOG_ERROR_ENABLE
|
||||
#define LOG_DEBUG_ENABLE
|
||||
#define LOG_INFO_ENABLE
|
||||
/* #define LOG_DUMP_ENABLE */
|
||||
#define LOG_CLI_ENABLE
|
||||
#include "debug.h"
|
||||
extern void usr_uart_send_cmd(u8* data,u16 len);
|
||||
extern void usr_ble_send_data(u8* data,u16 len);///BLE发送接口,MTU 244
|
||||
extern void usr_write_pair_info();
|
||||
extern void usr_clear_pair_info();
|
||||
extern void usr_mcu_data_recieve(const unsigned char* hex_array, int array_length);
|
||||
// 计算校验和并验证
|
||||
static int verify_checksum(const unsigned char* packet_data, int length)
|
||||
{
|
||||
|
||||
unsigned int calculated_checksum = 0;
|
||||
calculated_checksum=0;
|
||||
// 从帧头开始计算到倒数第二个字节的和
|
||||
for (int i = 0; i < length; i++) {
|
||||
calculated_checksum += packet_data[i];
|
||||
}
|
||||
// 对256取余
|
||||
calculated_checksum %= 256;
|
||||
|
||||
return calculated_checksum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const u8 check_mcu[7]={0x55,0xAA,0x00,0x08,0x00,0x00,0x07};
|
||||
void usr_check_mcu_all_sta()///查询 mcu 工作状态,MCU上报所有DP点数值(用做APP显示)
|
||||
{
|
||||
usr_uart_send_cmd(check_mcu,7);
|
||||
}
|
||||
|
||||
const u8 set_default[12]={0x55 ,0xAA ,0x00 ,0x06 ,0x00 ,0x05 ,0x18 ,0x01 ,0x00 ,0x01 ,0x01 ,0x24};
|
||||
void usr_set_device_default()
|
||||
{
|
||||
usr_uart_send_cmd(check_mcu,12);
|
||||
}
|
||||
|
||||
static u8 set_led_sta[12]={0x55 ,0xAA ,0x00 ,0x06 ,0x00 ,0x05 ,0x11 ,0x01 ,0x00 ,0x01 ,0xff ,0xff};
|
||||
void usr_set_led_sta(u8 flag)
|
||||
{
|
||||
if(flag)set_led_sta[10]=0x01;
|
||||
else set_led_sta[10]=0x00;
|
||||
set_led_sta[11]=verify_checksum(set_led_sta,11);
|
||||
usr_uart_send_cmd(set_led_sta,12);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
指示WiFI状态:
|
||||
0x00:Smartconfig 配网模式 (灯快闪);
|
||||
0x01:AP配网模式 (灯慢闪);
|
||||
0x02:WiFi配置成功但未连上路由(灯熄灭);
|
||||
0x04:已连上路由器且连接到云端 (灯长亮);
|
||||
*/
|
||||
static u8 send_ble_sta[8]={0x55,0xAA,0x00,0x03,0x00,0x01,0xff,0xff};
|
||||
void usr_send_ble_sta(u8 flag)
|
||||
{
|
||||
send_ble_sta[6]=flag;
|
||||
send_ble_sta[7]=verify_checksum(send_ble_sta,7);
|
||||
usr_uart_send_cmd(send_ble_sta,8);
|
||||
printf("usr_send_ble_sta\n");
|
||||
}
|
||||
//extern void usr_set_fool_val(u8 wei_cnt);
|
||||
extern u8 usr_weishi_type;
|
||||
extern u8 usr_weishi_fenshu;
|
||||
static u8 weishi_mcu[15]={0x55,0xAA,0x00,0x06 ,0x00 ,0x08 ,0x03 ,0x02 ,0x00 ,0x04 ,0x00 ,0x00 ,0x00 ,0x02 ,0xFF};
|
||||
void usr_set_werishi(int cnt,u8 weishi_type)
|
||||
{
|
||||
if(cnt>0)
|
||||
{
|
||||
//usr_set_fool_val(cnt);
|
||||
usr_weishi_type=weishi_type;
|
||||
usr_weishi_fenshu=cnt;
|
||||
weishi_mcu[13]=cnt;
|
||||
weishi_mcu[14]=verify_checksum(weishi_mcu,14);
|
||||
printf("weishi cnt = %d\n",weishi_mcu[13]);
|
||||
printf("checksum = %x\n",weishi_mcu[14]);
|
||||
usr_uart_send_cmd(weishi_mcu,15);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
extern void usr_clear_pair_info();
|
||||
const u8 reset_ble[7] = {0x55,0xAA,0x03,0x04,0x00,0x00,0x06};
|
||||
const u8 ble_reset_rsp[7] = {0x55,0xAA,0x00,0x04,0x00,0x00,0x03};
|
||||
const u8 ble_le_sta_rsp[7] = {0x55,0xAA,0x03,0x03,0x00,0x00,0x05};
|
||||
void usr_uart_data_recieve(u8* data,u16 len)///UART数据接收
|
||||
{
|
||||
// log_info("uart_recieve: ");
|
||||
// printf_buf(data, len);
|
||||
if(!memcmp(reset_ble,data,7))
|
||||
{
|
||||
usr_uart_send_cmd(ble_reset_rsp,7);
|
||||
usr_clear_pair_info();
|
||||
return;
|
||||
}
|
||||
if(!memcmp(ble_le_sta_rsp,data,7))
|
||||
{
|
||||
return;
|
||||
}
|
||||
usr_mcu_data_recieve(data,len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_PACKETS 12 // 最大包数量
|
||||
#define MAX_PACKET_LEN 128 // 单个包的最大长度
|
||||
|
||||
typedef struct {
|
||||
unsigned char data[MAX_PACKET_LEN]; // 包的数据
|
||||
int length; // 包的长度(包括帧头和校验和)
|
||||
int data_length; // 数据部分的长度(不包括帧头和校验和)
|
||||
int valid; // 校验是否通过
|
||||
} Packet;
|
||||
|
||||
typedef struct {
|
||||
Packet packets[MAX_PACKETS]; // 解析出的包数组
|
||||
int count; // 解析出的包数量
|
||||
int valid_count; // 校验通过的包数量
|
||||
} PacketResult;
|
||||
|
||||
|
||||
// 计算校验和并验证
|
||||
int verify_checksum(const unsigned char* packet_data, int length) {
|
||||
if (length < 3) return 0; // 至少要有帧头+1数据+1校验和
|
||||
|
||||
unsigned char calculated_checksum = 0;
|
||||
// 从帧头开始计算到倒数第二个字节的和
|
||||
for (int i = 0; i < length - 1; i++) {
|
||||
calculated_checksum += packet_data[i];
|
||||
}
|
||||
// 对256取余
|
||||
calculated_checksum %= 256;
|
||||
|
||||
// 比较计算值和包中的校验和(最后一个字节)
|
||||
return (calculated_checksum == packet_data[length - 1]);
|
||||
}
|
||||
|
||||
void parse_packets(const unsigned char* hex_array, int array_length, PacketResult* result) {
|
||||
result->count = 0;
|
||||
result->valid_count = 0;
|
||||
int start_index = -1;
|
||||
|
||||
for (int i = 0; i < array_length - 1 && result->count < MAX_PACKETS; i++) {
|
||||
if (hex_array[i] == 0x55 && hex_array[i + 1] == 0xAA) {
|
||||
if (start_index != -1) {
|
||||
// 保存上一个包的信息
|
||||
int packet_length = i - start_index;
|
||||
if (packet_length > MAX_PACKET_LEN) {
|
||||
packet_length = MAX_PACKET_LEN; // 截断超长包
|
||||
}
|
||||
|
||||
// 复制数据
|
||||
memcpy(result->packets[result->count].data,
|
||||
hex_array + start_index,
|
||||
packet_length);
|
||||
|
||||
// 设置包信息
|
||||
result->packets[result->count].length = packet_length;
|
||||
result->packets[result->count].data_length = packet_length - 3; // 减去帧头和校验和
|
||||
|
||||
// 验证校验和
|
||||
result->packets[result->count].valid = verify_checksum(
|
||||
result->packets[result->count].data,
|
||||
packet_length);
|
||||
|
||||
if (result->packets[result->count].valid) {
|
||||
result->valid_count++;
|
||||
}
|
||||
|
||||
result->count++;
|
||||
}
|
||||
start_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理最后一个包
|
||||
if (start_index != -1 && start_index < array_length && result->count < MAX_PACKETS) {
|
||||
int packet_length = array_length - start_index;
|
||||
if (packet_length > MAX_PACKET_LEN) {
|
||||
packet_length = MAX_PACKET_LEN;
|
||||
}
|
||||
|
||||
// 复制数据
|
||||
memcpy(result->packets[result->count].data,
|
||||
hex_array + start_index,
|
||||
packet_length);
|
||||
|
||||
// 设置包信息
|
||||
result->packets[result->count].length = packet_length;
|
||||
result->packets[result->count].data_length = packet_length - 3;
|
||||
|
||||
// 验证校验和
|
||||
result->packets[result->count].valid = verify_checksum(
|
||||
result->packets[result->count].data,
|
||||
packet_length);
|
||||
|
||||
if (result->packets[result->count].valid) {
|
||||
result->valid_count++;
|
||||
}
|
||||
|
||||
result->count++;
|
||||
}
|
||||
}
|
||||
void usr_tuya_data_ana(const unsigned char* data,int len);
|
||||
void usr_mcu_data_recieve(const unsigned char* hex_array, int array_length)
|
||||
{
|
||||
PacketResult result;
|
||||
parse_packets(hex_array,array_length,&result);
|
||||
printf("Total packets parsed: %d\n", result.count);
|
||||
printf("Valid packets: %d\n", result.valid_count);
|
||||
|
||||
for (int i = 0; i < result.count; i++) {
|
||||
printf("\nPacket %d: Length=%d, Data Length=%d, Valid=%s\n", i+1,
|
||||
result.packets[i].length,
|
||||
result.packets[i].data_length,
|
||||
result.packets[i].valid ? "Yes" : "No");
|
||||
|
||||
if (result.packets[i].valid) {
|
||||
printf("Data: ");
|
||||
printf_buf(result.packets[i].data, result.packets[i].length);
|
||||
usr_tuya_data_ana(result.packets[i].data,result.packets[i].length);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("Invalid checksum - Packet ignored\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
//void usr_set_fool_val(unsigned char wei_cnt);
|
||||
extern void usr_data_notify_auto();
|
||||
extern unsigned char usr_weishi_type;
|
||||
extern unsigned char usr_weishi_fenshu;
|
||||
extern void usr_weishi_notify();
|
||||
extern unsigned char usr_led_on_flag;
|
||||
void usr_tuya_data_ana(const unsigned char* data,int len)
|
||||
{
|
||||
switch(data[6])///DP ID
|
||||
{
|
||||
case 0x01:///喂食计划
|
||||
break;
|
||||
case 0x02:///一键喂食
|
||||
break;
|
||||
case 0x03:///手动喂食
|
||||
|
||||
break;
|
||||
case 0x04:///喂食状态
|
||||
switch(data[10])
|
||||
{
|
||||
case 0x00:///就绪
|
||||
usr_weishi_type=0;
|
||||
usr_weishi_fenshu=1;
|
||||
usr_data_notify_auto();
|
||||
break;
|
||||
case 0x01:///正在喂食
|
||||
// usr_set_fool_val(usr_weishi_fenshu);
|
||||
// usr_weishi_notify();
|
||||
break;
|
||||
case 0x02:///喂食结束
|
||||
usr_weishi_type=0;
|
||||
usr_weishi_fenshu=1;
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 0x0a:///电池电量
|
||||
///电池电量=data[13];
|
||||
break;
|
||||
case 0x0b:///充电状态
|
||||
///充电状态=data[10]
|
||||
break;
|
||||
case 0x0d:///故障上报
|
||||
///故障=data[10]
|
||||
/*
|
||||
bit0:food_jam
|
||||
bit1:food_shortages
|
||||
bit2:food_run_out
|
||||
bit3:desiccant
|
||||
bit4:battery_low
|
||||
bit5:device_stuck
|
||||
*/
|
||||
break;
|
||||
case 0x0e:///喂食结果上报
|
||||
break;
|
||||
case 0x11:///状态指示灯应答
|
||||
///LED开关=data[10]
|
||||
usr_led_on_flag=data[10];
|
||||
break;
|
||||
case 0x17:///慢放投喂
|
||||
break;
|
||||
case 0x18:///恢复出厂设置应答
|
||||
break;
|
||||
case 0x1c:///MCU获取BLE模块的本地时间
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
int ana_demo() {
|
||||
// 示例数据
|
||||
unsigned char hex_data[] = {
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x0E, 0x01, 0x00, 0x00, 0x0A, 0x7F, 0x0F, 0x26, 0x01, 0x01, 0x7F, 0x0F, 0x28, 0x01, 0x01, 0x90,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x08, 0x03, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x1B,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x05, 0x04, 0x04, 0x00, 0x01, 0x00, 0x17,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x08, 0x0A, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x21,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x05, 0x0D, 0x05, 0x00, 0x01, 0x00, 0x21,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x05, 0x11, 0x01, 0x00, 0x01, 0x01, 0x22,
|
||||
0x55, 0xAA, 0x03, 0x07, 0x00, 0x05, 0x17, 0x01, 0x00, 0x01, 0x00, 0x87,
|
||||
0x55, 0xAA, 0x03, 0x1C, 0x00, 0x00, 0x17
|
||||
};
|
||||
|
||||
int data_length = sizeof(hex_data) / sizeof(hex_data[0]);
|
||||
PacketResult result;
|
||||
|
||||
parse_packets(hex_data, data_length, &result);
|
||||
|
||||
printf("Total packets parsed: %d\n", result.count);
|
||||
printf("Valid packets: %d\n", result.valid_count);
|
||||
|
||||
for (int i = 0; i < result.count; i++) {
|
||||
printf("\nPacket %d: Length=%d, Data Length=%d, Valid=%s\n", i+1,
|
||||
result.packets[i].length,
|
||||
result.packets[i].data_length,
|
||||
result.packets[i].valid ? "Yes" : "No");
|
||||
|
||||
if (result.packets[i].valid) {
|
||||
printf("Data: ");
|
||||
printf_buf(result.packets[i].data, result.packets[i].length);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("Invalid checksum - Packet ignored\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user