SatCat5
gui_display.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
23 
24 #pragma once
25 #include <satcat5/gui_icons.h>
26 #include <satcat5/log.h>
27 #include <satcat5/pkt_buffer.h>
28 #include <satcat5/polling.h>
29 
30 namespace satcat5 {
31  namespace gui {
33  struct Cursor { // GCOVR_EXCL_LINE
34  u16 r;
35  u16 c;
36  u32 fg;
37  u32 bg;
38 
39  Cursor() = default;
40  Cursor(const Cursor& t) = default;
41  Cursor& operator=(const Cursor& t) = default;
42  };
43 
45  union DrawArg {
46  const void* ptr;
47  u32 color;
48  s16 scroll;
49  u32 count;
50  u16 rc[2];
51 
52  DrawArg() = default;
53  DrawArg(const DrawArg& t) = default;
54  DrawArg& operator=(const DrawArg& t) = default;
55  };
56 
61  class DrawCmd {
62  public:
64  constexpr DrawCmd()
65  : m_opcode(0), m_arg1(0), m_arg2{.ptr = 0} {}
67  constexpr DrawCmd(u8 opcode, u8 arg1, const DrawArg& arg2)
68  : m_opcode(opcode), m_arg1(arg1), m_arg2(arg2) {}
69 
73  bool rc(u16 r, u16 c) const;
74 
77  u16 height() const;
78  u16 width() const;
80 
81  protected:
82  friend satcat5::gui::Canvas;
83 
84  // Shortcuts for typecasting the "m_arg2.ptr" argument.
85  inline const satcat5::gui::Icon8x8* icon8() const
86  { return (const Icon8x8*)m_arg2.ptr; }
87  inline const satcat5::gui::Icon16x16* icon16() const
88  { return (const Icon16x16*)m_arg2.ptr; }
89  inline const satcat5::gui::Icon32x32* icon32() const
90  { return (const Icon32x32*)m_arg2.ptr; }
91 
93  void update(Cursor& cursor) const;
94 
95  u8 m_opcode; // Command opcode (e.g., "draw icon")
96  u8 m_arg1; // Argument 1 (varies by opcode)
97  satcat5::gui::DrawArg m_arg2; // Argument 2 (varies by opcode)
98  };
99 
105  class Display {
106  public:
110  virtual bool draw(
111  const satcat5::gui::Cursor& cursor,
112  const satcat5::gui::DrawCmd& cmd) = 0;
113 
119  virtual bool scroll(s16 rows) { return true; }
120 
123  inline u16 height() const { return m_height; }
124  inline u16 width() const { return m_width; }
126 
127  protected:
130  Display(u16 h, u16 w) : m_height(h), m_width(w) {}
131  ~Display() {}
132 
135  const u16 m_height, m_width;
136  };
137 
141  public:
144  explicit Canvas(satcat5::gui::Display* display);
145 
148  Canvas(satcat5::gui::Display* display, u8* buffer, unsigned bsize);
149 
151  bool color_fg(u32 color);
153  bool color_bg(u32 color);
155  bool cursor(u16 r, u16 c);
156 
158  bool clear(u32 color);
159 
165  bool draw_icon(const satcat5::gui::Icon8x8* icon, u8 mag = 1);
166  bool draw_icon(const satcat5::gui::Icon16x16* icon, u8 mag = 1);
167  bool draw_icon(const satcat5::gui::Icon32x32* icon, u8 mag = 1);
169 
174  bool draw_rect(u16 h, u16 w, bool fg = true);
175 
179  u16 draw_text(const char* msg,
180  const satcat5::gui::Font8x8& font = BASIC_FONT, u8 mag = 1);
181  u16 draw_text(const char* msg,
182  const satcat5::gui::Font16x16& font, u8 mag = 1);
183  u16 draw_text(const char* msg,
184  const satcat5::gui::Font32x32& font, u8 mag = 1);
186 
196  u16 raw_text(const char* msg,
197  const satcat5::gui::Font8x8& font = BASIC_FONT, u8 mag = 1);
198  u16 raw_text(const char* msg,
199  const satcat5::gui::Font16x16& font, u8 mag = 1);
200  u16 raw_text(const char* msg,
201  const satcat5::gui::Font32x32& font, u8 mag = 1);
203 
207  bool scroll(s16 rows);
208 
209  // Other accessors.
210  inline const satcat5::gui::Cursor& cursor() const
211  { return m_cursor_draw; }
212  inline satcat5::gui::Display* display()
213  { return m_display; }
214  inline u16 height() const
215  { return m_display->height(); }
216  inline u16 width() const
217  { return m_display->width(); }
218 
219  protected:
220  bool cmd_retry();
221  bool draw_char(char ch, const satcat5::gui::DrawCmd& cmd, u16& total_rows);
222  bool draw_eol(u16 height, u16& total_rows);
223  bool enqueue(const satcat5::gui::DrawCmd& cmd);
224  bool execute(const satcat5::gui::DrawCmd& cmd);
225  bool finalize();
226  void poll_demand() override;
227 
228  satcat5::gui::Display* const m_display; // Pointer to display device.
229  satcat5::gui::Cursor m_cursor_draw; // Device draw position
230  satcat5::gui::Cursor m_cursor_user; // User-API draw position
231  satcat5::gui::DrawCmd m_cmd_retry; // Retry current command?
232  satcat5::io::PacketBuffer m_buffer; // Optional command queue
233  };
234 
237  struct LogColors {
238  u32 bg_text, fg_text;
239  u32 bg_error, fg_error;
240  u32 bg_warn, fg_warn;
241  u32 bg_info, fg_info;
242  u32 bg_debug, fg_debug;
243 
244  LogColors() = default;
245  LogColors(const LogColors& t) = default;
246  LogColors& operator=(const LogColors& t) = default;
247  };
248 
251  public:
256  LogToDisplay(
257  satcat5::gui::Canvas* canvas,
258  const satcat5::gui::LogColors& colors,
259  u16 row_min = 0, u16 row_count = 0);
260 
262  void log_event(s8 priority, unsigned nbytes, const char* msg) override;
263 
265  inline void set_colors(const satcat5::gui::LogColors& colors)
266  { m_colors = colors; }
267 
268  protected:
269  satcat5::gui::Canvas* const m_canvas;
270  satcat5::gui::LogColors m_colors;
271  const u16 m_row_min, m_row_count;
272  u16 m_row_next;
273  };
274  }
275 }
User-facing interface for drawing graphical elements on a screen.
Definition: gui_display.h:140
u16 raw_text(const char *msg, const satcat5::gui::Font8x8 &font=BASIC_FONT, u8 mag=1)
Draw a partial line of text with the designated font.
Definition: gui_display.cc:200
bool clear(u32 color)
Clear the entire display contents.
Definition: gui_display.cc:160
bool cursor(u16 r, u16 c)
Set cursor position for subsequent commands.
Definition: gui_display.cc:154
bool draw_rect(u16 h, u16 w, bool fg=true)
Draw a solid rectangle using the specified color.
Definition: gui_display.cc:180
bool draw_icon(const satcat5::gui::Icon8x8 *icon, u8 mag=1)
Draw icon with the designated location and color.
Definition: gui_display.cc:165
bool color_bg(u32 color)
Set background color for subsequent commands.
Definition: gui_display.cc:148
bool scroll(s16 rows)
On supported displays, scroll the designated scrollable window.
Definition: gui_display.cc:227
void poll_demand() override
Deferred event handler, called after request().
Definition: gui_display.cc:299
u16 draw_text(const char *msg, const satcat5::gui::Font8x8 &font=BASIC_FONT, u8 mag=1)
Draw a full line of text with the designated font.
Definition: gui_display.cc:185
bool color_fg(u32 color)
Set foreground color for subsequent commands.
Definition: gui_display.cc:142
Canvas(satcat5::gui::Display *display)
Link this object to a Display in immediate mode.
Definition: gui_display.cc:124
Required API for display devices.
Definition: gui_display.h:105
virtual bool draw(const satcat5::gui::Cursor &cursor, const satcat5::gui::DrawCmd &cmd)=0
Draw pixels to the screen at the designated location.
u16 width() const
Total size of the display, in pixels.
Definition: gui_display.h:124
const u16 m_height
Total size of the display, in pixels.
Definition: gui_display.h:135
virtual bool scroll(s16 rows)
Optional: Advance the predefined viewport by N pixels.
Definition: gui_display.h:119
u16 height() const
Total size of the display, in pixels.
Definition: gui_display.h:123
Display(u16 h, u16 w)
Constructor sets the size of this display in pixels.
Definition: gui_display.h:130
Each "draw command" updates a rectangular region of pixels.
Definition: gui_display.h:61
u16 width() const
Size of the rectangular update region.
Definition: gui_display.cc:92
u16 height() const
Size of the rectangular update region.
Definition: gui_display.cc:77
void update(Cursor &cursor) const
Update cursor-state automatically for easier chaining.
Definition: gui_display.cc:107
constexpr DrawCmd(u8 opcode, u8 arg1, const DrawArg &arg2)
Construct a specific command.
Definition: gui_display.h:67
bool rc(u16 r, u16 c) const
Get the new pixel value at the designated row and column.
Definition: gui_display.cc:52
constexpr DrawCmd()
Construct an empty command.
Definition: gui_display.h:64
The Font class maps characters to fixed-size icons.
Definition: gui_icons.h:86
Service for forwarding Log events to a display adapter.
Definition: gui_display.h:250
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Implement the required API from log::EventHandler.
Definition: gui_display.cc:320
void set_colors(const satcat5::gui::LogColors &colors)
Adjust color parameters.
Definition: gui_display.h:265
LogToDisplay(satcat5::gui::Canvas *canvas, const satcat5::gui::LogColors &colors, u16 row_min=0, u16 row_count=0)
Link this log service to the designated display/canvas.
Definition: gui_display.cc:306
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
Defines the interface for accepting Log messages.
Definition: log.h:72
An "OnDemand" object is polled only on request.
Definition: polling.h:131
"Icon" API for small monochrome images
const Font8x8 BASIC_FONT
A basic fixed-width 8x8 font.
Diagnostic logging to UART and/or Ethernet ports.
Core event-processing loop for SatCat5 software.
Cursor object tracks position and foreground/background colors.
Definition: gui_display.h:33
u32 fg
Foreground color (format defined by display)
Definition: gui_display.h:36
u16 r
Row coordinate (0 = top)
Definition: gui_display.h:34
u32 bg
Background color (format defined by display)
Definition: gui_display.h:37
u16 c
Column coordinate (0 = left)
Definition: gui_display.h:35
Plain-old-data struct for a 16x16 monochrome image.
Definition: gui_icons.h:46
Plain-old-data struct for a 32x32 monochrome image.
Definition: gui_icons.h:65
Plain-old-data struct for an 8x8 monochrome image.
Definition: gui_icons.h:25
Color parameters for the LogToDisplay class.
Definition: gui_display.h:237
Argument for a draw command (see below).
Definition: gui_display.h:45
const void * ptr
Pointer to an icon or other object.
Definition: gui_display.h:46
u32 color
Display-specific color argument.
Definition: gui_display.h:47
s16 scroll
Scrolling parameter (signed)
Definition: gui_display.h:48
u16 rc[2]
A row and column (coordinate or size)
Definition: gui_display.h:50
u32 count
Any other counter.
Definition: gui_display.h:49