; ***************************************************************************
; copyright   : (C) 2024 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 APP_MOTION_KEEPUPTIME = 150 ; 15s



; ***************************************************************************
; data

.dseg

appMotionKeepupData:   .byte FILTER_KEEPUP_DATA_SIZE
appMotionValSchedData: .byte VALSCHED_DATA_SIZE



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

.cseg

; ---------------------------------------------------------------------------
; @routine AppMotion_Init @global
;

AppMotion_Init:
  rcall Motion_Init
  
  ldi yl, LOW(appMotionKeepupData)
  ldi yh, HIGH(appMotionKeepupData)
  rcall FilterKeepUp_Init
  ldi r16, LOW(APP_MOTION_KEEPUPTIME)
  ldi r17, HIGH(APP_MOTION_KEEPUPTIME)
  rcall FilterKeepUp_SetRestartValue

  ldi yl, LOW(appMotionValSchedData)
  ldi yh, HIGH(appMotionValSchedData)
  rcall ValueScheduler_Init
  ldi r16, (1<<VALSCHED_FLAGS_REPEAT1_BIT)  ; only repeat report interval for active motion
  std Y+VALSCHED_OFFS_FLAGS, r16
  
  ret
; @end



; ---------------------------------------------------------------------------
; @routine AppMotion_Every100ms @global
;

AppMotion_Every100ms:
  rcall Motion_GetValue                 ; get value into R16

  ldi yl, LOW(appMotionKeepupData)
  ldi yh, HIGH(appMotionKeepupData)
  rcall FilterKeepUp_SetValue           ; (R17)
  rcall FilterKeepUp_Every100ms
  rcall FilterKeepUp_GetValue           ; get value into R16

  ldi yl, LOW(appMotionValSchedData)
  ldi yh, HIGH(appMotionValSchedData)
  rcall ValueScheduler_SetValue         ; (R17)
  rcall ValueScheduler_Every100ms       ; (R16, R17)
  rcall ValueScheduler_CheckSend        ; (R16)
  brcc AppMotion_Every100ms_end
  ; prepare and send value message
  rcall appMotionSendValue
  brcc AppMotion_Every100ms_end         ; jmp on error
  ldi yl, LOW(appMotionValSchedData)
  ldi yh, HIGH(appMotionValSchedData)
  rcall ValueScheduler_MarkSent         ; (R16)
AppMotion_Every100ms_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionSendValue
;
; @return CFLAGS set if message enqueued, cleared on error
; @clobbers R17, R18, R19, R20, R21, R22, Y (R16, R23, R24, R25)

appMotionSendValue:
  ldi yl, LOW(appMotionValSchedData)
  ldi yh, HIGH(appMotionValSchedData)
  rcall ValueScheduler_GetValue       ; get value to r16

  ldi r17, VALUE_ID_MOTION            ; VALUE ID
  ldi r22, AQHOME_VALUETYPE_MOTION    ; VALUE TYPE
  rcall Main_Send8BitValueReport
#ifdef MODULES_LED1
  rcall Led1_SetActivityTiming        ; (R18, R19, R20)
#endif
  ret
; @end





