; ***************************************************************************
; 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_NETSTATS_ASM
#define AQH_AVR_GUI2_NETSTATS_ASM

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


.equ NETSTATWIN_OFFS_BEGIN   = CDIALOG_SIZE
.equ NETSTATWIN_OFFS_TIMER   = NETSTATWIN_OFFS_BEGIN+0
.equ NETSTATWIN_SIZE         = NETSTATWIN_OFFS_BEGIN+1


.equ NETSTATWIN_TIMERVAL              = 50  ; update every 5 secs


.equ NETSTATWIN_SEL_PACKETSIN  = 1
.equ NETSTATWIN_SEL_PACKETSOUT = 2
.equ NETSTATWIN_SEL_ECONTENT   = 3
.equ NETSTATWIN_SEL_EIO        = 4
.equ NETSTATWIN_SEL_EMSGSIZE   = 5



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

.cseg


; ---------------------------------------------------------------------------
; @routine NetStatWin_new @global
;
; @param Y pointer to GUIAPP
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object

NetStatWin_new:
  bigcall GuiApp_GetRootWindow
  brcc NetStatWin_new_ret
  mov xl, r18                         ; use root window as parent for main window
  mov xh, r19
  ldi r24, LOW(NETSTATWIN_SIZE)
  ldi r25, HIGH(NETSTATWIN_SIZE)
  bigcall Object_Alloc                ; Y=space (!r16, !r17, !X)
  brcc NetStatWin_new_ret
  rcall NetStatWin_Init
NetStatWin_new_ret:
  ret
; @end



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

NetStatWin_Init:
  ldi r16, (1<<OBJECT_OPTS_TIMER_BIT)                                                                  ; OPTS
  ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
  ldi r20, LOW(RESSOURCE_TXT_NETSTATS)
  ldi r21, HIGH(RESSOURCE_TXT_NETSTATS)
  ldi r22, LOW(NetStatWinWin_Config*2)
  ldi r23, HIGH(NetStatWinWin_Config*2)
  bigcall CDialog_Init               ; Y=main window
  brcc NetStatWin_Init_ret

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

  ; set timer
  ldi r16, NETSTATWIN_TIMERVAL
  std Y+NETSTATWIN_OFFS_TIMER, r16

NetStatWin_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine NetStatWin_OnToGui @global
;
; @param Y pointer to dialog
; @clobbers any, !Y

NetStatWin_OnTimer:
  ldd r16, Y+OBJECT_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_VISIBLE_BIT)
  breq NetStatWin_OnTimer_ret

  ldd r16, Y+NETSTATWIN_OFFS_TIMER
  tst r16
  breq NetStatWin_OnTimer_ret
  dec r16
  std Y+NETSTATWIN_OFFS_TIMER, r16
  brne NetStatWin_OnTimer_ret
  rcall NetStatWin_toGui
  ldi r16, NETSTATWIN_TIMERVAL
  std Y+NETSTATWIN_OFFS_TIMER, r16
NetStatWin_OnTimer_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine NetStatWin_OnMkContent @global
;
; @param  Y       pointer to dialog
; @clobbers any, !Y

NetStatWin_OnMkContent:
  bigcall CDialog_CreateContent
  bigcall MainWindow_GetContentWidget  ; r19:r18=content window
  brcc NetStatWin_OnMkContent_ret
  mov xl, r18                          ; use content window as parent
  mov xh, r19
  bigcall C03App_CreateBackButton
NetStatWin_OnMkContent_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine NetStatWin_OnToGui @global
;
; @param Y pointer to dialog
; @clobbers any, !Y

NetStatWin_OnToGui:
  rcall NetStatWin_toGui
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine NetStatWin_toGui @global
;
; @param  Y       pointer to dialog
; @clobbers any, !Y, !Z

NetStatWin_toGui:
  lds xl, netInterfaceData+NET_IFACE_OFFS_PACKETSIN_LOW
  lds xh, netInterfaceData+NET_IFACE_OFFS_PACKETSIN_HIGH
  ldi r16, NETSTATWIN_SEL_PACKETSIN
  bigcall CDialog_SetChildValue

  lds xl, netInterfaceData+NET_IFACE_OFFS_PACKETSOUT_LOW
  lds xh, netInterfaceData+NET_IFACE_OFFS_PACKETSOUT_HIGH
  ldi r16, NETSTATWIN_SEL_PACKETSOUT
  bigcall CDialog_SetChildValue

  lds xl, netInterfaceData+NET_IFACE_OFFS_ERR_CONTENT_LOW
  lds xh, netInterfaceData+NET_IFACE_OFFS_ERR_CONTENT_HIGH
  ldi r16, NETSTATWIN_SEL_ECONTENT
  bigcall CDialog_SetChildValue

  lds xl, netInterfaceData+NET_IFACE_OFFS_ERR_IO_LOW
  lds xh, netInterfaceData+NET_IFACE_OFFS_ERR_IO_HIGH
  ldi r16, NETSTATWIN_SEL_EIO
  bigcall CDialog_SetChildValue

  lds xl, netInterfaceData+NET_IFACE_OFFS_ERR_MSGSIZE_LOW
  lds xh, netInterfaceData+NET_IFACE_OFFS_ERR_MSGSIZE_HIGH
  ldi r16, NETSTATWIN_SEL_EMSGSIZE
  bigcall CDialog_SetChildValue

  ret
; @end





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


NetStatWinWin_Config:
  .db CWIDGETCFG_MAINWIDGET_TYPE_MLAYOUT, 0                 ; TYPE, FLAGS
  .dw 0                                                     ; STYLE
  .db 2, 0                                                  ; PARAM1, PARAM2
  
  ; line 1, 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_PACKETSIN                               ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 1, right widget
  .db CWIDGETCFG_WIDGET_TYPE_VALUELABEL, 0                  ; TYPE, FLAGS
  .db NETSTATWIN_SEL_PACKETSIN, 0                           ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_END<<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, 0                                                  ; MINVAL, MAXVAL


  ; line 2, 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_PACKETSOUT                              ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 2, right widget
  .db CWIDGETCFG_WIDGET_TYPE_VALUELABEL, 0                  ; TYPE, FLAGS
  .db NETSTATWIN_SEL_PACKETSOUT, 0                          ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_END<<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, 0                                                  ; MINVAL, MAXVAL

  ; line 3, 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_ECONTENT                                ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 3, right widget
  .db CWIDGETCFG_WIDGET_TYPE_VALUELABEL, 0                  ; TYPE, FLAGS
  .db NETSTATWIN_SEL_ECONTENT, 0                            ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_END<<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, 0                                                  ; MINVAL, MAXVAL

  ; line 4, 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_EIO                                     ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 4, right widget
  .db CWIDGETCFG_WIDGET_TYPE_VALUELABEL, 0                  ; TYPE, FLAGS
  .db NETSTATWIN_SEL_EIO, 0                            ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_END<<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, 0                                                  ; MINVAL, MAXVAL

  ; line 5, 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_EMSGSIZE                                ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 5, right widget
  .db CWIDGETCFG_WIDGET_TYPE_VALUELABEL, 0                  ; TYPE, FLAGS
  .db NETSTATWIN_SEL_EMSGSIZE, 0                            ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_END<<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, 0                                                  ; MINVAL, MAXVAL

  ; end of configuration
  .dw 0





NetStatWin_DefaultSignalmap:
  ; header
  .dw CDialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0, OBJECT_SIGNAL_TIMER,     LOW(NetStatWin_OnTimer),     HIGH(NetStatWin_OnTimer)
  .db 0, DIALOG_SIGNAL_TOGUI,     LOW(NetStatWin_OnToGui),     HIGH(NetStatWin_OnToGui)
  .db 0, DIALOG_SIGNAL_MKCONTENT, LOW(NetStatWin_OnMkContent), HIGH(NetStatWin_OnMkContent)

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



#endif

