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





; ***************************************************************************
; type 0


.macro SK6812_SENDBIT0             ;  0    1
  rol r16                          ; +1   +1
  brcs l_send1                     ; +1   +2
  ; send 0
  nop                              ; +1
  sbi SK6812_PORT, SK6812_PINNUM   ; +2
;  nop                              ; +1
  nop                              ; +1
  cbi SK6812_PORT, SK6812_PINNUM   ; +2
  rjmp l_end                       ; +2
l_send1:
  ; send 1
  sbi SK6812_PORT, SK6812_PINNUM   ;      +2
  nop                              ;      +1
  nop                              ;      +1
  nop                              ;      +1
  cbi SK6812_PORT, SK6812_PINNUM   ;      +2
  nop                              ;      +1
l_end:
;  nop                              ; +1   +1
.endmacro                          ; 12   12



.macro SK6812_SENDBYTE0
  SK6812_SENDBIT0
  SK6812_SENDBIT0
  SK6812_SENDBIT0
  SK6812_SENDBIT0

  SK6812_SENDBIT0
  SK6812_SENDBIT0
  SK6812_SENDBIT0
  SK6812_SENDBIT0
.endmacro



; @param r18 red value
; @param r19 green value
; @param r20 blue value
; @param r21 white value

.macro SK6812_SENDQUADRUPEL0
  mov r16, r19                ; G  + 1
  SK6812_SENDBYTE0            ;    +12
  mov r16, r18                ; R  + 1
  SK6812_SENDBYTE0            ;    +12
  mov r16, r20                ; B  + 1
  SK6812_SENDBYTE0            ;    +12
  mov r16, r21                ; W  + 1
  SK6812_SENDBYTE0
.endmacro



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

sk6812SetAllColor0:
  sbi SK6812_DDR, SK6812_PINNUM                 ; set to output
  cbi SK6812_PORT, SK6812_PINNUM

  push r15
    in r15, SREG
    cli

    lds r24, sk6812NumLeds
    lds r25, sk6812NumLeds+1
    
sk6812SetAllColor0_loop:
      SK6812_SENDQUADRUPEL0
      sbiw r25:r24, 1                           ; +2
;      dec r24                                   ; +1
      breq sk6812SetAllColor0_loopEnd           ; +1
    rjmp sk6812SetAllColor0_loop                ; +2
sk6812SetAllColor0_loopEnd:
    ; reset
    cbi SK6812_PORT, SK6812_PINNUM
    Utils_WaitNanoSecs 80000, 0, r23            ; wait for 80us

;    sbi SK6812_PORT, SK6812_PINNUM

    out SREG, r15
  pop r15
  ret
; @end









