pachas

Dependencies:   mbed QEI FastPWM

nextion_interface.cpp

Committer:
miguelangel_2511
Date:
2020-04-20
Revision:
7:f4e248182d31
Parent:
6:d38287621cca
Child:
8:208d965a3bd2

File content as of revision 7:f4e248182d31:


#include "mbed.h"
#include "stdint.h"
#include "project_defines.h"
#include "nextion_interface.h"
#include "encoder_interface.h"
#include "pressure_sensors.h"
#include "stepper_motor_driver.h"
#include "ventilator.h"


/* Object definition */
Serial  nextion_display(NEXTION_DISPLAY_TX_PIN,NEXTION_DISPLAY_RX_PIN);
// For testing pusposes only
//Serial pc(USBTX, USBRX);

/* Global variable definition */

uint16_t volume_setpoint_display = VOLUME_SETPOINT_VALUE_DEFAULT;
uint16_t volume_measured_display = 480;
uint8_t resp_frequency_display = RESP_FREQUENCY_VALUE_DEFAULT;
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},
                                            {161,1,319,159},
                                            {321,1,478, 159}
                                            };  

float i_e_ratio_display_table[I_E_RATIO_INDEX_LIMIT][2]={
                                        {1.0,2.0},
                                        {1.0,2.5},
                                        {1.0,3.0}
                                        };


/* Function definition */

void Nextion_Interface_Initialize(void){
    /* Configure baud rate */ 
    nextion_display.baud(NEXTION_DISPLAY_BAUD_RATE);
    //pc.baud(115200);
    
    //pc.printf("PUERTO SERIAL HABILITADO\n\n\r");
    
    /* Set font color */
    nextion_display.printf("t6.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);
    
    /* Print default values */
    Volume_Setpoint_Fix();
    Resp_Frequency_Fix();
    I_E_Ratio_Fix(); 
     
} 




void Parameter_Selection_Box_Update(void){
     
     /* 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("%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("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;  
      }     
     
     
     /* 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("%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("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;  
      }
          
}  


void Parameter_Selection_Box_Remove(void){
      
      /* Remove the selection box from the current position */

      switch(parameter_selection_index){
            case 0:
                nextion_display.printf("t6.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("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;   
      }
               
}    
    
    
    
    
void Volume_Setpoint_Display_Update(void){

    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("%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);
} 


void Volume_Setpoint_Fix(void){

    volume_setpoint = volume_setpoint_display;
    stepper_parameters_update_flag = 1;
    
    /* Print volume value */
    nextion_display.printf("t6.txt=\"%d mL\"", 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);
} 


void Resp_Frequency_Display_Update(void){

    resp_frequency_display = 10 + resp_frequency_index;
    
    /* Print frequency value */
    nextion_display.printf("t7.txt=\"%d RPM\"", 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 Resp_Frequency_Fix(void){

    resp_frequency = resp_frequency_display;
    stepper_parameters_update_flag = 1;
    
    /* Print frequency value */
    nextion_display.printf("t7.txt=\"%d RPM\"", 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){
    
    inspiration_time_display = i_e_ratio_display_table[i_e_ratio_index][0];
    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);   
}  


void I_E_Ratio_Fix(void){
    
    inspiration_time = inspiration_time_display;
    expiration_time = expiration_time_display;
    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("%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);
    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);
    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
}

void Diff_Pressure_Display_Update(void){
    nextion_display.printf("t0.txt=\"%.2f mL\"", diff_pressure_psi);
    nextion_display.printf("%c%c%c", 0xff, 0xff, 0xff);
}