; ***************************************************************************
; 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_A_C03_ASM
#define AQH_AVR_A_C03_ASM


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

.equ C03APP_OFFS_BEGIN         = GUIAPP_SIZE
.equ C03APP_OFFS_WMENU_LO      = C03APP_OFFS_BEGIN+0
.equ C03APP_OFFS_WMENU_HI      = C03APP_OFFS_BEGIN+1
.equ C03APP_OFFS_WCLIMATE_LO   = C03APP_OFFS_BEGIN+2
.equ C03APP_OFFS_WCLIMATE_HI   = C03APP_OFFS_BEGIN+3
.equ C03APP_OFFS_WLIGHT_LO     = C03APP_OFFS_BEGIN+4
.equ C03APP_OFFS_WLIGHT_HI     = C03APP_OFFS_BEGIN+5
.equ C03APP_OFFS_WWINDOWS_LO   = C03APP_OFFS_BEGIN+6
.equ C03APP_OFFS_WWINDOWS_HI   = C03APP_OFFS_BEGIN+7
.equ C03APP_OFFS_WDEBUG_LO     = C03APP_OFFS_BEGIN+8
.equ C03APP_OFFS_WDEBUG_HI     = C03APP_OFFS_BEGIN+9
.equ C03APP_OFFS_WNETSTATS_LO  = C03APP_OFFS_BEGIN+10
.equ C03APP_OFFS_WNETSTATS_HI  = C03APP_OFFS_BEGIN+11
.equ C03APP_SIZE               = C03APP_OFFS_BEGIN+12


; selectors for main windows
.equ C03APP_SEL_BACK     = 1
.equ C03APP_SEL_MENU     = 2
.equ C03APP_SEL_CLIMATE  = 3
.equ C03APP_SEL_LIGHT    = 4
.equ C03APP_SEL_WINDOWS  = 5
.equ C03APP_SEL_DEBUG    = 6
.equ C03APP_SEL_NETSTATS = 7




; ***************************************************************************
; data

.dseg



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

.cseg



; ---------------------------------------------------------------------------
; @routine C03App_new @global
;
; @return CFLAG set of okay, cleared otherwise
; @return Y     address of newly created object
; @param r16    value for OBJECT_OFFS_OPTS
; @clobbers any

C03App_new:
  ldi r24, LOW(C03APP_SIZE)
  ldi r25, HIGH(C03APP_SIZE)
  bigcall Object_Alloc              ; (!r16, !r17)
  brcc C03App_new_ret
  rcall C03App_Init                 ; (r16, r17, X)
  sec
C03App_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_Init @global
;
; @param Y address of object in SDRAM
; @param r16 value for OBJECT_OFFS_OPTS

C03App_Init:
  ; call base class
  bigcall GuiApp_Init                          ; (r16, r17, X)

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

  ; create main windows
  rcall c03AppCreateWinMenu     ; (any, !Y)
  rcall c03AppCreateWinClimate  ; (any, !Y)
  rcall c03AppCreateWinNetStats ; (any, !Y)
  rcall c03AppCreateWinDebug    ; (any, !Y)
  rcall c03AppCreateScreenSaver ; (any, !Y)

  ; enter menu window
  rcall C03App_EnterMenuWin
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine c03AppCreateWinMenu
;
; @clobbers !X

c03AppCreateWinMenu:
  push yl
    push yh
      bigcall MenuWin_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateWinMenu_ret
  std Y+C03APP_OFFS_WMENU_LO, xl
  std Y+C03APP_OFFS_WMENU_HI, xh
  ldi r16, C03APP_SEL_MENU
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinMenu_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine c03AppCreateWinClimate
;
; @clobbers !X

c03AppCreateWinClimate:
  push yl
    push yh
      bigcall ClimateWin_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateWinClimate_ret
  std Y+C03APP_OFFS_WCLIMATE_LO, xl
  std Y+C03APP_OFFS_WCLIMATE_HI, xh
  ldi r16, C03APP_SEL_CLIMATE
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinClimate_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine c03AppCreateWinDebug
;
; @param Y ptr to this guiapp
; @clobbers !X

c03AppCreateWinDebug:
#if 0
  push yl
    push yh
      push yl
        push yh
          bigcall LightSettings_new
          mov r20, yl
          mov r21, yh
        pop yh
      pop yl
      bigcall LightSettingsWin_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateWinDebug_ret
  std Y+C03APP_OFFS_WDEBUG_LO, xl
  std Y+C03APP_OFFS_WDEBUG_HI, xh
  ldi r16, C03APP_SEL_DEBUG
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinDebug_ret:
  ret
#endif

#if 0
  push yl
    push yh
      ldi r20, LOW(RESSOURCE_TXT_LIGHTSETTINGS)
      ldi r21, HIGH(RESSOURCE_TXT_LIGHTSETTINGS)
      bigcall DlgNumInput_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateWinDebug_ret
  std Y+C03APP_OFFS_WDEBUG_LO, xl
  std Y+C03APP_OFFS_WDEBUG_HI, xh
  ldi r16, C03APP_SEL_DEBUG
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinDebug_ret:
  ret
#endif

#if 0
  push yl
    push yh

      ldi r20, LOW(RESSOURCE_TXT_MOTIONSENSOR1)
      ldi r21, HIGH(RESSOURCE_TXT_MOTIONSENSOR1)
      bigcall DlgNodeValueId_new
      mov xl, yl
      mov xh, yh
      
      push xl
        push xh
          ldi xl, 5
          clr xh
          bigcall DlgNodeValueId_SetValueId
        pop xh
      pop xl
      sec
    pop yh
  pop yl
  brcc c03AppCreateWinDebug_ret
  std Y+C03APP_OFFS_WDEBUG_LO, xl
  std Y+C03APP_OFFS_WDEBUG_HI, xh
  
  ldi r16, C03APP_SEL_DEBUG
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinDebug_ret:
  ret
#endif


#if 1
  push yl
    push yh

      push yl
        push yh
          bigcall LightSettings_new
          ldi r16, 5
          std Y+LIGHTSETTINGS_OFFS_SOURCE1_NODEADDR, r16
          mov r20, yl
          mov r21, yh
        pop yh
      pop yl

      bigcall DlgLightConns_new
      mov xl, yl
      mov xh, yh
      sec
    pop yh
  pop yl
  brcc c03AppCreateWinDebug_ret
  std Y+C03APP_OFFS_WDEBUG_LO, xl
  std Y+C03APP_OFFS_WDEBUG_HI, xh
  
  ldi r16, C03APP_SEL_DEBUG
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinDebug_ret:
  ret
#endif

; @end





; ---------------------------------------------------------------------------
; @routine c03AppCreateWinNetStats
;
; @clobbers !X

c03AppCreateWinNetStats:
  push yl
    push yh
      bigcall NetStatWin_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateWinNetStats_ret
  std Y+C03APP_OFFS_WNETSTATS_LO, xl
  std Y+C03APP_OFFS_WNETSTATS_HI, xh
  ldi r16, C03APP_SEL_NETSTATS
  rcall c03AppSetTargetAndSelector
  sec
c03AppCreateWinNetStats_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine c03AppCreateScreenSaver
;
; @clobbers !Y

c03AppCreateScreenSaver:
  bigcall GuiApp_GetRootWindow
  brcc c03AppCreateScreenSaver_ret
  push yl
    push yh
      mov xl, r18  ; use root window as parent for main window
      mov xh, r19
      ldi r16, (1<<WIDGET_OPTS_INPUT_BIT) | (1<<OBJECT_OPTS_TIMER_BIT) | (1<<OBJECT_OPTS_MSGRECV_BIT)
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      bigcall SimpleSaver_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc c03AppCreateScreenSaver_ret
  bigcall GuiApp_SetScreenSaver
  sec
c03AppCreateScreenSaver_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine c03AppSetTargetAndSelector
;
; @param r16 selector
; @param Y   pointer to this guiapp
; @param X   pointer to new main window to set target/selector to
; @clobbers none

c03AppSetTargetAndSelector:
  adiw xh:xl, OBJECT_OFFS_SELECTOR
  st X, r16
  sbiw xh:xl, OBJECT_OFFS_SELECTOR
  adiw xh:xl, OBJECT_OFFS_TARGET_LO
  st X+, yl  ; set target
  st X, yh
  sbiw xh:xl, (OBJECT_OFFS_TARGET_LO+1)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_EnterMenuWin
;
; @param Y ptr to this guiapp

C03App_EnterMenuWin:
  ldd xl, Y+C03APP_OFFS_WMENU_LO
  ldd xh, Y+C03APP_OFFS_WMENU_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_EnterMenuWin_ret      ; nope, jump
  bigcall GuiApp_EnterWindow
C03App_EnterMenuWin_ret:
  sec
  ret
; @end




; ---------------------------------------------------------------------------
; @routine C03App_OnCmdClimate
;
; @param Y ptr to this guiapp

C03App_OnCmdClimate:
  ldd xl, Y+C03APP_OFFS_WCLIMATE_LO
  ldd xh, Y+C03APP_OFFS_WCLIMATE_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_OnCmdClimate_ret      ; nope, ignore this signal
  bigcall GuiApp_EnterWindow
C03App_OnCmdClimate_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_OnCmdLight
;
; @param Y ptr to this guiapp

C03App_OnCmdLight:
  ldd xl, Y+C03APP_OFFS_WLIGHT_LO
  ldd xh, Y+C03APP_OFFS_WLIGHT_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_OnCmdLight_ret        ; nope, ignore this signal
  bigcall GuiApp_EnterWindow
C03App_OnCmdLight_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_OnCmdWindows
;
; @param Y ptr to this guiapp

C03App_OnCmdWindows:
  ldd xl, Y+C03APP_OFFS_WWINDOWS_LO
  ldd xh, Y+C03APP_OFFS_WWINDOWS_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_OnCmdWindows_ret      ; nope, ignore this signal
  bigcall GuiApp_EnterWindow
C03App_OnCmdWindows_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_OnCmdDebug
;
; @param Y ptr to this guiapp

C03App_OnCmdDebug:
  ldd xl, Y+C03APP_OFFS_WDEBUG_LO
  ldd xh, Y+C03APP_OFFS_WDEBUG_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_OnCmdDebug_ret      ; nope, ignore this signal
  bigcall GuiApp_EnterWindow
C03App_OnCmdDebug_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_OnCmdNetStats
;
; @param Y ptr to this guiapp

C03App_OnCmdNetStats:
  ldd xl, Y+C03APP_OFFS_WNETSTATS_LO
  ldd xh, Y+C03APP_OFFS_WNETSTATS_HI
  mov r16, xl                       ; was window created?
  or r16, xh
  breq C03App_OnCmdNetStats_ret      ; nope, ignore this signal
  bigcall GuiApp_EnterWindow
C03App_OnCmdNetStats_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_OnCmdBack
;
; @param Y ptr to this guiapp

C03App_OnCmdBack:
  bigcall GuiApp_LeaveWindow
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine C03App_CreateButton
;
; @param X       parent
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param r21:r20 img ressource
; @param r22     selector
; @param r23     button mode
; @return CFLAG  set if new object created, cleared on error
; @return Y      button created
; @clobbers any, !X

C03App_CreateButton:
  bigjmp ImageButton_new
; @end



; ---------------------------------------------------------------------------
; @routine C03App_CreateBackButton
;
; @param X parent
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object
; @clobbers any

C03App_CreateBackButton:
  ldi r16, 0
  ldi r17, (WIDGET_PACK_END<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_END<<WIDGET_PACK_VSELF0_BIT) ; PACK
  ldi r20, LOW(RESSOURCE_IMG_ARROWBACK)
  ldi r21, HIGH(RESSOURCE_IMG_ARROWBACK)
  ldi r22, C03APP_SEL_BACK
  bigcall C03App_CreateButton
  brcc C03App_CreateBackButton

  bigcall Widget_GetApp
  brcc C03App_CreateBackButton_ret
  std Y+OBJECT_OFFS_TARGET_LO, r18
  std Y+OBJECT_OFFS_TARGET_HI, r19
C03App_CreateBackButton_ret:
  ret
; @end






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

C03App_DefaultSignalmap:
  ; header
  .dw GuiApp_DefaultSignalmap*2  ; next table to use
  ; entries
  .db C03APP_SEL_BACK, WIDGET_SIGNAL_COMMAND,   LOW(C03App_OnCmdBack),     HIGH(C03App_OnCmdBack)
  .db C03APP_SEL_MENU, MENUWIN_SIGNAL_CLIMATE,  LOW(C03App_OnCmdClimate),  HIGH(C03App_OnCmdClimate)
  .db C03APP_SEL_MENU, MENUWIN_SIGNAL_LIGHT,    LOW(C03App_OnCmdLight),    HIGH(C03App_OnCmdLight)
  .db C03APP_SEL_MENU, MENUWIN_SIGNAL_WINDOWS,  LOW(C03App_OnCmdWindows),  HIGH(C03App_OnCmdWindows)
  .db C03APP_SEL_MENU, MENUWIN_SIGNAL_DEBUG,    LOW(C03App_OnCmdDebug),    HIGH(C03App_OnCmdDebug)
  .db C03APP_SEL_MENU, MENUWIN_SIGNAL_NETSTATS, LOW(C03App_OnCmdNetStats), HIGH(C03App_OnCmdNetStats)

  .db 0, 0, 0, 0  ; end of table



#endif


