;***************************************************************************
;
; File	: "Menu.asm"
; By	: Hugo Vos
; E-mail: hugo@hvos.myweb.nl
;	
; Menu routine
;
;***************************************************************************

menu:
	ldi	dispfunc, Def_menuitem		; Variable defines active menu option
	ldi	Tempvar, $00			; Temp. value


start_menu:

	ldi	Ax, Dispclear			; Load display clear command in Ax reg.
	ldi	Bx, disp_CTRL			; Select display control mode
	rcall	disp_lcd

	ldi	Ax, low(menutxt)		; write "menu" to te display
	ldi	Bx, high(menutxt)
	ldi	Cx, $06				; starting at position 06
	rcall	Str2disp

;***************************************************************************
;
; check for menu item "RTC klok" settings
;
;***************************************************************************

	cpi	dispfunc, $00			; check if RTC clock setting menu is selected
	brne	timer_menu			
	
	ldi	Ax, low(rtc)			; write "RTC instellen" to the display
	ldi	Bx, high(rtc)
	ldi	Cx, $40				; starting at the 2snd line
	rcall	Str2disp	

	push	dispfunc
	
	sbrc	Tempvar, 0			; check for select key pressed
	rcall	set_hour
	
	pop	dispfunc			; if pressed then start set RTC clock.

timer_menu:
;***************************************************************************
;
; check for menu item "Schakelklok"
;
;***************************************************************************

	cpi	dispfunc, $01			; check for menu item "timer klok"
	brne	module_menu
	
	ldi	Ax, low(klok)			; write "Schakelklok" to te display
	ldi	Bx, high(klok)
	ldi	Cx, $40				; starting at the second displ line
	rcall	Str2disp	

	push	dispfunc

	sbrc	Tempvar, 0			; check for select key pressed
	rcall	set_SWklok			; if pressed then start set RTC clock.
	
	pop	dispfunc

module_menu:
;***************************************************************************
;
; check for menu item "mudule lijst"
;
;***************************************************************************

	cpi	dispfunc, $02			; check for menu item "timer klok"
	brne	manual_menu
	
	ldi	Ax, low(module)			; write "Schakelklok" to te display
	ldi	Bx, high(module)
	ldi	Cx, $40				; starting at the second displ line
	rcall	Str2disp	

	push	dispfunc

	sbrc	Tempvar, 0			; check for select key pressed
	rcall	module_list			; if pressed then start set RTC clock.
	
	pop	dispfunc


manual_menu:
;**************************************************************************
;
; Check for munu item "handmatig"
;
;**************************************************************************

	cpi	dispfunc, $03
	brne	input_key
	
	ldi	Ax, low(manual)
	ldi	Bx, high(manual)
	ldi	Cx, $40
	rcall	Str2disp
	
	push	dispfunc
	
	sbrc	Tempvar, 0
	rcall	manual_set
	
	pop	dispfunc		
	
;***************************************************************************
;
; check for witch key is pressed and take action on it.
;
;***************************************************************************

input_key:

	rcall	Key_released			; Check for all key's released

	sbrc	Tempvar, 0
	rjmp	menu

	clr	Tempvar
	rcall	input_keyread

	cpi	Ax, plus_key			; check for + key pressed
	brne	no_pluskey
	inc	dispfunc
	cpi	dispfunc, max_menuitem
	brne	no_clear
	clr	dispfunc
no_clear:	
	rjmp	start_menu

no_pluskey:

	cpi	Ax, min_key			; check for - key pressed
	brne	no_minkey
	dec	dispfunc
	cpi	dispfunc, $FF
	brne	start_menu
	ldi	dispfunc, max_menuitem
	dec	dispfunc
	rjmp	start_menu

no_minkey:

	cpi	Ax, select_key			; check for select key pressed
	brne	no_selkey
	ldi	Tempvar, $FF
	rjmp	start_menu
	
no_selkey:

	cpi	Ax, return_key
;	cpi	Ax, $01
	brne	input_key
	rjmp	start_loop