; ***************************************************************************
; 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 APP_DOOR_KEEPUPTIME = 50 ; 5s

.equ APP_DOOR_STATE_OPEN        = 255
.equ APP_DOOR_STATE_CLOSED      = 0



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

.dseg

appDoorKeepupData:   .byte FILTER_KEEPUP_DATA_SIZE
appDoorValSchedData: .byte VALSCHED_DATA_SIZE



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

.cseg

; ---------------------------------------------------------------------------
; @routine AppDoor_Init @global
;

AppDoor_Init:
  ldi yl, LOW(appDoorKeepupData)
  ldi yh, HIGH(appDoorKeepupData)
  rcall FilterKeepUp_Init
  ldi r16, LOW(APP_DOOR_KEEPUPTIME)
  ldi r17, HIGH(APP_DOOR_KEEPUPTIME)
  rcall FilterKeepUp_SetRestartValue

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



; ---------------------------------------------------------------------------
; @routine AppDoor_Every100ms @global
;

AppDoor_Every100ms:
  rcall TCRT1K_GetValue                 ; get value into R16
  ldi yl, LOW(appDoorKeepupData)
  ldi yh, HIGH(appDoorKeepupData)
  brcc AppDoor_Every100ms_l1
  rcall FilterKeepUp_SetValue           ; (R17)
  
AppDoor_Every100ms_l1:
  rcall FilterKeepUp_Every100ms
  rcall FilterKeepUp_GetValue           ; get value into R16

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



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

appDoorSendValue:
  ldi yl, LOW(appDoorValSchedData)
  ldi yh, HIGH(appDoorValSchedData)
  rcall ValueScheduler_GetValue       ; get value to r16
  tst r16
  ldi r16, APP_DOOR_STATE_CLOSED
  breq appDoorSendValue_send
  ldi r16, APP_DOOR_STATE_OPEN
appDoorSendValue_send:
  ldi r17, VALUE_ID_TCRT1K            ; VALUE ID
  ldi r22, AQHOME_VALUETYPE_DOOR      ; VALUE TYPE
  rcall Main_Send8BitValueReport
#ifdef MODULES_LED1
  rcall Led1_SetActivityTiming        ; (r16)
#endif
  ret
; @end





