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



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

.dseg

beeperSimpleTimer:      .byte 1
beeperSimpleOnTime:     .byte 1
beeperSimpleOffTime:    .byte 1
beeperSimpleRepeat:     .byte 1



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

.cseg


BEEPER_SIMPLE_BEGIN:



; ---------------------------------------------------------------------------
; BeeperSimple_Init
;
; @return CFLAG set if okay, clear on error
; @clobbers r16

BeeperSimple_Init:
  sbi BEEPER_SIMPLE_DDR, BEEPER_SIMPLE_PINNUM  ; out
  cbi BEEPER_SIMPLE_PORT, BEEPER_SIMPLE_PINNUM ; off
  clr r16
  sts beeperSimpleTimer, r16
  sts beeperSimpleOnTime, r16
  sts beeperSimpleOffTime, r16
  sts beeperSimpleRepeat, r16
  sec
  ret



; ---------------------------------------------------------------------------
; @routine BeeperSimple_Every100ms @global
;
; @clobbers r16

BeeperSimple_Every100ms:
  lds r16, beeperSimpleTimer
  tst r16
  brne BeeperSimple_Every100ms_dec
  lds r17, beeperSimpleRepeat
  tst r17
  brne BeeperSimple_Tick_restartTimer
  rjmp BeeperSimple_Every100ms_ret
BeeperSimple_Every100ms_dec:
  dec r16
  breq BeeperSimple_Every100ms_zero
  rjmp BeeperSimple_Every100ms_setTimer
BeeperSimple_Every100ms_zero:
  sbis BEEPER_SIMPLE_PORT, BEEPER_SIMPLE_PINNUM ; skip next op if BEEPER is on
    rjmp BeeperSimple_Tick_isOff
  ; is on
  cbi BEEPER_SIMPLE_PORT, BEEPER_SIMPLE_PINNUM ; off
  lds r16, beeperSimpleOffTime
  rjmp BeeperSimple_Every100ms_setTimer
BeeperSimple_Tick_isOff:
  lds r16, beeperSimpleRepeat
  tst r16
  breq BeeperSimple_Every100ms_setTimer   ; no repeat, go stop timer
  dec r16
  sts beeperSimpleRepeat, r16
  brne BeeperSimple_Tick_restartTimer
  ; repeat counter reached 0, stop timer
  rjmp BeeperSimple_Every100ms_setTimer
BeeperSimple_Tick_restartTimer:
  sbi BEEPER_SIMPLE_PORT, BEEPER_SIMPLE_PINNUM ; on
  lds r16, beeperSimpleOnTime
BeeperSimple_Every100ms_setTimer:
  sts beeperSimpleTimer, r16
BeeperSimple_Every100ms_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine BeeperSimple_BeepShort @global
;
; Short beep.
;
; @clobbers R18, R19, R20

BeeperSimple_BeepShort:
  ldi r18, 2
  ldi r19, 1
  ldi r20, 1
  rjmp BeeperSimple_SetTiming
; @end



; ---------------------------------------------------------------------------
; @routine BeeperSimple_SetTiming @global
;
; Set blinking timing for LED. Switch LED on.
; @param r18 ontime  (in 1/10s)
; @param r19 offtime (in 1/10s)
; @param r20 repeat counter
; @clobbers r16
;

BeeperSimple_SetTiming:
  cbi BEEPER_SIMPLE_PORT, BEEPER_SIMPLE_PINNUM ; off
  sts beeperSimpleOnTime, r18
  sts beeperSimpleOffTime, r19
  sts beeperSimpleRepeat, r20
  clr r16
  sts beeperSimpleTimer, r16
  ret
; @end



BEEPER_SIMPLE_END:
.equ MODULE_SIZE_BEEPER_SIMPLE = BEEPER_SIMPLE_END-BEEPER_SIMPLE_BEGIN



