;
; mode_settings.inc: handles settings mode
;
; author: Marko Kanala, raato ]a t[ mulletronic.com
;
; Allows editing the following sequencer settings:
;
; - din input on/off
; - din output on/off
; - midi sync in on/off
; - midi sync out on/off
; - midi notes out on/off
; - midi channel 0-16
;
; LICENSE
; Creative Commons Attribution-NonCommercial-ShareALike 2.5
;
; You are free:
;
; * to copy, distribute, display, and perform the work
; * to make derivative works
;
; Under the following conditions:
;	
; 1. Attribution. You must attribute the work in the manner specified by the 
;    author or licensor.
; 2. Noncommercial. You may not use this work for commercial purposes.
; 3. Share Alike. If you alter, transform, or build upon this work, you may 
;    distribute the resulting work only under a license identical to this one.
;
; * For any reuse or distribution, you must make clear to others the license 
;   terms of this work.
; * Any of these conditions can be waived if you get permission from the 
;   copyright holder.
;
; Your fair use and other rights are in no way affected by the above.
;
; This is a human-readable summary of the Legal Code.
; See full license at http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode
;
; Copyright: Marko Kanala (raato@mulletronic.com).
;

; called when the settings mode is entered
mode_settings_enter:
	SETDISPLAY	s_s_main		; see lcd.inc
	clrf	selected_setting
	clrf	select_pressed
	; clear all leds to indicate settings mode
	clrf	leds_busa_1
	clrf	leds_busa_2
	clrf	leds_busb_1
	return

; called in continuous loop when in settings mode
mode_settings:
	; refresh lcd display if needed	
	call	lcd_refresh

	; do we have key events ?
	IFNOEVENT	EVENT_KEY, s_no_keyevents

	; exits settings mode
	KEYEVENT	KEY_STOPCONT, s_exit

;	KEYEVENT	KEY_S1, s_stepkey	; din in
;	KEYEVENT	KEY_S2, s_stepkey	; din out
;	KEYEVENT	KEY_S3, s_stepkey	; midi sync in
;	KEYEVENT	KEY_S4, s_stepkey	; midi sync out
;	KEYEVENT	KEY_S5, s_stepkey	; midi notes out
;	KEYEVENT	KEY_S6, s_stepkey	; midi channel
;	KEYEVENT	KEY_S7, s_stepkey
;	KEYEVENT	KEY_S8, s_stepkey
;	KEYEVENT	KEY_S9, s_stepkey
;	KEYEVENT	KEY_S10, s_stepkey
;	KEYEVENT	KEY_S11, s_stepkey
;	KEYEVENT	KEY_S12, s_stepkey
;	KEYEVENT	KEY_S13, s_stepkey
;	KEYEVENT	KEY_S14, s_stepkey	; select (for channel)
;	KEYEVENT	KEY_S15, s_stepkey	; on
;	KEYEVENT	KEY_S16, s_stepkey	; off
	; optimized for space
	STEPKEYEVENT	s_stepkey

	KEYEVENT	KEY_PATTPLAYWRITE, s_pattplaywrite
	KEYEVENT	KEY_TRACKPLAYWRITE, s_trackplaywrite

s_no_keyevents:
;  set default display
	SETDISPLAY	s_s_main
	call	s_refreshleds
	return

s_keydone_lcdrefresh:
	FORCEDISPLAYREFRESH
; all keyhandlers should call this afterwards
s_keydone:
	call	s_refreshleds
s_keydone_keepleds:
	CLEAREVENT	EVENT_KEY
	return

; lights up the selected settings
s_refreshleds:
	movf	selected_setting, W	
	call	get_bit
	movwf	leds_busa_1			; settings only in lower half
	clrf	leds_busa_2
	return

; switches to track play mode and exists
s_trackplaywrite:
	call	s_save_settings
	SETMODE		MODE_TPLAY
	call	mode_trackplay_enter
	bra 	s_keydone_keepleds

; switches to pattern play mode and exists
s_pattplaywrite:
	call	s_save_settings
	SETMODE		MODE_PPLAY
	call	mode_pattplay_enter
	bra 	s_keydone_keepleds

; exits from settings mode to the previous mode
s_exit:
	call	s_save_settings
	CLEARSETTINGSMODE				; sets the previous mode
	IFMODECALL	MODE_PPLAY, mode_pattplay_enter
	IFMODECALL	MODE_PWRITE, mode_pattwrite_enter
	IFMODECALL	MODE_TPLAY, mode_trackplay_enter
	IFMODECALL	MODE_TWRITE, mode_trackwrite_enter
	bra		s_keydone_keepleds

; handles step key presses
s_stepkey:
	; shift pressed, commands
	IFKEYDOWN	DOWNKEY_SHIFTFUNC, s_shiftcommand
	movlw	0
	cpfseq	select_pressed
	bra		s_select_value
s_stepkey_normal:
	; no "select" pressed
	KEYEVENT	KEY_S14, s_select_pressed
	KEYEVENT	KEY_S15, s_on_pressed
	KEYEVENT	KEY_S16, s_off_pressed
	; lower keys select setting
	movlw	7
	cpfslt	keypress
	bra		s_keydone		; ignore keypresses 7 - 13
s_stepkey_select_setting:
	decf	keypress, W
	movwf	selected_setting
	bra		s_keydone_lcdrefresh

; [select] pressed
s_select_pressed:
	; allowed only for setting 5 (midi channel)
	movlw	5
	cpfseq	selected_setting
	bra		s_keydone
	movff	selected_setting, select_pressed
	bra		s_keydone_lcdrefresh

; [on] pressed
s_on_pressed:
	; not allowed for setting 5 (midi channel)
	movlw	5
	cpfslt	selected_setting
	bra		s_keydone
	movf	selected_setting, W
	call	get_bit
	iorwf	settings
	bra		s_keydone_lcdrefresh

; [off] pressed
s_off_pressed:
	; not allowed for setting 5 (midi channel)
	movlw	5
	cpfslt	selected_setting
	bra		s_keydone
	movf	selected_setting, W
	call	get_bit
	comf	WREG
	andwf	settings	
	bra		s_keydone_lcdrefresh

; select is pressed for a setting and a value should
;  be selected
s_select_value:
	; ... only midi channel is selectable and
	;     translates directly to channel number
	movff	keypress, settings_midi_channel
	clrf	select_pressed
	bra		s_keydone_lcdrefresh

; loads settings from flash
s_load_settings:
	movlw	eeprom_settings
	call	eeprom_load
	movff	EEDATA, settings
	movlw	eeprom_midi_channel
	call	eeprom_load
	movff	EEDATA, settings_midi_channel

	; sanity check

	movlw	17
	cpfslt	settings_midi_channel
	bra		s_reset_defaults
	movlw	0
	cpfsgt	settings_midi_channel
	bra		s_reset_defaults
	return

s_reset_defaults:
	movlw	default_settings
	movwf	settings
	movlw	default_midi_channel
	movwf	settings_midi_channel
	return

; saves settings to flash
s_save_settings:
	movff	settings, EEDATA
	movlw	eeprom_settings
	call	eeprom_save
	movff	settings_midi_channel, EEDATA
	movlw	eeprom_midi_channel
	call	eeprom_save
	return

; -- Commands -->

; handles shifted commands
;
;  s16 == exit
s_shiftcommand:
	KEYEVENT	KEY_S16, s_exit
	bra 	s_keydone

External Labels :

lcd_refresh
s_refreshleds
get_bit
s_save_settings
mode_trackplay_enter
mode_pattplay_enter
eeprom_load
eeprom_save