;********************************************************************************************************
;*																										*
;*	Modulo de manejo del terminal																		*
;*																										*
;*	Variables que necesita:																				*
;*		FLAGS_TECLADO	->	byte que tendrá los flags que indican la tecla que está pulsada					*
;*		ESTADO_PANTALLA	->	En que estado tenemos la pantalla											*
;*		LINEA_MENU	-> En que linea estamos (para menus con muchas lineas)								*
;*		CURSOR_PORT	->	Puerto donde se localizan las teclas ARRIBA, ABAJO, DERECHA e IZQUIERDA			*
;*		MENU_PORT	->	Puerto donde se localiza la tecla MENU											*
;*		ENTER_PORT	->	Puerto donde se localiza la tecla ENTER											*
;*																										*
;*	Modifica:																							*
;*		FLAGS_TECLADO																							*
;*																										*
;********************************************************************************************************

include "terminal.inc"

;************************************
;*                                  *
;* Comprobamos el teclado			*
;*	Retorna FLAGS_TECLADO			*
;*                                  *
;************************************
CompruebaTeclado
	clrf	FLAGS_TECLADO

	; Las configuramos como entradas
	BANKSEL	1
	bsf		CURSOR_PORT, ARRIBA
	bsf		CURSOR_PORT, ABAJO
	bsf		CURSOR_PORT, DERECHA
	bsf		CURSOR_PORT, IZQUIERDA
	bsf		MENU_PORT, MENU
	bsf		ENTER_PORT, ENTER
	BANKSEL	0

	; Comprobamos la tecla de menu
	btfss	MENU_PORT, MENU ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	MENU_PORT, MENU ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_MENU
	goto	fin_teclado

	; Comprobamos la tecla de enter
	btfss	ENTER_PORT, ENTER ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	ENTER_PORT, ENTER ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_ENTER
	goto	fin_teclado

	; Comprobamos la tecla de arriba
	btfss	CURSOR_PORT, ARRIBA ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	CURSOR_PORT, ARRIBA ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_ARRIBA
	goto	fin_teclado

	; Comprobamos la tecla de abajo
	btfss	CURSOR_PORT, ABAJO ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	CURSOR_PORT, ABAJO ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_ABAJO
	goto	fin_teclado

	; Comprobamos la tecla de derecha
	btfss	CURSOR_PORT, DERECHA ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	CURSOR_PORT, DERECHA ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_DERECHA
	goto	fin_teclado

	; Comprobamos la tecla de izquierda
	btfss	CURSOR_PORT, IZQUIERDA ; 0 -> nada, 1 -> accion
	goto	no_hay_tecla
	call	Pausa1ms
	btfsc	CURSOR_PORT, IZQUIERDA ; 0 -> nada, 1 -> accion
	bsf		FLAGS_TECLADO, FLAG_IZQUIERDA
	goto	fin_teclado

no_hay_tecla

fin_teclado
	BANKSEL	1
	; Las configuramos como salidas otra vez
	bcf		CURSOR_PORT, ARRIBA
	bcf		CURSOR_PORT, ABAJO
	bcf		CURSOR_PORT, DERECHA
	bcf		CURSOR_PORT, IZQUIERDA
	bcf		MENU_PORT, MENU
	bcf		ENTER_PORT, ENTER
	BANKSEL	0

	return







;*************************************************
;*************************************************
;**                                  			**
;** MENUSES :)									**
;**                                  			**
;*************************************************
;*************************************************

;************************************
;*                                  *
;* Comprobamos si esta pulsada la	*
;* tecla "menu"						*
;*                                  *
;************************************
CompruebaMenu
	call	CompruebaTeclado
	btfss	FLAGS_TECLADO, FLAG_MENU        ; ¿esta pulsada la tecla de menu?
	goto	no_hay_menu
	
	movlw	MENU_PRINCIPAL_AJUSTES			; Si, sacamos menu principal
	movwf	ESTADO_PANTALLA
	call	SacaMenuPrincipal

no_hay_menu

	return

;************************************
;*                                  *
;* Pone el cursor en la linea que 	*
;* toca								*
;*                                  *
;************************************
PonCursor

comprueba_linea_0
	movf	LINEA_MENU, W
	sublw	0
	btfss	STATUS, Z
	goto	borra_linea_0
	movlw	LCD_ROW_0
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	'>'
	call	LcdWriteData
	movlw	' '
	call	LcdWriteData
	goto	comprueba_linea_1
borra_linea_0
	movlw	LCD_ROW_0
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	' '
	call	LcdWriteData
	call	LcdWriteData

comprueba_linea_1
	movf	LINEA_MENU, W
	sublw	1
	btfss	STATUS, Z
	goto	borra_linea_1
	movlw	LCD_ROW_0
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	'>'
	call	LcdWriteData
	movlw	' '
	call	LcdWriteData
	goto	comprueba_linea_2
borra_linea_1
	movlw	LCD_ROW_1
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	' '
	call	LcdWriteData
	call	LcdWriteData

comprueba_linea_2
	movf	LINEA_MENU, W
	sublw	2
	btfss	STATUS, Z
	goto	borra_linea_2
	movlw	LCD_ROW_2
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	'>'
	call	LcdWriteData
	movlw	' '
	call	LcdWriteData
	goto	comprueba_linea_3
borra_linea_2
	movlw	LCD_ROW_2
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	' '
	call	LcdWriteData
	call	LcdWriteData

comprueba_linea_3
	movf	LINEA_MENU, W
	sublw	3
	btfss	STATUS, Z
	goto	borra_linea_3
	movlw	LCD_ROW_3
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	'>'
	call	LcdWriteData
	movlw	' '
	call	LcdWriteData
	goto	fin_pon_cursor
borra_linea_3
	movlw	LCD_ROW_3
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	movlw	' '
	call	LcdWriteData
	call	LcdWriteData

fin_pon_cursor
	return

;****************************
;*							*
;*	Menu principal			*
;*							*
;****************************
MensajeMenuPrincipalTitulo		addwf PCL,1
								retlw 'M'
								retlw 'e'
								retlw 'n'
								retlw 'u'
								retlw ' '
								retlw 'p'
								retlw 'r'
								retlw 'i'
								retlw 'n'
								retlw 'c'
								retlw 'i'
								retlw 'p'
								retlw 'a'
								retlw 'l'
								retlw 0x0
MensajeMenuPrincipalAjustes		addwf PCL,1
								retlw 'A'
								retlw 'j'
								retlw 'u'
								retlw 's'
								retlw 't'
								retlw 'e'
								retlw 's'
								retlw 0x0
MensajeMenuPrincipalAcciones	addwf PCL,1
								retlw 'A'
								retlw 'c'
								retlw 'c'
								retlw 'i'
								retlw 'o'
								retlw 'n'
								retlw 'e'
								retlw 's'
								retlw 0x0

EscribeMenuPrincipalTitulo		clrf LCD_RESULT
EscribeMenuPrincipalTitulo1		movf LCD_RESULT, 0
								call MensajeMenuPrincipalTitulo
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuPrincipalTitulo1

EscribeMenuPrincipalAjustes		clrf LCD_RESULT
EscribeMenuPrincipalAjustes1	movf LCD_RESULT, 0
								call MensajeMenuPrincipalAjustes
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuPrincipalAjustes1

EscribeMenuPrincipalAcciones	clrf LCD_RESULT
EscribeMenuPrincipalAcciones1	movf LCD_RESULT, 0
								call MensajeMenuPrincipalAcciones
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuPrincipalAcciones1


;************************************
;*                                  *
;* Saca el menu principal			*
;*                                  *
;************************************
SacaMenuPrincipal
	call	LcdClearScreen

	; Titulo del menu principal
	movlw	LCD_ROW_0
	addlw	LCD_COL_0
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	call	EscribeMenuPrincipalTitulo
	
	movlw	1						; Estamos en la linea 1
	movwf	LINEA_MENU

	movlw	LCD_ROW_1
	addlw	LCD_COL_2
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	call	EscribeMenuPrincipalAjustes
	
	movlw	LCD_ROW_2
	addlw	LCD_COL_2
	iorlw	LCD_SET_DDRAM
	call	LcdWriteCommand
	call	EscribeMenuPrincipalAcciones



loop_menu_principal
	; Ponemos el cursor donde toca
	call	PonCursor
	
	; Esperamos teclas
	call	CompruebaTeclado
comprueba_menu
	btfss	FLAGS_TECLADO, FLAG_MENU
	goto	comprueba_enter			; La tecla menyu no esta pulsada, comprobamos la de enter
	goto	fin_menu_principal		; Pulsada menu, salimos del menu

comprueba_enter
	btfss	FLAGS_TECLADO, FLAG_ENTER
	goto	comprueba_arriba		; La tecla enter no esta pulsada, comprobamos la de arriba
	goto	fin_menu_principal		; Pulsada enter, entradmos en el menu que toque

comprueba_arriba
	btfss	FLAGS_TECLADO, FLAG_ARRIBA
	goto	comprueba_abajo			; La tecla arriba no esta pulsada, comprobamos la de abajo
	goto	actualiza_cursor_menu_principal

comprueba_abajo
	btfss	FLAGS_TECLADO, FLAG_ABAJO
	goto	loop_menu_principal		; la tecla abajo no esta pulsada, volvemos a empezar
	goto	actualiza_cursor_menu_principal

actualiza_cursor_menu_principal
	movf	ESTADO_PANTALLA, W
	sublw	MENU_PRINCIPAL_AJUSTES
	btfss	STATUS, Z
	goto	pon_cursor_acciones
pon_cursor_ajustes
	movlw	MENU_PRINCIPAL_AJUSTES	; Estabamos en acciones, pasamos a ajustes
	movwf	ESTADO_PANTALLA
	movlw	1
	movwf	LINEA_MENU
	goto	loop_menu_principal
pon_cursor_acciones
	movlw	MENU_PRINCIPAL_ACCIONES	; Estabamos en ajustes, pasamos a acciones
	movwf	ESTADO_PANTALLA
	movlw	2
	movwf	LINEA_MENU
	goto	loop_menu_principal


fin_menu_principal
	movlw	MAIN						; Estamos en la pantalla principal
	movwf	ESTADO_PANTALLA
	
	return







;****************************
;*							*
;*	Menu ajustes			*
;*							*
;****************************
MensajeMenuAjustesTitulo		addwf PCL,1
								retlw 'M'
								retlw 'e'
								retlw 'n'
								retlw 'u'
								retlw ' '
								retlw 'a'
								retlw 'j'
								retlw 'u'
								retlw 's'
								retlw 't'
								retlw 'e'
								retlw 's'
								retlw 0x0
MensajeMenuAjustesHora			addwf PCL,1
								retlw 'H'
								retlw 'o'
								retlw 'r'
								retlw 'a'
								retlw 0x0
MensajeMenuAjustesTemperatura	addwf PCL,1
								retlw 'T'
								retlw 'e'
								retlw 'm'
								retlw 'p'
								retlw 'e'
								retlw 'r'
								retlw 'a'
								retlw 't'
								retlw 'u'
								retlw 'r'
								retlw 'a'
								retlw 0x0
MensajeMenuAjustesPH			addwf PCL,1
								retlw 'p'
								retlw 'H'
								retlw 0x0
MensajeMenuAjustesIluminacion	addwf PCL,1
								retlw 'I'
								retlw 'l'
								retlw 'u'
								retlw 'm'
								retlw 'i'
								retlw 'n'
								retlw 'a'
								retlw 'c'
								retlw 'i'
								retlw 'o'
								retlw 'n'
								retlw 0x0
MensajeMenuAjustesCebado		addwf PCL,1
								retlw 'C'
								retlw 'e'
								retlw 'b'
								retlw 'a'
								retlw 'd'
								retlw 'o'
								retlw 0x0
MensajeMenuAjustesOleaje		addwf PCL,1
								retlw 'O'
								retlw 'l'
								retlw 'e'
								retlw 'a'
								retlw 'j'
								retlw 'e'
								retlw 0x0

EscribeMenuAjustesTitulo		clrf LCD_RESULT
EscribeMenuAjustesTitulo1		movf LCD_RESULT, 0
								call MensajeMenuAjustesTitulo
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesTitulo1

EscribeMenuAjustesHora			clrf LCD_RESULT
EscribeMenuAjustesHora1			movf LCD_RESULT, 0
								call MensajeMenuAjustesHora
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesHora1

EscribeMenuAjustesTemperatura	clrf LCD_RESULT
EscribeMenuAjustesTemperatura1	movf LCD_RESULT, 0
								call MensajeMenuAjustesTemperatura
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesTemperatura1

EscribeMenuAjustesPH			clrf LCD_RESULT
EscribeMenuAjustesPH1			movf LCD_RESULT, 0
								call MensajeMenuAjustesPH
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesPH1

EscribeMenuAjustesIluminacion	clrf LCD_RESULT
EscribeMenuAjustesIluminacion1	movf LCD_RESULT, 0
								call MensajeMenuAjustesIluminacion
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesIluminacion1

EscribeMenuAjustesCebado		clrf LCD_RESULT
EscribeMenuAjustesCebado1		movf LCD_RESULT, 0
								call MensajeMenuAjustesCebado
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesCebado1

EscribeMenuAjustesOleaje		clrf LCD_RESULT
EscribeMenuAjustesOleaje1		movf LCD_RESULT, 0
								call MensajeMenuAjustesOleaje
								iorlw 0
								btfsc STATUS, 2
								return
								call LcdWriteData
								incf LCD_RESULT, 1
								goto EscribeMenuAjustesOleaje1




