; menu.asm
;
; Copyright (C) 2006,2007 Nuno Sucena Almeida
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
;


	include	<proc.inc>
	include <i2c.inc>

	udata
W_temp	res	1

;--------------------------------------------------------------------
; external functions
	extern	uart_send
	extern	uart_send_ee_string
	extern	uart_receive

	extern	i2c_setup
	extern	i2c_ioe_setup
	extern	i2c_ioe_rotate
	extern	i2c_ioe_cycle_vertical
	extern	i2c_ioe_set_outputs
	extern	i2c_debug

	extern	resetvector

	extern	i2c_eeprom_read_uart
	extern	i2c_eeprom_read_sequential_uart
	extern	i2c_eeprom_write_uart
	extern	i2c_eeprom_test

;--------------------------------------------------------------------
; external symbols
	extern	ee_ct1fox
	extern	ee_prompt_ready
	extern	ee_prompt_i2c
	extern	ee_prompt_config
	extern	ee_prompt_eeprom
	extern	ee_prompt_bootloader
	extern	ee_OK
	extern	delay10

;--------------------------------------------------------------------
; check key macro
check_key	macro	key,option
	;; save 'W' to a temp location
	banksel	W_temp
	movwf	W_temp
	;; compare 'W' with 'key'
	xorlw	key
	;; if 'W'=='key' the exclusive-or will give '0'
	;; and the status Z bit will have the value '1'
	btfsc	STATUS,Z
	goto	option
	movf	W_temp,W
	endm

;--------------------------------------------------------------------
; menu macro
show_menu	macro	prompt
	movlw	prompt
 	call	uart_send_ee_string
	movlw	"\r"
	call	uart_send
	movlw	"\n"
	call	uart_send
  	call	uart_receive
	endm

;--------------------------------------------------------------------
;


	code
main_menu:
	movlw		ee_ct1fox
	call		uart_send_ee_string
 	show_menu	ee_prompt_ready
	check_key	A'i',menu_i2c
	check_key	A'c',menu_config
	check_key	A'e',menu_eeprom
	check_key	A'b',menu_bootloader
	goto		main_menu

menu_i2c:
	call		i2c_debug
	show_menu	ee_prompt_i2c
	check_key	A's',menu_i2c_setup
	check_key	A'e',menu_ioe_setup
	check_key	A'm',main_menu
	check_key	A'C',menu_i2c_sdl_HIGH
	check_key	A'c',menu_i2c_sdl_LOW
	check_key	A'A',menu_i2c_sda_HIGH
	check_key	A'a',menu_i2c_sda_LOW
	check_key	A'r',menu_ioe_rotate
	check_key	A'v',menu_ioe_values
	check_key	A't',menu_ioe_cycle_vertical
	goto		menu_i2c
menu_i2c_sdl_HIGH:
	scl_HIGH
	goto		menu_i2c_end
menu_i2c_sdl_LOW:
	scl_LOW
	goto		menu_i2c_end
menu_i2c_sda_HIGH:
	sda_HIGH
	goto		menu_i2c_end
menu_i2c_sda_LOW:
	sda_LOW
	goto		menu_i2c_end
menu_i2c_setup:
	call		i2c_setup
	goto		menu_i2c_end
menu_ioe_setup:
	call		i2c_ioe_setup
	goto		menu_i2c_end
menu_ioe_rotate:
	call		i2c_ioe_rotate
	goto		menu_i2c_end
menu_ioe_cycle_vertical:
	call		i2c_ioe_cycle_vertical
	goto		menu_i2c_end
menu_ioe_values:
	call		i2c_ioe_set_outputs
menu_i2c_end:
	movlw		ee_OK
	call		uart_send_ee_string
	goto		menu_i2c

menu_config:
	show_menu	ee_prompt_config
	check_key	A'm',main_menu
	goto		menu_config

menu_eeprom:
	show_menu	ee_prompt_eeprom
	check_key	A'm',main_menu
	check_key	A'r',menu_eeprom_read
	check_key	A's',menu_eeprom_read_sequential
	check_key	A'w',menu_eeprom_write
	check_key	A't',menu_eeprom_test
	goto		menu_eeprom
menu_eeprom_read:
	call		i2c_eeprom_read_uart
	goto		menu_eeprom
menu_eeprom_read_sequential:
	call		i2c_eeprom_read_sequential_uart
	goto		menu_eeprom
menu_eeprom_write:
	call		i2c_eeprom_write_uart
	goto		menu_eeprom
menu_eeprom_test:
	call		i2c_eeprom_test
	goto		menu_eeprom

menu_bootloader:
	show_menu	ee_prompt_bootloader
	check_key	A'm',main_menu
	check_key	A'B',menu_bootloader_reset
	goto		menu_bootloader
menu_bootloader_reset:
	;; jump to reset vector
	goto		0

	global	main_menu
	end