pachas

Dependencies:   mbed QEI FastPWM

nextion_interface.cpp

Committer:
miguelangel_2511
Date:
2020-04-20
Revision:
6:d38287621cca
Child:
7:f4e248182d31

File content as of revision 6:d38287621cca:


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

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


/* 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(9600);
    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);
}