;   ***************************************************************
;   * Copyright (c) 2001, Embed Inc (http://www.embedinc.com)     *
;   *                                                             *
;   * Permission to copy this file is granted as long as this     *
;   * copyright notice is included in its entirety at the         *
;   * beginning of the file, whether the file is copied in whole  *
;   * or in part and regardless of whether other information is   *
;   * added to the copy.                                          *
;   *                                                             *
;   * The contents of this file may be used in any way,           *
;   * commercial or otherwise.  This file is provided "as is",    *
;   * and Embed Inc makes no claims of suitability for a          *
;   * particular purpose nor assumes any liability resulting from *
;   * its use.                                                    *
;   ***************************************************************
;
;   This module contains the main application routine that is run after
;   the system is initialized.
;
         include "hal.inc"

         extern  eyes_100ms  ;do 100mS processing to possibly update the eyes
         extern  soun_100ms  ;do 100mS processing to possibly start a new sound
         extern  ad_start    ;start a new A/D conversion
         extern  ad_read     ;process the result from a completed A/D conversion
         extern  eyes_off    ;have eyes handler enter OFF mode
         extern  soun_off    ;have sound handler enter OFF mode

         extern_flags        ;declare global flag bits EXTERN
;
;***********************************************************************
;
;   Configuration constants.
;
offtime  equ     30          ;min time to stay off if event detected, 100mS units
lbank    equ     0           ;register bank for the local state of this module
;
;   Derived constants.
;
lbankadr equ     bankadr(lbank) ;address with local state register bank
;
;***********************************************************************
;
;   Local state.
;
.bank#v(lbank) udata

cntoff   res     1           ;100mS ticks left to stay off

.main    code
;
;***********************************************************************
;
;   Routine MAIN
;
;   This entry point is jumped to from the INIT module, once system
;   initialization is complete.
;
         glbent  main
;
;**********
;
;   Initialize the state managed by this module.
;
         dbankif lbankadr
         clrf    cntoff      ;init to off timeout has expired
;
;**********
;
;   Main application loop.  Back here to look for something to do.
;   Whenever something is found that needs handling, a handler routine
;   is jumped to.  All handler routines jump back to LOOP_MAIN when done.
;   This means events are checked in priority order, with high priority
;   events checked earlier.
;
         glbent  loop_main
;
;   Check for an event was detected which requires the system to enter OFF mode.
;
         dbankif gbankadr
         btfss   flag_newoff ;a new event detected that requires off mode ?
         goto    no_off      ;no

         bcf     flag_newoff ;clear the event condition
         bsf     flag_off    ;set OFF mode
         dbankif lbankadr
         movlw   offtime     ;reset off timeout counter
         movwf   cntoff
         ;
         ;   Do the system processing to go OFF.
         ;
         gcall   eyes_off    ;shut off the eyes
         gcall   soun_off    ;shut off the sound

no_off   unbank
;
;   Check for time to start a new A/D conversion.
;
         dbankif gbankadr
         btfss   flag_adconv
         goto    no_adconv
         gjump   ad_start
no_adconv unbank
;
;   Check for A/D conversion has completed, need to process the result.
;
         dbankif pir1
         btfss   pir1, adif
         goto    no_addone
         gjump   ad_read
no_addone unbank
;
;   Do the 100mS processing if an unhandled 100mS tick is indicated.
;
         dbankif gbankadr
         btfss   flag_100ms  ;time to do 100mS processing ?
         goto    no_100ms    ;no
         ;
         ;   We get here once every 100mS.
         ;
         gcall   eyes_100ms  ;possibly update the eyes state
         gcall   soun_100ms  ;possibly start a new sound
         dbankif gbankadr
         bcf     flag_100ms  ;reset pending 100mS tick flag

         dbankif gbankadr
         btfss   flag_off    ;in OFF mode ?
         goto    done_on     ;no, already ON
         dbankif lbankadr
         decfsz  cntoff      ;one less 100mS tick until off timeout expired
         goto    done_on     ;OFF mode timeout not expired yet
         dbankif gbankadr
         bcf     flag_off    ;exit OFF mode, back to ON mode
done_on  unbank              ;done checking for coming out of OFF mode

no_100ms unbank
;
;   Nothing was found that needed handling.  Go back and check everything
;   again.
;
         goto    loop_main

         end