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

#ifndef AQH_AVR_GUI2_AQHOME_LIGHTSETTINGS_ASM
#define AQH_AVR_GUI2_AQHOME_LIGHTSETTINGS_ASM



; ***************************************************************************
; defines


.equ LIGHTSETTINGS_OFFS_BEGIN            = LIST_SIZE
.equ LIGHTSETTINGS_OFFS_ID               = LIGHTSETTINGS_OFFS_BEGIN+0
.equ LIGHTSETTINGS_OFFS_IFACE_TYPE       = LIGHTSETTINGS_OFFS_BEGIN+1
.equ LIGHTSETTINGS_OFFS_NAME             = LIGHTSETTINGS_OFFS_BEGIN+2  ; Main, cup board etc.
.equ LIGHTSETTINGS_OFFS_IDX              = LIGHTSETTINGS_OFFS_BEGIN+3  ; e.g. main light 1
.equ LIGHTSETTINGS_OFFS_NODEADDR         = LIGHTSETTINGS_OFFS_BEGIN+4
.equ LIGHTSETTINGS_OFFS_ROOM             = LIGHTSETTINGS_OFFS_BEGIN+5
.equ LIGHTSETTINGS_OFFS_LEDNUM_LO        = LIGHTSETTINGS_OFFS_BEGIN+6
.equ LIGHTSETTINGS_OFFS_LEDNUM_HI        = LIGHTSETTINGS_OFFS_BEGIN+7
.equ LIGHTSETTINGS_OFFS_ONTIME_LO        = LIGHTSETTINGS_OFFS_BEGIN+8
.equ LIGHTSETTINGS_OFFS_ONTIME_HI        = LIGHTSETTINGS_OFFS_BEGIN+9
.equ LIGHTSETTINGS_OFFS_SOURCE1_NODEADDR = LIGHTSETTINGS_OFFS_BEGIN+10
.equ LIGHTSETTINGS_OFFS_SOURCE1_VALUEID  = LIGHTSETTINGS_OFFS_BEGIN+11
.equ LIGHTSETTINGS_OFFS_SOURCE2_NODEADDR = LIGHTSETTINGS_OFFS_BEGIN+12
.equ LIGHTSETTINGS_OFFS_SOURCE2_VALUEID  = LIGHTSETTINGS_OFFS_BEGIN+13
.equ LIGHTSETTINGS_OFFS_BSOURCE_NODEADDR = LIGHTSETTINGS_OFFS_BEGIN+14
.equ LIGHTSETTINGS_OFFS_BSOURCE_VALUEID  = LIGHTSETTINGS_OFFS_BEGIN+15
.equ LIGHTSETTINGS_SIZE                  = LIGHTSETTINGS_OFFS_BEGIN+16



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

.cseg



; ---------------------------------------------------------------------------
; @routine LightSettings_new @global
;
; @return Y pointer to newly allocated LightSettings data
; @clobbers r16, r17, r18, r19, r24, r25, X

LightSettings_new:
  ldi r24, LOW(LIGHTSETTINGS_SIZE)
  ldi r25, HIGH(LIGHTSETTINGS_SIZE)
  bigcall Heap_AllocAndZero            ; (r16, r17, r18, r19, r24, r25, X)
  brcc LightSettings_new_ret
  mov yl, xl
  mov yh, xh
  bigcall List_InitObject              ; (r16)
  sec
LightSettings_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LightSettings_free @global
;
; @param Y pointer to LightSettings data
; @clobbers r16, r17, r24, r25, X

LightSettings_free:
  bigcall List_FiniObject
  mov xl, yl
  mov xh, yh
  bigjmp Heap_Free   ; (r16, r17, r24, r25, X)
; @end



; ---------------------------------------------------------------------------
; @routine LightSettings_ReadFromEeprom @global
;
; @param Y pointer to LightSettings data
; @param r17 length of TLV data in EEPROM
; @param X points to begin of TLV data in EEPROM
; @clobbers r16, r17, X

LightSettings_ReadFromEeprom:
  push yl
    push yh
      adiw yh:yl, LIGHTSETTINGS_OFFS_BEGIN
      bigcall Eeprom_ReadBytes             ; (r16, r17, X, Y)
    pop yh
  pop yl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LightSettings_WriteToEeprom @global
;
; @param Y pointer to LightSettings data
; @param r17 length of TLV data in EEPROM
; @param X points to begin of TLV data in EEPROM
; @clobbers r16, r17, r18, X

LightSettings_WriteToEeprom:
  push yl
    push yh
      adiw yh:yl, LIGHTSETTINGS_OFFS_BEGIN
      bigcall Eeprom_WriteBytes             ; (r16, r17, r18, X, Y)
    pop yh
  pop yl
  ret
; @end






#endif

