#include "rprintf.h"
#include "rtc.h"
#include "globals.h"
#include "cmdline.h"
#include <stdio.h>
#include <string.h>
#include "cmds.h"
#include "serial_fuggv.h"

extern TCS tcs;
extern char filename[255];

void helpFunction(void)
{
	rprintfCRLF();

	rprintf("Processor freq.: %d MHz\r\n",F_CPU/1000000);
	rprintf("Baudrate: %d\r\n\r\n",_baudr);

	rprintf("Available commands:\r\n");
	rprintf("help      - displays available commands\r\n");
	rprintf("gettime\r\n");	
	rprintf("settime year month day hour min sec\r\n");
	rprintf("printsettings\r\n");
	rprintf("ssp periodtime - set sampling period\r\n");
	rprintf("start\r\n");
	rprintf("stop\r\n");
	rprintf("dir\r\n");
	rprintf("download\r\n");
	rprintf("remove\r\n");
	rprintf("streaming on\r\n");
	rprintf("streaming off\r\n");
	rprintf("mmctest\r\n");
	
	rprintfCRLF();
	
//	teszt();
}

void setTimeCommand(void)
{
	unsigned char year, month, day, hours, minutes, seconds, hundredths;
			
	rprintfCRLF();
    
	i2c_init();	
	
	getTime(&year, &month, &day, &hours, &minutes, &seconds, &hundredths);
	rprintf("Current time before (y,m,d,h,d,s): %d/%d/%d %d:%d:%d",year,month,day,hours,minutes,seconds);
	rprintfCRLF();	
	
	setTime(cmdlineGetArgInt(1),cmdlineGetArgInt(2),cmdlineGetArgInt(3),
		cmdlineGetArgInt(4),cmdlineGetArgInt(5),cmdlineGetArgInt(6),0);

	getTime(&year, &month, &day, &hours, &minutes, &seconds, &hundredths);
	rprintf("Current time (y,m,d,h,d,s): %d/%d/%d %d:%d:%d",year,month,day,hours,minutes,seconds);
	
	rprintfCRLF();
}

void download(void)
{
	EmbeddedFile	f;
	int sm = merunk;
	stop();
	
	rprintfCRLF();
    
	if(efs_init(&fs,0)!=0) {
		rprintf("Filesystem init error!\n");
		return;
	}

	if(file_fopen(&f,&fs.myFs, cmdlineGetArgStr(1), MODE_READ)!=0) {
		rprintf("File opening error!\n");		
		return;
	}
	
	rprintf("<BEGIN>");
	rprintfCRLF();
	while (1) {
		unsigned char RtcBuffer[6];
		unsigned char year, month, day, hours, minutes, seconds, hundredths;						
		int olvasott = file_read(&f, sizeof(RtcBuffer), (void*) &RtcBuffer);
		if (olvasott != sizeof(RtcBuffer)) break;
		file_read(&f, sizeof(tcs), (void*) &tcs);
		
		decodeTime(RtcBuffer,&year,&month,&day,&hours,&minutes,&seconds,&hundredths);
		rprintf("Time: %d-%d-%d %d:%d:%d.%d",year,month,day,hours,minutes,seconds,hundredths);
		rprintfCRLF();

		rprintf("Sampling period: %d",tcs.sampling_period);rprintfCRLF();
		rprintf("Number of elements: %d",tcs.puffer_tele);rprintfCRLF();
		rprintf("Sum: %d",tcs.osszeg);rprintfCRLF();
		rprintf("Avg: %d",tcs.atlag);rprintfCRLF();
		rprintf("STD: %d",tcs.szoras);rprintfCRLF();
		rprintf("Scale: %d",tcs.szorzo);rprintfCRLF();
		rprintf("Min.val.: %d",tcs.minv);rprintfCRLF();
		rprintf("Max.val.: %d",tcs.maxv);rprintfCRLF();
	}
	rprintf("<END>");
	rprintfCRLF();
	
	file_fclose(&f);

	fs_umount(&fs.myFs);
	PORTB = 0xff;
	DDRB = 0xff;	
	
	if (sm) start();
}

void remove(void)
{
	EmbeddedFile	f,f2;
	
	stop();
	
	if(efs_init(&fs,0)!=0) {
		rprintf("Filesystem init error!\n");
		return;
	}

	if(file_fopen(&f,&fs.myFs, "files.lst", MODE_READ)!=0) {
		rprintf("File opening error: files.lst!\n");		
		return;
	}
	if(file_fopen(&f2,&fs.myFs, "files.tmp", MODE_WRITE)!=0) {
		rprintf("File opening error: files.tmp!\n");		
		return;
	}
	
	while (1) {
		char sor[255];
		int olvasott = file_read(&f, 255, (void*) sor);
		if (olvasott != 255) break;
		if (strcmp(sor,cmdlineGetArgStr(1))) file_write(&f2, 255, (void*) sor);
	}
	
	file_fclose(&f);
	file_fclose(&f2);	
	fs_umount(&fs.myFs);
	PORTB = 0xff;
	DDRB = 0xff;	

	if(efs_init(&fs,0)!=0) {
		rprintf("Filesystem init error!\n");
		return;
	}


	if(file_fopen(&f,&fs.myFs, "files.lst", MODE_WRITE)!=0) {
		rprintf("File opening error: files.lst 2!\n");		
		return;
	}
	if(file_fopen(&f2,&fs.myFs, "files.tmp", MODE_READ)!=0) {
		rprintf("File opening error: files.tmp 2!\n");		
		return;
	}
	
	while (1) {
		char sor[255];
		int olvasott = file_read(&f2, 255, (void*) sor);
		if (olvasott != 255) break;
		file_write(&f, 255,  (void*) sor);
	}
	
	file_fclose(&f);
	file_fclose(&f2);	
	fs_umount(&fs.myFs);
	PORTB = 0xff;
	DDRB = 0xff;	

	rprintfCRLF();
	
	start();
}

void getTimeCommand(void)
{
	unsigned char year, month, day, hours, minutes, seconds, hundredths;
			
	rprintfCRLF();
    
	i2c_init();	
	
	getTime(&year, &month, &day, &hours, &minutes, &seconds, &hundredths);
	rprintf("Current time (y,m,d,h,d,s): %d/%d/%d %d:%d:%d",year,month,day,hours,minutes,seconds);
	
	rprintfCRLF();
}


void printSettings(void) {
	rprintfCRLF();
	rprintf("Sampling period time: %d ms",tcs.sampling_period);
	rprintfCRLF();	
	rprintf("Buffer size: %d",tcs.puffer_tele);
	rprintfCRLF();	
}

void start(void) {
	EmbeddedFile	f;	
	unsigned char RtcBuffer[6];
	
	rprintfCRLF();
	rprintf("Measuring started!");
	rprintfCRLF();	
	
	i2c_init();		
	GetRTC(RtcBuffer);
	
	sprintf(filename,"%c%c%c%c%c%c%c%c.%c%c",
		'A'+RtcBuffer[1]/16,'A'+RtcBuffer[1]%16,
		'A'+RtcBuffer[2]/16,'A'+RtcBuffer[2]%16,
		'A'+RtcBuffer[3]/16,'A'+RtcBuffer[3]%16,
		'A'+RtcBuffer[4]/16,'A'+RtcBuffer[4]%16,
		'A'+RtcBuffer[5]/16,'A'+RtcBuffer[5]%16
		);
	rprintfStr(filename); rprintf(" created.");
	rprintfCRLF();

	if(efs_init(&fs,0)!=0) {
		rprintf("Filesystem init error!\n");
		return;
	}

	if(file_fopen(&f,&fs.myFs, "files.lst", MODE_APPEND)!=0) {
		rprintf("File opening error!\n");		
		return;
	}
	file_write(&f, 255, (void*) filename);

	file_fclose(&f);
	fs_umount(&fs.myFs);
	PORTB = 0xff;
	DDRB = 0xff;
	
	merunk = 1;
}

void stop(void) {
	rprintfCRLF();
	rprintf("Measuring stopped!");
	rprintfCRLF();	
	merunk = 0;
}

void dir(void) {
	EmbeddedFile	f;
	
	rprintfCRLF();
    
	if(efs_init(&fs,0)!=0) {
		rprintf("Filesystem init error!\n");
		return;
	}

	if(file_fopen(&f,&fs.myFs, "files.lst", MODE_READ)!=0) {
		rprintf("File opening error!\n");		
		return;
	}
	
	rprintf("Files:");
	rprintfCRLF();
	
	while (1) {
		char sor[255];
		int olvasott = file_read(&f, 255, (void*) sor);
		if (olvasott != 255) break;
		
		rprintfStr(sor);
		rprintfCRLF();
	}
	
	file_fclose(&f);

	fs_umount(&fs.myFs);
	PORTB = 0xff;
	DDRB = 0xff;	
	
}

void setSamplingPeriod(void) {
	rprintfCRLF();
	tcs.sampling_period = cmdlineGetArgInt(1);
	rprintfCRLF();
}

void streaming_on_off(void) {
	if (!strcmp(cmdlineGetArgStr(1),"on")) {
		streaming = 1;
		rprintf("Streaming is on");
	}
	else
	{
		streaming = 0;
		rprintf("Streaming is off");
	}
	rprintfCRLF();
}

void streaming_off(void) {
}

void mmctest(void) {
    DDRB = 0xff;
	PORTB = cmdlineGetArgInt(1);
	rprintf("PINB = %d\r\n", PINB);
}
