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

#ifndef AQH_AVR_DEVICE_C02_WIN_NETSTATS_ASM
#define AQH_AVR_DEVICE_C02_WIN_NETSTATS_ASM


.cseg


WinNetStats_Init:
  clr r16
  sts winNetstatsUpdateNum, r16
  sts winNetstatsUpdateNum+1, r16

  ; setup
  ldi yl, LOW(winNetstats)
  ldi yh, HIGH(winNetstats)
  bigcall TitleWindow_Init

  ldi zl, LOW(STYLE_WIN_FONT*2)
  ldi zh, HIGH(STYLE_WIN_FONT*2)
  std Y+WIN_OFFS_FONT_LO, zl
  std Y+WIN_OFFS_FONT_HI, zh
  
  bigcall TitleWindow_SetFullSize  
  bigcall TitleWindow_SetStyleColors

  ret
; @end



; @param @0 Y
; @param @1 label addr

.macro WINNETSTATS_PRINTLABEL
  ; packets in
  ldi zl, LOW(@1 * 2)
  ldi zh, HIGH(@1 * 2)
  ldi r16, 2         ; X
  mov r4, r16
  clr r5
  ldi r16, LOW(STYLE_WIN_TITLE_HEIGHT+@0)   ; Y
  mov r6, r16
  ldi r16, HIGH(STYLE_WIN_TITLE_HEIGHT+@0)
  mov r7, r16
  bigcall Window_DrawTextFlash
.endmacro



; @param @0 X
; @param @1 Y
; @param @2 data address

.macro WINNETSTATS_PRINTDATA
  ldi r16, @0       ; X
  mov r4, r16
  clr r5
  ldi r16, LOW(STYLE_WIN_TITLE_HEIGHT+@1)   ; Y
  mov r6, r16
  ldi r16, HIGH(@1)
  mov r7, r16
  lds r16, @2        ; data
  lds r17, @2+1
  bigcall Window_WriteHexWordAt
.endmacro



WinNetStats_Show:
  ; draw title window basics (with title)
  ldi yl, LOW(winNetstats)
  ldi yh, HIGH(winNetstats)
  ldi zl, LOW(winNetstats_title*2)
  ldi zh, HIGH(winNetstats_title*2)
  bigcall TitleWindow_DrawTitle

  bigcall TitleWindow_ClearContentArea

  ; packets in
  WINNETSTATS_PRINTLABEL 2, winNetstats_lPacketsIn
  ; packets out
  WINNETSTATS_PRINTLABEL 24, winNetstats_lPacketsOut
  ; eContent
  WINNETSTATS_PRINTLABEL 46, winNetstats_lContentErr
  ; eIO
  WINNETSTATS_PRINTLABEL 68, winNetstats_lIoErr
  ; eMsgSize
  WINNETSTATS_PRINTLABEL 90, winNetstats_lMsgSizeErr

  ; draw horizontal line
  ldi r16, STYLE_WIN_TITLE_HEIGHT+112
  mov r6, r16
  clr r7
  clr r4
  clr r5
  ldi r16, LOW(DISPLAY_WIDTH)
  mov r8, r16
  ldi r16, HIGH(DISPLAY_WIDTH)
  mov r9, r16
  clr r2
  clr r3
  bigcall Display_DrawHLine

  ; X
  WINNETSTATS_PRINTLABEL 114, winNetstats_lX
  ; Y
  WINNETSTATS_PRINTLABEL 136, winNetstats_lY
  ; Z
  WINNETSTATS_PRINTLABEL 158, winNetstats_lZ
  ; update num
  WINNETSTATS_PRINTLABEL 180, winNetstats_lUpdate

  rcall WinNetStats_Update

  ret
; @end



WinNetStats_Update:
  lds r16, winNetstatsUpdateNum
  inc r16
  sts winNetstatsUpdateNum, r16

  ldi yl, LOW(winNetstats)
  ldi yh, HIGH(winNetstats)

  ; packets in
  WINNETSTATS_PRINTDATA 160, 2, netInterfaceData+NET_IFACE_OFFS_PACKETSIN_LOW
  ; packets out
  WINNETSTATS_PRINTDATA 160, 24, netInterfaceData+NET_IFACE_OFFS_PACKETSOUT_LOW
  ; eContent
  WINNETSTATS_PRINTDATA 160, 46, netInterfaceData+NET_IFACE_OFFS_ERR_CONTENT_LOW
  ; eIO
  WINNETSTATS_PRINTDATA 160, 68, netInterfaceData+NET_IFACE_OFFS_ERR_IO_LOW
  ; eMsgSize
  WINNETSTATS_PRINTDATA 160, 90, netInterfaceData+NET_IFACE_OFFS_ERR_MSGSIZE_LOW

  ; X
  WINNETSTATS_PRINTDATA 160, 114, xpt2046CurrentX
  WINNETSTATS_PRINTDATA 240, 114, xpt2046RawX
  ; Y
  WINNETSTATS_PRINTDATA 160, 136, xpt2046CurrentY
  WINNETSTATS_PRINTDATA 240, 136, xpt2046RawY
  ; Z
  WINNETSTATS_PRINTDATA 160, 158, xpt2046CurrentZ
  ; update num
  WINNETSTATS_PRINTDATA 160, 180, winNetstatsUpdateNum

  ret
; @end



winNetstats_title:       .db "Network Statistics", 0, 0

winNetstats_lPacketsIn:  .db "Pkgs In :", 0
winNetstats_lPacketsOut: .db "Pkgs Out:", 0
winNetstats_lContentErr: .db "eContent:", 0
winNetstats_lIoErr:      .db "eIO     :", 0
winNetstats_lMsgSizeErr: .db "eMsgSize:", 0
winNetstats_lX:          .db "X       :", 0
winNetstats_lY:          .db "Y       :", 0
winNetstats_lZ:          .db "Z       :", 0
winNetstats_lUpdate:     .db "Update  :", 0
.dseg

winNetstats:
  .byte WIN_SIZE



.dseg

winNetstatsUpdateNum: .byte 2


#endif
