// This file was generated by SquareLine Studio // SquareLine Studio version: SquareLine Studio 1.5.1 // LVGL version: 8.3.6 // Project name: ESP32S3_UI #include "ui.h" #include "driver/ledc.h" #include "esp_err.h" #include "esp_log.h" #include "FluxWifi.h" #include "FluxUart.h" #include "FluxSD.h" #include "esp_vfs_fat.h" #include "sdmmc_cmd.h" #include "driver/sdmmc_host.h" #include "FluxProtocol.h" /*选择是否使能Debug页面的操作*/ #define MODE_TEST 0 #define LOWERST_LIGHT_LEVEL (7222) #define RESULT_RIGHT lv_obj_set_style_bg_color(ui_pageHome_panelTestResult,lv_color_hex(0x00FF00),LV_PART_MAIN);\ lv_label_set_text(ui_pageHome_labelTestResult,"V"); #define RESULT_FAULT lv_obj_set_style_bg_color(ui_pageHome_panelTestResult,lv_color_hex(0xFF0000),LV_PART_MAIN);\ lv_label_set_text(ui_pageHome_labelTestResult,"X"); /*更新当前测试标准*/ static void standardVolumeRefresh(void); /*根据呼吸频率调整积分时间*/ void comulateTimeSend(void); /*亮度调节回调函数*/ void on_sliderLight_valueChanged(lv_event_t * e) { // Your code here // Your code here uint32_t value = 8192-lv_slider_get_value(e->target); //ESP_LOGI("lightSliderCallback", "value = %u", value); if (value>LOWERST_LIGHT_LEVEL) { value = LOWERST_LIGHT_LEVEL; } ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, value)); // Update duty to apply the new value ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); } /*设备类型选择回调函数*/ void on_dropdownDeviceType_valueChanged(lv_event_t * e) { char buf[32]={}; uint16_t index = lv_dropdown_get_selected(e->target); lv_dropdown_get_selected_str(e->target,buf,32); lv_label_set_text(ui_pageHome_labelTitle, buf); if (index == Spirit3) { lv_spinbox_set_range(ui_pageHome_spinboxStage,1,4); }else{ lv_spinbox_set_range(ui_pageHome_spinboxStage,1,6); } } /*SD卡读取回调函数*/ void on_buttonReadSDCard_clicked(lv_event_t * e) { /*首先清空显示页面*/ lv_textarea_set_text(ui_pageSDCard_textAreaSDCard,""); esp_vfs_fat_info(MOUNT_POINT,&total_bytes,&free_bytes); free_percent = (float)free_bytes / (float)total_bytes * 100; printf("total:%llu free:%llu\n",total_bytes,free_bytes); lv_label_set_text_fmt(ui_pageSDCard_labelSDCardFree,"%.2f free",free_percent); lv_label_set_text_fmt(ui_pageSDCard_labelSDCardUsed,"used: %.2f GB",(double)(total_bytes-free_bytes)/(1024*1024*1024.0)); lv_label_set_text_fmt(ui_pageSDCard_labelSDCardSize,"size: %.2f GB",(double)total_bytes/(1024*1024*1024.0)); lv_arc_set_value(ui_pageSDCard_arcSDCardFree,free_percent); } /************************************ WIFI调节函数 ******************************************* */ /*wifi搜索回调函数*/ void on_buttonSearchWifi_clicked(lv_event_t * e) { wifi_scan(); } /*WiFi连接选项修改回调函数*/ void on_dropDownWifiName_valueChanged(lv_event_t * e) { // Your code here int currentIndex=0; currentIndex = lv_dropdown_get_selected(e->target); lv_label_set_text_fmt(ui_pageWifiConnect_labelRssi, "RSSI:%d dBm ", ap_info[currentIndex].rssi); lv_label_set_text_fmt(ui_pageWifiConnect_labelConnectWifi,"connect to %s",ap_info[currentIndex].ssid); if (strcmp((char*)ap_info[currentIndex].ssid,"yuwell")==0) { lv_textarea_set_text(ui_pageWifiConnect_textAreaWifiPassword, "yuwell123456"); } } /*WIFI连接按键按下回调函数*/ void on_buttonConnectWifi_clicked(lv_event_t * e) { if(strcmp(lv_label_get_text(ui_pageWifiConnect_labelConnectWifi),"disconnect") == 0 ) { esp_wifi_disconnect(); ESP_ERROR_CHECK(esp_wifi_stop()); return; } int currentIndex=0; ESP_ERROR_CHECK(esp_wifi_stop()); currentIndex = lv_dropdown_get_selected(ui_pageWifiConnect_dropdownWifiName); // Your code here wifi_config_t wifi_config = { .sta = { .ssid = "yuwell", .password = "yuwell123456", /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8). * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. */ .threshold = { .authmode = ap_info[currentIndex].authmode, }, .sae_pwe_h2e = WPA3_SAE_PWE_BOTH, .sae_h2e_identifier = "", }, }; lv_dropdown_get_selected_str(ui_pageWifiConnect_dropdownWifiName,(char*)wifi_config.sta.ssid,32); strncpy((char*)wifi_config.sta.password,lv_textarea_get_text(ui_pageWifiConnect_textAreaWifiPassword),64); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start() ); ESP_LOGI("TAG", "wifi_init_sta finished."); esp_wifi_connect(); } /*WIFI密码清空按键按下回调函数*/ void on_buttonClearPassword_clicked(lv_event_t * e) { // Your code here lv_textarea_set_text(ui_pageWifiConnect_textAreaWifiPassword, ""); } /************************************ 测试流程函数 ******************************************* */ /*pageHome 开始测试按键按下回调函数*/ int32_t timeS = 0; void on_buttonStartTest_clicked(lv_event_t * e) { /*下发积分时间*/ comulateTimeSend(); timeS = lv_spinbox_get_value(ui_pageFluxRead_spinboxTime); /*下发启动测试指令*/ FLUX_TEST_START; #if 0 lv_label_set_text(ui_pageHome_labelStartTest,"Testing..."); vTaskDelay(timeS*1000/portTICK_PERIOD_MS); lv_event_send(ui_pageFluxRead_buttonReadFlux,LV_EVENT_CLICKED,NULL); lv_label_set_text(ui_pageHome_labelStartTest,"Test Complete!"); #endif } /*流量计当前示数回传函数——Debug模式*/ void on_buttonFluxRead_clicked(lv_event_t * e) { /*选择是否使用Debug中的操作*/ #if MODE_TEST flux_frame.DATA[1] = (unsigned char)lv_spinbox_get_value(ui_pageDebug_spinboxAddr); flux_frame.DATA[3] = (unsigned char)lv_spinbox_get_value(ui_pageDebug_spinboxNum); /*测试模式可手动设置功能码*/ if (lv_dropdown_get_selected(ui_pageDebug_dropdownFunctionCode) == 0) { flux_frame.FUNC = FLUX_FUNC_READ; }else{ flux_frame.FUNC = FLUX_FUNC_SET; } ESP_ERROR_CHECK(ModbusRTU_Send_0306(ECHO_UART_PORT_NUM1,&flux_frame)); #endif } /*流量计当前示数显示—Debug模式*/ void on_buttonClearTextarea_clicked(lv_event_t * e) { #if MODE_TEST lv_textarea_set_text(ui_pageDebug_textAreaContent, ""); #endif } /*pageHome 呼吸频率减操作*/ void on_buttonRateMinus_clicked(lv_event_t * e) { uint16_t value = lv_spinbox_get_value(ui_pageHome_spinboxRate); lv_spinbox_set_value(ui_pageHome_spinboxRate,value-5); } /*pageHome 呼吸频率加操作*/ void on_buttonRatePlus_clicked(lv_event_t * e) { uint16_t value = lv_spinbox_get_value(ui_pageHome_spinboxRate); lv_spinbox_set_value(ui_pageHome_spinboxRate,value+5); } /*pageHome 切换挡位之后*/ void on_spinboxStage_valueChanged(lv_event_t * e) { /*获取当前挡位并更新标准体积数据*/ int32_t value = lv_spinbox_get_value(e->target); lv_label_set_text_fmt(ui_pageHome_labelResultStage,"%ld",value); standardVolumeRefresh(); } /*pageHome 频率值改变*/ int32_t Ratevalue = 0; void on_spinboxRate_valueChanged(lv_event_t * e) { /*更改下方测试结果部分的频率信息*/ Ratevalue = lv_spinbox_get_value(e->target); lv_label_set_text_fmt(ui_pageHome_labelResultRate,"%ld",Ratevalue); /*刷新标准体积数据*/ standardVolumeRefresh(); /*下发测试时间*/ comulateTimeSend(); } /*pageHome 标准体积数据刷新*/ static void standardVolumeRefresh(void) { /*获取当前的呼吸频率及挡位*/ fluxProtocol.currentBPM = lv_spinbox_get_value(ui_pageHome_spinboxRate); fluxProtocol.currentStage = lv_spinbox_get_value(ui_pageHome_spinboxStage); /*获取当前测试设备类型*/ fluxProtocol.currentDevice = lv_dropdown_get_selected(ui_pageSetting_dropdownDeviceType); if (fluxProtocol.currentDevice == Spirit3) { /*判定当前测试设备为Spirit-3*/ /*获取标准体积数据*/ fluxProtocol.nominalVolumeStage = Spirit3_Volume[fluxProtocol.currentStage-1]; }else if (fluxProtocol.currentDevice == Spirit6) { /*判定当前测试设备为Spirit-6*/ fluxProtocol.nominalVolumeStage = Spirit6_Volume[fluxProtocol.currentStage-1]; }else if(fluxProtocol.currentDevice == YU_Lite8) { /*判定当前测试设备为YU-Lite-8*/ fluxProtocol.nominalVolumeStage = YULite8_Volume[fluxProtocol.currentStage-1]; } float Volume_nominal = fluxProtocol.nominalVolumeStage /fluxProtocol.currentBPM*1.0; float VolumeMax = Volume_nominal*1.15; float VolumeMin = Volume_nominal*0.85; lv_label_set_text_fmt(ui_pageHome_labelNominalVolume,"%.2f",Volume_nominal); lv_label_set_text_fmt(ui_pageHome_labelNominalVolumeMax,"%.2f",VolumeMax); lv_label_set_text_fmt(ui_pageHome_labelNominalVolumeMin,"%.2f",VolumeMin); /*根据标准值判定当前检测结果是否合格*/ RefreshResult(); } /*pageFluxRead 获取当前流量数据*/ void on_buttonReadFlux_clicked(lv_event_t * e) { flux_frame.DATA[1] = 0; flux_frame.DATA[3] = 120; flux_frame.FUNC = FLUX_FUNC_READ; /*下发问询120个参数*/ ESP_ERROR_CHECK(ModbusRTU_Send_0306(ECHO_UART_PORT_NUM1,&flux_frame)); } /*更新测试结果 —— 功能已屏蔽*/ void on_labelTestVolume_valueChanged(lv_event_t * e) { #if 0 /* 测试结果刷新放到label更新函数中 */ RefreshResult(); #endif } /*更新工作模式 —— 功能已屏蔽*/ void on_dropDownWorkmode_valueChanged(lv_event_t * e) { #if 0 FluxMachineData.workMode = (enum FluxMachineWorkMode)lv_dropdown_get_selected(e->target); #endif } /*向流量计下发计时时间*/ void on_spinboxTime_valueChanged(lv_event_t * e) { #if 0 /* 0x10 设置多个参数报文格式 */ /* 地址 功能码 起始地址 寄存器数量 字节计数(写入寄存器数量*2) 数据1 数据2 。。。 CRC */ /* 0x01 0x10 0x00 02 0x00 02 0x04 0x0078 0x0077 CRC*/ /*下发配置参数(其实只是配置时间)*/ flux_frame2Reg.ADDR = FLUX_ADDR; flux_frame2Reg.FUNC = FLUX_FUNC_SET; /*时间寄存器起始地址*/ flux_frame2Reg.regAddr = 0x0002; flux_frame2Reg.regNum = 0x0002; flux_frame2Reg.ByteNum = 0x04; /*将时间的浮点数转为字节数组*/ uint8_t bytes[4]; float time = (float)lv_spinbox_get_value(e->target); memcpy(bytes, &time, 4); ESP_LOGI("FluxProtocol", "time:%f", time); ESP_LOGI("FluxProtocol", "time:0-%02x 1-%02x 2-%02x 3-%02x", bytes[0],bytes[1],bytes[2],bytes[3]); flux_frame2Reg.DATA[0] = bytes[3]; flux_frame2Reg.DATA[1] = bytes[2]; flux_frame2Reg.DATA[2] = bytes[1]; flux_frame2Reg.DATA[3] = bytes[0]; ESP_ERROR_CHECK(ModbusRTU_Send_0x10(ECHO_UART_PORT_NUM1,&flux_frame2Reg)); #elif 1 /*根据当前频率进行时间调节*/ comulateTimeSend(); #endif } /*启动流量计*/ void on_buttonFluxStart_clicked(lv_event_t * e) { #if 0 /*下发启动命令*/ flux_frame.ADDR = FLUX_ADDR; flux_frame.FUNC = FLUX_FUNC_SET_SINGLE; flux_frame.DATA[0] = 0x00; flux_frame.DATA[1] = 0x01; flux_frame.DATA[2] = 0x00; flux_frame.DATA[3] = 0x01; ESP_ERROR_CHECK(ModbusRTU_Send_0306(ECHO_UART_PORT_NUM1,&flux_frame)); #elif 1 FLUX_TEST_START; #endif } /*根据spinbox中的值下发积分时间*/ void comulateTimeSend(void) { /*获取当前呼吸频率*/ Ratevalue = lv_spinbox_get_value(ui_pageHome_spinboxRate); switch (Ratevalue) { case 15: /*呼吸频率为15BPM时,积分时间设定为40秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,40); break; case 20: /*呼吸频率为20BPM时,积分时间设定为30秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,30); break; case 25: /*呼吸频率为25BPM时,积分时间设定为24秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,24); break; case 30: /*呼吸频率为30BPM时,积分时间设定为20秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,20); break; case 35: /*呼吸频率为35BPM时,积分时间设定为16秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,18); break; case 40: /*呼吸频率为40BPM时,积分时间设定为15秒*/ lv_spinbox_set_value(ui_pageFluxRead_spinboxTime,15); break; default: break; } #if 0 /*使用0306报文格式*/ uint8_t bytes[4]; float time = (float)lv_spinbox_get_value(ui_pageFluxRead_spinboxTime); memcpy(bytes, &time, 4); /*下发命令*/ flux_frame.ADDR = FLUX_ADDR; flux_frame.FUNC = FLUX_FUNC_SET_SINGLE; flux_frame.DATA[0] = 0x00; flux_frame.DATA[1] = 0x02; flux_frame.DATA[2] = bytes[3]; flux_frame.DATA[3] = bytes[2]; ESP_ERROR_CHECK(ModbusRTU_Send_0306(ECHO_UART_PORT_NUM1,&flux_frame)); #endif }