//	--------------------------------------------------------------------
//	Arduino Console Stub
//	----------------------------------------------------------------------------

#if _MSC_VER

#include "ArduWin.h"
#include <conio.h>
#include <time.h>

extern void setup();
extern void loop();

CSerial Serial;
CSerial SerialUSB;
CSerial Serial1;
int REG_PIOC_PDSR;
uint16_t PORTD;
uint16_t PORTB;
uint8_t PINB;
uint8_t PIND;

//	----------------------------------------------------------------------------
//	Timers
//	----------------------------------------------------------------------------

unsigned long millis()
{
	return (clock() * 1000) /  CLOCKS_PER_SEC;
}

void delay(unsigned long delayms)
{
	unsigned long u = millis() + delayms;
	while (u > millis());
}

//	----------------------------------------------------------------------------
//	I/O
//	----------------------------------------------------------------------------

void pinMode(int,int){}
void digitalWrite(int,int){}
void analogWrite(int,int){}
bool digitalRead(int){	return 0;}
int analogRead(int){	return 0;}
void analogReadResolution(int){}

int TC_GetStatus(int,int){return 0;}

//	----------------------------------------------------------------------------
//	Serial
//	----------------------------------------------------------------------------

void CSerial::begin(long)
{
	buffer[0] = 0;
	buflen = 0;
}

void CSerial::print(char *pString)
{
	printf("%s", pString);
}

void CSerial::print(int value, int)
{
	printf("%d", value);
}

void CSerial::println()
{
	printf("\r\n");
}

void CSerial::println(char *pString)
{
	printf("%s\r\n", pString);
}

void CSerial::println(int value, int)
{
	printf("%d\r\n", value);
}

void CSerial::println(unsigned int value, int)
{
	printf("%u\r\n", value);
}
void CSerial::write(void*,int){}

void CSerial::println(unsigned long value, int)
{
	printf("%lu\r\n", value);
}

int CSerial::available() 
{
	return buflen;
}

int CSerial::availableForWrite() 
{
	return 1;
}

char CSerial::read() 
{ 
	char c = 0;
	if (buflen > 0)
	{
		c = buffer[0];
		memcpy(&buffer[0], &buffer[1], --buflen);
	}
	return c;
}
void CSerial::_append(char c)
{
	CSerial::buffer[buflen] = c;
	if (++buflen >= 1024)
	{
		buflen--;
	}
}
void CSerial::setTimeout(int timeout){}
int CSerial::readBytes(char *c,int len){return 0;}
void delayMicroseconds(int us){};
unsigned long micros(){return 0;}

//	----------------------------------------------------------------------------
//	Main
//	----------------------------------------------------------------------------

#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
	setup();
	for(;;)
	{
		if (_kbhit())
		{
			Serial._append((char)_getch());
		}
		loop();
	}
}

#endif