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


.cseg



DEBUG1:
  ldi r19, 50
  ldi r20, 1
  ldi r21, 9
  rcall blinkLed
  rjmp DEBUG1


DEBUG2:
  ldi r19, 50
  ldi r20, 5
  ldi r21, 5
  rcall blinkLed
  rjmp DEBUG2


DEBUG3:
  ldi r19, 50
  ldi r20, 18
  ldi r21, 2
  rcall blinkLed
  rjmp DEBUG3



DEBUG4:
  ldi r19, 50
  ldi r20, 18
  ldi r21, 2
  rcall blinkLed

  ldi r19, 50
  ldi r20, 1
  ldi r21, 9
  rcall blinkLed
  
  rjmp DEBUG4



; @param r19 loop count
; @param r20 on time
; @param r21 off time
; @clobbers (R19, R22)

blinkLed:
  sbi LED1_DDR, LED1_PINNUM     ; out
blinkLed_loop:
    cbi LED1_PORT, LED1_PINNUM  ; on
    mov r22, r20
    rcall waitForMultiple100ms              ; (R22)
    sbi LED1_PORT, LED1_PINNUM  ; off
    mov r22, r21
    rcall waitForMultiple100ms              ; (R22)
    dec r19
  brne blinkLed_loop
  ret


; @param r22 number of 100ms periods to wait
waitForMultiple100ms:
waitForMultiple100ms_loop:
    push r22
      rcall waitFor100ms
    pop r22
    dec r22
  brne waitForMultiple100ms_loop
  ret


waitFor100ms:
  ldi r22, 10
waitFor100ms_loop:
    push r22
      rcall waitFor10ms
    pop r22
    dec r22
  brne waitFor100ms_loop
  ret

waitFor10ms:
  ldi r22, 100
waitFor10ms_loop:
    push r22
      bigcall Utils_WaitFor100MicroSecs
    pop r22
    dec r22
  brne waitFor10ms_loop
  ret



#endif ; COMMON_DEBUG_H

