;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Copyright (c) 2002 TIMEX Corporation.
; 
; This document is the property of, and contains information proprietary to
; TIMEX Corporation.  It is to be returned upon request.  Reproduction by any
; means, disclosure, or use of its contents for other than the expressed
; purpose for which it is loaned, without the written consent of TIMEX
; Corporation, is strictly prohibited.
;
; File Name    : cntset.asm
; Purpose      : Counter Application Set State Manager
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntSetStateManager
; Description : Counter Application Set State Manager
; Assumptions : Display is cleared on first time entry into the state
; Input(s)    : None
; Output(s)   : None
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        IF @DEF('SUBROUTINE')
            UNDEF SUBROUTINE
        ENDIF
        DEFINE  SUBROUTINE      "'cntSetStateManager'"

        GLOBAL  cntSetStateManager

cntSetStateManager:
                
                ; Set IYReg the address of the counter ASD.
                ld      IY, [CORECurrentASDAddress]

                ld      A, [CORECurrentEvent]

                cp      A, #CNT_STATEENTRY
                jr      Z, cntSetStateStateEntryEvent

                cp      A, #CNT_MODEDEPRESS
                jr      Z, cntSetStateModeDepressEvent

                cp      A, #CNT_STOPRESETDEPRESS
                jr      Z, cntSetStateStopResetDepressEvent

                cp      A, #CNT_CWPULSES
                jr      Z, cntSetStateCWPulseEvent

                cp      A, #CNT_CCWPULSES
                jr      Z, cntSetStateCCWPulseEvent

                cp      A, #CNT_CROWNHOME
                jr      NZ, cntSetStateExit

                ;**************************************************************
                ;
                ;                        CROWN HOME
                ;
                ;**************************************************************

                ld      B, #CNTDEFAULTSTATE
                CORE_REQ_STATE_CHANGE


cntSetStateExit:

                ret

cntSetStateStateEntryEvent:
                ;**************************************************************
                ;
                ;                          STATE ENTRY
                ;
                ;**************************************************************

                ; Enable pulse mode to change values.
                CORE_ENABLE_PULSE_MODE
                
                ; Mask start/split key.  This event is not needed.
                CORE_MASK_KEYS  (CNTSWITCHMASK_STARTSPLIT | CNTSWITCHMASK_EL)
                
                ; Clear the bit indicating that the first set item is the counter data.
                ld      HL, #CNTTempFlags
                and     [HL], #@LOW(~bCNTSetDirection)
                
                ;--------------------------------------------------------------
                ; Refresh the display and request blinking on the editable field.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                jr      cntSetRedisplayAndReqBlink  ; **EXTERNAL JUMP


cntSetStateModeDepressEvent:
cntSetStateStopResetDepressEvent:
                ;**************************************************************
                ; 
                ;                   STOP/RESET & MODE DEPRESS
                ; 
                ;**************************************************************

                ; Load the address to HLReg and toggle the set direction flag.
                ld      HL, #CNTTempFlags
                xor     [HL], #bCNTSetDirection
                            
                ;--------------------------------------------------------------
                ; Clear the entire display.
                ; Destroys BAReg, HLReg.
                ;--------------------------------------------------------------
                LCD_CLR_DISPLAY
    
                ;--------------------------------------------------------------
                ; Refresh the display and request blinking on the editable field.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                jr      cntSetRedisplayAndReqBlink  ; **EXTERNAL JUMP


cntSetStateCWPulseEvent:
                ;**************************************************************
                ; 
                ;              CW PULSE (Increment Field Value)
                ; 
                ;**************************************************************

                ; Check the item to be set.
                ld      HL, #CNTTempFlags
                bit     [HL], #bCNTSetDirection
                jr      NZ, cntSetStateToggleDirection

                ;--------------------------------------------------------------
                ; Add counter data by acceleration value.
                ; Destroys BAReg, IXReg, HLReg.
                ; Input: IYReg - ASD address
                ;        COREEventArgument - Number of pulses
                ;--------------------------------------------------------------
                car     cntAddDataByAcceleration

                ;--------------------------------------------------------------
                ; Refresh the display and request blinking on the editable field.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                jr      cntSetRedisplayAndReqBlink  ; **EXTERNAL JUMP


cntSetStateCCWPulseEvent:
                ;**************************************************************
                ;
                ;              CCW PULSE (Decrement Field Value)
                ;
                ;**************************************************************
            
                ; Check the item to be set.
                ld      HL, #CNTTempFlags
                bit     [HL], #bCNTSetDirection
                jr      NZ, cntSetStateToggleDirection

                ;--------------------------------------------------------------
                ; Subtract counter data by acceleration value.
                ; Destroys BAReg, IXReg.
                ; Input: IYReg - ASD address
                ;        COREEventArgument - Number of pulses
                ;--------------------------------------------------------------
                car     cntSubDataByAcceleration

                ;--------------------------------------------------------------
                ; Refresh the display and request blinking on the editable field.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                jr      cntSetRedisplayAndReqBlink  ; **EXTERNAL JUMP


cntSetStateToggleDirection:
                
                ; Toggle count-up/countdown bit.
                ld      A, [IY + CNTFLAGSOFFSET]
                xor     A, #bCNTCountDown
                ld      [IY + CNTFLAGSOFFSET], A

                ;--------------------------------------------------------------
                ; Clear line 2 only so that the display would not look like 
                ;   garbage when changing from "DOWN" to "UP".
                ; Destroys AReg, IXReg.
                ;--------------------------------------------------------------
                LCD_CLR_MAIN_DM_LINE2

                ;--------------------------------------------------------------
                ; Refresh the display and request blinking on the editable field.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                jr      cntSetRedisplayAndReqBlink  ; **EXTERNAL JUMP


