#include ".\main.h"

#define LED1 PIN_C6  //CHANGE PIN_XX TO YOUR LED PIN NUMBER 
#define LED2 PIN_C5  //CHANGE PIN_XX TO YOUR LED PIN NUMBER
#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_B5


//#define MAXDUTY   (int16)160   // 50 kHz
#define MAXDUTY   (int16)400   // 20 kHz
//#define MAXDUTY   (int16)800   // 10 kHz
#define HALFDUTY  (int16)MAXDUTY/2
//#define DIVISOR   199 // 10 kHz  
#define DIVISOR   99 // 20 kHz  
//#define DIVISOR   39  // 50 kHz

#define MAX_OUTPUT   530
#define MIN_OUTPUT   220
#define raw_step     50
#define fine_step    5



/// Linear interpolation parameters
//

#define k   0.333961
#define q   206.952


unsigned int16 setpoint=43;
unsigned int16 adc_value; 
int1 output_permit=FALSE;

#INT_AD
void adc_handler() {

     adc_value = read_adc(ADC_READ_ONLY);
      if(output_permit == TRUE)
      {
         if (adc_value <= setpoint) 
         {
            set_pwm2_duty((int16)255);      // zapnuti menice
            output_low(LED2);
         }
         else         // vypnuti menice
         {
           set_pwm2_duty((int16)MAXDUTY);
           output_high(LED2);
         }

      }
      else
      {
           set_pwm2_duty((int16)MAXDUTY);
           output_high(LED2);
      }
      read_adc(ADC_START_ONLY);
}

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 int1  button_press;  // semafor pro cteni tlacitek
float output_voltage;
float voltage_setpoint = 300;

   setup_adc_ports(sAN0|VSS_VREF);
   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,DIVISOR,1); 
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   set_pwm1_duty(HALFDUTY);
   set_pwm2_duty(MAXDUTY);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   setup_oscillator(OSC_8MHZ);

   enable_interrupts(INT_AD);
   enable_interrupts(GLOBAL);

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

   set_pwm2_duty((int16)MAXDUTY);

   lcd_init();
   set_adc_channel(0);
   
   lcd_gotoxy(1,1);
   lcd_putc("HVPS01A");
   lcd_gotoxy(1,2);
   lcd_putc(" MLAB.cz");
   Delay_ms(1000);
   
   lcd_putc("\f");

   read_adc(ADC_START_ONLY);
   
   while(true)
   {
      if(input(OUTPUT_ENABLE)) output_permit=TRUE;
      else output_permit=FALSE;

      output_voltage = adc_value * k + q;

      lcd_gotoxy(1,1);
      printf(lcd_putc,"%3.0f",output_voltage);
      lcd_gotoxy(1,2);

      voltage_setpoint = setpoint * k + q;

      if(input(OUTPUT_ENABLE))printf(lcd_putc,"Set:%3.0f ", voltage_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(voltage_setpoint < (MAX_OUTPUT - raw_step) ) voltage_setpoint += raw_step;
            }
         }

         if(!input(S2))
         {
            delay_ms(20);
            if(!input(S2))
            {
               button_press=true;
               sound_beep(100,600);
               if(voltage_setpoint > (raw_step + MIN_OUTPUT) ) voltage_setpoint -= raw_step;
            }
         }

         if(!input(S3))
         {
            delay_ms(20);
            if(!input(S3))
            {
               button_press=true;
               sound_beep(100,500);
               if(voltage_setpoint < MAX_OUTPUT - fine_step  )voltage_setpoint += fine_step ;
            }
         }

         if(!input(S4))
         {
            delay_ms(20);
            if(!input(S4))
            {
               button_press=true;
               sound_beep(100,400);
               if(voltage_setpoint > (fine_step + MIN_OUTPUT) ) voltage_setpoint -= fine_step;
            }
         }

         setpoint =(int16) ((voltage_setpoint - q) / k);
      }
      
      if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false;
      output_toggle(LED1);
    }

}