; ***************************************************************************
; 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_VALUELABEL_ASM
#define AQH_AVR_GUI2_VALUELABEL_ASM


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

; IMGVIEW in flash
.equ VLABEL_OFFS_VLABEL              =  WIDGET_SIZE
.equ VLABEL_OFFS_POSTKOMMADIGITS     =  VLABEL_OFFS_VLABEL+0
.equ VLABEL_OFFS_RESERVED            =  VLABEL_OFFS_VLABEL+1
.equ VLABEL_SIZE                     =  VLABEL_OFFS_VLABEL+2


; SDRAM data for IMGVIEW
.equ VLABEL_SD_OFFS_VLABEL           = WIDGET_SD_SIZE
.equ VLABEL_SD_OFFS_VALUE_LO         = VLABEL_SD_OFFS_VLABEL+0
.equ VLABEL_SD_OFFS_VALUE_HI         = VLABEL_SD_OFFS_VLABEL+1
.equ VLABEL_SD_SIZE                  = VLABEL_SD_OFFS_VLABEL+2


; signals
.equ VLABEL_SIGNAL_SETVALUE       =  WIDGET_SIGNAL_NEXTFREE+0
.equ VLABEL_SIGNAL_NEXTFREE       =  WIDGET_SIGNAL_NEXTFREE+1




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

.cseg



; ---------------------------------------------------------------------------
; @routine ImageView_OnCreate @global
;
; @param Z byte address of widget object (for LPM!)
; @return CFLAG set if signal handled
; @clobbers any, !Z

ValueLabel_OnCreate:
  bigcall Widget_OnCreate
  bigcall Widget_GetSdramPtr            ; (none)
  clr r16
  std Y+VLABEL_SD_OFFS_VALUE_LO, r16
  std Y+VLABEL_SD_OFFS_VALUE_HI, r16
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ValueLabel_OnSetValue @global
;
; @param Z byte address of widget object (for LPM!)
; @param X new value for background color
; @return CFLAG set if signal handled
; @clobbers r17

ValueLabel_OnSetValue:
  bigcall OBJ_IsObject                   ; (none)
  brcc ValueLabel_OnSetValue_ret
  push zl
    push zh
      rcall Widget_GetSdramPtr           ; (none)
      std Y+VLABEL_SD_OFFS_VALUE_LO, xl
      std Y+VLABEL_SD_OFFS_VALUE_HI, xh
      ldd r17, Y+WIDGET_SD_OFFS_FLAGS
      ori r17, (1<<WIDGET_FLAGS_DIRTY_BIT)
      std Y+WIDGET_SD_OFFS_FLAGS, r17
    pop zh
  pop zl
ValueLabel_OnSetValue_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ValueLabel_OnDraw @global
;
; @param Z byte address of widget object (for LPM!)
; @return CFLAG set if signal handled
; @clobbers any, !Z

ValueLabel_OnDraw:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  breq ValueLabel_OnDraw_ret

  bigcall Widget_Clear

  adiw zh:zl, WIDGET_OFFS_OPTS_LO  ; get OPTS low
  lpm r14, Z
  sbiw zh:zl, WIDGET_OFFS_OPTS_LO

  push r14
    rcall valueLabelWriteValue
  pop r14

  sbrs r14, WIDGET_OPTSLO_BORDER_BIT
    rjmp ValueLabel_OnDraw_done
  ldi r16, LOW(STYLE_BUTTON_COL_BORDER)
  mov r2, r16
  ldi r16, HIGH(STYLE_BUTTON_COL_BORDER)
  mov r3, r16
  bigcall Widget_DrawBorder

ValueLabel_OnDraw_done:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  cbr r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+WIDGET_SD_OFFS_FLAGS, r16

ValueLabel_OnDraw_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine valueLabelWriteValue
;
; @param  Z       byte address of object (for LPM!)
; @param  R14     opts low
; @clobbers any, !Z

valueLabelWriteValue:
  push zl
    push zh
      bigcall Widget_GetSdramPtr       ; (none)
      ; convert value to string
      ldd r20, Y+VLABEL_SD_OFFS_VALUE_LO
      ldd r21, Y+VLABEL_SD_OFFS_VALUE_HI
      adiw zh:zl, VLABEL_OFFS_POSTKOMMADIGITS
      lpm r24, Z
      sbiw zh:zl, VLABEL_OFFS_POSTKOMMADIGITS
      rcall IntToAscii                          ; X=pointer to text
      ; align according to string size
      ldi r18, STYLE_WIN_FONT_WIDTH
      rcall Widget_CalcStringWidthSDRAM         ; r13:r12=string width
      rcall Widget_AlignPosXY                   ; (r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19)
      bigcall Widget_DrawTextSDRAM
    pop zh
  pop zl
  ret
; @end



ValueLabel_DefaultSignalmap:
  .db 0, OBJECT_SIGNAL_CREATE,   LOW(ValueLabel_OnCreate),   HIGH(ValueLabel_OnCreate)
  .db 0, WIDGET_SIGNAL_DRAW,     LOW(ValueLabel_OnDraw),     HIGH(ValueLabel_OnDraw)
  .db 0, VLABEL_SIGNAL_SETVALUE, LOW(ValueLabel_OnSetValue), HIGH(ValueLabel_OnSetValue)
  .db 0, 0, 0, 0  ; end of table





#endif

