From b788d31733fb2b13389da12a69de8896435693d7 Mon Sep 17 00:00:00 2001 From: virskl Date: Tue, 17 Nov 2026 16:39:55 +0100 Subject: [PATCH 1/2] The library's types as the chapter Types asks (OIL 41) A char is stored and the line's length counted in two statements (T-14), and ChangeMapping() walks its pairs of modules by one counter rather than two in one declaration (T-13, T-14). The header of a font is read by one function into a struct, where CheckFont() and GetGlyph() each assigned four variables declared ahead (T-13), which also takes a copy the chapter Functions missed (FN-13). Command passes a command with a value on in a function of its own (T-13). A condition of three parts and a sum in a condition are named steps (T-18), and the auto of a port register says why its type cannot be written (T-8). On the Uno, in every variant, every example is 8 bytes of flash smaller, Shutdown 12 and SerialControl 22; RAM is the same. --- OIL.md | 12 +++++++ src/Command/Command.cpp | 52 ++++++++++++++++------------ src/Command/Command.h | 1 + src/Font/Font.cpp | 77 +++++++++++++++++++++-------------------- src/Matrix/Panel.cpp | 8 +++-- src/Max7219/Chain.cpp | 3 +- src/Text/Text.cpp | 8 +++-- 7 files changed, 94 insertions(+), 67 deletions(-) diff --git a/OIL.md b/OIL.md index b9c6e89..f03e6ff 100644 --- a/OIL.md +++ b/OIL.md @@ -1207,6 +1207,18 @@ column and row of `ControlDotPanel`, which `ControlCommandTest` and the order of digits)` is left too: `SimulatorTest` pins the call that escapes a char, the frame log of the page only the eye. +As reviewed for the chapter Types on 2026-11-17, its review parts T-1, T-8, T-13, T-14, T-18, T-19 +and T-23, T-17 being done above. As built for the library: a char is stored and the length counted +in two statements (T-14); `internal::Panel::ChangeMapping()` walks its pairs of modules by one +counter rather than two in one declaration (T-13, T-14); the header of a font is read by one +function into a struct, where `CheckFont()` and `GetGlyph()` each assigned four variables declared +ahead, which also takes a copy the chapter Functions missed (T-13, FN-13); `Command` passes a +command with a value on in a function of its own, where its status was declared ahead (T-13); a +condition of three parts in `CheckFont()` and a sum in that of `CharShiftTask()` are named steps +(T-18); and the `auto` of a port register says why the type cannot be written (T-8). On the Uno, +in every variant, every example 8 bytes of flash smaller, Shutdown 12 and SerialControl 22; the +same RAM. + ### 42 A newer avr-gcc as a cross-check `max_matrix::Byte` (41) made the examples 30 to 50 bytes of flash larger on the Uno at first, diff --git a/src/Command/Command.cpp b/src/Command/Command.cpp index 8f561cc..edf89ef 100644 --- a/src/Command/Command.cpp +++ b/src/Command/Command.cpp @@ -70,7 +70,8 @@ Result Command::Feed(Matrix& matrix, char c, Print& answer) { } if (c != kLineEnd) { if (line_length_ < MAXMATRIX_COMMAND_LINE_LENGTH) { - line_[line_length_++] = c; + line_[line_length_] = c; + line_length_++; } else { line_too_long_ = true; } @@ -103,36 +104,41 @@ Result Command::Feed(Matrix& matrix, char c, Print& answer) { // with the end of the name, and passes it on by name. Result Command::Execute(Matrix& matrix, Print& answer) { uint8_t const separator{Find(0, kValueSeparator)}; - Status status{Error::kOk}; - if (line_[separator] == internal::kNullCharacter) { - if (0 == strcmp_P(line_.data(), PSTR("picture"))) { - AnswerPicture(matrix, answer); - return CommandState::kAnswered; - } - if (0 != strcmp_P(line_.data(), PSTR("clear"))) { - return Error::kUnknownCommand; - } - matrix.Clear(); - } else { + if (line_[separator] != internal::kNullCharacter) { line_[separator] = internal::kNullCharacter; - auto const value{static_cast(separator + 1)}; - if (0 == strcmp_P(line_.data(), PSTR("text"))) { - status = ExecuteText(matrix, value); - } else if (0 == strcmp_P(line_.data(), PSTR("textShift"))) { - status = ExecuteTextShift(matrix, value); - } else if (0 == strcmp_P(line_.data(), PSTR("setDot"))) { - status = ExecuteDot(matrix, value); - } else { - status = ExecuteSetting(matrix, value); + Status const status{ + ExecuteWithValue(matrix, static_cast(separator + 1))}; + if (!status.IsOk()) { + return status.GetError(); } + return CommandState::kDone; + } + if (0 == strcmp_P(line_.data(), PSTR("picture"))) { + AnswerPicture(matrix, answer); + return CommandState::kAnswered; } - if (!status.IsOk()) { - return status.GetError(); + if (0 != strcmp_P(line_.data(), PSTR("clear"))) { + return Error::kUnknownCommand; } + matrix.Clear(); return CommandState::kDone; } +// Passes a command NAME=VALUE on by its name, which ends where `value` starts. +Status Command::ExecuteWithValue(Matrix& matrix, uint8_t value) { + if (0 == strcmp_P(line_.data(), PSTR("text"))) { + return ExecuteText(matrix, value); + } + if (0 == strcmp_P(line_.data(), PSTR("textShift"))) { + return ExecuteTextShift(matrix, value); + } + if (0 == strcmp_P(line_.data(), PSTR("setDot"))) { + return ExecuteDot(matrix, value); + } + return ExecuteSetting(matrix, value); +} + // Returns the position of the first `c` in the line from `from` on, or that of // the line's null character if there is none. The line always ends with one, // as Feed() puts it there. diff --git a/src/Command/Command.h b/src/Command/Command.h index e27afa4..a6bee21 100644 --- a/src/Command/Command.h +++ b/src/Command/Command.h @@ -96,6 +96,7 @@ class Command { MAXMATRIX_NODISCARD Result Execute(Matrix& matrix, Print& answer); MAXMATRIX_NODISCARD uint8_t Find(uint8_t from, char c) const; + MAXMATRIX_NODISCARD Status ExecuteWithValue(Matrix& matrix, uint8_t value); MAXMATRIX_NODISCARD Status ExecuteTextShift(Matrix& matrix, uint8_t value); MAXMATRIX_NODISCARD Status ExecuteText(Matrix& matrix, uint8_t value) const; MAXMATRIX_NODISCARD Status ExecuteDot(Matrix& matrix, uint8_t value); diff --git a/src/Font/Font.cpp b/src/Font/Font.cpp index 0831f44..8a03f43 100644 --- a/src/Font/Font.cpp +++ b/src/Font/Font.cpp @@ -61,6 +61,28 @@ uint8_t ReadByte(span font, size_t position) { return pgm_read_byte(&font[position]); } +/* what the header of a font says: its first and last code, the height of its + * chars, and where the width of its first char lies */ +struct FontHeader { + uint16_t first; + uint16_t last; + uint8_t height; + size_t offset; +}; + +// Reads the header of `font`; one that is not of version 1 is read as one of +// version 2, as CheckFont() takes no other. +FontHeader ReadHeader(span font) { + if (ReadByte(font, kVersionPosition) == kVersion1) { + return FontHeader{ReadByte(font, kFirstPosition), + ReadByte(font, kLastPosition1), + ReadByte(font, kHeightPosition1), kHeaderLength1}; + } + return FontHeader{ReadNumber(font, kFirstPosition), + ReadNumber(font, kLastPosition2), + ReadByte(font, kHeightPosition2), kHeaderLength2}; +} + } // namespace /* @@ -205,35 +227,28 @@ Status Font::CheckFont(span font) { } uint8_t const version{ReadByte(font, kVersionPosition)}; - uint16_t first{0}; - uint16_t last{0}; - uint8_t height{0}; - size_t offset{0}; - - if (version == kVersion1) { - first = ReadByte(font, kFirstPosition); - last = ReadByte(font, kLastPosition1); - height = ReadByte(font, kHeightPosition1); - offset = kHeaderLength1; - } else if ((version == kVersion2) && (font.size() >= kHeaderLength2)) { - first = ReadNumber(font, kFirstPosition); - last = ReadNumber(font, kLastPosition2); - height = ReadByte(font, kHeightPosition2); - offset = kHeaderLength2; - } else { + bool const header_whole{ + (version == kVersion1) || + ((version == kVersion2) && (font.size() >= kHeaderLength2))}; + if (!header_whole) { return Error::kNotAFont; } - if ((first > last) || (height < 1) || (height > kRowNumberOfModule)) { + FontHeader const header{ReadHeader(font)}; + bool const has_chars{header.first <= header.last}; + bool const height_fits{(header.height >= 1) && + (header.height <= kRowNumberOfModule)}; + if (!has_chars || !height_fits) { return Error::kNotAFont; } + size_t offset{header.offset}; /* the loop ends at the last char rather than past it, as a last char of * 0xFFFF has no next */ - for (uint16_t code{first};; code++) { + for (uint16_t code{header.first};; code++) { if (offset >= font.size()) { return Error::kNotAFont; } offset += ReadByte(font, offset) + 1u; - if (code == last) { + if (code == header.last) { break; } } @@ -246,35 +261,21 @@ Status Font::CheckFont(span font) { * five bytes from flash cost less than the RAM to keep them. */ Result Font::GetGlyph(span font, char c) { - bool const version_1{ReadByte(font, kVersionPosition) == kVersion1}; + FontHeader const header{ReadHeader(font)}; uint16_t const code{static_cast(c)}; - uint16_t first{0}; - uint16_t last{0}; - uint8_t height{0}; - size_t offset{0}; - if (version_1) { - first = ReadByte(font, kFirstPosition); - last = ReadByte(font, kLastPosition1); - height = ReadByte(font, kHeightPosition1); - offset = kHeaderLength1; - } else { - first = ReadNumber(font, kFirstPosition); - last = ReadNumber(font, kLastPosition2); - height = ReadByte(font, kHeightPosition2); - offset = kHeaderLength2; - } - if ((code < first) || (code > last)) { + if ((code < header.first) || (code > header.last)) { return Error::kUnknownChar; } - for (uint16_t skipped{first}; skipped < code; skipped++) { + size_t offset{header.offset}; + for (uint16_t skipped{header.first}; skipped < code; skipped++) { offset += ReadByte(font, offset) + 1u; } uint8_t const width{ReadByte(font, offset)}; if (width == 0) { return Error::kUnknownChar; } - return Glyph{font.subspan(offset + 1).first(width), height}; + return Glyph{font.subspan(offset + 1).first(width), header.height}; } DecodedChar Font::DecodeChar(span text) { diff --git a/src/Matrix/Panel.cpp b/src/Matrix/Panel.cpp index c018d29..4c3b043 100644 --- a/src/Matrix/Panel.cpp +++ b/src/Matrix/Panel.cpp @@ -418,8 +418,12 @@ void Panel::ChangeMapping(MatrixOrientation matrix_orientation, MatrixOrientation const old_matrix_orientation{matrix_orientation_}; ModuleType const old_module_type{module_type_}; - for (int16_t left{0}, right{static_cast(GetNumberOfModules() - 1)}; - left <= right; left++, right--) { + uint8_t const modules{GetNumberOfModules()}; + // a module in the middle of an odd chain pairs with itself + auto const pairs{static_cast((modules + 1) / 2)}; + + for (uint8_t left{0}; left < pairs; left++) { + auto const right{static_cast(modules - 1 - left)}; uint8_t const left_column{ static_cast(left * kColumnNumberOfModule)}; uint8_t const right_column{ diff --git a/src/Max7219/Chain.cpp b/src/Max7219/Chain.cpp index 8bad938..c11c4ba 100644 --- a/src/Max7219/Chain.cpp +++ b/src/Max7219/Chain.cpp @@ -180,7 +180,8 @@ void Chain::ShiftOut(uint8_t value) { * The core's portOutputRegister() makes the register's address, a number read * from flash, a pointer, and SREG is the status register at its fixed * address: clang-tidy reports both on the Uno, where there is no other way to - * a port. + * a port. T-8: auto, as the register is a volatile uint8_t on the board and + * the mock's object of its own on the host. */ auto* const data_in_register{portOutputRegister( data_in_.port)}; /* NOLINT(performance-no-int-to-ptr) */ diff --git a/src/Text/Text.cpp b/src/Text/Text.cpp index 6f03187..50cd1d0 100644 --- a/src/Text/Text.cpp +++ b/src/Text/Text.cpp @@ -133,14 +133,16 @@ bool Text::CharShiftTask(Panel& panel) { panel.ShiftLeft(Byte{}); sprite_shift_counter_++; + /* the step after the char and the spaces behind it have come in */ + auto const done{ + static_cast(glyph_.columns.size() + space_between_chars_ + 1)}; + /* if sprite is completed set only spaces */ if (sprite_shift_counter_ <= glyph_.columns.size()) { X const left{static_cast(panel.GetNumberOfColumns() - sprite_shift_counter_)}; SetGlyph(panel, Point{left, Y{0}}, glyph_); - } else if (sprite_shift_counter_ == - static_cast(glyph_.columns.size() + - space_between_chars_ + 1)) { + } else if (sprite_shift_counter_ == done) { /* if sprite and spaces are completed task has finished */ sprite_shift_counter_ = kShiftIdle; return false; From d5fcad3e37c92f763dc8a39b7af756c0d5bef095 Mon Sep 17 00:00:00 2001 From: virskl Date: Tue, 17 Nov 2026 16:40:12 +0100 Subject: [PATCH 2/2] OIL.md: 41 is #129 as well --- OIL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OIL.md b/OIL.md index f03e6ff..d29c37c 100644 --- a/OIL.md +++ b/OIL.md @@ -50,7 +50,7 @@ Status: `open`, `in progress`, `done`, `dropped` (with the reason in the item). | 38 | Results that must not be ignored | 36 | done | #94 | | 39 | MC/DC coverage | 34 | done | #95, #96 | | 40 | Braced initialization checked | 34 | done | #93 | -| 41 | The template's rules that review checks | 36 | in progress | #99, #100, #101, #102, #104, #105, #106, #107, #113, #115, #116, #117, #118, #121, #122, #124, #125, #126, #127, #128 | +| 41 | The template's rules that review checks | 36 | in progress | #99, #100, #101, #102, #104, #105, #106, #107, #113, #115, #116, #117, #118, #121, #122, #124, #125, #126, #127, #128, #129 | | 42 | A newer avr-gcc as a cross-check | 41 | done | | | 43 | Bit operations in one class, from the template | 41 | done | #109 |