; ***************************************************************************
; 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.   *
; ***************************************************************************


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

.cseg



; ---------------------------------------------------------------------------
; @routine AppMotionLight_Init @global
;

AppMotionLight_Init:
  ; preset SRAM data area
  ldi xh, HIGH(appMotionLightDataBegin)
  ldi xl, LOW(appMotionLightDataBegin)
  clr r16
  ldi r17, (appMotionLightDataEnd-appMotionLightDataBegin)
  rcall Utils_FillSram
  
  rcall appMotionLightReadConfFromEeprom    ; (r16, r17, X, Y)
 
  ret
; @end



; ---------------------------------------------------------------------------
; @routine AppMotionLight_Fini @global
;

AppMotionLight_Fini:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightHasSource
;
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @clobbers r16, r24, Y

appMotionLightHasSource:
  ldi yl, LOW(appMotionLightMSources)
  ldi yh, HIGH(appMotionLightMSources)
  ldi r24, APP_MOTIONLIGHT_SOURCE_NUM
appMotionLightHasSource_loop:
    rcall appMotionLightCheckMsgSrc
    brcs appMotionLightHasSource_ret
    adiw yh:yl, APP_MOTIONLIGHT_SOURCE_SIZE
    dec r24
  brne  appMotionLightHasSource_loop
  clc
appMotionLightHasSource_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightIsBrightnessSource
;
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @clobbers R16, Y

appMotionLightIsBrightnessSource:
  ldi yl, LOW(appMotionLightLSource)
  ldi yh, HIGH(appMotionLightLSource)
  rjmp appMotionLightCheckMsgSrc
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightCheckMsgSrc
;
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @param Y pointer to addr/valueid pair to investigate
; @clobbers R16

appMotionLightCheckMsgSrc:
  ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
  cp r16, r22                                     ; check node addr
  brne appMotionLightCheckMsgSrc_no
  ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
  cp r16, r17                                     ; check value id
  brne appMotionLightCheckMsgSrc_no
  sec
  rjmp appMotionLightCheckMsgSrc_ret
appMotionLightCheckMsgSrc_no:
  clc
appMotionLightCheckMsgSrc_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightReadConfFromEeprom
;
; @clobbers r16, r17, X, Y

appMotionLightReadConfFromEeprom:
  ldi xl, LOW(EEPROM_OFFS_MAL_CONF)
  ldi xh, HIGH(EEPROM_OFFS_MAL_CONF)
  ldi yl, LOW(appMotionLightSettings)
  ldi yh, HIGH(appMotionLightSettings)
  ldi r17, APP_MOTIONLIGHT_EE_SIZE
  bigcall Eeprom_ReadBytes                  ; (r16, r17, X, Y)
  brcc appMotionLightReadConfFromEeprom_ret
  
  lds r18, appMotionLightOnTime
  lds r19, appMotionLightOnTime+1
  mov r16, r18
  and r16, r19
  inc r16
  brne appMotionLightReadConfFromEeprom_reload
  ldi r18, LOW(APP_MOTIONLIGHT_DEFAULT_ONTIME)
  ldi r19, HIGH(APP_MOTIONLIGHT_DEFAULT_ONTIME)
appMotionLightReadConfFromEeprom_reload:
  bigcall SK6812_SetAutoTimerReload             ; (none)

appMotionLightReadConfFromEeprom_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightWriteConfToEeprom
;
; @clobbers r16, r17, X, Y

appMotionLightWriteConfToEeprom:
  ldi xl, LOW(EEPROM_OFFS_MAL_CONF)
  ldi xh, HIGH(EEPROM_OFFS_MAL_CONF)
  ldi yl, LOW(appMotionLightSettings)
  ldi yh, HIGH(appMotionLightSettings)
  ldi r17, APP_MOTIONLIGHT_EE_SIZE
  bigjmp Eeprom_WriteBytes             ; (r16, r17, r18, X, Y)
; @end


