#ifndef TFT_H
#define TFT_H

#include "hardware/gpio.h"
#include "hardware/dma.h"
#include "tft.pio.h"
#include "pico/stdlib.h"
#include <string.h>
#include <stdio.h>

struct tft_config {
    PIO pio;
    uint sm;
    uint gpio_din;
    uint gpio_clk;
    int gpio_cs;
    uint gpio_dc;
    uint gpio_rst;
    uint gpio_bl;
    uint16_t width;
    uint16_t height;
};


// Note the delays have been shortened a little
static const uint8_t st7789_init_seq[] = {
        1, 20, 0x01,                        // Software reset
        1, 10, 0x11,                        // Exit sleep mode
        2, 2, 0x3a, 0x55,                   // Set colour mode to 16 bit
        2, 0, 0x36, 0x00,                   // Set MADCTL: row then column, refresh is bottom to top ????
        5, 0, 0x2a, 0x00, 0x00, 320 >> 8, 320 & 0xff,   // CASET: column addresses
        5, 0, 0x2b, 0x00, 0x00, 480 >> 8, 480 & 0xff, // RASET: row addresses
        1, 2, 0x13,                         // Normal display on, then 10 ms delay
        1, 2, 0x29,                         // Main screen turn on, then wait 500 ms
        0                                   // Terminate list
};


void tft_set_dc_cs(bool dc, bool cs);
void tft_write_cmd(PIO pio, uint sm, const uint8_t *cmd, size_t count);
void lcd_init(PIO pio, uint sm, const uint8_t *init_seq);
void tft_start_pixels(PIO pio, uint sm);
void tft_init(const struct tft_config* config, int dma_channel);
void tft_fill(uint16_t color);
void tft_push(PIO pio, uint sm, int dma_chan, uint32_t pixels[]);
#endif // TFT_H