;**************
; This file contains the command parsing routines...good stuff
;**************

;*****
; OK lets see what the command is..one at a time baby
;*****

check_cmds

;****
; Button commands
;****
;**
	movlw	BUT_CMD_STATE		; State command?
	subwf	rx_data, W		; Test it
	btfss	STATUS, Z
	goto	check_ccp_pic		; Nope

	movlw	BUT_CMD_STATE
	call	send_cmd

	movlw	BUT_BANK0
	call	send_cmd

	fifo_skip_nempty f_but_fifo	; Skip this stuff and send 0 if its empty
	goto	but_nada

;	DISABLE_IRQ
	fifo_get f_but_fifo, 7, tmp_cmds ; get next byte in fifo..
;	ENABLE_IRQ

	movf	tmp_cmds, W		; move it to W
	call	send_cmd		; send it
	goto	check_done		; and were done
but_nada
	movlw	0
	call	send_cmd
	goto 	check_done
;**
;****
; PIC commands..
;****
check_ccp_pic
;**
	movlw	PIC_CMD_PING		; Test for PING
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	pic_ccp_temp

	movlw	PIC_CMD_PONG
	call	send_cmd
	goto	check_done

pic_ccp_temp
	movlw	PIC_CMD_TEMP		; Test for TEMP
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_up_logo		; Nope, next

;	DISABLE_IRQ
	bcf	SYS_OPTS, SOPT_OK2I2C
	call	read_temp
	; OK got request for temp byte
	movlw	PIC_CMD_TEMP
	call	send_cmd
	movf	Local_TempMSB, W
	call	send_cmd
	movf	Local_TempLSB, W
	call	send_cmd
	bsf	SYS_OPTS, SOPT_OK2I2C
;	ENABLE_IRQ
	goto	check_done
;**
pic_ccp_up_logo
	movlw	PIC_CMD_LOGO_UPLD		; Test for upload of logo data
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_fan

	clrf	EEPROM_Addr_H
	clrf	EEPROM_Addr_L

	bcf	SYS_OPTS, SOPT_OK2I2C	; disable write_leds
	call	logo_read_random	; set address

	movlw	PIC_CMD_OK		; Tell host OK to proceede.
	call	send_cmd

	movlw	GRAPHIC_ON		; turn on graphics
	movwf	COMMANDBUF
	call	lcd_send_cmd

	call	lcd_home_graph
;	movlw	'L'
;	call	lcd_put_char

c_c_u_l_loop
	call	RX_GET_BUFFER_WAIT	; Get byte

	movlw	CMD_END			; check for CMD_END
	subwf	rx_data, W	
;	call	serial_isterm		; Is it the term sequence?
	btfss	STATUS, Z
	goto	c_c_u_l_done		; Got it

	; Didnt, write to EEPROM
	movf	rx_data, W
	movwf	EEPROM_Data

	movwf	DATABUFA
	movlw	DATA_WR_INC
	movwf	COMMANDBUF
	call	lcd_send_1data_cmd

;	DISABLE_IRQ
	call	logo_write_byte		; write byte to EEPROM
	
	Wait	10 Millisec, 0

	call	logo_read_current	; read it back

;	ENABLE_IRQ
	movf	EEPROM_Data, W
	call	send_cmd

	call	logo_addr_inc		; incrament address pointer

	movlw	PIC_CMD_READY		; send READY to host
	call	send_cmd

	goto	c_c_u_l_loop		; loop again


c_c_u_l_done
	movlw	TEXT_ON
	movwf	COMMANDBUF
	call	lcd_send_cmd
	call	lcd_clear_text
	movlw	'D'
	call	lcd_put_char
	call	send_ok
	bsf	SYS_OPTS, SOPT_OK2I2C
	goto	check_done
;**
pic_ccp_fan
	movlw	PIC_CMD_FAN
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_beep

	; got fan controll command
	call	RX_GET_BUFFER_WAIT	; Get on/off state
	movlw	CMD_ON
	subwf	rx_data, W
	btfsc	STATUS, Z
	goto	fan_on
	goto	fan_off
fan_on
	bcf	FAN_CTRL
	goto	fan_done
fan_off
	bsf	FAN_CTRL
fan_done
	call	send_ok
	goto	check_done
;**
pic_ccp_beep
	movlw	PIC_CMD_BEEP
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_contrast

	call	RX_GET_BUFFER_WAIT

	movlw	0x01			; Test if it was beep #1
	subwf	rx_data, W
	btfsc	STATUS, Z
	goto	do_beep			; It was

	movlw	0x02			; Test it if was beep #2
	subwf	rx_data, W
	btfsc	STATUS, Z
	goto	do_err_beep		; It was

	call	send_badcmd		; It wasnt, send BADCMD

	goto	check_done
do_beep
	call	beep
	goto 	pic_ccp_beep_done
do_err_beep
	call	error_beep
pic_ccp_beep_done
	call	send_ok			; OK!
	goto	check_done
;**
pic_ccp_contrast
	movlw	PIC_CMD_CONT
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_stime

	call	RX_GET_BUFFER_WAIT

	movlw	0x01
	subwf	rx_data, W
	btfsc	STATUS, Z
	goto	cont_up
	goto	cont_down

cont_up
	movlw	d'1'
	call	lcd_contrast_adj
	goto	check_done
cont_down
	movlw	d'63'
	call	lcd_contrast_adj
	goto	check_done

;**
pic_ccp_stime
	movlw	PIC_CMD_STIME
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	pic_ccp_gtime

	; Got command to set time, now get and set bytes

;	DISABLE_IRQ
	bcf	INTCON, TMR0

	; Get and set seconds
	movlw	0x00
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data
	bcf	RTC_Data, 7			; Make sure CE is active (low)
	call	rtc_write_time

	; Get and set minutes
	movlw	0x01
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data

	call	rtc_write_time

	; Get and set hours
	movlw	0x02
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data
	bsf	RTC_Data, 6			; Make sure format is 12 hour (high)

	call	rtc_write_time

	; Get and set day
	movlw	0x04
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data

	call	rtc_write_time

	; Get and set month
	movlw	0x05
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data

	call	rtc_write_time

	; Get and set year
	movlw	0x06
	movwf	RTC_Addr

	call	RX_GET_BUFFER_WAIT
	movf	rx_data, W
	call	Bin2BCD
	movwf	RTC_Data

	call	rtc_write_time

	call	send_ok
	bsf	INTCON, TMR0
;	ENABLE_IRQ
	goto	check_done
;**
pic_ccp_gtime
	movlw	PIC_CMD_GTIME
	subwf	rx_data, W
	btfss	STATUS, Z
	goto	check_ccl_power

	DISABLE_IRQ
	; Get and set seconds
	movlw	0x00
	call	rtc_get_time
	call	send_cmd

	; Get and set minutes
	movlw	0x01
	call	rtc_get_time
	call	send_cmd

	; Get and set hours
	movlw	0x02
	call	rtc_get_time
	call	send_cmd

	; Get and set day
	movlw	0x04
	call	rtc_get_time
	call	send_cmd

	; Get and set month
	movlw	0x05
	call	rtc_get_time
	call	send_cmd

	; Get and set year
	movlw	0x06
	call	rtc_get_time
	call	send_cmd

	call	send_ok
	ENABLE_IRQ

	goto	check_done

check_ccl_power
	movlw	PIC_CMD_PWR		; Is PWR command?
	subwf	rx_data, W		; test if it?
	btfss	STATUS, Z
	goto	check_ccl_lcd		; Nope

	movlw	PIC_CMD_OK		; Send OK!
	call	send_cmd

	Wait	10 Millisec, 0		; wait 10ms

	POWER_OFF
	POWER_OFF			; do it!		
	return

;**
;****
; LCD Commands...
;****
check_ccl_lcd
;**
	movlw	LCD_CMD_INIT		; Test for INIT
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	lcd_ccl_AWR

	; Got command to init display, do it
	call	init_lcd		; init...
	PCALL	send_ok			; tell them OK!
	goto	check_done
	return
;**
lcd_ccl_AWR
	movlw	LCD_CMD_AWR		; Auto mode-write?
	subwf	rx_data,W		; test..
	btfss	STATUS,Z
	goto 	lcd_ccl_CMD
	
	; OK, auto mode write..write till we get CMD_END

	movlw	DATA_AUTO_WR		; turn on auto-mode
	movwf	COMMANDBUF
	call	lcd_send_cmd		;

	movlw	PIC_CMD_READY
	call	send_cmd

;	DISABLE_IRQ			; Turn off interupts
	; From here on we do serial stuff by hand

amode_loop
	call	RX_GET_BUFFER_WAIT

;	call	serial_in_wait

	movlw	CMD_END			; End of data?
	subwf	rx_data, W
	btfsc	STATUS, Z
	goto	amode_done		; Yes. were done.

	; OK got a byte , write it to LCD
	movf	rx_data, W
	movwf	DATABUFA
	call	lcd_autosend
	goto	amode_loop

amode_done
	movlw	AUTO_DATA_RESET
	movwf	COMMANDBUF
	call	lcd_send_cmd

;	ENABLE_IRQ

	call	send_ok
	goto	check_done

;**
lcd_ccl_CMD
	movlw	LCD_CMD_CMD		; Test for command, no data
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	lcd_ccl_1DATA

	movlw	'd'
	PCALL	lcd_put_char

	; Got command
	call	RX_GET_BUFFER_WAIT	; Get the command byte...

        movwf   COMMANDBUF		; send it...
        call    lcd_send_cmd		; to the lcd
	call	lcd_put_char

	call	send_ok			; tell them OK!
	goto	check_done
;**
lcd_ccl_1DATA
	movlw	LCD_CMD_1DATA		; Test for 1 data byte command
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	lcd_ccl_2DATA

	; Got 1 byte command
	call	RX_GET_BUFFER_WAIT	; Get the command byte...

        movwf   COMMANDBUF		; load it up

	call	RX_GET_BUFFER_WAIT	; Get the command byte...

	movwf	DATABUFA		; Move to BUFA

        call    lcd_send_1data_cmd	; Send it
	call	send_ok			; tell them OK!
	goto	check_done
	return
;**
lcd_ccl_2DATA
	movlw	LCD_CMD_2DATA		; Test for 2 data byte command
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	lcd_ccl_CLRT

	; Got 2 byte command
	call	RX_GET_BUFFER_WAIT	; Get the command byte...

        movwf   COMMANDBUF		; load it up..

	call	RX_GET_BUFFER_WAIT	; Get the command byte...

	movwf	DATABUFB		; Move to BUFB

	call	RX_GET_BUFFER_WAIT	; Get the command byte...

	movwf	DATABUFA		; Move to BUFA

        call    lcd_send_2data_cmd	; Send it
	call	send_ok			; tell them OK!
	goto	check_done
;**
lcd_ccl_CLRT
	movlw	LCD_CMD_CLRT		; Test for clear TEXT
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto 	lcd_ccl_CLRG

	; Got command to clear text
	call	lcd_clear_text
	call	send_ok
	goto	check_done
;**
lcd_ccl_CLRG
	movlw	LCD_CMD_CLRG		; Test for clear GRAPH
	subwf	rx_data, W		; test..
	btfss	STATUS, Z		; Next..
	goto	check_null

	; Got command to clear graphics
	call	lcd_clear_graphic
	call	send_ok
	goto	check_done
;**

check_null
	goto	check_bad

check_done
	return

;***
; Called when a command check fails
;***
check_bad
	call	error_beep
	call	send_badcmd
	return

