para el ventilador

Dependencies:   QEI FastPWM

Revision:
8:208d965a3bd2
Parent:
7:f4e248182d31
Child:
9:95fdcdc0977e
--- a/ventilator.cpp	Mon Apr 20 21:58:36 2020 +0000
+++ b/ventilator.cpp	Thu Apr 23 17:05:36 2020 +0000
@@ -8,8 +8,12 @@
 #include "encoder_interface.h"
 #include "nextion_interface.h"
 #include "pressure_sensors.h"
+#include "stepper_motor_driver.h"
 #include "ventilator.h"
 
+/* Object definition */
+Ticker ticker_int_1ms; // Ticker interrupt
+
 
 /* Global variable definition */
 
@@ -30,6 +34,16 @@
 uint8_t first_time_in_state = 1;
 
 /* Function definition */
+
+void System_Initialize(void){
+    Buttons_Initialize();
+    Nextion_Interface_Initialize();
+    Pressure_Sensors_Initialize();
+    ticker_int_1ms.attach(&Ticker_ISR, 0.001);
+    Stepper_Driver_Init();
+}
+
+
 void Nextion_Encoder_Interaction(void){
 
  switch(main_screen_state){
@@ -37,6 +51,7 @@
                 if(Get_Button_Press(1 << ENTER_SW)){
                     rotary_encoder.reset();
                     main_screen_state = Main_Screen_Cursor_Enable;
+                    first_time_in_state = 1;
                 }
                 
                 break;  
@@ -63,7 +78,7 @@
             case Main_Screen_Volume_Setpoint_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t6.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t1.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                    // pc.printf("t6.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -87,7 +102,7 @@
             case Main_Screen_Resp_Frequency_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t7.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t4.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                    // pc.printf("t7.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -112,7 +127,7 @@
             case Main_Screen_I_E_Ratio_Adjust:
                 if(first_time_in_state){
                     first_time_in_state = 0;
-                    nextion_display.printf("t8.pco=%d", ADJUST_COLOR); // Change font color
+                    nextion_display.printf("t7.pco=%d", ADJUST_COLOR); // Change font color
                     nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
                     //pc.printf("t8.pco=%d\n\r", ADJUST_COLOR); // Change font color
                    // pc.printf("%c%c%c\n\r", 0xff, 0xff, 0xff);
@@ -142,54 +157,84 @@
  
  void Nextion_Update_Sensor_Values(void){
     
-    static float previous_pressure_01_psi = 0, previous_pressure_02_psi = 0;
-    static float previous_diff_pressure_psi = 0;
+    static float previous_pressure_02_psi = 0;
+    static float previous_volume_ml = 0;
+    static float previous_flow_lpm = 0;
     float delta;
     
     /* Check if anu value has change in a meningful way */
-    delta = pressure_01_psi - previous_pressure_01_psi;
-    if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Pressure_01_Display_Update();
+    delta = volume_ml - previous_volume_ml;
+    if((delta > VOLUME_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
+        Volume_Display_Update();
     } 
     
     delta = pressure_02_psi - previous_pressure_02_psi;
     if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Pressure_02_Display_Update();
+        Pressure_Display_Update();
     }
     
-    delta = diff_pressure_psi - previous_diff_pressure_psi;
-    if((delta > PRESSURE_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
-        Diff_Pressure_Display_Update();
+    delta = flow_lpm - previous_flow_lpm;
+    if((delta > FLOW_DISPLAY_THRESHOLD) || (delta < -PRESSURE_DISPLAY_THRESHOLD)){
+        Flow_Display_Update();
     }    
     
     
     /* Update the previous values */
-    previous_pressure_01_psi = pressure_01_psi;
+    previous_volume_ml = volume_ml;
     previous_pressure_02_psi = pressure_02_psi;
-    previous_diff_pressure_psi = diff_pressure_psi;
- 
+    previous_flow_lpm = flow_lpm;
+
  }
  
  
-  void Nextion_Update_Waveform_Values(void){
- 
-    uint8_t pressure_graph_val;
-    
-    /* Plot pressure 01 in a 0 to 1 PSI scale */
-    pressure_graph_val = (uint8_t)(pressure_01_psi * 128);
-    nextion_display.printf("add 10,0,%d", pressure_graph_val);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
+void Nextion_Update_Waveform_Values(void){
+    Nextion_Plot_Volume_Waveform();  
+    Nextion_Plot_Pressure_Waveform();  
+    Nextion_Plot_Flow_Waveform(); 
+}
+
+
+// Ticker Interrupt Service Routine
+void Ticker_ISR(void){
+   
+    static uint8_t debounce_conta_1ms = 0;
+    static uint8_t encoder_conta_1ms = 0;
+    static uint8_t sensor_conta_1ms = 0;
+    static uint8_t sensor_waveform_conta_1ms = 0;
+    static uint8_t sensor_display_conta_1ms = 0;
+     
+    debounce_conta_1ms++;
+    encoder_conta_1ms++;
+    sensor_conta_1ms++;
+    sensor_waveform_conta_1ms++;
+    sensor_display_conta_1ms++;
     
-    /* Plot pressure 02 in a 0 to 1 PSI scale */
-    pressure_graph_val = (uint8_t)(pressure_02_psi * 128);
-    nextion_display.printf("add 11,0,%d", pressure_graph_val); 
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff); 
-    
-    /* Plot differential pressure in a -1 PSI to 1 PSI scale */
-    pressure_graph_val = 64 + (uint8_t)(diff_pressure_psi * 64);
-    nextion_display.printf("add 1,0,%d", pressure_graph_val);
-    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);  
- 
-  }
+    // Read buttons and performs debounce action
+    if(debounce_conta_1ms == DEBOUNCE_PERIOD_MS){
+        debounce_conta_1ms = 0;
+        Button_Debounce();
+    }    
+    // Read encoder and update the proper indexes    
+    if(encoder_conta_1ms == ENCODER_READ_PERIOD_MS){
+        encoder_conta_1ms = 0;
+        Encoder_Read();
+    }        
+    // Indicates if it is time to acquire a new sample from the sensors
+    if(sensor_conta_1ms == PRESSURE_SENSOR_READ_PERIOD_MS){
+        sensor_conta_1ms = 0;
+        read_pressure_sensors_flag = 1;
+    }       
+    // Indicates if it is time to plot the volume, flow and pressure values */   
+    if(sensor_waveform_conta_1ms  == SENSOR_WAVEFORM_PLOT_PERIOD_MS){
+        sensor_waveform_conta_1ms  = 0;
+        pressure_sensor_waveform_update_flag = 1;
+    }    
+    // Indicates if it is time to display the volume, flow and pressure values */   
+    if(sensor_display_conta_1ms  == SENSOR_DISPLAY_PERIOD_MS){
+        sensor_display_conta_1ms  = 0;
+        pressure_sensor_display_update_flag = 1;
+    }               
+        
+}
  
         
\ No newline at end of file