123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #include "mba32a.h"
- #include "usart.h"
- /**
- * @breaf 外部定义
- */
- extern UART_HandleTypeDef huart3;
- extern UART_HandleTypeDef huart5;
- extern bool startdtu; //声明外部启动界面切换标志位
- extern Menu_table menu; //结构体
- extern bool L1bd;
- extern bool refresh;
- /**
- * @breaf 本页定义
- */
- Bt_frame btFrame;
- bool BT_CONN = false;
- bool connected = false; //连接断进程标志位
- bool resetBTmodule = false;
- uint8_t step_BT=0;
- uint16_t u5length=0;
- uint16_t resetFlag16=0;
- uint8_t g_MBA32A_8buf[1024]={0}; //蓝牙接收数组
- uint8_t g_MBA32A_8buf_Down[1024]={0} ; //蓝牙搬移数组
- uint16_t bt_dataLength=0; //bt接收有效数据长度
- uint8_t bt_error_num=0; //bt接收的错误事件
- uint32_t bt_serial_num=0; //bt接收的流水号
- uint8_t bt_battery=0; //bt剩余电量百分比
- uint8_t bt_log_num=0; //bt记录事件数量
- uint8_t bt_lock_machinesta=0; //bt锁(电机)状态(0x00开;0x01关;0x02异常)
- uint8_t bt_lock_ropesta=0; //bt锁(锁绳)状态(0x00插入;0x01拔出)
- uint16_t bt_hwver=0; //bt硬件版本号
- uint16_t bt_sfver=0; //bt软件版本号
- /**
- * @breaf 本页方法
- */
- uint8_t sendCmd_BT(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum);
- void MBA32AReset(void);
- /**
- * @breaf 蓝牙初始化
- */
- void MBA32A_Init(void)
- {
- startdtu = true;
- menu.home=0;
- btFrame.serialNum = 0x00000001;//流水号
- btFrame.userId = 0x64747531;//dtu1
- btFrame.Autoseal = 0x01;
-
- resetFlag16 = read_flash_16(resetDevAddr);
- if(resetFlag16==0xFFFF)//开机检测(首次启动)
- {
- if(sendCmd_BT("AT+RESET=1\r\n","OK",1,1))
- {
- Write_Information(resetDevAddr);//写入0xAAAA
- printf("复位设备成功\r\n");
- }
- else if(sendCmd_BT("AT+DISCONN=0\r\n","OK",1,1))
- {
- printf("取消连接所有设备\r\n");
- }
- else
- {
- printf("复位失败\r\n");
- }
- }
- else if(resetFlag16==0xAAAA)//开机检测(大于1次启动)
- {
- //printf("复位%X\r\n",resetFlag16);
- printf("非首次启动\r\n");
- }
-
- switch(step_BT)
- {
- case 0:
- {
- if(sendCmd_BT("AT+DEV?\r\n","+DEV:0,101000000003",1,1))
- {
- startdtu=false; //关闭开始界面
- BT_CONN=true;
- connected=true;
- if(sendCmd_BT("AT+AUTO_CFG=1\r\n","OK",1,1))
- {
- printf("自动重连打开\r\n");
- }
- printf("设备已连接\r\n");
- //L1bd=true;
- break;
- }
- else if(sendCmd_BT("AT+DEV?\r\n","+DEV:",1,1))
- {
- printf("未连接设备\r\n");
- }
- step_BT++;
- }
- case 1:
- {
- printf("设备未连接\r\n");
- if(!connected)//断进程标志位
- {
- if(sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:FFF0",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDS=0002\r\n","OK",1,1))
- {
- printf("设置主服务成功\n");
- step_BT++;
- }
- }
- else if(sendCmd_BT("AT+UUIDS?\r\n","+UUIDS:0002",1,1))
- {
- printf("UUIDS非默认值,符合\n");
- step_BT++;
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- }
- case 2:
- {
- if(!connected)//断进程标志位
- {
- if(sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FFF1",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDN=FF04\r\n","OK",1,1))
- {
- printf("设置读服务成功\n");
- step_BT++;
- }
- }
- else if(sendCmd_BT("AT+UUIDN?\r\n","+UUIDN:FF04",1,1))
- {
- printf("UUIDN非默认值,符合\n");
- step_BT++;
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- }
-
- case 3:
- {
- if(!connected)//断进程标志位
- {
- if(sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF2",1,1))//蓝牙芯片默认值,需要修改
- {
- if(sendCmd_BT("AT+UUIDW=FFF5\r\n","OK",1,1))
- {
- printf("设置写服务成功\n");
- step_BT++;
- }
- }
- else if(sendCmd_BT("AT+UUIDW?\r\n","+UUIDW:FFF5",1,1))
- {
- printf("UUIDW非默认值,符合\n");
- step_BT++;
- }
- else
- { //ERROR 其他值
- MBA32AReset();
- break;
- }
- }
- }
-
- case 4:
- {
- if(!connected)//断进程标志位
- {
- if(sendCmd_BT("AT+CONN=101000000003\r\n","+CONNECTED:0,101000000003",3,3))
- {
- startdtu=false; //关闭开始界面
- BT_CONN=true;
- //L1bd=true;
- if(sendCmd_BT("AT+AUTO_CFG=1\r\n","OK",1,1))
- {
- printf("自动重连打开\r\n");
- }
- printf("连接成功!");
- break;
- }
- else if(sendCmd_BT("AT+CONN=101000000003\r\n","+CONNECT TIMEOUT",3,5))
- {
- printf("连接超时!");
- MBA32AReset(); //后台复位
- break;
- }
- else
- {
- printf("连接失败! %s",g_MBA32A_8buf_Down);
- MBA32AReset();
- break;
- }
- }
- }
-
-
-
-
-
- }
-
-
- }
- /**
- * @breaf 模块复位函数(接收数组清0)
- */
- void MBA32AReset(void)
- {
- printf("Bt reset\r\n");
- HAL_GPIO_WritePin(REST_BT_GPIO_Port,REST_BT_Pin,GPIO_PIN_RESET);
- HAL_Delay(4000);
- step_BT=0;
- resetBTmodule=true;//freertos监测模块重启位
- }
- /**
- * @breaf 串口发送命令,有接收数组比较函数
- */
- void USART_SendString(UART_HandleTypeDef *huart, char *str)
- {
-
- HAL_UART_Transmit(huart, (uint8_t*)str, strlen(str), 0xFFFF);
- HAL_UART_Transmit(&huart3, (uint8_t*)str, strlen(str), 0xFFFF);
-
- }
- uint8_t sendCmd_BT(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
- {
- uint8_t i = 0;
- uint32_t time;
- for(i = 0; i < sendNum; i++)
- {
- time = timeOut * 10;
- USART_SendString(&huart5, pCmd);
- HAL_Delay(30);
- printf("-------------->%s",g_MBA32A_8buf_Down);
-
- while(time--)
- {
- if(strstr((const char *)g_MBA32A_8buf_Down, pRes) != NULL) // 如果检索到关键词
- {
- return 1;
- }
- HAL_Delay(100);
- }
- }
- return 0;
- }
- void Echo_BT(uint8_t echosize)
- {
- HAL_UART_Transmit(&huart3,g_MBA32A_8buf_Down, echosize, 0xFFFF);//TTL回显
- }
- /**
- * @breaf 查询蓝牙锁信息帧
- */
- void lock_searchInfo(void) //蓝牙查询信息帧
- {
- uint8_t header[2] = {0x66, 0x00};//Jl_lock帧头
- uint8_t serchInfo_CMD[] = {0x01, 0x00};//查询信息命令帧
- sendCombinedArray(header,sizeof(header),serchInfo_CMD,sizeof(serchInfo_CMD));
- Echo_BT(20);
- }
- /**
- * @breaf 问询蓝牙锁工作信息函数
- */
- void workinfo_W(void)
- {
- if(*g_MBA32A_8buf_Down == NULL)
- return;
- if(g_MBA32A_8buf_Down[0]==0x66 && g_MBA32A_8buf_Down[1]==0x00 )//未加密帧头(命令字0x01)
- {
- if(g_MBA32A_8buf_Down[2]==0x01)
- {
- bt_lock_machinesta = g_MBA32A_8buf_Down[11]; //电机状态
- bt_lock_ropesta = g_MBA32A_8buf_Down[12]; //锁绳状态
-
- // bt_dataLength = g_MBA32A_8buf_Down[3];//接收数据长度
- // bt_error_num = g_MBA32A_8buf_Down[4]; //错误事件数
- // // 将4个字节存入 bt_serial_num流水号
- // bt_serial_num |= g_MBA32A_8buf_Down[5]<<24;
- // bt_serial_num |= g_MBA32A_8buf_Down[6]<<16;
- // bt_serial_num |= g_MBA32A_8buf_Down[7]<<8;
- // bt_serial_num |= g_MBA32A_8buf_Down[8];
- // bt_battery = g_MBA32A_8buf_Down[9]; //电量百分比
- // bt_log_num = g_MBA32A_8buf_Down[10];//记录型事件
- // bt_hwver |= g_MBA32A_8buf_Down[13]<<8; //硬件版本号
- // bt_hwver |= g_MBA32A_8buf_Down[14];
- // bt_sfver |= g_MBA32A_8buf_Down[15]<<8; //软件版本号
- // bt_sfver |= g_MBA32A_8buf_Down[16];
-
- }
- else if((g_MBA32A_8buf_Down[2]==0x03) && (g_MBA32A_8buf_Down[3]==0x01))//施封(关锁完成)
- {
-
- }
- else if((g_MBA32A_8buf_Down[2]==0x02) && (g_MBA32A_8buf_Down[3]==0x01))//解封(关锁完成)
- {
- }
- }
- }
- void Scan_lock_status(void)
- {
- if(bt_lock_machinesta==0x01 && bt_lock_ropesta==0x00 && g_MBA32A_8buf_Down[0]==0x66)//0x01电机关 0x00锁绳插入
- {
- menu.home=0xF000;//关(全部关闭)
- }
- else if(bt_lock_machinesta==0x00 && bt_lock_ropesta==0x01 && g_MBA32A_8buf_Down[0]==0x66)
- {
- menu.home=0x1000;//开(全部开启)
- }
- else if(bt_lock_ropesta==0x00 && bt_lock_machinesta==0x00 && g_MBA32A_8buf_Down[0]==0x66)
- {
- menu.home=0x1000;//开(电机打开,锁绳插入,可按键拔出)
- }
- else if(bt_lock_ropesta==0x01 && bt_lock_machinesta==0x00 && g_MBA32A_8buf_Down[0]==0x66)
- {
- menu.home=0x1000;//开(电机关闭,锁绳拔出)
- }
- }
- /**
- * @breaf 蓝牙开锁信息帧
- */
- void open_lock(Bt_frame btFrame) //蓝牙开锁信息帧
- {
- uint8_t header[2] = {0x66, 0x00};//Jl_lock帧头
- uint8_t open_CMD[] = {0x02, 0x0C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, \
- 0x00,0x00,0x00,0x00, 0x00};//开锁命令帧
-
- uint32_t serialNum = btFrame.serialNum;//流水号
- open_CMD[2]=(serialNum>>24) & 0xff;
- open_CMD[3]=(serialNum>>16) & 0xff;
- open_CMD[4]=(serialNum>>8) & 0xff;
- open_CMD[5]= serialNum & 0xff;
-
- uint32_t userid = btFrame.userId;//用户id
- open_CMD[6]=(userid>>24) & 0xff;
- open_CMD[7]=(userid>>16) & 0xff;
- open_CMD[8]=(userid>>8) & 0xff;
- open_CMD[9]= userid & 0xff;
-
- uint32_t ntime = btFrame.Ntime;//现在时间
- open_CMD[10]=(ntime>>24) & 0xff;
- open_CMD[11]=(ntime>>16) & 0xff;
- open_CMD[12]=(ntime>>8) & 0xff;
- open_CMD[13]= ntime & 0xff;
-
- open_CMD[14] = btFrame.Autoseal;//自动施封时间
-
- sendCombinedArray(header,sizeof(header),open_CMD,sizeof(open_CMD));
- Echo_BT(20);
- }
- /**
- * @breaf 蓝牙关锁信息帧
- */
- void close_lock(Bt_frame btFrame) //蓝牙关锁信息帧
- {
- uint8_t header[2] = {0x66, 0x00};//Jl_lock帧头
- uint8_t close_CMD[] = {0x03, 0x0B};//关锁命令帧
- sendCombinedArray(header,sizeof(header),close_CMD,sizeof(close_CMD));
- Echo_BT(20);
- }
- void Seal_Unseal(void)
- {
- if (menu.enter == 4)
- {
- if ((menu.current == 8 || menu.current == 9 || menu.current == 0xa || menu.current == 0xb))
- {
- if (menu.up == 1)
- {
- // 施封操作
- close_lock(btFrame);
- HAL_Delay(1000);
- menu.current=0;
- refresh=true;
-
- }
- else if (menu.up == 0)
- {
- // 解封操作
- open_lock(btFrame);
- HAL_Delay(1000);
- menu.current=0;
- refresh=true;
- }
- }
- }
- }
- void tips()
- {
-
- }
|