blob: 039a1cda165b0fc779c913d3bed90f5a7cb29f4b [file] [log] [blame]
Chris Mumford54514cf2022-11-01 20:23:47 +00001// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <array>
17
18#include "pw_color/color.h"
Anthony DiGirolamod1b0bbb2024-04-10 20:15:41 +000019#include "pw_geometry/size.h"
20#include "pw_geometry/vector2.h"
Chris Mumford54514cf2022-11-01 20:23:47 +000021#include "pw_result/result.h"
22
23constexpr size_t kNumCharsWide = 52;
24constexpr size_t kNumRows = 9;
25
26constexpr pw::color::color_rgb565_t kBlackColor = 0x0000;
27constexpr pw::color::color_rgb565_t kWhiteColor = 0xffff;
28
29// Maintain a scrolling buffer of text. The "drawing" cursor location starts
30// at (0,0), and new characters are inserted right-to-left. Newline ('\n')
31// characters cause the text to be scrolled up - eventually rolling off the
32// top of the buffer to make space for new text rows at the bottom.
33class TextBuffer {
34 public:
35 // An ASCII character with a foreground and background color.
36 struct Char {
37 // Set to a cleared default state.
38 void Reset() {
39 ch = '\0';
40 foreground_color = kWhiteColor;
41 background_color = kBlackColor;
42 }
43
44 char ch = '\0';
45 pw::color::color_rgb565_t foreground_color = kWhiteColor;
46 pw::color::color_rgb565_t background_color = kBlackColor;
47 };
48
49 // A row of ASCII text characters.
50 struct TextRow {
51 void Clear() {
52 for (Char& ch : chars) {
53 ch.Reset();
54 }
55 }
56 std::array<Char, kNumCharsWide> chars;
57 };
58
59 // Insert a character at the current cursor location. The cursor will be
60 // moved right by one slot. Newline ('\n') characters will move the cursor to
61 // the next line, at column 0.
62 void DrawCharacter(const Char& ch);
63
64 // Return the size, in characters, of the text buffer.
Anthony DiGirolamod1b0bbb2024-04-10 20:15:41 +000065 pw::geometry::Size<int> GetSize() const {
66 return pw::geometry::Size<int>{kNumCharsWide, kNumRows};
Chris Mumford54514cf2022-11-01 20:23:47 +000067 }
68
69 // Return the character at the specified location.
Anthony DiGirolamod1b0bbb2024-04-10 20:15:41 +000070 pw::Result<Char> GetChar(pw::geometry::Vector2<int> loc) const;
Chris Mumford54514cf2022-11-01 20:23:47 +000071
72 private:
73 void ScrollUp();
74 void InsertNewline();
75
Anthony DiGirolamod1b0bbb2024-04-10 20:15:41 +000076 pw::geometry::Vector2<int> cursor_ = {0, 0};
Chris Mumford54514cf2022-11-01 20:23:47 +000077 bool character_wrap_enabled_ = false;
78 std::array<TextRow, kNumRows> text_rows_;
79};