; ***************************************************************************
; copyright   : (C) 2023 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

ledSimpleTimer:      .byte 1
ledSimpleOnTime:     .byte 1
ledSimpleOffTime:    .byte 1



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

.cseg


LED_SIMPLE_BEGIN:



; ---------------------------------------------------------------------------
; LedSimple_Init
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear on error
; USED: R1, R2, R3, R4, R16, R17, X

LedSimple_Init:
  sbi LED_SIMPLE_DDR, LED_SIMPLE_PINNUM  ; out
  cbi LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; on
  rcall LedSimple_SetDefaultTiming       ; (R16)
  sec
  ret


; ---------------------------------------------------------------------------
; @routine LedSimple_Every100ms @global
;
; @clobbers r16

LedSimple_Every100ms:
  lds r16, ledSimpleTimer
  dec r16
  breq LedSimple_Every100ms_zero
  rjmp LedSimple_Every100ms_setTimer
LedSimple_Every100ms_zero:
  sbic LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; skip next op if LED is on
    rjmp LedSimple_Tick_isOff
  ; is on
  sbi LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; off
  lds r16, ledSimpleOffTime
  rjmp LedSimple_Every100ms_setTimer
LedSimple_Tick_isOff:
  cbi LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; on
  lds r16, ledSimpleOnTime
LedSimple_Every100ms_setTimer:
  sts ledSimpleTimer, r16
  ret
; @end



; ---------------------------------------------------------------------------
; @routine LedSimple_SetDefaultTiming @global
;
; Set default timing for LED.
; @clobbers R18, R19
;

LedSimple_SetDefaultTiming:
  ldi r18, LED_SIMPLE_ONTIME
  ldi r19, LED_SIMPLE_OFFTIME
  rjmp LedSimple_SetTiming
; @end



; ---------------------------------------------------------------------------
; @routine LedSimple_SetFastTiming @global
;
; Set fast blinking timing for LED. Switch LED on.
; @clobbers R18, R19
;

LedSimple_SetFastTiming:
  ldi r18, 5
  ldi r19, 3
  rjmp LedSimple_SetTiming
; @end



; ---------------------------------------------------------------------------
; @routine LedSimple_SetTiming @global
;
; Set blinking timing for LED. Switch LED on.
; @param r18 ontime  (in 1/10s)
; @param r19 offtime (in 1/10s)
; @clobbers none
;

LedSimple_SetTiming:
  sts ledSimpleOnTime, r18
  sts ledSimpleTimer, r18
  sts ledSimpleOffTime, r19
  cbi LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; on
  ret
; @end



LED_SIMPLE_END:
.equ MODULE_SIZE_LED_SIMPLE = LED_SIMPLE_END-LED_SIMPLE_BEGIN



