#include "C:\Users\Honza\Documents\pic\poloha_slunce\main.h"

#include "math.h"

#define PIN_SDA  PIN_B0
#define PIN_SCL  PIN_B1
#use i2c(master, sda=PIN_SDA, scl=PIN_SCL)
//set RS232
#use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD


signed int16 AZIMUT;
signed int ELEVACE;

int stupne (float rad)
{
int a =(int) ((rad/3.14)*180);
return a;
}

float rad (float stup)
{
float a = ((stup/180)*3.14);
return a;
}

//provede vypocet dnu od zacatku roku
int16 den (int m, int16 d) //vstupem je mesic a den
{
switch (m) {

    case 1: d=d;

           break;

    case 2: d=31+d;

           break;
           
    case 3: d=58+d;

           break;

    case 4: d=89+d;

           break;
    case 5: d=119+d;

           break;

    case 6: d=150+d;

           break;
    case 7: d=180+d;

           break;

    case 8: d=211+d;

           break;
    case 9: d=242+d;

           break;

    case 10: d=262+d;

           break;
    case 11: d=303+d;

           break;

    case 12: d=333+d;

           break;

    default:printf("chyba");

           break; }
           
           
 printf("Den: %ld\r\n", d);          
    return d;

}

float deklinace (float d) //vstupem je den v roce
{
float b = 0.98630137;
d=((rad(23.45))*sin(rad(b*(d-81))));
return d;

}

float lstm (int gmt) //vstupem je GMT dane lokality
{
float a;
a=((rad(15))*gmt);
return a;
}


float eot (float d) //vstupem je den v roce
{
float a;
a=rad(0.98630137*(d-81));
a=9.87*sin(2*a)-7.53*cos(a)-1.5*sin(a);
return a;

}


float tc (int delka, float lstm, float eot) //vstupem je zemepisná delka, LSTM, EoT
{
float a;
a=4*(float)(delka-lstm)+eot;
return a;
}


float lst (float lt, float tc) //vstupem je den GMT dane lokality
{
float a;
a=lt+(tc/60);
return a;
}

float hra (float lst) //vstupem je den v roce
{
lst=(rad(15))*(lst-12);
return lst;

}

void poziceSL(int m, int d, int h, int min, int gmt, float sirka, float delka) //vstupem je mesic, den, hodina, minuty, GMT, zemepisna sirka ve stupnich
{
float den1;
float ele,azi,deklin, lstm1, eot1, tc1, lst1, hra1;
float lt = h+(min/60);

den1=(float) den(m,d);
deklin=deklinace(den1);
lstm1=lstm(gmt);
eot1=eot(den1);
tc1=tc(delka, lstm1, eot1);
lst1=lst(lt,tc1);
hra1=hra(lst1);

sirka=rad(sirka);
ele=asin((sin(deklin)*sin(sirka))+(cos(deklin)*cos(sirka)*cos(hra1)));
azi = acos(((sin(deklin)*cos(sirka))-(cos(deklin)*sin(sirka)*cos(hra1)))/(cos(ele)));

//printf("Azimut: %6.3f\r\n", azi);
//printf("Elevce: %6.3f\r\n", ele);
ELEVACE=stupne(ele);
AZIMUT= stupne(azi);
if(hra1>0)
{
AZIMUT= 360-AZIMUT;
}
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard

   //TODO: User Code

while(TRUE)
{
//do promenne AZIMUT vlosti azimut a do ELEVACE elevaci slunce
poziceSL(11, 18, 14, 0, 0, 49, 14); //int mesic, den, hodina, minuta, odchylka èasu od GMT, sirka, delka


printf("Azimut: %ld\r\n", AZIMUT);
printf("Elevce: %d\r\n", ELEVACE);
delay_ms(2000);

}




}