SatCat5
spi_ili9341.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Display device driver for the ILI9341
6 
7 #pragma once
8 
9 #include <satcat5/cfg_spi.h>
10 #include <satcat5/gui_display.h>
11 #include <satcat5/polling.h>
12 
13 namespace satcat5 {
14  namespace device {
15  namespace spi {
42  class Ili9341
43  : public satcat5::poll::Timer
44  , public satcat5::gui::Display
46  {
47  public:
51  static constexpr u8
52  MADCTL_MY = 0x80,
53  MADCTL_MX = 0x40,
54  MADCTL_MV = 0x20,
55  MADCTL_ML = 0x10,
56  MADCTL_BGR = 0x08,
57  MADCTL_MH = 0x04;
59 
63  static constexpr u8
69 
73  Ili9341(
74  satcat5::cfg::SpiGeneric* spi, u8 devidx, // SPI interface
75  u8 madctl = ADAFRUIT_ROT0); // LCD configuration
76 
78  bool busy() const;
79 
82  bool invert(bool inv);
83 
85  void reset();
86 
88  inline u16 scroll_pos() const {return m_scroll;}
89 
95  bool viewport(u16 top, u16 size);
96 
99  static constexpr u16 // R G B
100  COLOR_BLACK = 0x0000, // 0, 0, 0
101  COLOR_NAVY = 0x000F, // 0, 0, 123
102  COLOR_DARKGREEN = 0x03E0, // 0, 125, 0
103  COLOR_DARKCYAN = 0x03EF, // 0, 125, 123
104  COLOR_MAROON = 0x7800, // 123, 0, 0
105  COLOR_PURPLE = 0x780F, // 123, 0, 123
106  COLOR_OLIVE = 0x7BE0, // 123, 125, 0
107  COLOR_LIGHTGREY = 0xC618, // 198, 195, 198
108  COLOR_DARKGREY = 0x7BEF, // 123, 125, 123
109  COLOR_BLUE = 0x001F, // 0, 0, 255
110  COLOR_GREEN = 0x07E0, // 0, 255, 0
111  COLOR_CYAN = 0x07FF, // 0, 255, 255
112  COLOR_RED = 0xF800, // 255, 0, 0
113  COLOR_MAGENTA = 0xF81F, // 255, 0, 255
114  COLOR_YELLOW = 0xFFE0, // 255, 255, 0
115  COLOR_WHITE = 0xFFFF, // 255, 255, 255
116  COLOR_ORANGE = 0xFD20, // 255, 165, 0
117  COLOR_GRELLOW = 0xAFE5, // 173, 255, 41
118  COLOR_PINK = 0xFC18; // 255, 130, 198
120 
123  static constexpr satcat5::gui::LogColors DARK_THEME = {
124  COLOR_BLACK, COLOR_LIGHTGREY, // Text
125  COLOR_BLACK, COLOR_RED, // Error
126  COLOR_BLACK, COLOR_ORANGE, // Warning
127  COLOR_BLACK, COLOR_CYAN, // Info
128  COLOR_BLACK, COLOR_BLUE}; // Debug
129 
133  COLOR_WHITE, COLOR_DARKGREY, // Text
134  COLOR_RED, COLOR_BLACK, // Error
135  COLOR_ORANGE, COLOR_BLACK, // Warning
136  COLOR_LIGHTGREY, COLOR_BLACK, // Info
137  COLOR_LIGHTGREY, COLOR_BLACK}; // Debug
138 
139  protected:
140  // Required event handlers from parent classes.
141  bool draw(
142  const satcat5::gui::Cursor& cursor,
143  const satcat5::gui::DrawCmd& cmd) override;
144  bool scroll(s16 rows) override;
145  void spi_done(unsigned nread, const u8* rbytes) override;
146  void timer_event() override;
147 
148  // Internal event handlers.
149  bool in_viewport(u16 row) const;
150  void draw_next();
151  void init_next();
152  bool spi_cmd(u8 len, const u8* cmd, Ili9341* callback);
153  bool spi_madctl();
154  bool spi_vscrdef();
155  void try_twidth(u16 width);
156 
157  // Internal state.
158  satcat5::cfg::SpiGeneric* const m_spi;
159  satcat5::gui::Cursor m_cursor;
160  satcat5::gui::DrawCmd m_draw_cmd;
161  const u8 m_devidx, m_madctl;
162  u16 m_init_step, m_scroll, m_viewtop, m_viewsize;
163  u16 m_draw_step, m_draw_done, m_tile_col, m_tile_row, m_tile_width, m_tile_height;
164  };
165  }
166  }
167 }
SPI Event Handler callback for use with SpiGeneric.
Definition: cfg_spi.h:15
Polymorphic API for a generic SPI interface.
Definition: cfg_spi.h:26
Display device driver for the ILI9341.
Definition: spi_ili9341.h:46
static constexpr u16 COLOR_MAGENTA
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:113
static constexpr u16 COLOR_CYAN
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:111
static constexpr u16 COLOR_WHITE
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:115
u16 scroll_pos() const
Current scroll position.
Definition: spi_ili9341.h:88
static constexpr u16 COLOR_GREEN
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:110
static constexpr u16 COLOR_YELLOW
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:114
static constexpr u16 COLOR_LIGHTGREY
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:107
static constexpr u16 COLOR_MAROON
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:104
static constexpr u8 ADAFRUIT_ROT0
Adafruit panels all use BGR mode with MH = 0.
Definition: spi_ili9341.h:64
static constexpr u8 MADCTL_BGR
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:56
bool draw(const satcat5::gui::Cursor &cursor, const satcat5::gui::DrawCmd &cmd) override
Draw pixels to the screen at the designated location.
Definition: spi_ili9341.cc:162
static constexpr u8 MADCTL_MH
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:57
static constexpr u16 COLOR_DARKGREY
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:108
static constexpr u8 MADCTL_MX
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:53
static constexpr u8 MADCTL_MV
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:54
static constexpr u16 COLOR_NAVY
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:101
static constexpr u16 COLOR_PURPLE
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:105
static constexpr u16 COLOR_GRELLOW
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:117
static constexpr u16 COLOR_BLUE
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:109
bool viewport(u16 top, u16 size)
Configure the scrolling viewport:
Definition: spi_ili9341.cc:150
static constexpr satcat5::gui::LogColors LIGHT_THEME
Recommended colors for light theme (black text on white).
Definition: spi_ili9341.h:132
void timer_event() override
Child class MUST override this method.
Definition: spi_ili9341.cc:215
static constexpr satcat5::gui::LogColors DARK_THEME
Recommended colors for dark theme (white text on black).
Definition: spi_ili9341.h:123
static constexpr u8 MADCTL_MY
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:52
static constexpr u16 COLOR_PINK
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:118
Ili9341(satcat5::cfg::SpiGeneric *spi, u8 devidx, u8 madctl=ADAFRUIT_ROT0)
Constructor links to the specified SPI interface.
Definition: spi_ili9341.cc:112
static constexpr u8 ADAFRUIT_ROT90
Adafruit panels all use BGR mode with MH = 0.
Definition: spi_ili9341.h:65
static constexpr u16 COLOR_RED
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:112
static constexpr u8 ADAFRUIT_ROT180
Adafruit panels all use BGR mode with MH = 0.
Definition: spi_ili9341.h:66
static constexpr u16 COLOR_ORANGE
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:116
static constexpr u16 COLOR_DARKCYAN
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:103
bool busy() const
Busy with initialization or a previous command?
Definition: spi_ili9341.cc:134
static constexpr u16 COLOR_DARKGREEN
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:102
static constexpr u16 COLOR_OLIVE
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:106
void reset()
Initiate software reset.
Definition: spi_ili9341.cc:144
static constexpr u8 ADAFRUIT_ROT270
Adafruit panels all use BGR mode with MH = 0.
Definition: spi_ili9341.h:67
static constexpr u8 MADCTL_ML
Constants for the MADCTL register (Section 8.2.29).
Definition: spi_ili9341.h:55
bool scroll(s16 rows) override
Optional: Advance the predefined viewport by N pixels.
Definition: spi_ili9341.cc:194
static constexpr u16 COLOR_BLACK
Constant definitions for 16-bit color mode.
Definition: spi_ili9341.h:100
bool invert(bool inv)
Invert entire display.
Definition: spi_ili9341.cc:138
Required API for display devices.
Definition: gui_display.h:105
u16 width() const
Total size of the display, in pixels.
Definition: gui_display.h:124
Each "draw command" updates a rectangular region of pixels.
Definition: gui_display.h:61
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
"Display" and "Canvas" API for rendering text and graphics
Core event-processing loop for SatCat5 software.
Cursor object tracks position and foreground/background colors.
Definition: gui_display.h:33
Color parameters for the LogToDisplay class.
Definition: gui_display.h:237