#include ".\main.h"

#define LED1 PIN_C6  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED2 PIN_C5  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED3 PIN_C4  //CHANGE PIN_XX TO YOUR LED PIN NUMBER
#define LED4 PIN_D3  //CHANGE PIN_XX TO YOUR LED PIN NUMBER

#define S1 PIN_B0  //Raw up
#define S2 PIN_B1  //Raw down
#define S3 PIN_D0  //Fine up
#define S4 PIN_D1  //Fine down

#define BEEP PIN_D2  //piezo beeper

#define LCD_ENABLE_PIN  PIN_E0                                    ////
#define LCD_RS_PIN      PIN_E1                                    ////
#define LCD_RW_PIN      PIN_E2                                    ////
#define LCD_DATA4       PIN_D4                                    ////
#define LCD_DATA5       PIN_D5                                    ////
#define LCD_DATA6       PIN_D6                                    ////
#define LCD_DATA7       PIN_D7 
#include <lcd.c>

#define OUTPUT_ENABLE   PIN_C1

void sound_beep( unsigned int lenght, int16 frequency)
{
unsigned int i;
   
   for(i=0;i<=lenght;i++)
   {
      output_toggle(BEEP);
      delay_us(1/frequency);
   }
}

void main()
{
unsigned int16 setpoint=43;
unsigned int16 napeti;   
unsigned int16 plneni=0;
unsigned int1  button_press;  // semafor pro cteni tlacitek

   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_32);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,255,1);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   setup_oscillator(OSC_8MHZ);

   set_pwm1_duty(0);
   set_pwm2_duty(0);

   output_high(LED1);
   output_high(LED2);
   output_high(LED3);
   output_high(LED4);
   output_low(BEEP);

   lcd_init();
   
   lcd_gotoxy(1,1);
   lcd_putc("HVPS01A");
   lcd_gotoxy(1,2);
   lcd_putc(" MLAB.cz");
   Delay_ms(1000);
   
   lcd_putc("\f");
   
   while(true)
   {
   
      set_adc_channel(1);
      delay_us(100);
      napeti = (napeti+read_adc())/2;
      if(input(OUTPUT_ENABLE))
      {
         if (napeti <= setpoint) 
         {
            if (plneni < 300) plneni++;
            output_low(LED2);
         }
         else
         {
            if (plneni > 0) plneni--;
            output_high(LED2);
         }
         set_pwm1_duty(plneni);
         set_pwm2_duty(1023-plneni);
      }
      else
      {
         set_pwm1_duty(0);
         set_pwm2_duty(1023);
      }
      
      lcd_gotoxy(1,1);
      printf(lcd_putc,"%4lu",napeti);
      lcd_gotoxy(1,2);

      if(input(OUTPUT_ENABLE))printf(lcd_putc,"Set:%lu   "setpoint);
      else printf(lcd_putc,"DISABLED");

      if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
      {
         if(!input(S1))
         {
            delay_ms(20);
            if(!input(S1))
            {
               button_press=true;
               sound_beep(100,700);
               if(setpoint < (1023-5) )setpoint+=5;
            }
         }

         if(!input(S2))
         {
            delay_ms(20);
            if(!input(S2))
            {
               button_press=true;
               sound_beep(100,600);
               if(setpoint > 0x05 ) setpoint-=5;
            }
         }

         if(!input(S3))
         {
            delay_ms(20);
            if(!input(S3))
            {
               button_press=true;
               sound_beep(100,500);
               if(setpoint < 1023 )setpoint++;
            }
         }

         if(!input(S4))
         {
            delay_ms(20);
            if(!input(S4))
            {
               button_press=true;
               sound_beep(100,400);
               if(setpoint > 0x00 ) setpoint--;
            }
         }
      }
      
      if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false;

      output_toggle(LED1);
      delay_ms(10);
    }

}