pachas

Dependencies:   mbed QEI FastPWM

Revision:
11:5cb7ae8bd831
Parent:
10:b2d87404309a
Child:
12:3bc2465b034a
--- a/nextion_interface.cpp	Mon Apr 27 19:52:02 2020 +0000
+++ b/nextion_interface.cpp	Thu May 07 03:52:31 2020 +0000
@@ -12,8 +12,7 @@
 
 /* Object definition */
 Serial  nextion_display(NEXTION_DISPLAY_TX_PIN,NEXTION_DISPLAY_RX_PIN);
-// For testing pusposes only
-//Serial pc(USBTX, USBRX);
+
 
 /* Global variable definition */
 
@@ -23,7 +22,7 @@
 float   inspiration_time_display = INSPIRATION_TIME_VALUE_DEFAULT;
 float   expiration_time_display = EXPIRATION_TIME_VALUE_DEFAULT;
 uint8_t pressure_measured_display = 16;
-uint8_t gas_input_display = AIR_INPUT;
+
 
 uint16_t selection_box_coordinates[3][4]={
                                             {1,1,159,159},
@@ -71,13 +70,17 @@
     /* Print default values */
     Volume_Setpoint_Fix();
     Resp_Frequency_Fix();
-    I_E_Ratio_Fix(); 
+    I_E_Ratio_Fix();
+     
+    /* Print initial measured values */ 
+    Volume_Display_Update();
+    Flow_Display_Update();
+    Pressure_Display_Update();
      
 } 
 
 
 
-
 void Parameter_Selection_Box_Update(void){
      
      /* Remove the selection box from the previous position */
@@ -96,9 +99,18 @@
                 break;
             default:
                 break;  
+                          
       }     
      
      
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }
+     
      /* Draw the selection box in the new position */ 
       switch(parameter_selection_index){
             case 0:
@@ -116,6 +128,14 @@
             default:
                 break;  
       }
+      
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }      
           
 }  
 
@@ -140,6 +160,14 @@
             default:
                 break;   
       }
+      
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }      
                
 }    
     
@@ -148,11 +176,20 @@
     
 void Volume_Setpoint_Display_Update(void){
 
-    volume_setpoint_display = VOLUME_SETPOINT_MINIMUM_VALUE  + VOLUME_SETPOINT_STEP*volume_setpoint_index;
+    volume_setpoint_display = VOLUME_SETPOINT_MINIMUM_VALUE  + (VOLUME_SETPOINT_STEP*volume_setpoint_index);
     
     /* Print volume value */
     nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }
+        
 } 
 
 
@@ -164,6 +201,16 @@
     /* Print volume value */
     nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
+    volume_ml = 0.0;
 } 
 
 
@@ -174,6 +221,15 @@
     /* Print frequency value */
     nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
 }  
 
 
@@ -185,6 +241,15 @@
     /* Print frequency value */
     nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
 }  
 
 void I_E_Ratio_Display_Update(void){
@@ -195,6 +260,15 @@
     /* Print i:e ratio value */
     nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
 }  
 
 
@@ -207,21 +281,59 @@
     /* Print i:e ratio value */
     nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
 }  
 
+
 void Volume_Display_Update(void){
     nextion_display.printf("t11.txt=\"%d\"", (int16_t)volume_ml);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }
+    
 }
 
+
 void Flow_Display_Update(void){
-    nextion_display.printf("t14.txt=\"%d\"", (int32_t)flow_lpm);
+    nextion_display.printf("t14.txt=\"%.2f\"", flow_lpm);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    } 
+    
 }
 
 void Pressure_Display_Update(void){
-    nextion_display.printf("t15.txt=\"%.1f\"", (pressure_02_psi * PSI_TO_CMH2O_CONSTANT));
+    nextion_display.printf("t15.txt=\"%d\"", (int16_t)(pressure_02_psi * PSI_TO_CMH2O_CONSTANT));
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+    
 }
 
 
@@ -231,15 +343,23 @@
     int16_t x_pos, y_pos;
     uint16_t volume_scaled_value;
     
-    //volume_scaled_value = (uint16_t)((volume_ml - 0.5)*VOLUME_WAVEFORM_HEIGHT*2) ;
-    volume_scaled_value = (uint16_t)((volume_ml - 0.5)*4) ;
+    volume_scaled_value = VOLUME_WAVEFORM_OFFSET + (int16_t)(volume_ml * VOLUME_WAVEFORM_GAIN);
     
-    x_pos_counter++;
+    x_pos_counter = x_pos_counter + 1;
     
     if(x_pos_counter >= VOLUME_WAVEFORM_WIDTH){
-        nextion_display.printf("fill %d,%d,%d,%d,%d",VOLUME_WAVEFORM_X0,VOLUME_WAVEFORM_Y0,VOLUME_WAVEFORM_WIDTH,VOLUME_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("fill %d,%d,%d,%d,%d",VOLUME_WAVEFORM_X0,VOLUME_WAVEFORM_Y0,VOLUME_WAVEFORM_WIDTH,VOLUME_WAVEFORM_HEIGHT,WAVEFORM_BACKGROUND_COLOR);
         nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
         x_pos_counter = 1;
+        
+        /* Read pressure sensors if necessary */ 
+        if(read_pressure_sensors_flag){
+            Pressure_Sensors_Read();
+            Calculate_Flow_From_Pressure();
+            Calculate_Volume_From_Flow();
+            read_pressure_sensors_flag = 0;
+        }       
+        
     }
     
     x_pos = VOLUME_WAVEFORM_X0 + x_pos_counter;
@@ -253,8 +373,17 @@
     
     nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, GREEN);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-    
+ 
     prev_y_pos = y_pos; 
+ 
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }
+
 }
 
 
@@ -264,14 +393,23 @@
     uint16_t x_pos, y_pos;
     uint16_t flow_scaled_value;
     
-    flow_scaled_value = 79 + (int16_t)flow_lpm;
+    flow_scaled_value = FLOW_WAVEFORM_OFFSET + (int16_t)(flow_lpm*FLOW_WAVEFORM_GAIN);
     
-    x_pos_counter++;
+    x_pos_counter = x_pos_counter + 1;
     
     if(x_pos_counter >= FLOW_WAVEFORM_WIDTH){
-        nextion_display.printf("fill %d,%d,%d,%d,%d",FLOW_WAVEFORM_X0,FLOW_WAVEFORM_Y0,FLOW_WAVEFORM_WIDTH,FLOW_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("fill %d,%d,%d,%d,%d",FLOW_WAVEFORM_X0,FLOW_WAVEFORM_Y0,FLOW_WAVEFORM_WIDTH,FLOW_WAVEFORM_HEIGHT,WAVEFORM_BACKGROUND_COLOR);
         nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
         x_pos_counter = 1;
+        
+        /* Read pressure sensors if necessary */ 
+        if(read_pressure_sensors_flag){
+            Pressure_Sensors_Read();
+            Calculate_Flow_From_Pressure();
+            Calculate_Volume_From_Flow();
+            read_pressure_sensors_flag = 0;
+        }        
+        
     }
     
     x_pos = FLOW_WAVEFORM_X0 + x_pos_counter;
@@ -287,8 +425,17 @@
     
     nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, YELLOW);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+ 
+    prev_y_pos = y_pos; 
     
-    prev_y_pos = y_pos; 
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }    
+     
 }
 
 
@@ -298,14 +445,23 @@
     uint16_t x_pos, y_pos;
     uint16_t pressure_02_scaled_value;
     
-    pressure_02_scaled_value = (uint16_t)(pressure_02_psi * PRESSURE_WAVEFORM_HEIGHT) ;
+    pressure_02_scaled_value = PRESSURE_WAVEFORM_OFFSET + (uint16_t)(pressure_02_psi * PRESSURE_WAVEFORM_GAIN) ;
     
-    x_pos_counter++;
+    x_pos_counter = x_pos_counter + 1;
     
     if(x_pos_counter >= PRESSURE_WAVEFORM_WIDTH){
-        nextion_display.printf("fill %d,%d,%d,%d,%d",PRESSURE_WAVEFORM_X0,PRESSURE_WAVEFORM_Y0,PRESSURE_WAVEFORM_WIDTH,PRESSURE_WAVEFORM_HEIGHT,8591);
+        nextion_display.printf("fill %d,%d,%d,%d,%d",PRESSURE_WAVEFORM_X0,PRESSURE_WAVEFORM_Y0,PRESSURE_WAVEFORM_WIDTH,PRESSURE_WAVEFORM_HEIGHT,WAVEFORM_BACKGROUND_COLOR);
         nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
         x_pos_counter = 1;
+     
+        /* Read pressure sensors if necessary */ 
+        if(read_pressure_sensors_flag){
+            Pressure_Sensors_Read();
+            Calculate_Flow_From_Pressure();
+            Calculate_Volume_From_Flow();
+            read_pressure_sensors_flag = 0;
+        }         
+        
     }
     
     x_pos = PRESSURE_WAVEFORM_X0 + x_pos_counter;
@@ -319,6 +475,15 @@
     nextion_display.printf("line %d,%d,%d,%d,%d",(x_pos - 1), prev_y_pos, x_pos, y_pos, WHITE);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
     
-    prev_y_pos = y_pos; 
+    prev_y_pos = y_pos;
+    
+    /* Read pressure sensors if necessary */ 
+    if(read_pressure_sensors_flag){
+        Pressure_Sensors_Read();
+        Calculate_Flow_From_Pressure();
+        Calculate_Volume_From_Flow();
+        read_pressure_sensors_flag = 0;
+    }     
+     
 }