#include <main.h>

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6

#include <lcd.c>

void main()
{
   lcd_init();

set_tris_b(0b00000000);
set_tris_c(0b00000111);
set_tris_d(0x00);

   
output_low(LED1);
output_low(LED2);
output_low(LED3);
   
output_low(MOTOR_IN1);
output_low(MOTOR_IN2);
   
// lcd SHOULD DISPLAY 'system ready'???
//vIRTUAL TERMINAL DISPLAYS 'System Ready'   ???
 // include a delay???

lcd_putc("\fSystem Ready");
printf("System Ready\r\n");
delay_ms(500);
//_______________________________
//WAIT FOR SWITCH 1 TO BE PRESS
while(!input(SWI1));

output_high(LED1);  // LED1
output_low(LED2); 
output_low(LED3); 


lcd_putc("\fEngine Started");
printf("Engine Started\r\n");

delay_ms(300);
//_________________________________
// add details on virtual terminal???

//WAIT FOR SWITCH 2 TO BE PRESS
while(!input(SWI2));

output_low(LED1); 
output_high(LED2);   // LED2
output_low(LED3); 

lcd_putc("\fCoolant OK");

printf("Coolant OK\r\n");

delay_ms(300);
//_________________________________
//WAIT FOR SWITCH 3 TO BE PRESS
while(!input(SWI3));
   
output_low(LED1);    
output_low(LED2); 

delay_ms(300);
//__________________________________
//MOTOR START
lcd_putc("\fMotor Running");
printf("Motor Running\r\n");

output_high(MOTOR_IN1);
output_low(MOTOR_IN2);

delay_ms(3000);   // 3 seconds
//_________________________________
output_low(MOTOR_IN1);
output_low(MOTOR_IN2);
//_________________________________
 lcd_putc("\fALERT!");
printf("ALERT!\r\n");

// Blink LED3 (3 times, 100ms)
for(int i = 0; i < 3; i++) {
   output_high(LED3);
   delay_ms(100);
   output_low(LED3);
   delay_ms(100);
}


lcd_putc("\fShutdown Done");
printf("Shutdown Done\r\n");

while(TRUE) {
   // system stopped
}

}

