; ***************************************************************************
; 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 Main_SendValueReport
;
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R22     value type
; @return CFLAG on success, cleared on error
; @clobbers (R16, R17, R18, R19, R20, R21, R23, R24, R25, Y, X)

Main_SendValueReport:
  push r17
    bigcall NET_Buffer_Alloc               ; (R16, R17, X)
  pop r17
  brcc Main_SendValueReport_end          ; jmp on error
  push r16                               ; buffer num
    ldi r16, 0xff                        ; DEST addr
    adiw xh:xl, 1
    ldi yl, LOW(netInterfaceData)
    ldi yh, HIGH(netInterfaceData)
    bigcall NETMSG_ValueWriteReport      ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
    sbiw xh:xl, 1
  pop r16                                ; buffer num
  bigcall NET_Interface_AddOrReleaseOutMsg ; (R16, R17, R18, X)
Main_SendValueReport_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine Main_SendValueResponse
;
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R23     command
; @param  R25:R24 ref msg id
; @return CFLAG on success, cleared on error
; @clobbers R16, R17, R18, R19, R20, R21, R23, R24, R25, X, Y

Main_SendValueResponse:
  push r17
    bigcall NET_Buffer_Alloc             ; (R16, R17, X)
  pop r17
  brcc Main_SendValueResponse_end        ; jmp on error
  push r16                               ; buffer num
    ldi r16, 0xff                        ; DEST addr
    clr r22                              ; value type
    adiw xh:xl, 1
    ldi yl, LOW(netInterfaceData)
    ldi yh, HIGH(netInterfaceData)
    bigcall NETMSG_ValueWriteResponse    ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
    sbiw xh:xl, 1
  pop r16                                ; buffer num
  bigcall NET_Interface_AddOrReleaseOutMsg ; (R16, R17, R18, X)
Main_SendValueResponse_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine Main_Send8BitValue
;
; Send 8 bit value with denominator 1.
;
; @param  R16     value
; @param  R17     value id
; @param  R22     value type

Main_Send8BitValueReport:
  mov r18, r16                        ; R19:R18 = value
  clr r19
  ldi r20, 1                          ; R21:R20 = denom
  clr r21
  rjmp Main_SendValueReport
; @end


