增加设备离线时间计算
authorkyz <kongyuzhong@google.com>
Fri, 24 May 2024 06:41:18 +0000 (14:41 +0800)
committerkyz <kongyuzhong@google.com>
Fri, 24 May 2024 06:41:18 +0000 (14:41 +0800)
  增加bypass功能是否启用判断
  累计流量计算调用改为使用事件触发

Core/Src/main.c
user/user.h
user/user_app.c
user/user_cdc_rx_2.c
user/user_eth.c
user/user_flowmeter.c
user/user_lfs.c
user/user_lfs.h
user/user_report.c
user/user_ui.c
user/user_valve_ctrl.c

index cd68679fc9db07a25b933227ead201cd6e249075..7faf471ad53bde4fc32144edc69b62ae2e82a8cc 100644 (file)
@@ -132,7 +132,7 @@ int main(void)
   MX_UART4_Init();
   MX_OCTOSPI1_Init();
   MX_TIM1_Init();
-  //MX_IWDG1_Init();
+  MX_IWDG1_Init();
   MX_TIM3_Init();
   /* USER CODE BEGIN 2 */
 
@@ -316,17 +316,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
   }
   /* USER CODE BEGIN Callback 1 */
   else if( htim->Instance == TIM3 ){
-    extern void flow_total_update( uint32_t ms );
-#if 1
-    flow_total_update( 1000 ); //1秒定时器触发
-#else
-    static uint32_t cnt_1s = 0;
-    cnts++; //1 毫秒定时器触发
-    if( cnt_1s >= 1000 ){  //1000ms 计算一次累计流量
-      flow_total_update( cnt );
-      cnt_1s = 0;
-    }
-#endif
+               osEventFlagsSet( g_dev.ev_group, EVENT_TIMER_1S );
   }
   /* USER CODE END Callback 1 */
 }
index 16ad659d77141bda33fe7361daa979fc80376f99..a2ccc4a32a64d2619a3281163010e3ce02113612 100644 (file)
@@ -22,6 +22,7 @@
 #include "usart.h"
 #include "usb_device.h"
 #include "gpio.h"
+#include "user_lfs.h"
 //#include "fmc.h"
 //--------------------
 #define APP_VER_MAJOR 3
@@ -73,6 +74,7 @@ void  debug_printf(char* str , ...);
 #define EVENT_ADC1_UPDATE      ( 1<<4) //ADC1 DMA采集完成
 #define EVENT_ADC2_UPDATE      ( 1<<5) //ADC2 DMA采集完成
 #define EVENT_ADC3_UPDATE      ( 1<<6) //ADC3 DMA采集完成
+#define EVENT_TIMER_1S         ( 1<<7) //1秒定时器信息,用于保存累计流量和计算离线时间
 
 //SPI FLASH 空间分配, 一共4M
 //APP存储分区
@@ -229,6 +231,9 @@ typedef struct {
        char serial_number[32];
     alarm_t alarm;
                uint32_t hw_ver; //2 or 3
+               uint32_t has_bypass;
+               
+               uint32_t offline_secs; //offline time;
 }dev_info_t;
 
 extern TIM_HandleTypeDef htim2;   //htim2 用于触发DMA ADC采集
index 5d41ea08c2a8ef79f05aab3af5bf4ec13e21848a..085c427fcc9dfa0a1701ff31d68c5e3189e81fac 100644 (file)
@@ -58,6 +58,10 @@ dev_info_t g_dev;
 extern void debug_task_start( void );
 extern void wifi_task_start( void );
 extern void eth_task_start( void );
+
+extern void flow_total_update( uint32_t ms );
+extern void offline_time_update( uint32_t add_secs );
+
 void ui_task_start_h3( void );
 int32_t lcd_init_h3( void );
 void beep_init( void );
@@ -94,14 +98,20 @@ void StartDefaultTask(void *argument)
 
     dev_cfg_load( &g_dev.cfg );
     dev_cfg_v2_load( &g_dev.cfg2 );
-       lfs_load_device_serial( g_dev.serial_number, sizeof( g_dev.serial_number ) );
+         lfs_load_device_serial( g_dev.serial_number, sizeof( g_dev.serial_number ) );
+               g_dev.offline_secs = lfs_load_offline_time();
 
     g_dev.ev_group = xEventGroupCreate();
 
-    if( lcd_init_h3() == 0 )
+    if( lcd_init_h3() == 0 ){
                        g_dev.hw_ver = 3;
-               else
+                       g_dev.has_bypass = 0;
+                       g_dev.cfg.bypass = 0;
+                       g_dev.cfg.bypass_save = 0;
+               }else{
                        g_dev.hw_ver = 2;
+                       g_dev.has_bypass = 1;
+               }
 
     DBG_PRINTF( "WeldingController V%d.%d\n", APP_VER_MAJOR, APP_VER_MINOR );
     adcTaskHandle       = osThreadNew(user_adc_task, NULL, &adcTask_attributes);
@@ -118,13 +128,26 @@ void StartDefaultTask(void *argument)
                        void ui_task_start_h3( void );
                        ui_task_start_h3();
                }else{
-                       g_dev.hw_ver = 2;
                        uiTaskHandle  = osThreadNew(user_ui_task, NULL, &uiTask_attributes);
          }
 
+                HAL_TIM_Base_Start_IT( &htim3 );
+     EventBits_t uxBits;
      while(1){
-        HAL_IWDG_Refresh( &hiwdg1 ); //4s iwdg
-        osDelay(1000);
+                           uxBits = xEventGroupWaitBits(
+                g_dev.ev_group,   /* The event group being tested. */
+                EVENT_TIMER_1S, /* The bits within the event group to wait for. */
+                pdTRUE,        /* BIT_x should be cleared before returning. */
+                pdFALSE,       /* Don't wait for all bits, either bit will do. */
+                osWaitForever );/* Wait a maximum of xxx ms for either bit to be set. */
+
+         if( uxBits & EVENT_TIMER_1S ){
+                                               //debug_printf( "offline time:%d\n", g_dev.offline_secs );
+                                         flow_total_update( 1000 ); //1秒定时器触发
+                                         offline_time_update( 1 );  //离线更新,加1秒
+
+                                               HAL_IWDG_Refresh( &hiwdg1 ); //4s iwdg
+                               }
     }
 }
 
index 528bb800c7e3990e77b5ba2d22f431e8e7ef9068..5f738d50420aec519e7d8e1686926d46c05959f3 100644 (file)
@@ -519,7 +519,11 @@ int32_t handle_cdc_pkt( cdc_pkt_t *pkt )
                                          uint32_t coeff = get_u32_big( &pkt->pdata[16] );
                                          uint32_t bypass_save = get_u32_big( &pkt->pdata[20] );
                                          uint32_t bypass = get_u32_big( &pkt->pdata[24] );
-                                       
+
+                                         if( !g_dev.has_bypass ){
+                                                       bypass = 0;
+                                               }
+
                                                if( factor >=2 && factor <= 30 )
                                                        g_dev.cfg.save_gas_factor = factor;
                                                if( method <3 )
@@ -640,23 +644,6 @@ int32_t handle_cdc_pkt( cdc_pkt_t *pkt )
             resp[0] = 1;
             tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 );
             break;
-#if 0                                          
-        case 0xC2: //气路读取 1B   0:bypass_off, 1:bypass_on
-               resp[0] = g_dev.cfg.bypass;
-            tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 );
-            break;
-        case 0xC3: //气路选择 1B   0:bypass_off, 1:bypass_on
-            if( pkt->pdata[0] == 1 || pkt->pdata[0] == 0 ){
-                g_dev.cfg.bypass = pkt->pdata[0];
-                dev_cfg_store( &g_dev.cfg );
-                resp[0] = 1;
-                lcd_update_cfg();
-            }else{
-                resp[0] = 0;
-            }
-            tx_n = wrap_resp_pkt( tx_pkt, pkt->cmd, resp, 1 );
-            break;
-#endif
 #endif
 
     }
index 4d56541151ff40ca66ad47ca8e92b6bd1fc1fbdf..c47a61c58f247c921ea4fffa223ca0606e3d5d59 100644 (file)
@@ -230,6 +230,7 @@ int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port )
 
     tick = xTaskGetTickCount();
     while( client->connected==0 && xTaskGetTickCount() - tick < 5*1000 ){
+                       osDelay(100);
     }
     if( client->connected > 0 ){
         client->pcb = tpcb;
index 99d15ed1b445a2544a4681e3e80318f5b9455e72..7a86e26af9c77320f722401cf50c692b67c6d1ee 100644 (file)
@@ -202,10 +202,8 @@ extern float lfs_load_flow_total( void );
 
 void flow_total_update( uint32_t ms )
 {
-    if(  g_dev.flowmeter.flow_speed > 0 ){
-        //debug_printf( "------ update flow total %0.1f -> ", g_flow_total );
+         if( g_dev.flowmeter.id == FLOWMETER_SMC && g_dev.flowmeter.flow_speed > 0 ){
         g_flow_total += g_dev.flowmeter.flow_speed*ms/(60*1000.0f);
-        //debug_printf( " %0.1f \n", g_flow_total );
     }
 }
 
@@ -220,9 +218,6 @@ void user_flowmeter_task( void* unused )
     float flow_comp;
     float save_flow_total = 0;
 
-       osDelay( 100 );
-    save_flow_total = lfs_load_flow_total();
-
     //检查流量计类型
     g_dev.flowmeter.id = g_dev.cfg2.flowmeter;
     debug_printf( "flowmeter task start, flowmeter:%d ...\n", g_dev.flowmeter );
@@ -281,20 +276,10 @@ void user_flowmeter_task( void* unused )
 
     if( g_dev.flowmeter.id== FLOWMETER_SMC ){
 
-        //load flow_total
-        g_flow_total = lfs_load_flow_total();
-        g_dev.flowmeter.flow_total = g_flow_total;
-
-        HAL_TIM_Base_Start_IT( &htim3 );
-    }
-#if  0
-    if( g_dev.flowmeter.id== FLOWMETER_AFM0750 ){
-        //测试软件计算累计流量
+        //load flow_total from lfs
         g_flow_total = lfs_load_flow_total();
         g_dev.flowmeter.flow_total = g_flow_total;
-        HAL_TIM_Base_Start_IT( &htim3 );
     }
-#endif
 
     if( g_dev.flowmeter.id == FLOWMETER_MF4008 ){
         //MF4008 检查修正系数, 只开机检查, 如果通过上位机修改参数,需要重启系统。
@@ -358,9 +343,7 @@ void user_flowmeter_task( void* unused )
                 g_dev.flowmeter.tem = reg_values[3]/10.0f;
                 g_dev.flowmeter.state = reg_values[4];
                 flow_err = 0;
-#if 0
-                total_raw = g_flow_total;  //for test
-#endif
+
             }else{
                 flow_err ++;  //读取失败, 故障计数到10次报错
             }
@@ -406,7 +389,7 @@ void user_flowmeter_task( void* unused )
         g_dev.flowmeter.flow_speed = flow_raw;
         g_dev.flowmeter.flow_total = total_raw;
 
-        if(  g_dev.flowmeter.id == FLOWMETER_SMC ||  g_dev.flowmeter.id == FLOWMETER_AFM0750 ){
+        if(  g_dev.flowmeter.id == FLOWMETER_SMC ){
             static  int n = 0;
             if( n++ >= 100 ){ //10秒 保存一次
                 n = 0;
index e96045511030b424c8fb3266d197ee7fd59ddb6d..e24b5dbdb29c47559495c60c582b7dfc0f706069 100644 (file)
@@ -164,3 +164,23 @@ int32_t lfs_store_device_serial( char *sn )
 {
     return user_lfs_write( DEVICE_SN_PATH, (uint8_t*)sn, strlen( sn ) ) == strlen(sn)?0:-1;
 }
+
+uint32_t lfs_load_offline_time( void )
+{
+    uint32_t secs = 0;
+
+    if( user_lfs_read( OFFLINE_TIME_PATH, (uint8_t*)&secs, sizeof( secs ) ) == sizeof( secs ) ){
+        
+    }
+    return secs;
+}
+
+int32_t lfs_store_offline_time( uint32_t secs )
+{
+    int32_t ret = -1;
+
+    if( user_lfs_write( OFFLINE_TIME_PATH, (uint8_t*)&secs, sizeof( secs ) ) == sizeof( secs ) ){
+            ret = 0;
+    }
+    return ret;
+}
\ No newline at end of file
index 094029224f9859901fad737a03f20ed4d31cd4ab..29157fff9b561feb5a4983a9eb0a0404f92c0a94 100644 (file)
@@ -3,7 +3,8 @@
 
 #include "lfs.h"
 
-#define FLOW_TOTAL_PATH "flowmeter_total"
+#define FLOW_TOTAL_PATH "flow_total"
+#define OFFLINE_TIME_PATH "offline_time"
 #define DEVICE_SN_PATH  "device_serial"
 #define DEVICE_CONFIG_PATH "device_config"
 #define DEVICE_CONFIG_V2_PATH "device_config_v2"
@@ -16,5 +17,6 @@ float lfs_load_flow_total( void );
 int32_t lfs_store_flow_total( float total );
 int32_t lfs_load_device_serial( char *read_buffer, uint32_t buffer_size );
 int32_t lfs_store_device_serial( char *sn );
-
+uint32_t lfs_load_offline_time( void );
+int32_t lfs_store_offline_time( uint32_t secs );
 #endif //__USER_LFS_H__
index e55aa23cc0e0c28b7b7deb9019a2a58f2ad330f8..69efc71ebb71a65bc6057ec5dddc2064aeec2bd1 100644 (file)
@@ -43,6 +43,28 @@ static const char *fmt = "{\n\
 
 #endif
 
+void offline_time_update( uint32_t add_secs )
+       { 
+       static uint32_t n = 0;
+       if( g_dev.wifi_connected == 2 || g_dev.eth_connected == 2 ){
+               //在线
+               if( g_dev.offline_secs != 0 ){
+                       g_dev.offline_secs = 0;
+                       lfs_store_offline_time( 0 );
+               }
+               n = 0;
+               return;
+       }else{
+               //离线,更新离线时间
+               g_dev.offline_secs += add_secs;
+               if( n++ > 60 ){ //1分钟保存一次
+                       n = 0;
+                       //这个timer中断中调用,不能调用文件系统读写操作
+                       lfs_store_offline_time( g_dev.offline_secs );
+               }
+       }
+}
+
 static void mac_str_init( void )
 {
     uint8_t mac[6];
index 40fb829573309bded1bcfc21beb768f905bd5912..fd742757a8a78782f9551e6f59f883c26e1c3103 100644 (file)
@@ -222,6 +222,8 @@ void ui_handle_lcd_pkt( void )
                 }else if( ctrl_addr == lcd_valve_bypass_addr ){//比例阀旁路
                     uint8_t bypass = ctrl_value;
                     //g_dev.ui_bypass = bypass;
+                                                                               if( !g_dev.has_bypass )
+                                                                                       bypass = 0;
                                                                          g_dev.cfg.bypass = bypass;
                                                                          dev_cfg_store( &g_dev.cfg );
                 }else if( ctrl_addr == lcd_wifi_config_addr ){ //点击配置按钮
@@ -365,14 +367,14 @@ void lcd_write_u16( uint16_t addr, uint16_t value )
 
 void lcd_power_on( void )
 {
-       if( g_dev.hw_ver != 3 ){
+       if( g_dev.hw_ver == 2 ){
     HAL_GPIO_WritePin( LCM_EN_GPIO_Port, LCM_EN_Pin, GPIO_PIN_SET );
        }
 }
 
 void lcd_power_off( void )
 {
-       if( g_dev.hw_ver != 3 ){
+       if( g_dev.hw_ver == 2 ){
     HAL_GPIO_WritePin( LCM_EN_GPIO_Port, LCM_EN_Pin, GPIO_PIN_RESET );
        }
 }
index 748b1cd74639e4cacbb9b6f636efa17f61ea1826..1e532529368a35aa040041d8a39d6ae11c1718fb 100644 (file)
@@ -557,14 +557,16 @@ void user_valve_ctrl_task( void* unused )
   //          welding_data_store( g_dev.welding_time/100 );
         }
 
-        if( g_dev.cfg.bypass || tem_err_state  ){
-            //屏上点击旁路或温度故障,开启旁路
-            valve_bypass( 1 );           //旁路电磁阀开启
-            g_dev.valve_curr_out =  0;   //比例电磁阀关闭
-        }else if( !g_dev.cfg.bypass && !tem_err_state  ){
-            //恢复正常,关闭旁路
-            valve_bypass( 0 );           //旁路电磁阀关闭
-        }
+        if( g_dev.has_bypass){
+                                               if( g_dev.cfg.bypass || tem_err_state  ){
+                                                               //屏上点击旁路或温度故障,开启旁路
+                                                               valve_bypass( 1 );           //旁路电磁阀开启
+                                                               g_dev.valve_curr_out =  0;   //比例电磁阀关闭
+                                               }else{
+                                                               //恢复正常,关闭旁路
+                                                               valve_bypass( 0 );           //旁路电磁阀关闭
+                                               }
+                               }
 
         //更新比例阀驱动电流
         user_valve_set_current( g_dev.valve_curr_out );
@@ -640,7 +642,7 @@ void flow_low_alarm_check( void )
                 if(  now - alarm_start > 10*1000 ){
                         //计时到10秒,开直通阀
                         alarm = 3;
-                        if( g_dev.cfg.bypass == 0 ){
+                        if( g_dev.cfg.bypass == 0 && g_dev.has_bypass ){
                             g_dev.cfg.bypass = 1;
                             ui_cfg_remote_update(); //刷新lcd
                         }