Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ST7789 Display #902

Merged
merged 3 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,21 +715,23 @@ you specific needs.
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr><tr>
</tr>
</table>
<!--/drivertable-->
Expand Down
108 changes: 108 additions & 0 deletions examples/rp_pico/st7789/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2022, Nikolay Semenov
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/display/st7789.hpp>
#include <modm/driver/display/st7789/st7789_spi_interface.hpp>
#include <modm/processing/timer/periodic_timer.hpp>
#include <modm/src/modm/ui/display/image/skull_64x64.hpp>

struct Display
{
using Spi = SpiMaster0;
using Cs = modm::platform::Gpio17;
using Sck = modm::platform::Gpio18;
using Miso = modm::platform::GpioUnused;
using Mosi = modm::platform::Gpio19;
using DataCommands = modm::platform::Gpio20;
using Reset = modm::platform::Gpio21;
using Backlight = modm::platform::GpioUnused;

using Interface = modm::St7789SPIInterface<Spi, Cs, Reset, DataCommands>;
};

constexpr std::array<modm::color::Rgb565, 10> colors{
modm::color::html::Black, modm::color::html::Red, modm::color::html::Green,
modm::color::html::Blue, modm::color::html::Cyan, modm::color::html::Magenta,
modm::color::html::Yellow, modm::color::html::Gray, modm::color::html::Orange,
modm::color::html::White,
};

uint32_t
rand(uint32_t limit)
{
static uint32_t rand_next = 0;
rand_next = rand_next * 1103515245 + 12345;
return (uint32_t)(rand_next / 65536) % limit;
}

int
main()
{
Board::initialize();

Uart0::connect<GpioOutput1::Rx>();
Uart0::initialize<Board::SystemClock, 115200_Bd>();

modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull> terminal;

Display::Spi::connect<Display::Sck::Sclk, Display::Mosi::Tx>();
Display::Spi::initialize<Board::SystemClock, 62_MHz>();

using LCD = modm::St7789<Display::Interface, 240, 240>;
LCD lcd;

lcd.initialize();

lcd.clear();
lcd.drawImage({88, 88}, modm::accessor::asFlash(bitmap::skull_64x64));

while (true)
{
char c = 0;
if (terminal.read(c))
{
if ('0' <= c && c <= '9')
{
lcd.clear(colors[c - '0'].color);
} else if (c == 'f')
{
for (size_t i = 0; i < 1000; ++i)
{
lcd.setColor(colors[i % colors.size()]);
lcd.drawCircle({rand(lcd.getWidth()), rand(lcd.getHeight())}, 16);
}
} else if (c == 'r')
{
static uint8_t orientation = 0;
lcd.setOrientation((LCD::Driver::Orientation)(orientation++ % 4));
} else if (c == 's' || c == 'S')
{
lcd.setSleep(c == 's');
} else if (c == 'i' || c == 'I')
{
lcd.setIdle(c == 'i');
} else if (c == 'o' || c == 'O')
{
c == 'o' ? lcd.turnOff() : lcd.turnOn();
} else if (c == 'v' || c == 'V')
{
lcd.setInversion(c == 'V');
} else if (c == 'b' || c == 'B')
{
lcd.setRgbBgrOrder(c == 'b' ? LCD::Driver::RgbBgrOrder::BGR
: LCD::Driver::RgbBgrOrder::RGB);
}
}
}

return 0;
}
13 changes: 13 additions & 0 deletions examples/rp_pico/st7789/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:rp-pico</extends>
<options>
<option name="modm:build:build.path">../../../build/rp_pico/st7789</option>
</options>
<modules>
<module>modm:platform:spi:0</module>
<module>modm:platform:uart:0</module>
<module>modm:processing:timer</module>
<module>modm:driver:st7789</module>
<module>modm:build:scons</module>
</modules>
</library>
109 changes: 109 additions & 0 deletions src/modm/driver/display/st7789.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2022, Nikolay Semenov
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include <modm/ui/display/color_graphic_display.hpp>

#include "st7789/st7789_driver.hpp"

namespace modm
{

/// @ingroup modm_driver_st7789
template<typename Interface, uint16_t Width = 240, uint16_t Height = 320>
class St7789 : public ColorGraphicDisplay, public St7789Driver<Interface, Width, Height>
{
public:
using Driver = St7789Driver<Interface, Width, Height>;

using Driver::initialize;

uint16_t
getWidth() const final
{
switch (Driver::orientation_)
{
case Driver::Orientation::Portrait90:
[[fallthrough]];
case Driver::Orientation::Portrait270:
return Height;
default:
return Width;
}
}

uint16_t
getHeight() const final
{
switch (Driver::orientation_)
{
case Driver::Orientation::Portrait90:
[[fallthrough]];
case Driver::Orientation::Portrait270:
return Width;
default:
return Height;
}
}

size_t
getBufferWidth() const final
{
return Width;
}

size_t
getBufferHeight() const final
{
return Height;
}

void
setPixel(int16_t x, int16_t y) final
{
setPixel(x, y, foregroundColor);
}

color::Rgb565
getPixel(int16_t, int16_t) const final
{
return color::html::Black;
}

void
clearPixel(int16_t x, int16_t y) final
{
setPixel(x, y, backgroundColor);
}

using Driver::clear;
void
clear() final
{
Driver::clear(backgroundColor.color);
}

void
update() final
{ /* noop */
}

private:
void
setPixel(int16_t x, int16_t y, const color::Rgb565 &color)
{
Driver::setClipping(x, y, 1, 1);
Driver::writeData({reinterpret_cast<const uint8_t *>(&color.color), sizeof(color.color)});
}
};

} // namespace modm
29 changes: 29 additions & 0 deletions src/modm/driver/display/st7789.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
#
# Copyright (c) 2022, Nikolay Semenov
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":driver:st7789"
module.description = "ST7789 full-color LCD controller"

def prepare(module, options):
module.depends(
":architecture:delay",
":ui:display")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/display"
env.copy("st7789.hpp")
env.copy("st7789/st7789_driver.hpp")
env.copy("st7789/st7789_driver_impl.hpp")
env.copy("st7789/st7789_protocol.hpp")
env.copy("st7789/st7789_spi_interface.hpp")
108 changes: 108 additions & 0 deletions src/modm/driver/display/st7789/st7789_driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2022, Nikolay Semenov
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include <modm/ui/display/orientation.hpp>
#include <span>

#include "st7789_protocol.hpp"

namespace modm
{

namespace detail::st7789
{
enum ResolutionLimits : uint16_t
{
MaxWidth = 240,
MaxHeight = 320,
};
}

/// @ingroup modm_driver_st7789
template<typename Interface, uint16_t Width = 240, uint16_t Height = 320>
requires(Width <= detail::st7789::MaxWidth && Height <= detail::st7789::MaxHeight)
class St7789Driver

{
public:
void
initialize();

void
clear(uint16_t color);

void
setClipping(uint16_t x, uint16_t y, uint16_t width, uint16_t height);

using Orientation = glcd::Orientation;
void setOrientation(Orientation);
Orientation
getOrientation() const;

public:
using Command = detail::st7789::Command;

void sendCommand(Command);

template<typename Data>
void
sendCommand(Command, Data &&);

using data = Interface::data_t;

enum class ByteOrder : uint8_t
{
Swap2Bytes,
Passthrough,
};

template<ByteOrder OrderOfBytes = ByteOrder::Swap2Bytes>
void writeData(data);

public:
void
hardReset();

void
softReset();

void
turnOn();
void
turnOff();

void
setIdle(bool);

void
setSleep(bool);

void
setInversion(bool);

using RgbBgrOrder = detail::st7789::MemoryDataAccessControl::RgbBgrOrder;
void setRgbBgrOrder(RgbBgrOrder);

protected:
Orientation orientation_{Orientation::Landscape0};

using MemoryDataAccessControl = detail::st7789::MemoryDataAccessControl;
MemoryDataAccessControl madctl_{};

uint16_t offset_x_{};
uint16_t offset_y_{};
};

} // namespace modm

#include "st7789_driver_impl.hpp"
Loading