2021-03-24 12:18:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "hardware/spi.h"
|
|
|
|
#include "hardware/gpio.h"
|
2021-05-14 18:12:37 +01:00
|
|
|
#include "../../common/pimoroni_common.hpp"
|
2021-03-24 12:18:56 +00:00
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
|
|
|
class ST7789 {
|
2021-04-20 10:54:10 +01:00
|
|
|
spi_inst_t *spi = PIMORONI_SPI_DEFAULT_INSTANCE;
|
2021-04-23 13:22:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
//--------------------------------------------------
|
|
|
|
private:
|
2021-03-24 12:18:56 +00:00
|
|
|
// screen properties
|
|
|
|
uint16_t width;
|
|
|
|
uint16_t height;
|
|
|
|
uint16_t row_stride;
|
2021-04-23 13:35:19 +01:00
|
|
|
uint32_t dma_channel;
|
|
|
|
|
2021-03-24 12:18:56 +00:00
|
|
|
// interface pins with our standard defaults where appropriate
|
2021-04-20 10:54:10 +01:00
|
|
|
uint cs = SPI_BG_FRONT_CS;
|
|
|
|
uint dc = SPI_DEFAULT_MISO;
|
|
|
|
uint sck = SPI_DEFAULT_SCK;
|
|
|
|
uint mosi = SPI_DEFAULT_MOSI;
|
|
|
|
uint miso = PIN_UNUSED; // used as data/command
|
|
|
|
uint bl = SPI_BG_FRONT_PWM;
|
|
|
|
uint vsync = PIN_UNUSED; // only available on some products
|
2021-03-24 12:18:56 +00:00
|
|
|
|
2021-05-06 14:20:59 +01:00
|
|
|
uint32_t spi_baud = 16 * 1000 * 1000;
|
2021-03-24 12:18:56 +00:00
|
|
|
|
|
|
|
public:
|
2021-04-20 10:54:10 +01:00
|
|
|
// frame buffer where pixel data is stored
|
|
|
|
uint16_t *frame_buffer;
|
|
|
|
|
2021-04-19 16:36:31 +01:00
|
|
|
ST7789(uint16_t width, uint16_t height, uint16_t *frame_buffer, BG_SPI_SLOT slot) :
|
|
|
|
width(width), height(height), frame_buffer(frame_buffer) {
|
|
|
|
switch(slot) {
|
|
|
|
case BG_SPI_FRONT:
|
2021-04-20 10:54:10 +01:00
|
|
|
cs = SPI_BG_FRONT_CS;
|
|
|
|
bl = SPI_BG_FRONT_PWM;
|
2021-04-19 16:36:31 +01:00
|
|
|
break;
|
|
|
|
case BG_SPI_BACK:
|
2021-04-20 10:54:10 +01:00
|
|
|
cs = SPI_BG_BACK_CS;
|
|
|
|
bl = SPI_BG_BACK_PWM;
|
2021-04-19 16:36:31 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 12:18:56 +00:00
|
|
|
ST7789(uint16_t width, uint16_t height, uint16_t *frame_buffer) :
|
|
|
|
width(width), height(height), frame_buffer(frame_buffer) {}
|
|
|
|
|
|
|
|
ST7789(uint16_t width, uint16_t height, uint16_t *frame_buffer,
|
|
|
|
spi_inst_t *spi,
|
2021-04-20 10:54:10 +01:00
|
|
|
uint cs, uint dc, uint sck, uint mosi, uint miso = PIN_UNUSED, uint bl = PIN_UNUSED) :
|
|
|
|
spi(spi),
|
|
|
|
width(width), height(height),
|
|
|
|
cs(cs), dc(dc), sck(sck), mosi(mosi), miso(miso), bl(bl), frame_buffer(frame_buffer) {}
|
2021-03-24 12:18:56 +00:00
|
|
|
|
2021-04-23 13:22:00 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//--------------------------------------------------
|
|
|
|
public:
|
2021-03-24 12:18:56 +00:00
|
|
|
void init(bool auto_init_sequence = true, bool round = false);
|
|
|
|
|
2021-03-30 15:15:13 +01:00
|
|
|
spi_inst_t* get_spi() const;
|
2021-04-20 10:54:10 +01:00
|
|
|
uint get_cs() const;
|
|
|
|
uint get_dc() const;
|
|
|
|
uint get_sck() const;
|
|
|
|
uint get_mosi() const;
|
|
|
|
uint get_bl() const;
|
2021-03-30 15:15:13 +01:00
|
|
|
|
2021-03-24 12:18:56 +00:00
|
|
|
void command(uint8_t command, size_t len = 0, const char *data = NULL);
|
|
|
|
void vsync_callback(gpio_irq_callback_t callback);
|
|
|
|
void update(bool dont_block = false);
|
|
|
|
void set_backlight(uint8_t brightness);
|
|
|
|
void flip();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|