/* * Copyright (c) 2006-2020, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-05-26 Administrator the first version */ #include #include "hmi.h" #include #include #include #include #include #include #include "stdarg.h" #include "string.h" #include "sht30_collect.h" #include "beep.h" #include "easyflash.h" #include "sound.h" #include "ymodem.h" #define DBG_TAG "hmi.c" #define DBG_LVL LOG_LVL_INFO #include #include "dtu_uart2.h" #include #define HMI_THREAD_PRIORITY 24 //线程优先级 #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小 #define HMI_THREAD_TIMESLICE 10 //时间片 #define CONTROL_MOTOR_PRIORITY 18 #define CONTROL_MOTOR_STACK_SIZE 2048 #define CONTROL_MOTOR_TIMESLICE 5 static char msg_pool[256]; static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1]; void list_mem(void); /**/ Device_Status_typedef device_status; time_t time(time_t *t); /* 串口接收消息结构*/ struct rx_msg { rt_device_t dev; rt_size_t size; }; struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/ static rt_thread_t hmi1 = RT_NULL; static rt_thread_t ctr_motor = RT_NULL; rt_device_t serial; /*串口设备句柄*/ /* 消息队列控制块 */ static struct rt_messagequeue rx_mq;//接收数据消息队列 static void HMI_Decode(char *cmd,uint8_t cmd_len); static rt_err_t uart_input(rt_device_t dev, rt_size_t size); static int HMI_uart4_init(void) { serial = rt_device_find(UART_NAME); /*修改串口配置参数*/ config.baud_rate = BAUD_RATE_115200; //修改波特率9600 config.data_bits = DATA_BITS_8; //数据位8 config.stop_bits = STOP_BITS_1; //停止位1 config.bufsz = 512; //缓冲区128字节 config.parity = PARITY_NONE; //无奇偶校验 /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */ rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config); // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */ //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); /* 以 DMA 接收及轮询发送模式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); return RT_EOK; } /*串口屏写数据*/ void HMI_WRITE(char *fmt, ...) { va_list ap; //末尾需要拼接的3个ff const char hmi_hex[3]={0xff,0xff,0xff}; static char str[MAX_SIZE]; memset(str,'\0',sizeof(str)); rt_size_t str_length; va_start(ap,fmt); str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap); if (str_length > MAX_SIZE - 1) str_length = MAX_SIZE - 1; rt_device_write(serial, 0, str, str_length); va_end(ap); rt_device_write(serial, 0,hmi_hex,3); } /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { struct rx_msg msg; rt_err_t result; msg.dev = dev; msg.size = size; result = rt_mq_send(&rx_mq, &msg, sizeof(msg)); if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full!\n"); } return result; } /*入口函数*/ static void Hmi_thread_entry(void *parameter) { struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length; while(1) { rt_memset(&msg, 0, sizeof(msg)); /* 从消息队列中读取消息*/ result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50); if (result == RT_EOK) { rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size); rx_buffer[rx_length] = '\0'; rt_kprintf("%s\n",rx_buffer); //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾 HMI_Decode(rx_buffer,rx_length); } /*********************************************************************************************************************/ //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str); } } /*******************************************************************************************/ /***************************** 解析从屏幕收到的数据 begin **********************************/ static void HMI_Decode(char *cmd,uint8_t cmd_len) { char *var; if(device_status.refresh_num==0) device_status.refresh_num = 10; if(strstr(rx_buffer,"getFullData") != NULL){ time_t now = time(RT_NULL); static struct tm tm; gmtime_r(&now, &tm); char *readinfo = makeJson_ReadInfo(); if(device_status.refresh_num==5) { device_status.refresh_num = 15; DTU_Write("%s",readinfo); list_mem(); device_status.hmi_fresh = 1; rt_thread_delay(100); } //在解析getinfo中清除超时 if(device_status.refresh_timeout==30) { rt_sprintf(device_status.sim,"1"); rt_sprintf(device_status.net,"1"); rt_sprintf(device_status.csq,"0"); device_status.refresh_timeout=0; } rt_free(readinfo); device_status.refresh_num--; device_status.refresh_timeout++; HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid); //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1); HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg2); //更新固件版本号 HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver); //更新固件编译日期 HMI_WRITE("debug.t4.txt=\"%s\"",CompileTime); // HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl); HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile); //更新dtu/rtu版本 HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g); //更新时间 HMI_WRITE("menu.rtc.txt=\"%02d:%02d:%02d\"",tm.tm_hour,tm.tm_min,tm.tm_sec); //HMI_WRITE("workSta.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour); HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min); HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec); //更新电压 HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage); //更新电流 HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power); //更新温湿度数据 HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity)); HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture)); //更新电池状态 HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status); //更新上仓门状态 HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status); //更新下仓门状态 HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status); //更新排水/落虫 HMI_WRITE("workSta.tds.val=%d",device_status.clear_status); //更新工作状态 HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status); //更新温控状态 HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status); //更新雨控状态 HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status); //更新光控状态 HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status); //更新CSQ HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq); HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq); //更新收集时间 HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time); //更新加热时间 HMI_WRITE("workSta.heatTime.val=%d",device_status.heat_time_min); HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime); // HMI_WRITE("sysSet.vol.val=%d",device_status.vol); //更新加热温度 HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem); HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue); //更新加热状态 HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status); //更新熄屏时间 HMI_WRITE("thsp=%d",device_status.thsp); //更新密码 static char pwd_char[10]; //整型转字符串 itoa(device_status.passWord,pwd_char,10); HMI_WRITE("inputPasswd.pwdData.txt=\"%s\"",pwd_char); //更新时间设置中的参数 //更新定时模式 HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch); //更新光控定时时长 HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar); //更新时控开始 HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar); //更新时控结束 HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar); //SIM卡状态 HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim)); //ID HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei); HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei); //数据上传间隔 HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim); //语音音量 HMI_WRITE("sysSet.vol.val=%d",device_status.vol); //更新联网状态 if(atoi(device_status.net)==1) { HMI_WRITE("menu.net.val=1"); HMI_WRITE("workSta.net.val=1"); } else { HMI_WRITE("workSta.net.val=0"); HMI_WRITE("menu.net.val=0"); } if(atoi(device_status.csq)==0) { HMI_WRITE("menu.net.val=1"); HMI_WRITE("workSta.net.val=2"); } } else if (strstr(rx_buffer,"getClockData")!=NULL) { //更新定时模式 HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch); //更新光控定时时长 HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar); //更新时控开始 HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar); //更新时控结束 HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar); } else if (strstr(rx_buffer,"button")!=NULL) { rt_kprintf("按键!\n"); DTU_Write("button"); } //拍照 else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) { device_status.photo_ws = 1; } //清扫 else if (strstr(rx_buffer,"clear")!=NULL) { rt_kprintf("\n清扫!\n"); } //震动 else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) { rt_kprintf("\n震动!\n"); } //打开补光 else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) { rt_kprintf("\n打开补光!!!\n"); ctr_led(ON); } //关闭补光 else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) { rt_kprintf("\n关闭补光!!!\n"); ctr_led(OFF); } //打开灯管 else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL) { rt_kprintf("Open Lamp!\n"); ctr_lamp(ON); } //关闭灯管 else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL) { rt_kprintf("Close Lamp!\n"); ctr_lamp(OFF); } //关闭加热 else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) { device_status.heat_status = 0; ctr_Heat(OFF); rt_kprintf("Close Heat!\n"); } //打开加热 else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) { device_status.heat_status = 1; ctr_Heat(ON); rt_kprintf("Open Heat!\n"); } //打开上仓门 else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){ rt_kprintf("Open UP!\n"); device_status.ctr_up_motor = 1; } //关闭上仓门 else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){ rt_kprintf("CLOSE UP!\n"); device_status.ctr_up_motor = 2; } //打开下仓门 else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){ device_status.ctr_down_motor = 1; rt_kprintf("Open down!\n"); } //关闭下仓门 else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){ device_status.ctr_down_motor = 2; rt_kprintf("CLOSE down!\n"); } //定时设置 else if (strstr(rx_buffer,"saveClockSet=")!=NULL) { char sun_str[3],start_str[3],stop_str[3],timeswitch_str[3]; rt_kprintf("时间设置!\n"); var=strstr(rx_buffer,"="); var++; strncpy(sun_str,var,2); if(atoi(sun_str) > 0 && atoi(sun_str) <12) { device_status.sunBar=atoi(sun_str); time_t now; time(&now); static struct tm nowstamp; nowstamp =*localtime(&now); nowstamp.tm_hour = nowstamp.tm_hour - 8; static rt_uint32_t nowstamp_num = 0; nowstamp_num = mktime(&nowstamp); device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num; ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); } else { rt_kprintf("参数设置异常!!!"); } //时控开始时间 strncpy(start_str,var+2,2); if(atoi(start_str) >= 0 && atoi(start_str) < 24) { device_status.startBar=atoi(start_str); ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); } else { rt_kprintf("参数设置异常!!!"); } // strncpy(stop_str,var+4,2); if(atoi(stop_str) >= 0&& atoi(stop_str) < 24) { device_status.stopBar=atoi(stop_str); ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); } else { rt_kprintf("参数设置异常!!!"); } // strncpy(timeswitch_str,var+6,2); if(atoi(timeswitch_str) >= 0 && atoi(timeswitch_str) <= 1) { device_status.timeSwitch=atoi(timeswitch_str); ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); } else { rt_kprintf("参数设置异常!!!"); } MA_save(); rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar); } else if (strstr(rx_buffer,"saveSystemSet")!=NULL) { char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5]; rt_kprintf("系统设置!\n"); var = strstr(rx_buffer,"="); var ++; strncpy(clt_time_str,var,2); if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30) { device_status.clt_time = atoi(clt_time_str); //收集时间 ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); } strncpy(heattime_str,var+2,2); if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30) { rt_kprintf("加热时间:%d\n",atoi(heattime_str)); device_status.heattime = atoi(heattime_str); //加热时间 ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); } strncpy(heatvale_str,var+4,3); if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130) { device_status.heatvalue=atoi(heatvale_str); ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); } strncpy(vol_str,var+7,3); if(atoi(vol_str)>0&&atoi(vol_str)<8) { device_status.vol = atoi(vol_str); //音量 DTU_Write("volume="); } strncpy(thsp_str,var+10,4); if(atoi(thsp_str)>0&&atoi(thsp_str)<3600) { device_status.thsp = atoi(thsp_str); //熄屏时间 } else { rt_kprintf("参数设置失败!!!\n"); } //MA_save(); rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp); } else if (strstr(rx_buffer,"rtc_h=")!=NULL) { char rtc_h[2]; var = strstr(rx_buffer,"="); var++; strncpy(rtc_h,var,2); if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24) { set_time(atoi(rtc_h), 99, 99); } } else if (strstr(rx_buffer,"rtc_m=")!=NULL) { char rtc_m[2]; var = strstr(rx_buffer,"="); var++; strncpy(rtc_m,var,2); if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24) { set_time(99, atoi(rtc_m), 99); } } else if (strstr(rx_buffer,"rtc_s=")!=NULL) { char rtc_s[2]; var = strstr(rx_buffer,"="); var++; strncpy(rtc_s,var,2); if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24) { set_time(99, 99, atoi(rtc_s)); } } else if(strstr(rx_buffer,"saveUploadSet=")!=NULL) { static char dattim_str[3]; rt_kprintf("上传间隔设置!\n"); var = strstr(rx_buffer,"="); var ++; strncpy(dattim_str,var,2); if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120) { device_status.dattim =atoi(dattim_str); //数据上传间隔 MA_save(); } else { rt_kprintf("设置失败!!!\n"); } rt_kprintf("数据上传设置:%d\n",device_status.dattim); } //光控测试 else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL) { rt_kprintf("光控测试!!!"); ctr_lamp(ON); device_status.sun_test = 1; } //恢复出厂设置电池 else if (strstr(rx_buffer,"resetdcDefault")!=NULL) { ResetParamDefault_DC(); MA_save(); } //恢复出厂设置-交流 else if (strstr(rx_buffer,"resetDefault")!=NULL) { ResetParamDefault(); MA_save(); } //获取SIM卡信息 else if(strstr(rx_buffer,"getSim")!=NULL) { //http://sim.openluat.com/api/wechat/openapidoc DTU_Write("getsiminfo"); } else if (strstr(rx_buffer,"DebugEnable")!=NULL) { Debug_log_ON(); } else if (strstr(rx_buffer,"DebugDisable")!=NULL) { Debug_log_OFF(); } else if (strstr(rx_buffer,"SwversionV1")!=NULL) { device_status.version_type = 1; sprintf(device_status.driver, "%s",VERSION_ZP); ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型 MA_save(); } else if (strstr(rx_buffer,"SwversionV1")!=NULL) { device_status.version_type = 4; sprintf(device_status.driver, "%s",VERSION_HK); ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型 MA_save(); } else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL) { HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power); } else if (strstr(rx_buffer,"checkupdate")!=NULL) { time_t now = time(RT_NULL); static struct tm tm; gmtime_r(&now, &tm); HMI_WRITE("hardcheck.tip_rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); HMI_WRITE("hardcheck.p0.pic=%d",70); //RTC时间判断,如果当前年份小于2020,提示需要设置时间 if(tm.tm_year+1900<2020) { rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); HMI_WRITE("hardcheck.tip_rtc.txt=%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); } } //进入bootloader模式 else if (strstr(rx_buffer,"bootloader")!=NULL) { device_status.stop_in_bootloader = 1; ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader)); rt_hw_cpu_reset(); } //软件重启 else if (strstr(rx_buffer,"reboot")!=NULL) { rt_hw_cpu_reset(); } } //入口函数-电机控制及 static void Ctr_Mortor_entry(void* parameter) { rt_uint32_t limit_current = 2000; //推杆电机限制电流1A static rt_uint16_t num_down_open = 1500; // static rt_uint16_t num_down_close = 1500; // static rt_uint16_t photo_led_sec = 300; //45S //上限位 rt_pin_mode(CTR_UPO_LIMIT,PIN_MODE_INPUT); rt_pin_mode(CTR_UPC_LIMIT,PIN_MODE_INPUT); //下限位 rt_pin_mode(CTR_DWO_LIMIT,PIN_MODE_INPUT); rt_pin_mode(CTR_DWC_LIMIT,PIN_MODE_INPUT); while(1) { if(device_status.ctr_up_motor==1) { //打开上仓门 if(rt_pin_read(CTR_UPO_LIMIT)==1) { ctr_cangmen_up(OPEN); } else { ctr_cangmen_up(OFF); } //如果未工作且上仓门电机电流大于1A,立即停止,记录到Flash,上报预警 if(atoi(device_status.power)>limit_current) { ctr_cangmen_up(OFF); rt_kprintf("*************************上仓门电机打开堵转!!!**********************%d\n",atoi(device_status.power)); } } else if(device_status.ctr_up_motor==2) { if(rt_pin_read(CTR_UPC_LIMIT)==1) { ctr_cangmen_up(CLOSE); } else { ctr_cangmen_up(OFF); } if(atoi(device_status.power)>limit_current) { ctr_cangmen_up(OFF); rt_kprintf("************************上仓门电机关闭堵转!!!**********************%d\n",atoi(device_status.power)); } } if (device_status.ctr_down_motor == 1) { if(rt_pin_read(CTR_DWO_LIMIT)==1) { ctr_cangmen_down(OPEN); } else { ctr_cangmen_down(OFF); } num_down_open--; if(atoi(device_status.power)>limit_current) { ctr_cangmen_down(OFF); num_down_open = 1500; rt_kprintf("*************************下仓门电机打开堵转!!!**********************%d\n",atoi(device_status.power)); } if (num_down_open == 0) { ctr_cangmen_down(OFF); num_down_open = 1500; } } else if (device_status.ctr_down_motor == 2) { if(rt_pin_read(CTR_DWC_LIMIT)==1) { ctr_cangmen_down(CLOSE); } else { ctr_cangmen_down(OFF); } num_down_close--; if(atoi(device_status.power)>limit_current) { ctr_cangmen_down(OFF); num_down_close = 1500; rt_kprintf("*************************下仓门电机关闭堵转!!!**********************%d\n",atoi(device_status.power)); } if (num_down_close == 0) { ctr_cangmen_down(OFF); num_down_close = 1500; } } if(device_status.sun_test==1) { SunTest(); } if(device_status.photo_ws==1) { //打开补光灯 if(photo_led_sec==100) { ctr_led(ON); DTU_Write("takephoto.mp3"); //延时 rt_thread_delay(1000); rt_kprintf("\n测试拍照!\n"); Hk_Take_io(); } photo_led_sec--; if(photo_led_sec==0) { ctr_led(OFF); photo_led_sec = 100; device_status.photo_ws = 0; HMI_WRITE("sysTest.switchPhoto.val=0"); HMI_WRITE("sysTest.switchFill.val=0"); } } rt_thread_delay(20); //rt_kprintf("action num is:%d%d%d",num_up_open,num_up_close,num_down_open,num_down_close,num_clear); } } /*屏幕写数据*/ int Hmi_Fresh(void) { //初始化串口4 rt_err_t ret = RT_EOK; /* 初始化消息队列 */ rt_mq_init(&rx_mq, "rx_mq", msg_pool, /* 存放消息的缓冲区 */ sizeof(struct rx_msg), /* 一条消息的最大长度 */ sizeof(msg_pool), /* 存放消息的缓冲区大小 */ RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ /* 设置接收回调函数 */ rt_device_set_rx_indicate(serial, uart_input); /* 创建线程*/ hmi1 = rt_thread_create("Hmi_Fresh_thread", Hmi_thread_entry, RT_NULL, HMI_THREAD_STACK_SIZE, HMI_THREAD_PRIORITY, HMI_THREAD_TIMESLICE); /*打开上仓门的线程*/ ctr_motor = rt_thread_create("Ctr motor", Ctr_Mortor_entry, RT_NULL, CONTROL_MOTOR_STACK_SIZE, CONTROL_MOTOR_PRIORITY, CONTROL_MOTOR_TIMESLICE); if(ctr_motor != RT_NULL) { rt_thread_startup(ctr_motor); } /* 如果获得线程控制块,启动这个线程 */ if (hmi1 != RT_NULL) { rt_thread_startup(hmi1); } else { ret = RT_ERROR; } return ret; } void Check_ParamConfig(void) { time_t now = time(RT_NULL); static struct tm tm; gmtime_r(&now, &tm); if(easyflash_init()==EF_NO_ERR) //初始化成功 { ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值 ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值 ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序 device_status.rebootcnt++; ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt)); rt_kprintf("REBOOT_TIME:%d-%d-%d\n",device_status.rebootcnt,device_status.iSave,device_status.update_flag); } else { rt_kprintf("easyflash init ERRPR!!!\n"); } //保存默认屏幕配置 if(device_status.iSave!=FLASH_FLAG) { device_status.iSave=FLASH_FLAG; device_status.clt_time = 20; //收集时间 device_status.heattime = 20; //加热时间 device_status.heatvalue=110;//加热温度 device_status.thsp = 300; //默认熄屏时间5分钟 device_status.sunBar = 6; //光控定时时长 device_status.startBar = 20; //定时开始时间 device_status.stopBar = 0; //定时结束时间 device_status.tph = 70; //高温保护阈值 device_status.tpl = 5; //低温保护阈值 device_status.shake_ws = 1; //震动开关,默认开 device_status.shake_sec = 1; //震动时间,s device_status.vol = 7; //音量 device_status.timeSwitch = 0; //工作模式 device_status.passWord = 8888; //屏幕密码 device_status.work_done = 0; //工作完成标志位 device_status.dattim = 20; //数据上传间隔 device_status.ds = 1; //设备状态 device_status.version_type = 0; //版本类型 ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); // ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds)); ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关 ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间 ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp)); ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord)); ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间 ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型 if(device_status.version_type==1) { sprintf(device_status.driver, "%s",VERSION_ZP); } else if (device_status.version_type==4) { sprintf(device_status.driver, "%s",VERSION_HK); } rt_kprintf("SOFTWARE VERSION:%s\n",device_status.driver); rt_kprintf("set flash paramconfis:\n"); rt_kprintf("stime:%d\n",device_status.iSave); rt_kprintf("stime:%d\n",device_status.clt_time); rt_kprintf("startBar:%d\n",device_status.startBar); rt_kprintf("stopBar:%d\n",device_status.stopBar); rt_kprintf("timeSwitch:%d\n",device_status.timeSwitch); rt_kprintf("sunBar:%d\n",device_status.sunBar); rt_kprintf("jtime:%d\n",device_status.heattime); rt_kprintf("jheat:%d\n",device_status.heatvalue); rt_kprintf("thsp:%d\n", device_status.thsp); rt_kprintf("密码:%d\n",device_status.passWord); }//从easyflash读取参数配置 else if (device_status.iSave==FLASH_FLAG) { ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔 ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间 ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间 ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间 ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式 ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间 ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间 ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度 ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关 ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间 ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护 ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护 ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔 ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间 ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码 ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID ef_get_env_blob("ds",&device_status.ds, sizeof(device_status.ds),NULL); //开关机状态 ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型 ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH device_status.light_over_tim = device_status.sunBar * 3600; //光控计数 device_status.work_done = 0; //工作完成标志位 if(device_status.dattim==0) { device_status.dattim = 20; } if(device_status.version_type!=3) { app_partion_backup(); //固件备份至df_area device_status.version_type = 3; ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); } device_status.ds=1; rt_sprintf(device_status.driver, "GK3.0"); rt_kprintf("SOFTWARE VERSION:%s-imei:%s--iccid:%s\n",device_status.driver,device_status.imei,device_status.iccid); rt_kprintf("read flash paramconfis:%d-%s-%s\r\n",device_status.version_type,device_status.driver_bl,device_status.driver_compile); rt_kprintf("开机次数:%d\n",device_status.rebootcnt); rt_kprintf("收集时间:%d\n",device_status.clt_time); rt_kprintf("定时开始时间:%d\n",device_status.startBar); rt_kprintf("定时结束时间:%d\n",device_status.stopBar); rt_kprintf("timeSwitch:%d\n",device_status.timeSwitch); rt_kprintf("光控定时时长:%d\n",device_status.sunBar); rt_kprintf("收集时间:%d\n",device_status.heattime); rt_kprintf("加热温度:%d\n",device_status.heatvalue); rt_kprintf("震动开关:%d\n",device_status.shake_ws); rt_kprintf("震动时间:%d\n",device_status.shake_sec); rt_kprintf("高温保护:%d\n",device_status.tph); rt_kprintf("低温保护:%d\n",device_status.tpl); rt_kprintf("thsp:%d\n", device_status.thsp); rt_kprintf("密码:%d\n",device_status.passWord); rt_kprintf("数据上传间隔:%d\n",device_status.dattim); } rt_kprintf("当前时间:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控 if(device_status.update_flag==1) { device_status.update_flag = 0; ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag)); ymodem_ota(1,0); } else { device_status.update_flag = 0; //初始化联网状态 rt_sprintf(device_status.net,"1"); rt_sprintf(device_status.csq,"0"); rt_sprintf(device_status.sim,"1"); ctr_router(ON); Hmi_Fresh(); dtu_data_play(); sht30_collect(); mb_master_samlpe(); uint16_t connect_num=0; //等待联网, while(atoi(device_status.net)==1) { char *readinfo = makeJson_ReadInfo(); DTU_Write("%s",readinfo); rt_free(readinfo); rt_thread_delay(1000); connect_num++; rt_kprintf("联网中......%d\n",connect_num); //超时时间为3分钟 if(connect_num==30) { device_status.timeSwitch = 0; //保存光控变量 ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); rt_kprintf("Time not set:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); set_date(2021, 1, 1); set_time(0, 0, 0); break; } if(tm.tm_year>=2021) break; } DTU_Write("%s",makeJson_Warn("turnon", "device_stm32")); } } void ResetParamDefault(void) { device_status.clt_time = 20; //收集时间 device_status.dattim = 20; //数据上传间隔 device_status.startBar = 20; //定时开始时间 device_status.stopBar = 5; //定时结束时间 device_status.heattime = 20; //加热时间 device_status.shake_ws = 1; //针对开关 device_status.shake_sec = 1; //震动时间1-100ms device_status.sunBar = 8; //光控定时时长 device_status.tph = 70; //高温保护阈值 device_status.tpl = 5; //低温保护阈值 device_status.timeSwitch = 0; //定时模式 device_status.heatvalue=110;//加热温度 device_status.ds = 1; //设备状态开机 device_status.thsp = 300; device_status.passWord = 8888; ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds)); ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关 ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间 ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp)); ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord)); DTU_Write("reset.mp3"); } void ResetParamDefault_DC(void) { device_status.clt_time = 20; //收集时间 device_status.dattim = 20; //数据上传间隔 device_status.startBar = 20; //定时开始时间 device_status.stopBar = 0; //定时结束时间 device_status.heattime = 20; //加热时间 device_status.shake_ws = 1; //针对开关 device_status.shake_sec = 1; //震动时间1-100ms device_status.sunBar = 4; //光控定时时长 device_status.tph = 70; //高温保护阈值 device_status.tpl = 5; //低温保护阈值 device_status.timeSwitch = 0; //定时模式 device_status.heatvalue=110; device_status.ds = 1; //设备状态开机 device_status.thsp = 300; device_status.passWord = 8888; device_status.timeSwitch = 0; ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds)); ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time)); ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar)); ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar)); ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch)); ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar)); ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime)); ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue)); ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph)); ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl)); ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关 ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间 ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp)); ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord)); DTU_Write("reset.mp3"); } //固件备份 void app_partion_backup(void) { rt_kprintf("固件恢复!!!\n"); const char *dst_part_name = "df_area"; const char *src_part_name = "app"; const struct fal_partition *dst_part; const struct fal_partition *src_part; dst_part = fal_partition_find(dst_part_name); src_part = fal_partition_find(src_part_name); if (dst_part == RT_NULL || src_part == RT_NULL) { if (dst_part == RT_NULL) rt_kprintf("%s partition is not exist!\n", dst_part_name); if (src_part == RT_NULL) rt_kprintf("%s partition is not exist!\n", src_part_name); } else { rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name); if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0) { int clone_pos = 0; int clone_len = 0, clone_tol_len; rt_uint8_t *buf = rt_malloc(4096); if (dst_part->len < src_part->len) clone_tol_len = dst_part->len; else clone_tol_len = src_part->len; while ((clone_pos < clone_tol_len) && (buf != RT_NULL)) { clone_len = fal_partition_read(src_part, clone_pos, buf, 4096); if (clone_len < 0) { rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name); break; } if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0) { rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name); break; } HMI_WRITE("#"); rt_kprintf("#"); clone_pos += clone_len; } if (clone_pos >= clone_tol_len) { rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len); } else { rt_kprintf("Clone partition failed!"); } if (buf) rt_free(buf); } } } void get_uid(rt_uint32_t* UID) { UID[0] = (rt_uint32_t)(READ_REG(*((rt_uint32_t*)UID_BASE))); UID[1] = (rt_uint32_t)(READ_REG(*((rt_uint32_t*)(UID_BASE + 4U)))); UID[2] = (rt_uint32_t)(READ_REG(*((rt_uint32_t*)(UID_BASE + 8U)))); rt_kprintf("Read stm32f103 uid:%s",UID); } void Debug_log_ON(void) { rt_console_set_device("uart4"); finsh_set_device("uart4"); } void Debug_log_OFF(void) { rt_console_set_device("uart1"); finsh_set_device("uart1"); rt_mq_init(&rx_mq, "rx_mq", msg_pool, /* 存放消息的缓冲区 */ sizeof(struct rx_msg), /* 一条消息的最大长度 */ sizeof(msg_pool), /* 存放消息的缓冲区大小 */ RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ rt_device_set_rx_indicate(serial, uart_input); hmi1 = rt_thread_create("Hmi_Fresh_thread", Hmi_thread_entry, RT_NULL, HMI_THREAD_STACK_SIZE, HMI_THREAD_PRIORITY, HMI_THREAD_TIMESLICE); if (hmi1 != RT_NULL) { rt_thread_startup(hmi1); } } MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON); MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF); // INIT_BOARD_EXPORT(HMI_uart4_init);