; ***************************************************************************
; 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_W_LIGHTSETTINGS_ASM
#define AQH_AVR_W_LIGHTSETTINGS_ASM


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

.equ LIGHTSETTINGSWIN_NUM_COLORS    = 8


.equ LIGHTSETTINGSWIN_OFFS_BEGIN            = CDIALOG_SIZE
.equ LIGHTSETTINGSWIN_OFFS_SETTINGS_LO      = LIGHTSETTINGSWIN_OFFS_BEGIN+0
.equ LIGHTSETTINGSWIN_OFFS_SETTINGS_HI      = LIGHTSETTINGSWIN_OFFS_BEGIN+1
.equ LIGHTSETTINGSWIN_SIZE                  = LIGHTSETTINGSWIN_OFFS_BEGIN+2


; selectors
.equ LIGHTSETTINGSWIN_SEL_DIALOG_OK    = 1
.equ LIGHTSETTINGSWIN_SEL_DIALOG_ABORT = 2
.equ LIGHTSETTINGSWIN_SEL_NAME         = 3
.equ LIGHTSETTINGSWIN_SEL_SUFFIX       = 4
.equ LIGHTSETTINGSWIN_SEL_LEDCOUNT     = 5
.equ LIGHTSETTINGSWIN_SEL_ONTIME       = 6
.equ LIGHTSETTINGSWIN_SEL_CONNECTIONS  = 7





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

.cseg



; ---------------------------------------------------------------------------
; @routine LightSettingsWin_new @global
;
; @param Y pointer to GUIAPP
; @param r21:r20 pointer to settings
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object

LightSettingsWin_new:
  bigcall GuiApp_GetRootWindow        ; r19:r18=root window (none)
  brcc LightSettingsWin_new_ret
  mov xl, r18                         ; use root window as parent for main window
  mov xh, r19
  ldi r24, LOW(LIGHTSETTINGSWIN_SIZE)
  ldi r25, HIGH(LIGHTSETTINGSWIN_SIZE)
  push r20
    push r21
      bigcall Object_Alloc            ; Y=space (!r16, !r17, !X)
    pop r21
  pop r20
  brcc LightSettingsWin_new_ret
  rcall LightSettingsWin_Init
LightSettingsWin_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LightSettingsWin_Init @global
;
; @param Y pointer to newly allocated widget data
; @param X pointer to root window
; @param r21:r20 pointer to settings
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

LightSettingsWin_Init:
  push r20
    push r21
      ldi r16, 0                                                                                           ; OPTS
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      ldi r20, LOW(RESSOURCE_TXT_LIGHTSETTINGS)
      ldi r21, HIGH(RESSOURCE_TXT_LIGHTSETTINGS)
      ldi r22, LOW(LightSettingsWin_Config*2)
      ldi r23, HIGH(LightSettingsWin_Config*2)
      bigcall CDialog_Init               ; Y=main window
    pop r21
  pop r20
  brcc LightSettingsWin_Init_ret

  ; store data
  std Y+LIGHTSETTINGSWIN_OFFS_SETTINGS_LO, r20
  std Y+LIGHTSETTINGSWIN_OFFS_SETTINGS_HI, r21

  ; set default signal map
  ldi r16, LOW(LightSettingsWin_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(LightSettingsWin_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

LightSettingsWin_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LightSettingsWin_toGui @global
;
; @param  Y       pointer to dialog

LightSettingsWin_toGui:
  ldd zl, Y+LIGHTSETTINGSWIN_OFFS_SETTINGS_LO
  ldd zh, Y+LIGHTSETTINGSWIN_OFFS_SETTINGS_HI

  ; name
  ldd xl, Z+LIGHTSETTINGS_OFFS_NAME
  clr xh
  ldi r16, LIGHTSETTINGSWIN_SEL_NAME
  bigcall CDialog_SetChildValue

  ; suffix
  ldd xl, Z+LIGHTSETTINGS_OFFS_IDX
  clr xh
  ldi r16, LIGHTSETTINGSWIN_SEL_SUFFIX
  bigcall CDialog_SetChildValue

  ; LED count
  ldd xl, Z+LIGHTSETTINGS_OFFS_LEDNUM_LO
  ldd xh, Z+LIGHTSETTINGS_OFFS_LEDNUM_HI
  ldi r16, LIGHTSETTINGSWIN_SEL_LEDCOUNT
  bigcall CDialog_SetChildValue

  ; ON time
  ldd xl, Z+LIGHTSETTINGS_OFFS_ONTIME_LO
  ldd xh, Z+LIGHTSETTINGS_OFFS_ONTIME_HI
  ldi r16, LIGHTSETTINGSWIN_SEL_ONTIME
  bigcall CDialog_SetChildValue

  ret
; @end





; ***************************************************************************
; signal handlers





; ***************************************************************************
; data in FLASH


LightSettingsWin_Config:
  .db CWIDGETCFG_MAINWIDGET_TYPE_MLAYOUT, 0                 ; TYPE, FLAGS
  .dw 0                                                     ; STYLE
  .db 2, 0                                                  ; PARAM1, PARAM2
  
  ; line 1: name
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
         (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDNAME                                 ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_TEXTSEL, 0                     ; TYPE, FLAGS
  .db LIGHTSETTINGSWIN_SEL_NAME, 0                          ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw (LightSettingsWin_Names*2)                            ; PARAM1, PARAM2 (pointer to name list)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 2: suffix
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
         (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDNAME                                 ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db LIGHTSETTINGSWIN_SEL_SUFFIX, 0                        ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 9                                                  ; MINVAL, MAXVAL

  ; line 3: LED count
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDCOUNT                                ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db LIGHTSETTINGSWIN_SEL_LEDCOUNT, 0                      ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 300                                                ; MINVAL, MAXVAL

  ; line 4: ON time
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDONTIME                               ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db LIGHTSETTINGSWIN_SEL_ONTIME, 0                        ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 60000                                              ; MINVAL, MAXVAL

  ; line 5: connections (TODO)

  ; end of configuration
  .dw 0


LightSettingsWin_Names:
  .dw RESSOURCE_TXT_LEDNAME_MAIN
  .dw RESSOURCE_TXT_LEDNAME_CUPBOARD
  .dw RESSOURCE_TXT_LEDNAME_WALL
  .dw 0



LightSettingsWin_DefaultSignalmap:
  ; header
  .dw CDialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0, 0, 0, 0  ; end of table




#endif

