#include "picsys18.h"
// 整備中

// #define __CLCD__

void
setup()
{
	pinMode(LED8, OUTPUT) ;
	pinMode(LED9, OUTPUT) ;
	digitalWrite(LED8, LOW) ;
	digitalWrite(LED9, LOW) ;

	pinMode(CN7, OUT_SERVO1) ;
	pinMode(CN8, OUT_SERVO2) ;

	pinMode(SW1, INPUT) ;
	pinMode(SW2, INPUT) ;
	pinMode(SW3, INPUT) ;
	pinMode(SW4, INPUT) ;
	pinMode(SW5, INPUT) ;
	pinMode(SW6, INPUT) ;

#if defined(__CLCD__)
	CLCD_initDefault() ;
#else
	SEG7_initDefault() ;
#endif
	
	analogReference(EXTERNAL) ;
	
	USB_begin() ;
}


int cIndex = 0 ;
char buff[50] ;
BOOL eol = FALSE ;

// BYTE dxIndex[] = {DX0, DX1, DX2, DX3, DX4, DX5, DX6, DX7, DX8, DX9, DX10, DX11, DX12, DX13, DX14, DX15, DX16, DX17, DX18, DX19} ;
// BYTE axIndex[] = {AX0, AX1, AX2, AX3, AX4, AX5} ;
BYTE ledIndex[] = {NULL, LED8, LED9} ;
BYTE swIndex[] = {NULL, SW1, SW2, SW3, SW4, SW5, SW6} ;
BYTE servoIndex[] = {NULL, CN7, CN8} ;

char
posParam(void)
{
	int i ;
	for (i = 1 ; buff[i]; i++) {
		if (buff[i] == ':') {
			return i+1 ;
		}
	}
	
	return 0 ;	
}	

void
loop()
{
    int c, v, i, j ;
    float temp ; // 温度の計算は実数で
	
	if (USB_attached() && USB_ready(30)) {
		while (USB_available()) {
			c = USB_getc() ;
			USB_putc(c) ;
			if (c == '\r') {
				USB_putc('\n') ;
				buff[cIndex] = '\0' ;
				eol = TRUE ;
			} else {
				buff[cIndex++] = c ;
				eol = FALSE ;
			}					
		}		
		
		if (eol) {
			switch (buff[0]) {
				case 'h':
				case 'H':
					USB_putrs("PICSYS18-SP") ;
					USB_putrs("\r\nUP:") ;
					USB_puti(millis()) ;
					USB_putrs("\r\nBR:") ;
					USB_puti(USB_baudrate()) ;
					break ;
#if 0
				case 'd':
				case 'D':
					i = atoi(&buff[1]) ;
					if ((j = posParam()) == 0) {
						USB_puti(digitalRead(dxIndex[i])) ;
					} else {
						digitalWrite(dxIndex[i], atoi(&buff[j])) ;
					}
					break ;
				case 'a':
				case 'A':
					i = atoi(&buff[1]) ;
					USB_puti(analogRead(axIndex[i])) ;
					break ;
#endif
				case 'l':
				case 'L':
					i = atoi(&buff[1]) ;
					if ((j = posParam()) == 0) {
						if (i < 8)
							USB_puti(LED_bitGet(i)) ;
						else		
							USB_puti(digitalRead(ledIndex[i-8])) ;
					} else {
						if (i < 8)
							LED_bitWrite(i, atoi(&buff[j])) ;
						else
							digitalWrite(ledIndex[i-8], atoi(&buff[j])) ;
					}
					break ;
				case 's':
				case 'S':
					i = atoi(&buff[1]) ;
					USB_putc(digitalRead(swIndex[i]) ? '0' : '1') ;
					break ;
				case 'v':
				case 'V':
					i = atoi(&buff[1]) ;
					if ((j = posParam()) == 0) {
						USB_puti(digitalRead(servoIndex[i])) ;
					} else {
						digitalWrite(servoIndex[i], atoi(&buff[j])) ;
					}
					break ;
				case 't':
				case 'T':
					v = analogRead(TEMP_SENSOR) ;
					temp = 4.096 / 1024 * v ;  // 読み取った値を電圧に換算
					temp -= 0.4 ;  // 温度センサーの0度のオフセット:400mV
					temp /= 0.01953 ; // １度あたり19.53mVの電圧増加
					USB_putf(temp, 2) ;
					break ;
				case 'p':
				case 'P':
					v = analogRead(PHOTO_SENSOR) ;
					USB_puti(v) ;
					break ;
#if defined(CLCD_BOOSTER)
				case 'c':
				case 'C':
					i = atoi(&buff[1]) - 1 ;
					if (i < 0) {
						CLCD_clear() ;
					} else {	
						CLCD_setCursor(0, i) ;
						CLCD_puts(&buff[3]) ;
						USB_puts(&buff[3]) ;
					}
					break ;
#endif
			}
			
			USB_putrs("\r\n") ;
			cIndex = 0 ;
			eol = FALSE ;
		}
	}
	
	delayMilliseconds(100) ;
	digitalWrite(LED8, (millis()/500) & 1) ;
}
