522 lines
16 KiB
C
522 lines
16 KiB
C
#include "cmd_queue.h"
|
||
#include <stdlib.h>
|
||
#include "atcom.h"
|
||
#include "eeprom.h"
|
||
|
||
|
||
char *pEnd;
|
||
char qrstr[50];//二维码字符串
|
||
|
||
uint8_t cmd_buffer[CMD_MAX_SIZE]; //指令缓存
|
||
static uint16_t current_screen_id = 0; //当前画面ID
|
||
uint8_t years,months,weeks,days,hours,minutes,sec;
|
||
|
||
|
||
bool refresh = true;
|
||
bool qrfresh = true;
|
||
float ao_out,addr_num;
|
||
int screen_tim_cnt = 0;
|
||
|
||
/*!
|
||
* \brief 消息处理流程
|
||
* \param msg 待处理消息
|
||
* \param size 消息长度
|
||
*/
|
||
void ProcessMessage( PCTRL_MSG msg, uint16_t size )
|
||
{
|
||
uint8 cmd_type = msg->cmd_type; //指令类型
|
||
uint8 ctrl_msg = msg->ctrl_msg; //消息的类型
|
||
uint8 control_type = msg->control_type; //控件类型
|
||
uint16 screen_id = PTR2U16(&msg->screen_id); //画面ID
|
||
uint16 control_id = PTR2U16(&msg->control_id); //控件ID
|
||
uint32 value = PTR2U32(msg->param); //数值
|
||
|
||
switch(cmd_type)
|
||
{
|
||
case NOTIFY_TOUCH_PRESS: //触摸屏按下
|
||
case NOTIFY_TOUCH_RELEASE: //触摸屏松开
|
||
NotifyTouchXY(cmd_buffer[1],PTR2U16(cmd_buffer+2),PTR2U16(cmd_buffer+4));
|
||
break;
|
||
case NOTIFY_WRITE_FLASH_OK: //写FLASH成功
|
||
NotifyWriteFlash(1);
|
||
break;
|
||
case NOTIFY_WRITE_FLASH_FAILD: //写FLASH失败
|
||
NotifyWriteFlash(0);
|
||
break;
|
||
case NOTIFY_READ_FLASH_OK: //读取FLASH成功
|
||
NotifyReadFlash(1,cmd_buffer+2,size-6); //去除帧头帧尾
|
||
break;
|
||
case NOTIFY_READ_FLASH_FAILD: //读取FLASH失败
|
||
NotifyReadFlash(0,0,0);
|
||
break;
|
||
case NOTIFY_READ_RTC: //读取RTC时间
|
||
NotifyReadRTC(cmd_buffer[2],cmd_buffer[3],cmd_buffer[4],cmd_buffer[5],cmd_buffer[6],cmd_buffer[7],cmd_buffer[8]);
|
||
break;
|
||
case NOTIFY_CONTROL:
|
||
{
|
||
if(ctrl_msg==MSG_GET_CURRENT_SCREEN) //画面ID变化通知
|
||
{
|
||
NotifyScreen(screen_id); //画面切换调动的函数
|
||
}
|
||
else
|
||
{
|
||
switch(control_type)
|
||
{
|
||
case kCtrlButton: //按钮控件
|
||
NotifyButton(screen_id,control_id,msg->param[1]);
|
||
break;
|
||
case kCtrlText: //文本控件
|
||
NotifyText(screen_id,control_id,msg->param);
|
||
break;
|
||
case kCtrlProgress: //进度条控件
|
||
NotifyProgress(screen_id,control_id,value);
|
||
break;
|
||
case kCtrlSlider: //滑动条控件
|
||
NotifySlider(screen_id,control_id,value);
|
||
break;
|
||
case kCtrlMeter: //仪表控件
|
||
NotifyMeter(screen_id,control_id,value);
|
||
break;
|
||
case kCtrlMenu: //菜单控件
|
||
NotifyMenu(screen_id,control_id,msg->param[0],msg->param[1]);
|
||
break;
|
||
case kCtrlSelector: //选择控件
|
||
NotifySelector(screen_id,control_id,msg->param[0]);
|
||
break;
|
||
case kCtrlRTC: //倒计时控件
|
||
NotifyTimer(screen_id,control_id);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* \brief 画面切换通知
|
||
* \details 当前画面改变时(或调用GetScreen),执行此函数
|
||
* \param screen_id 当前画面ID
|
||
*/
|
||
void NotifyScreen(uint16_t screen_id)
|
||
{
|
||
//TODO: 添加用户代码
|
||
current_screen_id = screen_id; //在工程配置中开启画面切换通知,记录当前画面ID
|
||
refresh = true;
|
||
qrfresh = true;
|
||
|
||
if(screen_id == 2)
|
||
{
|
||
screen_tim_cnt = 0;//加载页面计时
|
||
}
|
||
|
||
//二维码文本准备
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
|
||
if(screen_id == 3) { strcat(qrstr,"QMXCS&\0");}
|
||
if(screen_id == 4) { strcat(qrstr,"KGLCS&\0");}
|
||
// if(screen_id == 5) { strcat(qrstr,"模拟量测试&\0");}
|
||
if(screen_id == 6) { strcat(qrstr,"24VDCCS&\0");}
|
||
if(screen_id == 7) { strcat(qrstr,"QXCDZSJCS&\0");}
|
||
if(screen_id == 8) { strcat(qrstr,"JBWCCS&\0");}
|
||
if(screen_id == 9) { strcat(qrstr,"HCCS&\0");}
|
||
if(screen_id == 10) { strcat(qrstr,"SQCS&\0");}
|
||
if(screen_id == 11) { strcat(qrstr,"EDXCPCCS&\0");}
|
||
if(screen_id == 12) { strcat(qrstr,"2hLHCS&\0");}
|
||
if(screen_id == 13) { strcat(qrstr,"ALL&\0");}
|
||
}
|
||
|
||
/*!
|
||
* \brief 触摸坐标事件响应
|
||
* \param press 1按下触摸屏,3松开触摸屏
|
||
* \param x x坐标
|
||
* \param y y坐标
|
||
*/
|
||
void NotifyTouchXY(uint8_t press,uint16_t x,uint16_t y)
|
||
{
|
||
//TODO: 添加用户代码
|
||
}
|
||
|
||
int pow_grade = 1;
|
||
int last_connect_state = 5;
|
||
/*!
|
||
* \brief 更新数据
|
||
*/
|
||
void UpdateUI(void)
|
||
{
|
||
//文本设置和显示 定时50ms刷新一次
|
||
if(current_screen_id == 0)
|
||
{
|
||
SetTextFloat(0,13,HoldReg[37],0,0);//设备ID
|
||
}
|
||
|
||
if(current_screen_id == 1)
|
||
{
|
||
if(refresh)
|
||
{
|
||
delay_us(10);
|
||
SetTextValue(1,8,(uchar *)wifiname);//wifi名称
|
||
SetTextValue(1,9,(uchar *)wifipassword);//wifi密码
|
||
SetTextValue(1,10,(uchar *)myip);//本地IP
|
||
SetTextValue(1,11,(uchar *)hisip);//目标IP
|
||
SetTextValue(1,12,(uchar *)port);//端口号
|
||
|
||
refresh = false;
|
||
}
|
||
if(!(last_connect_state == connect_state))
|
||
{
|
||
last_connect_state = connect_state;
|
||
refresh = true;
|
||
switch(last_connect_state)
|
||
{
|
||
case 0: SetTextValue(1,2,(uchar *)"wifi未连接"); break;
|
||
case 1: SetTextValue(1,2,(uchar *)"wifi已连接,未获取IP"); break;
|
||
case 2: SetTextValue(1,2,(uchar *)"wifi已连接,已获取IP"); break;
|
||
case 3: SetTextValue(1,2,(uchar *)"wifi正在连接"); break;
|
||
case 4: SetTextValue(1,2,(uchar *)"wifi断开"); break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if(current_screen_id == 2)
|
||
{
|
||
|
||
if(screen_tim_cnt < 360)
|
||
{
|
||
screen_tim_cnt += 2;
|
||
SetProgressValue(2,2,screen_tim_cnt);//环形进度条
|
||
SetControlVisiable(2,3,0);//隐藏确认按钮
|
||
}
|
||
else if(screen_tim_cnt < 500)
|
||
{
|
||
SetProgressValue(2,2,360);//环形进度条
|
||
screen_tim_cnt = 600;
|
||
}
|
||
else
|
||
{
|
||
SetProgressValue(2,2,360);//环形进度条
|
||
}
|
||
if(wifi_set_state == 0)
|
||
{
|
||
refresh = true;
|
||
screen_tim_cnt = 600;
|
||
SetControlVisiable(2,3,1);//显示确认按钮
|
||
}
|
||
}
|
||
if(current_screen_id == 4)//开关量输入输出
|
||
{
|
||
AnimationPlayFrame(4,3,(DisState[0]>>0)&0x01);
|
||
AnimationPlayFrame(4,4,(DisState[0]>>1)&0x01);
|
||
|
||
SetButtonValue(4,5,(CoilState[0]>>0)&0x01);
|
||
SetButtonValue(4,6,(CoilState[0]>>1)&0x01);
|
||
SetButtonValue(4,7,(CoilState[0]>>2)&0x01);
|
||
}
|
||
if(current_screen_id == 5)
|
||
{
|
||
SetTextFloat(5,4,InputReg[12]/1000.0,3,1);//反馈电流
|
||
SetTextFloat(5,13,InputReg[1]/1000.0,3,1);//AI1输入
|
||
SetMeterValue(5,3,InputReg[1]);//AI输入电流仪表显示
|
||
}
|
||
if(current_screen_id == 6)
|
||
{
|
||
SetTextInt32(6,13,InputReg[0],0,0);//电压输入
|
||
SetMeterValue(6,5,InputReg[0]);//电压输入仪表显示
|
||
}
|
||
if(current_screen_id != 2)
|
||
AnimationPlayFrame(current_screen_id,66,HoldReg[25] - 1);
|
||
|
||
//二维码文本添加
|
||
if(qrfresh)
|
||
{
|
||
if((current_screen_id > 2) && (current_screen_id < 14))
|
||
SetTextValue(current_screen_id,2,(uchar*)qrstr);
|
||
qrfresh = false;
|
||
}
|
||
|
||
}
|
||
|
||
/*!
|
||
* \brief 按钮控件通知
|
||
* \details 当按钮状态改变(或调用GetControlValue)时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param state 按钮状态:0弹起,1按下
|
||
*/
|
||
void NotifyButton(uint16_t screen_id, uint16_t control_id, uint8_t state)
|
||
{
|
||
if(screen_id == 1)//网络配置界面
|
||
{
|
||
if(control_id == 13)//重连wifi
|
||
wifi_set_state = 1;
|
||
if(control_id == 14)//重连服务器
|
||
wifi_set_state = 12;
|
||
|
||
}
|
||
if(screen_id == 3)
|
||
{
|
||
if(control_id == 7)
|
||
{
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"YQQMXCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
if(control_id == 8)
|
||
{
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"DQQQMXCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
if(control_id == 9)
|
||
{
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"JXQIPFHCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
}
|
||
if(screen_id == 4)//开关量输出
|
||
{
|
||
if(control_id ==5)//开
|
||
{
|
||
if(state)
|
||
{
|
||
CoilState[0] = CoilState[0] | 0x01;
|
||
CoilState[0] = CoilState[0] & 0xfd;
|
||
CoilState[0] = CoilState[0] & 0xfb;
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"QKCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
else
|
||
CoilState[0] = CoilState[0] & 0xfe;
|
||
}
|
||
if(control_id ==6)//关
|
||
{
|
||
if(state)
|
||
{
|
||
CoilState[0] = CoilState[0] | 0x02;
|
||
CoilState[0] = CoilState[0] & 0xfb;
|
||
CoilState[0] = CoilState[0] & 0xfe;
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"QGCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
else
|
||
CoilState[0] = CoilState[0] & 0xfd;
|
||
}
|
||
if(control_id ==7)//ESD
|
||
{
|
||
if(state)
|
||
{
|
||
CoilState[0] = CoilState[0] | 0x04;
|
||
CoilState[0] = CoilState[0] & 0xfe;
|
||
CoilState[0] = CoilState[0] & 0xfd;
|
||
sprintf(qrstr,"%d",HoldReg[37]);
|
||
strcat(qrstr,"&");
|
||
strcat(qrstr,"ESDCS&\0");
|
||
qrfresh = 1;
|
||
}
|
||
else
|
||
CoilState[0] = CoilState[0] & 0xfb;
|
||
}
|
||
|
||
}
|
||
if(screen_id == 5)
|
||
{
|
||
if(control_id > 6 && control_id < 12)
|
||
{
|
||
ao_out = (control_id - 6) * 4;//电流输出快捷键
|
||
SetSliderValue(5,6,ao_out * 1000);//滑块跟随变化
|
||
SetTextFloat(5,5,ao_out,3,1);//输入值跟随变化
|
||
HoldReg[0] = ao_out * 1000;
|
||
}
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* \brief 文本控件通知
|
||
* \details 当文本通过键盘更新(或调用GetControlValue)时,执行此函数
|
||
* \details 文本控件的内容以字符串形式下发到MCU,如果文本控件内容是浮点值,
|
||
* \details 则需要在此函数中将下发字符串重新转回浮点值。
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param str 文本控件内容
|
||
*/
|
||
void NotifyText(uint16_t screen_id, uint16_t control_id, uint8_t *str)
|
||
{
|
||
if(screen_id == 0)
|
||
{
|
||
HoldReg[37] = strtof((const char *)str,&pEnd);//设备ID
|
||
}
|
||
if(screen_id == 1)
|
||
{
|
||
if(control_id == 8)
|
||
{
|
||
strcpy(wifiname,(const char*)str);//设置wifi名称
|
||
ee_writebytes(str,100,strlen((const char*)str)+1);//保存数据,+1为了将末尾\0同时保存
|
||
}
|
||
if(control_id == 9)
|
||
{
|
||
strcpy(wifipassword,(const char*)str);//设置wifi密码
|
||
ee_writebytes(str,200,strlen((const char*)str)+1);//保存数据,+1为了将末尾\0同时保存
|
||
}
|
||
if(control_id == 10)
|
||
{
|
||
|
||
}
|
||
if(control_id == 11)
|
||
{
|
||
strcpy(hisip,(const char*)str);//设置目标IP
|
||
ee_writebytes(str,300,strlen((const char*)str)+1);//保存数据,+1为了将末尾\0同时保存
|
||
}
|
||
if(control_id == 12)
|
||
{
|
||
strcpy(port,(const char*)str);//设置端口号
|
||
ee_writebytes(str,400,strlen((const char*)str)+1);//保存数据,+1为了将末尾\0同时保存
|
||
}
|
||
}
|
||
if(screen_id == 5)
|
||
{
|
||
if(control_id == 5)
|
||
{
|
||
HoldReg[0] = strtof((const char *)str,&pEnd) * 1000;//文本输入控制AO1电流输出
|
||
SetSliderValue(5,6,HoldReg[0]);//滑块跟随变化
|
||
}
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* \brief 进度条控件通知
|
||
* \details 调用GetControlValue时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param value 值
|
||
*/
|
||
void NotifyProgress(uint16_t screen_id, uint16_t control_id, uint32_t value)
|
||
{
|
||
//TODO: 添加用户代码
|
||
}
|
||
|
||
/*!
|
||
* \brief 滑动条控件通知
|
||
* \details 当滑动条改变(或调用GetControlValue)时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param value 值
|
||
*/
|
||
void NotifySlider(uint16_t screen_id, uint16_t control_id, uint32_t value)
|
||
{
|
||
if(screen_id == 5)
|
||
{
|
||
if(control_id == 6)
|
||
{
|
||
HoldReg[0] = value;//滑块控制AO1输出电流
|
||
SetTextFloat(5,5,(float)(value / 1000.0),3,1);//文本输入跟随变化
|
||
}
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* \brief 仪表控件通知
|
||
* \details 调用GetControlValue时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param value 值
|
||
*/
|
||
void NotifyMeter(uint16_t screen_id, uint16_t control_id, uint32_t value)
|
||
{
|
||
//TODO: 添加用户代码
|
||
}
|
||
|
||
/*!
|
||
* \brief 菜单控件通知
|
||
* \details 当菜单项按下或松开时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param item 菜单项索引
|
||
* \param state 按钮状态:0松开,1按下
|
||
*/
|
||
void NotifyMenu(uint16_t screen_id, uint16_t control_id, uint8_t item, uint8_t state)
|
||
{
|
||
if(screen_id == 4)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
/*!
|
||
* \brief 选择控件通知
|
||
* \details 当选择控件变化时,执行此函数
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
* \param item 当前选项
|
||
*/
|
||
void NotifySelector(uint16_t screen_id, uint16_t control_id, uint8_t item)
|
||
{
|
||
|
||
}
|
||
|
||
/*!
|
||
* \brief 定时器超时通知处理
|
||
* \param screen_id 画面ID
|
||
* \param control_id 控件ID
|
||
*/
|
||
void NotifyTimer(uint16_t screen_id, uint16_t control_id)
|
||
{
|
||
|
||
}
|
||
|
||
/*!
|
||
* \brief 读取用户FLASH状态返回
|
||
* \param status 0失败,1成功
|
||
* \param _data 返回数据
|
||
* \param length 数据长度
|
||
*/
|
||
void NotifyReadFlash(uint8_t status,uint8_t *_data,uint16_t length)
|
||
{
|
||
//TODO: 添加用户代码
|
||
}
|
||
|
||
/*!
|
||
* \brief 写用户FLASH状态返回
|
||
* \param status 0失败,1成功
|
||
*/
|
||
void NotifyWriteFlash(uint8_t status)
|
||
{
|
||
//TODO: 添加用户代码
|
||
}
|
||
|
||
/*!
|
||
* \brief 读取RTC时间,注意返回的是BCD码
|
||
* \param year 年(BCD)
|
||
* \param month 月(BCD)
|
||
* \param week 星期(BCD)
|
||
* \param day 日(BCD)
|
||
* \param hour 时(BCD)
|
||
* \param minute 分(BCD)
|
||
* \param second 秒(BCD)
|
||
*/
|
||
void NotifyReadRTC(uint8_t year,uint8_t month,uint8_t week,uint8_t day,uint8_t hour,uint8_t minute,uint8_t second)
|
||
{
|
||
sec =(0xff & (second>>4))*10 +(0xf & second); //BCD码转十进制
|
||
years =(0xff & (year>>4))*10 +(0xf & year);
|
||
months =(0xff & (month>>4))*10 +(0xf & month);
|
||
weeks =(0xff & (week>>4))*10 +(0xf & week);
|
||
days =(0xff & (day>>4))*10 +(0xf & day);
|
||
hours =(0xff & (hour>>4))*10 +(0xf & hour);
|
||
minutes =(0xff & (minute>>4))*10 +(0xf & minute);
|
||
}
|
||
|
||
|
||
|