;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 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    : cntdef.asm
; Purpose      : Counter Application Default State Manager
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntDefaultStateManager
; Description : Counter Application Default State Manager.
; Assumptions : Display is cleared on first time entry into the state.
; Input(s)    : CORECurrentEvent  - system event to be processed
;               COREEventArgument - event extra information
; Output(s)   : None
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

        GLOBAL  cntDefaultStateManager

cntDefaultStateManager:

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

                ld      A, [CORECurrentEvent]

                ; Check if state entry event.
                cp      A, #CNT_STATEENTRY
                jr      Z, cntDefaultStateStateEntryEvent

                ; Check if start/split depress event.
                cp      A, #CNT_STARTSPLITDEPRESS
                jr      Z, cntDefaultStateStartSplitDepressEvent

                ; Check if stop/reset depress event.
                cp      A, #CNT_STOPRESETDEPRESS
                jr      Z, cntDefaultStateStopResetDepressEvent

                ; Check if stop/reset release event.
                cp      A, #CNT_STOPRESETRELEASE
                jr      Z, cntDefaultStateStopResetReleaseEvent

                ; Check if mode depress event.
                cp      A, #CNT_MODEDEPRESS
                jr      Z, cntDefaultStateModeDepressEvent

                ; Check if timeout hi-res done event.
                cp      A, #CNT_TIMEOUTDONEHIGHRES
                jr      Z, cntDefaultStateTimeoutHiResDoneEvent

                ; Check if crown set event.
                cp      A, #CNT_CROWNSET
                jr      NZ, cntDefaultStateExit

                ;**************************************************************
                ;
                ;                       CROWN SET
                ;
                ;**************************************************************
            
                ld      B, #CNTSETBANNERSTATE
                CORE_REQ_STATE_CHANGE

cntDefaultStateExit:

                ret


cntDefaultStateStateEntryEvent:
                ;**************************************************************
                ;
                ;                      STATE ENTRY
                ;
                ;**************************************************************

                ; Suspend ring event.  Not used in this state.
                CORE_SUSPEND_RING_EVENTS

                CORE_ENABLE_SWITCH_RELEASE

;------------------------------------------------------------------------------
;
; W A R N I N G !!! This is a fall through.  Do not rearrange.
;
;------------------------------------------------------------------------------


cntDefaultSubStateEntry:
cntDefaultStateStopResetReleaseEvent:
                ;**************************************************************
                ;
                ;                      STOP/RESET RELEASE
                ;
                ;**************************************************************

                push    IY
                LCD_CLR_DISPLAY                
                LCD_DISP_SEG_MSG_COUNT
                pop     IY

                ;--------------------------------------------------------------
                ; Displays an arrow on the small dot matrix.  The position will
                ;  depend on the count direction.
                ; Destroys BAReg, HLReg, IXReg.
                ; Input: IYReg - ASD address
                ;--------------------------------------------------------------
                car     cntDisplayArrowOnSDM

                ;--------------------------------------------------------------
                ; Displays the counter data on the main dot matrix using 
                ;   large fonts.
                ; Destroys BAReg, HLReg, IXReg.
                ;--------------------------------------------------------------
                jr      cntDisplayCounterData       ; **EXTERNAL JUMP


cntDefaultStateStartSplitDepressEvent:
                ;**************************************************************
                ;
                ;                      START/SPLIT DEPRESS
                ;
                ;**************************************************************

                ; Cancel current switch release.  Not needed in this state.
                HW_KBD_CANCEL_CURRENT_SWITCH_RELEASE

                ; Get the current counter value.
                ld      HL, IY
                add     HL, #CNTDATALOOFFSET
                ld      HL, [HL]
                
                ; Load AReg with the counter status flag data and check the 
                ;   counting direction.
                ld      A, [IY + CNTFLAGSOFFSET]
                bit     A, #bCNTCountDown
                jr      Z, cntDefaultStartSplitDepressCountUp

                ;==============================================================
                ;
                ;                       COUNT DOWN
                ;
                ;==============================================================
                
                ; Check whether it is in the minimum value.
                cp      HL, #CNTMINDATA
                jr      C, cntDefaultStartSplitDepressExit
                jr      Z, cntDefaultStartSplitDepressExit

                ;--------------------------------------------------------------
                ; Subtract 1 to the counter data.
                ; Destroys BAReg, IXReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                car     cntSubDataBy1
                jr      cntDefaultSSDispDataAndReqAlert

cntDefaultStartSplitDepressCountUp:

                ;==============================================================
                ;
                ;                       COUNT UP
                ;
                ;==============================================================
                
                ; Check whether it is in the minimum value.
                cp      HL, #CNTMAXDATA
                jr      NC, cntDefaultStartSplitDepressExit
                 
                ;--------------------------------------------------------------
                ; Add 1 to the counter data.
                ; Destroys BAReg, IXReg, HLReg.
                ; Input: IYReg - ASD address.
                ;--------------------------------------------------------------
                car     cntAddDataBy1

cntDefaultSSDispDataAndReqAlert:
                ;--------------------------------------------------------------
                ; Displays the counter data on the main dot matrix using 
                ;   large fonts.
                ; Destroys BAReg, HLReg, IXReg.
                ;--------------------------------------------------------------
                car     cntDisplayCounterData

                ;--------------------------------------------------------------
                ; Generate alert to indicated that it has successfully 
                ;   decremented the counter.
                ;--------------------------------------------------------------
                AUDSTART_SYSTEM_MELODY AUDSWBEEPMELODY, AUDNOMELODYDONEEVENT


cntDefaultStartSplitDepressExit:

                ret



cntDefaultStateStopResetDepressEvent:
                ;**************************************************************
                ;
                ;                      STOP/RESET DEPRESS
                ;
                ;**************************************************************

                ; Get the current counter value and check whether it is in the 
                ;   minimum value.

                add     IY, #CNTDATALOOFFSET
                ld      BA, [IY]

                cp      BA, #CNTMINDATA
                jr      Z, cntDefaultStopResetDepressExit

                ; Not yet in its minimum.

                ; Request 2sec timeout.
                CORE_REQ_TIMEOUT_HIRES  CNTHIRESTO_2SECONDS

                LCD_CLR_DISPLAY
                LCD_DISP_SMALL_DM_MSG_HOLD_TO_RESET

cntDefaultStopResetDepressExit:

                ret


cntDefaultStateModeDepressEvent:
                ;**************************************************************
                ;
                ;                          MODE DEPRESS
                ;
                ;**************************************************************

                CORE_REQ_MODE_CHANGE_NEXT

                ret


cntDefaultStateTimeoutHiResDoneEvent:
                ;**************************************************************
                ;
                ;                      TIMEOUT DONE HI-RES
                ;
                ;**************************************************************

                ; Cancel current switch release.  Not needed in this state.
                HW_KBD_CANCEL_CURRENT_SWITCH_RELEASE
                                
                AUDSTART_SYSTEM_MELODY AUDSWBEEPMELODY, AUDNOMELODYDONEEVENT

                ; Clear counter data.
                ld      A, #0
                ld      [IY + CNTDATALOOFFSET], A
                ld      [IY + CNTDATAHIOFFSET], A
                
                ; Redisplay everything.
                jr      cntDefaultSubStateEntry

