#if !defined(__RPI_PIXLEDS_H__)
#define __RPI_PIXLEDS_H__

#include <stdint.h>

#include "rpi_dma_utils.h"

#if PHYS_REG_BASE == PI_4_REG_BASE // Timings for RPi v4 (1.5 GHz)
#define SMI_TIMING 10, 15, 30, 15  // 400 ns cycle time
#else                              // Timings for RPi v0-3 (1 GHz)
#define SMI_TIMING 10, 10, 20, 10  // 400 ns cycle time
#endif

#define TX_TEST 0           // If non-zero, use dummy Tx data
#define LED_D0_PIN 8        // GPIO pin for D0 output
#define LED_NCHANS 8        // Number of LED channels (8 or 16)
#define LED_NBITS 24        // Number of data bits per LED
#define LED_PREBITS 4       // Number of zero bits before LED data
#define LED_POSTBITS 4      // Number of zero bits after LED data
#define BIT_NPULSES 3       // Number of O/P pulses per LED bit
#define CHAN_MAXLEDS 6 * 60 // Maximum number of LEDs per channel
#define CHASE_MSEC 20       // Delay time for chaser light test
#define REQUEST_THRESH 2    // DMA request threshold
#define DMA_CHAN 10         // DMA channel to use

// Length of data for 1 row (1 LED on each channel)
#define LED_DLEN (LED_NBITS * BIT_NPULSES)

// Transmit data type, 8 or 16 bits
#if LED_NCHANS > 8
#define TXDATA_T uint16_t
#else
#define TXDATA_T uint8_t
#endif

// Ofset into Tx data buffer, given LED number in chan
#define LED_TX_OSET(n) (LED_PREBITS + (LED_DLEN * (n)))

// Size of data buffers & NV memory, given number of LEDs per chan
#define TX_BUFF_LEN(n) (LED_TX_OSET(n) + LED_POSTBITS)
#define TX_BUFF_SIZE(n) (TX_BUFF_LEN(n) * sizeof(TXDATA_T))
#define VC_MEM_SIZE (PAGE_SIZE + TX_BUFF_SIZE(CHAN_MAXLEDS))

void test_leds();
int str_rgb(char *s, int rgbs[][LED_NCHANS], int chan);
void set_color(uint32_t rgb, TXDATA_T *txd);
void rgb_txdata(int *rgbs, TXDATA_T *txd);
void swap_bytes(void *data, int len);
int hexdig(char c);
void map_devices(void);
void fail(char *s);
void terminate_smi(int sig);
void init_smi(int ledChan, int ns, int setup, int hold, int strobe);
void setup_smi_dma(MEM_MAP *mp, int nsamp, TXDATA_T **txdata);
void start_smi(MEM_MAP *mp);

#endif // __RPI_PIXLEDS_H__