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


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

.equ LED_FAST_REPEATS     = 20
.equ LED_FAST_ONTIME      = 2
.equ LED_FAST_OFFTIME     = 2


.equ LED_ACTIVITY_REPEATS = 1
.equ LED_ACTIVITY_ONTIME  = 3
.equ LED_ACTIVITY_OFFTIME = 5


.equ LED_ID_REPEATS       = 30
.equ LED_ID_ONTIME        = 5
.equ LED_ID_OFFTIME       = 5



; ***************************************************************************
; macros


; ---------------------------------------------------------------------------
; @macro M_LED_INIT
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_INIT
  sbi LED@0_DDR, LED@0_PINNUM  ; out
  cbi LED@0_PORT, LED@0_PINNUM ; on
  rcall Led@0_SetDefaultTiming ; (R16)
  sec
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro M_LED_EVERY100MS
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_EVERY100MS
  lds r16, led@0Timer
  tst r16
  breq l_ret
  dec r16
  breq l_zero
  rjmp l_setTimer
l_zero:
  sbic LED@0_PORT, LED@0_PINNUM ; skip next op if LED is on
    rjmp l_isOff
  ; is on
  sbi LED@0_PORT, LED@0_PINNUM ; off
  lds r16, led@0OffTime
  rjmp l_setTimer
l_isOff:
  lds r16, led@0Repeat
  tst r16
  breq l_restartTimer
  cpi r16, 0xff
  brne l_decRepeat
  clr r16
  rjmp l_setTimer
l_decRepeat:
  dec r16
  sts led@0Repeat, r16
  brne l_restartTimer
  ; repeat counter reached 0, enter heartbeat mode
  rcall Led@0_SetDefaultTiming
  rjmp l_ret
l_restartTimer:
  cbi LED@0_PORT, LED@0_PINNUM ; on
  lds r16, led@0OnTime
l_setTimer:
  sts led@0Timer, r16
l_ret:
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro M_LED_SETDEFAULTTIMING
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_SETDEFAULTTIMING
  ldi r18, LED@0_DEFAULT_ONTIME
  ldi r19, LED@0_DEFAULT_OFFTIME
  ldi r20, LED@0_DEFAULT_REPEATS
  rcall Led@0_SetTiming
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro M_LED_SETFASTTIMING
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_SETFASTTIMING
  ldi r18, 5
  ldi r19, 3
  ldi r20, LED_FAST_REPEATS
  rcall Led@0_SetTiming
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro M_LED_SETIDTIMING
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_SETIDTIMING
  ldi r18, LED_ID_ONTIME
  ldi r19, LED_ID_OFFTIME
  ldi r20, LED_ID_REPEATS
  rcall Led@0_SetTiming
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro M_LED_SETACTIVITYTIMING
;
; @param @0 idx (e.g. "1" for "LED1")

.macro M_LED_SETACTIVITYTIMING
  ldi r18, LED_ACTIVITY_ONTIME
  ldi r19, LED_ACTIVITY_OFFTIME
  ldi r20, LED_ACTIVITY_REPEATS
  rcall Led@0_SetTiming
.endmacro
; @end




; ---------------------------------------------------------------------------
; @macro M_LED_SETTIMING
;
; @param @0 idx (e.g. "1" for "LED1")


.macro M_LED_SETTIMING
  sts led@0OnTime, r18
  sts led@0Timer, r18
  sts led@0OffTime, r19
  sts led@0Repeat, r20
  tst r18
  brne l_1
  sbi LED@0_PORT, LED@0_PINNUM ; off
  rjmp l_ret
l_1:
  cbi LED@0_PORT, LED@0_PINNUM ; on
l_ret:
.endmacro
; @end




#endif


