; ***************************************************************************
; copyright   : (C) 2026 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_OnPacketReceived @global
;
; @clobbers any, -X

AppMotionLight_OnPacketReceived:
  ldi zl, LOW(appMotionLightMsgTable*2)
  ldi zh, HIGH(appMotionLightMsgTable*2)
  rjmp Main_HandleValueMsg



; ---------------------------------------------------------------------------
; @routine appMotionLightHandleReport
;
; @param R17     value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22     source address
; @param R23     command
; @param R25:R24 message id

appMotionLightHandleReport:
  ; is brightness source?
  rcall appMotionLightIsBrightnessSource       ; (R16, Y)
  brcs appMotionLightHandleReport_brightness
  ; is motion source?
  rcall appMotionLightHasSource                ; (r16, r24, Y)
  brcc appMotionLightHandleReport_ret
  ; motion detected?
  mov r16, r18
  or r16, r19
  breq appMotionLightHandleReport_ret          ; zero value, ignore

  ; motion detected, check for brightness source
  lds r16, (appMotionLightLSource+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR) ; do we have a source for brightness value?
  tst r16
  breq appMotionLightHandleReport_trigger      ; no, jmp and don't check brightness
  cpi r16, 0xff
  breq appMotionLightHandleReport_trigger      ; no, jmp and don't check brightness
  ; we have a brightness source configured, do we have a value?
  lds r16, appMotionLightFlags                 ; check last brightness reported
  andi r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
  breq appMotionLightHandleReport_ret          ; no brightness, yet
appMotionLightHandleReport_checkBrightness:
  lds r16, appMotionLightLSourceValue          ; check last brightness reported
  lds r17, appMotionLightLSourceValue+1
  lds r24, appMotionLightLastBrightness        ; r25:r24>r17:r16?
  lds r25, appMotionLightLastBrightness+1      ; appMotionLightLastBrightness>appMotionLightLSourceValue?
  sub r16, r24
  sbc r17, r25
  brcs appMotionLightHandleReport_ret          ; yes, too bright to turn on
appMotionLightHandleReport_trigger:
  bigcall SK6812_Trigger
  rjmp appMotionLightHandleReport_ret
  ; received report is our brightness source
appMotionLightHandleReport_brightness:
  sts appMotionLightLastBrightness, r18
  sts appMotionLightLastBrightness+1, r19
  lds r16, appMotionLightFlags
  ori r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
  sts appMotionLightFlags, r16
appMotionLightHandleReport_ret:
  clc                                          ; don't send ACK
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueOnTime
;
; @param R17     value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22     source address
; @param R23     command
; @param R25:R24 message id

appMotionLightSetValueOnTime:
  sts appMotionLightOnTime, r18
  sts appMotionLightOnTime+1, r19
  rcall SK6812_SetAutoTimerReload
  rjmp appMotionLightSetValueReturn
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource1
;
; @param R17     value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22     source address
; @param R23     command
; @param R25:R24 message id

appMotionLightSetValueSource1:
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18   ; peerAddr
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
  rjmp appMotionLightSetValueReturn
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource2
;
; @param R17     value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22     source address
; @param R23     command
; @param R25:R24 message id

appMotionLightSetValueSource2:
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18    ; peerAddr
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
  rjmp appMotionLightSetValueReturn
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueBValue
;
; @param R17     value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22     source address
; @param R23     command
; @param R25:R24 message id

appMotionLightSetValueBValue:
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18    ; peerAddr
  sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
  rjmp appMotionLightSetValueReturn
; @end



; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueReturn
;
; Common return code

appMotionLightSetValueReturn:
  rcall appMotionLightWriteConfToEeprom  ; (r16, r17, X, Y)
  ldi r23, NETMSG_CMD_VALUE_SET_ACK
  sec
  ret
; @end



appMotionLightMsgTable:
  .db NETMSG_CMD_VALUE_REPORT, 0,                    LOW(appMotionLightHandleReport),    HIGH(appMotionLightHandleReport)
  .db NETMSG_CMD_VALUE_SET,    VALUE_ID_MAL_ONTIME,  LOW(appMotionLightSetValueOnTime),  HIGH(appMotionLightSetValueOnTime)
  .db NETMSG_CMD_VALUE_SET,    VALUE_ID_MAL_SOURCE1, LOW(appMotionLightSetValueSource1), HIGH(appMotionLightSetValueSource1)
  .db NETMSG_CMD_VALUE_SET,    VALUE_ID_MAL_SOURCE2, LOW(appMotionLightSetValueSource2), HIGH(appMotionLightSetValueSource2)
  .db NETMSG_CMD_VALUE_SET,    VALUE_ID_MAL_BVALUE,  LOW(appMotionLightSetValueBValue),  HIGH(appMotionLightSetValueBValue)
  .db 0, 0, 0, 0




