/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 3.4.2013
Author  : NeVaDa
Company : 
Comments: 


Chip type               : ATmega64L
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*****************************************************/

// je treba zkusit zapinani a vypinani 40 kHz signalu, udelat aby bylo 5 kmitu




//#define SWITCH5 PINE.7                   // tlacitka - v klidu 1, stisknute 0
//#define SWITCH4 PINE.6
#define SWITCH1 PINE.5
#define SWITCH2 PINE.4
#define SWITCH3 PINE.3

//#define F_CPU 8000000L


#include <mega64.h>

#include <delay.h>

#include "t6963c.h"
#include "graphic.h"
#include "functions.h"

// Standard Input/Output functions
#include <stdio.h>


    unsigned int sound_speed;           // rychlost zvuku
    unsigned int temperature;           // teplota
    unsigned char period_signal=6;           // pocet period ve vyslanem signalu    
    unsigned char period_adc=4;             // perida prevodu adc 
    unsigned char scale=10;                // meritko grafu
//#define ADC_VREF_TYPE 0x00

    eeprom char e_period_signal;
    eeprom char e_period_adc;
    eeprom char e_scale;    

void main(void)
{

    int array[100];

    unsigned char b;
    unsigned int a,cas, m;

    unsigned char n, pom;
    float x;
    unsigned char status=1;  //1-mereni, 0-stop, 2-nastaveni         
    
    unsigned char setting=1;
    
    init_mcu();

///////////////////////////////////////////////////////////////////////////////////




    LCD_init(); 
    LCD_clr_text();
    LCD_clr_gr(0x00);      
    
       
    LCD_mode_set(LCD_MODE_EXOR);    
    LCD_display_mode(LCD_CURS_OFF,LCD_TEXT_ON_GRAPH_ON,1);       
    LCD_diacritics_generate();    
    
    
    if(e_period_signal==0xFF) period_signal = 6;          //nahrani z eeprom (pokud je vymazana tak defaultni hodnoty)
    else period_signal = e_period_signal;
    if(e_period_adc==0xFF) period_adc = 4;
    else period_adc = e_period_adc; 
    if(e_scale==0xFF) scale = 10;
    else scale = e_scale; 
    
    //period_adc=4;
    //period_signal=6;        // pocet kmitu v davce
    ADCSRA=0x80+period_adc;    //perioda prevodu adc      
    
    
    intro();
    while((SWITCH1&&SWITCH2&&SWITCH3));
    while(!(SWITCH1&&SWITCH2&&SWITCH3));
    LCD_clr_text();
    LCD_clr_gr(0x00);
    
while (1)
      {   
      
      
    
      
      
      if(status==1)              //MERENI
      { 
      //a=0;
      //b=0;
      // Place your code here 
  
      //a = read_adc_temperature(4);
      //a=a*2.5;      
      //temperature = temperature * 244;
      
      compute_temperature();
      show_temperature();
       
      
      ADCSRA=0x80+period_adc;    //perioda prevodu adc 
      
      ///////////////////////////////////////////////////////    
      
      for(n=0;n<99;n++)                      //smazani prubehu predchoziho
      {
      //LCD_set_pixel(n, 127-array[n], 1);
      
      LCD_line(n+13,113-array[n],n+14,113-array[n+1],0);                
      }      
      
      
      
      generate_usonic(period_signal);
      
      for(n=0;n<100;n++)              //prevod
      {
      array[n]=read_adc_usonic(0);
      //PORTA=PORTA+1;
      }
      
     
      //a = read_adc_usonic(0);
      //LCD_text_xy(0,1);                               
      
/*      a=array[0];                      //hledani maxima
      for (n=0;n<100;n++)
      { 
      if(array[n]>a) {  a=array[n];
                        cas=(n)*38; } 
      }
      show_int(a);  
      
      
      x=(cas/1000.0)*173.5; 
      LCD_text_xy(8,1);                              
      a=x;
      show_int(a);      */
      
      for (n=0;n<100;n++)               //deleni 10
      {
      array[n]=array[n]/scale;          
      if(array[n]>100) array[n]=100;
      }
      
      
      for(n=0;n<99;n++)                      //vykresleni
      {
      //LCD_set_pixel(n, 127-array[n], 1);
      
      LCD_line(n+13,113-array[n],n+14,113-array[n+1],1);                
      
      }
      
      //delay_ms(500);
      for(n=0;n<53;n++)            // osy
      {
      LCD_set_pixel(12+n*2,114,1);
      LCD_set_pixel(12,114-n*2,1);     
      }    
      
      icon_1();       //ikony

      LCD_text_xy(20,13);   //popis os
      LCD_char('t'); 
      LCD_text_xy(0,1);
      LCD_char('U');                
      
      
      //LCD_text_xy(8,2);                           
      //show_int(periods);
      
     for(m=0;m<500;m++)
      {      
      if(SWITCH1==0)
                {
                   status=2; 
                     
                    while(SWITCH1==0);
                }
      if(SWITCH2==0)
                {
                    //if(status==0) status=1;
                    status=0;
                    //if(status==1) status=0;
                    
                    while(SWITCH2==0);
                }                
      delay_ms(1);      
      }  
      
      
      /*for(m=0;m<500;m++)
      {      
      if(PINE.3==0)
                {
                    if(o==7)o=7;
                    else o=o+1;
                     
                    while(PINE.3==0);
                }
      if(PINE.4==0)
                {
                    if(o==0)o=0;
                    else o=o-1;
                    
                    while(PINE.4==0);
                }                
      delay_ms(1);      
      } */          
      
      //delay_ms(500);
       
      //ADCSRA=0x80+o;
      
      
      //LCD_clr_gr(0x00);
      

      
      }                  

      if(status==0)                   // PAUZA
      {
      if(SWITCH1==0)
                {  
                    //pom=status;
                    status=2; 
                     
                    while(SWITCH1==0);
                }
      if(SWITCH2==0)
                {
                    //if(status==0) status=1;
                    //if(status==1) status=0;
                    status=1;
                    
                    while(SWITCH2==0);
                }
      }
      

      if(status==2)                   //nastaveni
      {
            LCD_clr_text();
            LCD_clr_gr(0x00);
            setting=1;
            
            icon_2();
            
            LCD_text_xy(1,1);
            LCD_string("Nastaven");
            LCD_char_diacritic(I_DLOUHE_M);
            LCD_char(':');
            
            LCD_text_xy(1,3);
            LCD_string("perioda vzorkov");
            LCD_char_diacritic(A_DLOUHE_M);
            LCD_char('n');
            LCD_char_diacritic(I_DLOUHE_M);
            LCD_char(':'); 
            
            LCD_text_xy(8,4);
            show_period_adc();            
            
            LCD_text_xy(1,6);
            LCD_string("po");
            LCD_char_diacritic(C_HACEK_M);
            LCD_string("et kmitu v d");
            LCD_char_diacritic(A_DLOUHE_M);
            LCD_string("vce:");
            
            LCD_text_xy(8,7);
            show_char(period_signal);  
            
            LCD_text_xy(1,9);
            LCD_char('m');
            LCD_char_diacritic(E_HACEK_M);
            LCD_char_diacritic(R_HACEK_M);
            LCD_char_diacritic(I_DLOUHE_M);
            LCD_string("tko grafu:");
            
            LCD_text_xy(8,10);
            show_char(scale);   
            
            LCD_text_xy(11,10);
            LCD_string("x d");
            LCD_char_diacritic(E_HACEK_M);
            LCD_string("leno");                        
            
            LCD_text_xy(1,12);
            LCD_string("zp");
            LCD_char_diacritic(E_HACEK_M);
            LCD_char('t');                            
            
            LCD_display_mode(LCD_CURS_ON_BLINK_ON,LCD_TEXT_ON_GRAPH_ON,1);
            LCD_cursor_xy(1,3);
            
            while(setting!=44)
            {
            if(SWITCH1==0)
                {
                    if(setting==1)setting=11;
                    if(setting==2)setting=22;
                    if(setting==3)setting=33;
                    if(setting==4)setting=44; 
                    while(SWITCH1==0);
                }
            if(SWITCH2==0)
                {
                    setting=setting+1;
                    if(setting==5) setting=1;   
                    while(SWITCH2==0);
                } 
            if(SWITCH3==0)
                {
                    setting=setting-1;
                    if(setting==0) setting=4;   
                    while(SWITCH3==0);
                }
                
            if(setting==1) LCD_cursor_xy(1,3);
            if(setting==2) LCD_cursor_xy(1,6);
            if(setting==3) LCD_cursor_xy(1,9);
            if(setting==4) LCD_cursor_xy(1,12); 
            
            if(setting==11) {                       // nastaveni poctu kmitu v davce
                            LCD_cursor_xy(8,4);
                            while(setting!=1)
                            {
                                if(SWITCH2==0)
                                {
                                if(period_adc==1) period_adc=1;
                                else period_adc=period_adc-1;                                   
                                while(SWITCH2==0);
                                }
                                if(SWITCH3==0)
                                {
                                if(period_adc==7) period_adc=7;
                                else period_adc=period_adc+1;                                   
                                while(SWITCH3==0);
                                }
                                if(SWITCH1==0)
                                {
                                setting=1;                                   
                                while(SWITCH1==0);
                                }
                            LCD_text_xy(8,4);
                            show_period_adc();     
                            }
                            }
            
            if(setting==22) {                       // nastaveni poctu kmitu v davce
                            LCD_cursor_xy(8,7);
                            while(setting!=2)
                            {
                                if(SWITCH2==0)
                                {
                                if(period_signal==1) period_signal=1;
                                else period_signal=period_signal-1;                                   
                                while(SWITCH2==0);
                                }
                                if(SWITCH3==0)
                                {
                                if(period_signal==10) period_signal=10;
                                else period_signal=period_signal+1;                                   
                                while(SWITCH3==0);
                                }
                                if(SWITCH1==0)
                                {
                                setting=2;                                   
                                while(SWITCH1==0);
                                }
                            LCD_text_xy(8,7);
                            show_char(period_signal);    
                            }
                            }
                            
            if(setting==33) {                       // nastaveni poctu kmitu v davce
                            LCD_cursor_xy(8,10);
                            while(setting!=3)
                            {
                                if(SWITCH2==0)
                                {  
                                while(SWITCH2==0);
                                switch(scale) 
                                {
                                case 1: scale=10;
                                        break;
                                case 2: scale=1;
                                        break;
                                case 5: scale=2;
                                        break;
                                case 10:scale=5;
                                        break;
                                } 
                                //if(scale==10) scale=5;
                                //if(scale==5) scale=2;
                                //if(scale==2) scale=1;
                                //if(scale==1) scale=10;              
                                
                                }
                                if(SWITCH3==0)
                                { 
                                while(SWITCH3==0); 
                                switch(scale) 
                                { 
                                
                                case 1: scale=2;
                                        break;
                                case 2: scale=5;
                                        break;
                                case 5: scale=10;
                                        break;
                                case 10:scale=1;
                                        break;
                                } 
                                //if(scale==10) scale=1;
                                //if(scale==5) scale=10;
                                //if(scale==2) scale=5;
                                //if(scale==1) scale=2;                             
                                
                                }
                                if(SWITCH1==0)
                                {
                                setting=3;                                   
                                while(SWITCH1==0);
                                }
                            LCD_text_xy(8,10);
                            show_char(scale);    
                            }
                            }
            }
            status=1; 
            
            e_scale = scale;                    // ulozeni do eeprom pameti             
            e_period_adc = period_adc;
            e_period_signal = period_signal;       
            
            //jestli chci aby mi po kdyz je predtim pauza pak zustalo v pauze, tak status=pom musi byt ale musim vzkreslit teplotu, graf a ikony    
            
            LCD_display_mode(LCD_CURS_OFF,LCD_TEXT_ON_GRAPH_ON,1);
            LCD_clr_text();
            LCD_clr_gr(0x00);      
      }
      
      }
 /*     for(n=0;n<100;n++)
      {
      array[n]=n;
      } */ 
      
/*      while(1)
{        
      TCCR0 = 0x19;
      delay_us(150);
      TCCR0 = 0x18;
      delay_ms(10);
} */          


      //}   
      //delay_ms(500);
      //}        
      
}
