para el ventilador

Dependencies:   QEI FastPWM

Revision:
8:208d965a3bd2
Parent:
7:f4e248182d31
Child:
9:95fdcdc0977e
--- a/nextion_interface.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/nextion_interface.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -9,6 +9,7 @@
 #include "ventilator.h"
 
 
+
 /* Object definition */
 Serial  nextion_display(NEXTION_DISPLAY_TX_PIN,NEXTION_DISPLAY_RX_PIN);
 // For testing pusposes only
@@ -41,26 +42,39 @@
 
 void Nextion_Interface_Initialize(void){
     /* Configure baud rate */ 
+    nextion_display.baud(9600);
+    wait(1);
+    nextion_display.printf("baud=%d", NEXTION_DISPLAY_BAUD_RATE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    wait(1);
     nextion_display.baud(NEXTION_DISPLAY_BAUD_RATE);
-    //pc.baud(115200);
-    
-    //pc.printf("PUERTO SERIAL HABILITADO\n\n\r");
+    wait(1);
+
+    /* Draw separators for the displayed values */
+    nextion_display.printf("line 10,160,190,160,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 10,320,190,320,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 610,160,790,160,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+    nextion_display.printf("line 610,320,790,320,%d", WHITE);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);  
     
     /* Set font color */
-    nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+    nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
+    nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
     nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-    nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-    
-   // pc.printf("t6.pco=%d\n\r", NORMAL_COLOR);
-    //pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff); 
-   // pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-   // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-    //pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-    
+  /*  
+    pc.printf("t6.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff); 
+    pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
+    pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
+    pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
+ */   
     /* Print default values */
     Volume_Setpoint_Fix();
     Resp_Frequency_Fix();
@@ -76,22 +90,16 @@
      /* Remove the selection box from the previous position */
       switch(past_parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", NORMAL_COLOR);
-              //  pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;  
@@ -101,22 +109,16 @@
      /* Draw the selection box in the new position */ 
       switch(parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", SELECT_COLOR);
+                nextion_display.printf("t1.pco=%d", SELECT_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", SELECT_COLOR);
-               // pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", SELECT_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", SELECT_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t7.pco=%d\n\r", SELECT_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", SELECT_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", SELECT_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;  
@@ -131,22 +133,16 @@
 
       switch(parameter_selection_index){
             case 0:
-                nextion_display.printf("t6.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("t1.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t6.pco=%d", NORMAL_COLOR);
-               // pc.printf("%c%c%c", 0xff, 0xff, 0xff);
                 break;
             case 1:
+                nextion_display.printf("t4.pco=%d", NORMAL_COLOR);
+                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+                break;
+            case 2:
                 nextion_display.printf("t7.pco=%d", NORMAL_COLOR);
                 nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-              //  pc.printf("t7.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
-                break;
-            case 2:
-                nextion_display.printf("t8.pco=%d", NORMAL_COLOR);
-                nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-               // pc.printf("t8.pco=%d\n\r", NORMAL_COLOR);
-               // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
                 break;
             default:
                 break;   
@@ -162,10 +158,8 @@
     volume_setpoint_display = VOLUME_SETPOINT_MINIMUM_VALUE  + VOLUME_SETPOINT_STEP*volume_setpoint_index;
     
     /* Print volume value */
-    nextion_display.printf("t6.txt=\"%d mL\"", volume_setpoint_display);
+    nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t6.txt=\"%d mL\"\n\r", volume_setpoint_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 } 
 
 
@@ -175,10 +169,8 @@
     stepper_parameters_update_flag = 1;
     
     /* Print volume value */
-    nextion_display.printf("t6.txt=\"%d mL\"", volume_setpoint_display);
+    nextion_display.printf("t1.txt=\"%d\"", volume_setpoint_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t6.txt=\"%d mL\"\n\r", volume_setpoint_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 } 
 
 
@@ -187,10 +179,8 @@
     resp_frequency_display = 10 + resp_frequency_index;
     
     /* Print frequency value */
-    nextion_display.printf("t7.txt=\"%d RPM\"", resp_frequency_display);
+    nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t7.txt=\"%d RPM\"\n\r", resp_frequency_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 }  
 
 
@@ -200,11 +190,8 @@
     stepper_parameters_update_flag = 1;
     
     /* Print frequency value */
-    nextion_display.printf("t7.txt=\"%d RPM\"", resp_frequency_display);
+    nextion_display.printf("t4.txt=\"%d\"", resp_frequency_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-  //  pc.printf("t7.txt=\"%d RPM\"\n\r", resp_frequency_display);
-  //  pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);    
- 
 }  
 
 void I_E_Ratio_Display_Update(void){
@@ -213,10 +200,8 @@
     expiration_time_display = i_e_ratio_display_table[i_e_ratio_index][1];
     
     /* Print i:e ratio value */
-    nextion_display.printf("t8.txt=\"%2.1f/%2.1f\"", inspiration_time_display, expiration_time_display);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t8.txt=\"%2.1f/%2.1f\"\n\r", inspiration_time_display, expiration_time_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);   
+    nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
 }  
 
 
@@ -227,23 +212,120 @@
     stepper_parameters_update_flag = 1;
     
     /* Print i:e ratio value */
-    nextion_display.printf("t8.txt=\"%2.1f/%2.1f\"", inspiration_time_display, expiration_time_display);
+    nextion_display.printf("t7.txt=\"%2.1f : %2.1f\"", inspiration_time_display, expiration_time_display);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
-   // pc.printf("t8.txt=\"%2.1f/%2.1f\"\n\r", inspiration_time_display, expiration_time_display);
-   // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
 }  
 
-void Pressure_01_Display_Update(void){
-    nextion_display.printf("t1.txt=\"%.2f mL\"", pressure_01_psi);
+void Volume_Display_Update(void){
+    nextion_display.printf("t11.txt=\"%.2f\"", volume_ml);
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+}
+
+void Pressure_Display_Update(void){
+    nextion_display.printf("t15.txt=\"%.1f\"", (pressure_02_psi * PSI_TO_CMH2O_CONSTANT));
+    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+}
+
+void Flow_Display_Update(void){
+    nextion_display.printf("t14.txt=\"%.2f\"", flow_lpm);
     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
 }
 
-void Pressure_02_Display_Update(void){
-    nextion_display.printf("t2.txt=\"%.2f mL\"", pressure_02_psi);
+
+void Nextion_Plot_Volume_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (VOLUME_WAVEFORM_Y0 + VOLUME_WAVEFORM_HEIGHT - 1);
+    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)*2) ;
+    
+    x_pos_counter++;
+    
+    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("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = VOLUME_WAVEFORM_X0 + x_pos_counter;
+    y_pos = VOLUME_WAVEFORM_Y0 + VOLUME_WAVEFORM_HEIGHT - volume_scaled_value - 1;
+    
+    if(y_pos <= VOLUME_WAVEFORM_Y0){
+        y_pos = VOLUME_WAVEFORM_Y0 + 1;
+    }
+    
+    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; 
 }
 
-void Diff_Pressure_Display_Update(void){
-    nextion_display.printf("t0.txt=\"%.2f mL\"", diff_pressure_psi);
+
+void Nextion_Plot_Flow_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - 1);
+    uint16_t x_pos, y_pos;
+    uint16_t flow_scaled_value;
+    
+    flow_scaled_value = 79 + (int16_t)(flow_lpm * FLOW_WAVEFORM_HEIGHT);
+    
+    x_pos_counter++;
+    
+    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("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = FLOW_WAVEFORM_X0 + x_pos_counter;
+    y_pos = FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - flow_scaled_value - 1;
+    
+    
+    if(y_pos <= FLOW_WAVEFORM_Y0){
+        y_pos = VOLUME_WAVEFORM_Y0 + 1;
+    }else if(y_pos >= (FLOW_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT)){
+        y_pos = VOLUME_WAVEFORM_Y0 + FLOW_WAVEFORM_HEIGHT - 1;
+    
+    }
+    
+    
+    
+    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);
-}
\ No newline at end of file
+    
+    prev_y_pos = y_pos; 
+}
+
+
+void Nextion_Plot_Pressure_Waveform(void){
+    
+    static uint16_t x_pos_counter = 1, prev_y_pos = (PRESSURE_WAVEFORM_Y0 + PRESSURE_WAVEFORM_HEIGHT - 1);
+    uint16_t x_pos, y_pos;
+    uint16_t pressure_02_scaled_value;
+    
+    pressure_02_scaled_value = (uint16_t)((pressure_02_psi - 0.5)*PRESSURE_WAVEFORM_HEIGHT*2) ;
+    
+    x_pos_counter++;
+    
+    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("%c%c%c", 0xff, 0xff, 0xff);
+        x_pos_counter = 1;
+    }
+    
+    x_pos = PRESSURE_WAVEFORM_X0 + x_pos_counter;
+    y_pos = PRESSURE_WAVEFORM_Y0 + PRESSURE_WAVEFORM_HEIGHT - pressure_02_scaled_value - 1;
+    
+    
+    if(y_pos <= PRESSURE_WAVEFORM_Y0){
+        y_pos = PRESSURE_WAVEFORM_Y0 + 1;
+    }
+    
+    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; 
+}
+