#ifndef SK6805_H
#define SK6805_H

#include "main.h"
#include <stdint.h>


#define LED_COUNT       9
#define BITS_PER_LED    24


/*

  clock is set to 16 MHz. 1 tick is then 1/16 MHz = 0.0625 us
  PWM_period = (ARR + 1) * timer_tick = 20 * 0.0625 us = 1.25 us
    ARR is set in parameter settings period TIM2, counter = 19+1

  1.25/0.0625 = 20 ticks per period

  for example T0H = 4, 4/20 = .2 | .2*1.25 = 0.25 us

  this pwm period is 1.25 us with a 20 tick resolution

*/



#define SK6805_0        4     // T0H = 4  ticks = 4*0.0625 us = 0.25 us
#define SK6805_1        10    // T1H = 10 ticks = 10*0.0625 us = 0.625 us
#define RESET_SLOTS     80    // reset = 80 PWM periods = 80 * 1.25 us = 100 us

// reset slots on both endas to extra safe
#define PWM_BUF_LEN     (RESET_SLOTS + (LED_COUNT * BITS_PER_LED) + RESET_SLOTS)

// array size of type uint32_t

/*

  pwm_data[0] = 0;
  pwm_data[1] = 0;
  pwm_data[2] = 0;
  // ... leading reset slots ...

  pwm_data[120] = 4;   // first LED data bit
  pwm_data[121] = 10;  // next LED data bit
  pwm_data[122] = 4;
  // ...

  // trailing reset slots
  pwm_data[839] = 0;

*/

extern uint32_t pwm_data[PWM_BUF_LEN];

typedef struct
{
    uint8_t g;
    uint8_t r;
    uint8_t b;
} SK6805_Color;

extern SK6805_Color leds[LED_COUNT];

/*
  DMA busy/done flag.
  1 = ready, 0 = transfer in progress.
  volatile because it is modified from interrupt/callback code.
*/
extern volatile uint8_t sk6805_done;


void SK6805_Clear(void);
void SK6805_SetLed(uint32_t led_index, uint8_t r, uint8_t g, uint8_t b);
void SK6805_Show(void);


#endif /* SK6805_H */