picoled/src/ST7789.cpp

93 lines
2.5 KiB
C++

#include "picoled/ST7789.h"
#include <pico/stdlib.h>
namespace picoled
{
st7789::st7789(size_t width, size_t height, spi_inst_t* spi, uint8_t cs_pin, uint8_t dc_pin) :
oled(width, height),
spi(spi),
cs_pin(cs_pin),
dc_pin(dc_pin)
{
command(0x01); sleep_ms(150); // reset device
command(0x11); sleep_ms(10); // stop sleeping
command(0x3A, 0x66); sleep_ms(10); // set display format
{
constexpr size_t PARAM_COUNT = 4;
// no clue where these numbers come from
// see https://github.com/adafruit/Adafruit-ST7735-Library/blob/62112b90eddcb2ecc51f474e9fe98b68eb26cb2a/Adafruit_ST7789.cpp#L111-L117
const uint8_t params[PARAM_COUNT] =
{
0x00,
0x35,
0x00,
0xBB
};
command(0x2A, params, PARAM_COUNT); // set column address range
}
{
constexpr size_t PARAM_COUNT = 4;
// no clue where these numbers come from
// see https://github.com/adafruit/Adafruit-ST7735-Library/blob/62112b90eddcb2ecc51f474e9fe98b68eb26cb2a/Adafruit_ST7789.cpp#L111-L117
const uint8_t params[PARAM_COUNT] =
{
0x00,
0x28,
0x01,
0x17
};
command(0x2B, params, PARAM_COUNT); // set row address range
}
command(0x36, 0b11000000); // set memory addressing mode
command(0x21); // turn on display inversion
command(0x13); sleep_ms(10); // set display to normal mode
command(0x29); sleep_ms(10); // turn on display
}
void st7789::update_impl() const
{
command(0x2C, inactive_buffer().data_pointer(), get_width() * get_height() * 3);
}
void st7789::command(uint8_t cmd) const
{
command(cmd, nullptr, 0);
}
void st7789::command(uint8_t cmd, uint8_t param) const
{
command(cmd, &param, 1);
}
void st7789::command(uint8_t cmd, const uint8_t* params, size_t param_count) const
{
gpio_put(cs_pin, false); // select chip
gpio_put(dc_pin, false); // command mode
spi_write_blocking(spi, &cmd, 1);
gpio_put(dc_pin, true); // parameters are sent via data mode
spi_write_blocking(spi, params, param_count);
gpio_put(cs_pin, true); // deselect chip
}
pixel st7789::format(const pixel& p)
{
const uint16_t red_scaled = p.red() * 0x3F / 0xFF; // scale to 6-bit color
const uint16_t green_scaled = p.green() * 0x3F / 0xFF; // scale to 6-bit color
const uint16_t blue_scaled = p.blue() * 0x3F / 0xFF; // scale to 6-bit color
const uint8_t red = (red_scaled & 0xFF) << 2;
const uint8_t green = (green_scaled & 0xFF) << 2;
const uint8_t blue = (blue_scaled & 0xFF) << 2;
return { red, green, blue };
}
}