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



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

.dseg

ledSignalTimer:      .byte 1
ledSignalFlags:      .byte 1
ledSignalRunFlags:   .byte 1
ledSignalBitCounter: .byte 1


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

.cseg


LED_SIGNAL_BEGIN:



; ---------------------------------------------------------------------------
; LedSignal_Init
;
; @return CFLAG: set if okay, clear on error
; USED: R1, R2, R3, R4, R16, R17, X

LedSignal_Init:
  sbi LED_SIGNAL_DDR, LED_SIGNAL_PINNUM  ; out
  clr r16
  sts ledSignalFlags, r16
  ldi r16, 100
  sts ledSignalTimer, r16
  sec
  ret



; ---------------------------------------------------------------------------
; @routine LedSignal_Every100ms @global
;
; @clobbers r16, r17

LedSignal_Every100ms:
  lds r16, ledSignalTimer
  dec r16
  breq LedSignal_Every100ms_timer0
  sts ledSignalTimer, r16
  cpi r16, 80
  brcs LedSignal_Every100ms_checkBit
  brne LedSignal_Every100ms_ret
  rcall LedSignal_Restart                ; (r16, r17)
  rjmp LedSignal_Every100ms_ret
LedSignal_Every100ms_checkBit:
  lds r17, ledSignalRunFlags
  andi r17, 1
  brne LedSignal_Every100ms_bit1
LedSignal_Every100ms_bit0:
  cpi r16, 8
  breq LedSignal_Every100ms_ledOff
  rjmp LedSignal_Every100ms_ret
LedSignal_Every100ms_bit1:
  cpi r16, 3
  breq LedSignal_Every100ms_ledOff
  rjmp LedSignal_Every100ms_ret
LedSignal_Every100ms_ledOff:
  sbi LED_SIGNAL_PORT, LED_SIGNAL_PINNUM ; off
  rjmp LedSignal_Every100ms_ret
LedSignal_Every100ms_timer0:
  lds r17, ledSignalBitCounter
  dec r17
  brne LedSignal_Every100ms_nextBit
  ldi r17, 100
  sts ledSignalTimer, r17
  rjmp LedSignal_Every100ms_ret
LedSignal_Every100ms_nextBit:
  sts ledSignalBitCounter, r17
  lds r16, ledSignalRunFlags
  lsr r16
  sts ledSignalRunFlags, r16
  ldi r16, 10
  sts ledSignalTimer, r16
  cbi LED_SIGNAL_PORT, LED_SIGNAL_PINNUM ; on
LedSignal_Every100ms_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LedSignal_Restart @global
;
; @clobbers r16, r17

LedSignal_Restart:
  ldi r16, 8
  sts ledSignalBitCounter, r16
  ldi r16, 10
  sts ledSignalTimer, r16
  lds r16, ledSignalFlags
  sts ledSignalRunFlags, r16
  cbi LED_SIGNAL_PORT, LED_SIGNAL_PINNUM ; on
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LedSignal_SetFlag @global
;
; @param r16 flag mask
; @clobbers r17

LedSignal_SetFlag:
  lds r17, ledSignalFlags
  or r17, r16
  sts ledSignalFlags, r17
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LedSignal_ClrFlag @global
;
; @param r16 flag mask
; @clobbers r17

LedSignal_ClrFlag:
  lds r17, ledSignalFlags
  com r16
  and r17, r16
  com r16
  sts ledSignalFlags, r17
  ret
; @end




LED_SIGNAL_END:
.equ MODULE_SIZE_LED_SIGNAL = LED_SIGNAL_END-LED_SIGNAL_BEGIN



