; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************


; ***************************************************************************
; defines

.equ FILTER_KEEPUP_OFFS_TIMER_LOW         = 0
.equ FILTER_KEEPUP_OFFS_TIMER_HIGH        = 1
.equ FILTER_KEEPUP_OFFS_RESTARTVALUE_LOW  = 2
.equ FILTER_KEEPUP_OFFS_RESTARTVALUE_HIGH = 3
.equ FILTER_KEEPUP_DATA_SIZE              = 4



; ***************************************************************************
; code

.cseg



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_Init @global
;
; @param Y  pointer to filter data
; @clobbers r16

FilterKeepUp_Init:
  clr r16
  std Y+FILTER_KEEPUP_OFFS_TIMER_LOW, r16
  std Y+FILTER_KEEPUP_OFFS_TIMER_HIGH, r16
  ret
; @end



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_Fini @global
;
; @param Y  pointer to filter data

FilterKeepUp_Fini:
  ; nothing to do
  ret
; @end



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_GetValue @global
;
; @param Y  pointer to filter data
; @return R17:R16 value
; @clobbers none

FilterKeepUp_SetRestartValue:
  std Y+FILTER_KEEPUP_OFFS_RESTARTVALUE_LOW, r16
  std Y+FILTER_KEEPUP_OFFS_RESTARTVALUE_HIGH, r17
  ret
; @end



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_GetValue @global
;
; @param Y  pointer to filter data
; @return R16 value
; @clobbers R17

FilterKeepUp_GetValue:
  ldd r16, Y+FILTER_KEEPUP_OFFS_TIMER_LOW
  ldd r17, Y+FILTER_KEEPUP_OFFS_TIMER_HIGH
  or r16, r17
  breq FilterKeepUp_GetValue_end
  ldi r16, 1
FilterKeepUp_GetValue_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_SetValue @global
;
; @param Y  pointer to filter data
; @param R16 value
; @clobbers r17

FilterKeepUp_SetValue:
  tst r16
  breq FilterKeepUp_SetValue_end
  ldd r17, Y+FILTER_KEEPUP_OFFS_RESTARTVALUE_LOW
  std Y+FILTER_KEEPUP_OFFS_TIMER_LOW, r17
  ldd r17, Y+FILTER_KEEPUP_OFFS_RESTARTVALUE_HIGH
  std Y+FILTER_KEEPUP_OFFS_TIMER_HIGH, r17
FilterKeepUp_SetValue_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine FilterKeepUp_Every100ms @global
;
; @param Y  pointer to filter data
; @clobbers r24, r25

FilterKeepUp_Every100ms:
  ldd r24, Y+FILTER_KEEPUP_OFFS_TIMER_LOW
  ldd r25, Y+FILTER_KEEPUP_OFFS_TIMER_HIGH
  sbiw r25:r24, 1
  brcs FilterKeepUp_Every100ms_end
  std Y+FILTER_KEEPUP_OFFS_TIMER_LOW, r24
  std Y+FILTER_KEEPUP_OFFS_TIMER_HIGH, r25
FilterKeepUp_Every100ms_end:
  ret
; @end




