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

#if 0  ; disabled for now


; ***************************************************************************
; type 1


.macro SK6812_SENDBIT1
  lsl r16                          ; +1
  brcs l_sendOne                   ; +1 if false, +2 if branch taken
  ; send ZERO
  nop                              ; +1
  out SK6812_PORT, r25             ; +1   HIGH
  nop                              ; +1
  nop                              ; +1
  nop
  out SK6812_PORT, r24             ; +1
  nop                              ; +1
  rjmp l_end                       ; +2
l_sendOne:
  out SK6812_PORT, r25             ; +1   HIGH
  nop                              ; +1
  nop                              ; +1
  nop                              ; +1
  nop                              ; +1
  nop                              ; +1
  out SK6812_PORT, r24             ; +1
  nop                              ; +1
l_end:
.endmacro



; ---------------------------------------------------------------------------
; @routine sk6812SendByte1
;
; Sends a byte to the LED strip
; @param r16 byte
; @clobbers r16, r24, r25

sk6812SendByte1:
  ldi r24, 0
  ldi r25, (1<<SK6812_PINNUM)
  
  SK6812_SENDBIT1
  SK6812_SENDBIT1
  SK6812_SENDBIT1
  SK6812_SENDBIT1

  SK6812_SENDBIT1
  SK6812_SENDBIT1
  SK6812_SENDBIT1
  SK6812_SENDBIT1
  ret
; @end



; ---------------------------------------------------------------------------
; @routine sk6812SendQuadruple1
;
; Sends GRBW
; @param r18 red value
; @param r19 green value
; @param r20 blue value
; @param r21 white value
; @param Z   pointer to jumptable entry for SendByte function (called by ijmp)
; @clobbers r16 (r17, r24, r25)

sk6812SendQuadruple1:
  mov r16, r19     ; G
  rcall sk6812SendByte1  ; (r16, r17, r24, r25)
  mov r16, r18     ; R
  rcall sk6812SendByte1  ; (r16, r17, r24, r25)
  mov r16, r20     ; B
  rcall sk6812SendByte1  ; (r16, r17, r24, r25)
  mov r16, r21     ; W
  rcall sk6812SendByte1  ; (r16, r17, r24, r25)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine sk6812SetAllColor1
;
; Set all LEDs to same colour.
;
; @param r18 red value
; @param r19 green value
; @param r20 blue value
; @param r21 white value
; @clobbers r23 (r16, r17, r24, r25)

sk6812SetAllColor1:
  sbi SK6812_DDR, SK6812_PINNUM                 ; set to output
  cbi SK6812_PORT, SK6812_PINNUM
  push r15
    in r15, SREG
    cli
;    Utils_WaitNanoSecs 80000, 0, r22            ; wait for 80us
    nop
    nop
    lds r23, sk6812NumLeds
sk6812SetAllColor1_loop:
      rcall sk6812SendQuadruple1                ; (r16, r17, r24, r25, Z)
      dec r23
    brne sk6812SetAllColor1_loop

    ; reset
    cbi SK6812_PORT, SK6812_PINNUM
    Utils_WaitNanoSecs 80000, 0, r22            ; wait for 80us

;    sbi SK6812_PORT, SK6812_PINNUM

    out SREG, r15
  pop r15
sk6812SetAllColor1_done:
  ret
; @end



#endif


