#include <inttypes.h>
#include "glcd/glcd.h"
#include "glcd/lfsr.h"
#include "font/nums.h"
#include "font/f9x14.h"
#include "adc.h"
#include <string.h>


void glcdPrintCenter(glcdRect_t r, char *text, uint8_t InFlashStored) {
    r.X1 += (r.X2 - r.X1 - glcdCharsWidth(text, InFlashStored)) / 2;
    r.Y1 += (r.Y2 - r.Y1 - glcd_FontHeight) / 2;
    glcdMoveTo(r.X1, r.Y1);
    glcdPrint(text, InFlashStored);
}

void mywait(int x){
	int y;
	while(x>0){
		y=320;
		while(y>0){
			y--;
		}
		x--;
	}
}
	


int main(void) {

//----- Taster -----------------
    PORTC=0xFF;

//------- Timer1 PWM --------------------
    DDRD=0xFF;
    TCCR1A= (1<<WGM11)|(1<<COM1A1);
    TCCR1B= (1<<CS10)|(1<<WGM12)|(1<<WGM13);
    ICR1=200;
    OCR1A=100;
 
//---- ADC ------
   PORTA=0xFF;
   adc_init();

#ifndef USE_AUTOINIT
    glcdDisplayInit();
#endif
  
    glcdClearScreen(1);

    int rollwait=0;
    int temp;
    int val;
    int x_val[]={32,19,6};
    int x_index=0;
    int y_val[]={0,7,148,773,1554,7804,15616};
    int y_index=0;

 
    glcdCoord_t x=0;
    glcdCoord_t y=0;
    glcdColor_t color=RGB(0xFF, 0x00, 0x00);
    glcdColor_t bg=RGB(0x60, 0xFF, 0x60);
    


   glcdRect_t r,s,t;

   glcdSetRect(r, 15, 15, SCREEN_WIDTH-15, 15+18);
   glcdSetRect(s, 10, 150, 60, 170);
   glcdSetRect(t, 65, 150, 120, 170);

   glcdCoord_t y_old[SCREEN_WIDTH];

   glcdFillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, bg);

	for(x=0;x<SCREEN_WIDTH;x++){
		glcdSetPixel(x, 145, RGB(0x00, 0x00, 0xFF));
		if(x%32==3){
			glcdSetPixel(x, 146, RGB(0x00, 0x00, 0xFF));
			glcdSetPixel(x, 144, RGB(0x00, 0x00, 0xFF));
		}
	}
	for(y=145;y>0;y--){
		glcdSetPixel(1, y, RGB(0x00, 0x00, 0xFF));
		if(y%32==16){
			glcdSetPixel(0, y, RGB(0x00, 0x00, 0xFF));
			glcdSetPixel(2, y, RGB(0x00, 0x00, 0xFF));
		}
	}


        glcdSelectFont(f9x14, 0);           // font is stored in FLASH, demontrate read callback
        glcdPrintCenter(r, PSTR("zOszi"), 1); //itoa(val)  PSTR()

	glcdMoveTo(s.X1 + 2, s.Y1 + 2);
	glcdSelectFont(f9x14, 0);
	glcdPrint(PSTR("U=5V"), 1);

	glcdMoveTo(t.X1 + 2, t.Y1 + 2);
        glcdSelectFont(f9x14, 0);           
	glcdPrint(PSTR("T=\"512\"µs"), 1);


	

    while(1) {
	if(bit_is_set(PINC,0)==0){
		x_index++;
		x_index=x_index%3;
		glcdMoveTo(s.X1 + 2, s.Y1 + 2);
	        glcdSelectFont(f9x14, 0);
		switch(x_index){
			case 0: glcdPrint(PSTR("U=5V"), 1);
				break;
			case 1: glcdPrint(PSTR("U=3V"), 1);
				break;
			case 2: glcdPrint(PSTR("U=1V"), 1);
				break;
		}
		glcdWait(100);
	}

	if(bit_is_set(PINC,1)==0){
		x_index--;
		if(x_index<0){
			x_index=2;
		}
		x_index=x_index%3;
		glcdMoveTo(s.X1 + 2, s.Y1 + 2);
	        glcdSelectFont(f9x14, 0);
		switch(x_index){
			case 0: glcdPrint(PSTR("U=5V"), 1);
				break;
			case 1: glcdPrint(PSTR("U=3V"), 1);
				break;
			case 2: glcdPrint(PSTR("U=1V"), 1);
				break;
		}
		glcdWait(100);
	}
	if(bit_is_set(PINC,2)==0){
		y_index--;
		if(y_index<0){
			y_index=6;
		}
		y_index=y_index%7;
		glcdFillRect(65, 150, 150, 170, bg);
		glcdSetRect(t, 65, 150, 120, 170);
		glcdMoveTo(t.X1 + 2, t.Y1 + 2);
	        glcdSelectFont(f9x14, 0);           
		switch(y_index){
			case 0: glcdPrint(PSTR("T=\"512\"µs"), 1);
				break;
			case 1: glcdPrint(PSTR("T=1ms"), 1);
				break;
			case 2: glcdPrint(PSTR("T=10ms"), 1);
				break;
			case 3: glcdPrint(PSTR("T=50ms"), 1);
				break;
			case 4: glcdPrint(PSTR("T=100ms"), 1);
				break;
			case 5: glcdPrint(PSTR("T=500ms"), 1);
				break;
			case 6: glcdPrint(PSTR("T=1s"), 1);
				break;
		}
		glcdWait(200);
	}

	if(bit_is_set(PINC,3)==0){
		y_index++;
		y_index=y_index%7;
		glcdFillRect(65, 150, 150, 170, bg);
		glcdSetRect(t, 65, 150, 120, 170);
		glcdMoveTo(t.X1 + 2, t.Y1 + 2);
	        glcdSelectFont(f9x14, 0);           
		switch(y_index){
			case 0: glcdPrint(PSTR("T=\"512\"µs"), 1);
				break;
			case 1: glcdPrint(PSTR("T=1ms"), 1);
				break;
			case 2: glcdPrint(PSTR("T=10ms"), 1);
				break;
			case 3: glcdPrint(PSTR("T=50ms"), 1);
				break;
			case 4: glcdPrint(PSTR("T=100ms"), 1);
				break;
			case 5: glcdPrint(PSTR("T=500ms"), 1);
				break;
			case 6: glcdPrint(PSTR("T=1s"), 1);
				break;
		}
		glcdWait(200);
	}

	if(bit_is_set(PINC,4)==0){
//		rollwait--;
		glcdWait(200);
	}
	if(bit_is_set(PINC,5)==0){
//		rollwait++;
		glcdWait(200);
	}


      for(x=2;x<SCREEN_WIDTH;x++){
        val = adc_getValue(0);
	y=144-(val/x_val[x_index]);
	glcdSetPixel(x, y_old[x], bg);
	glcdSetPixel(x, y, color);
	y_old[x]=y;
	if(y_index!=0){
		mywait(y_val[y_index]);
	}
      }
 /*     while(rollwait>0){
	temp=10;
	while(temp<0){
		temp--;
	}
	rollwait--;
      }
*/

    }
}
/*
Spannungswerte: (fertig)
1V
3V
5V

Zeitwerte: (todo)
512µs  = 16µs/Pixel = 320 Takte = 0 Takte warten 
1ms    = 31µs/Pixel = 625 Takte = 305 Takte warten = 152 schleifendurchläufe ~ 142    /2=71 => 7
10ms   = 312µs/Pixel = 6250 Takte = 5930 Takte warten = 2965 ~ 2955	    /2=1477,5 => 148
50ms   = 1,5625ms/Pixel = 31250 Takte = 30930 Takte warten = 15465 ~ 15455  /2=7727,5 => 773
100ms  = 3,125ms/Pixel = 62500 Takte = 62180 Takte warten = 31090 ~ 31080   /2=15540  => 1554
500ms  = 15,625ms/Pixel = 312500 Takte = 312180 Takte warten = 156090 ~ 156080 \2=78040 => 7804
1s     = 31,250ms/Pixel = 625000 Takte = 624680 Takte warten = 312340 ~ 312330 /2 = 156165 => 15616



2 striche unten 2 oben
128u+128o+128u+128o=512 takte = 512µS = 51,2KHz
16µS Pro Pixel = 160KHz
1000000µS=1Hz, 1000µS=1KHz, 100µS =10KHz, 10µS =100KHz, 1µS = 1000KHz = 1 MHz
*/







