Skip to content
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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ exception goes at the end of a list, with its reason.
loopback, one client at a time, for the control program, which has no WebSocket.
- `SimulatorSocket` — what both hosts do with a socket: listen on loopback, never wait, send
all, and tell a connection that is gone.
- `SimulatorFile` — a whole file read into a string, the page the web host serves and the
panel `--expect` names.
- `SimulatorFile` — a whole file read into a string, the page the web host serves, the
panel `--expect` names and the pictures `DocuPictures --check` compares.
- `Avr/` — `mainAvr.cpp` and `SimulatorAvr`, the executable `SimulatorAvr`: the same simulator
for the `.elf` `avr-gcc` makes of an example (`--elf`). simavr runs it on an ATmega328P, its
pins 10, 11 and 13 and its SPI output drive the model, its UART is `Serial`. It writes the
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ add_compile_fail_tests(DIRECTORY ${PROJECT_SOURCE_DIR}/test/compile_fail STANDAR
# The pictures in docu/ are drawn from the library and the model rather than by hand: DocuPictures writes them, and the
# test fails when one in docu/ is no longer what the library would draw. Once, as the transfer path does not matter.
add_executable(DocuPictures docu/DocuPictures.cpp docu/DocuSvg.cpp)
target_link_libraries(DocuPictures PRIVATE MaxMatrix-bitbang)
target_link_libraries(DocuPictures PRIVATE MaxMatrix-bitbang SimulatorHosts)
add_test(NAME docu/pictures COMMAND DocuPictures --check ${PROJECT_SOURCE_DIR}/docu)

# The examples as the Uno runs them: the .elf avr-gcc makes of each, with bit banging and with SPI, in simavr against the
Expand Down
19 changes: 18 additions & 1 deletion OIL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| 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 |
| 42 | A newer avr-gcc as a cross-check | 41 | done | |
| 43 | Bit operations in one class, from the template | 41 | done | #109 |

Expand Down Expand Up @@ -1190,6 +1190,23 @@ whether something changed, as wxWidgets' `Enable()`, goes to `Discard()` as well
finds is `MAXMATRIX_NODISCARD` now, some thirty functions. A reference to the object itself or a
handle to what was just added is no result, AGENTS.md says why.

As built for the rest of the chapter: printing the panel at the end of a run and comparing it
with `--expect` are two functions (FN-1), as are writing the pictures of `DocuPictures` and
checking them, which reports a picture it cannot write now; `IsUpright()` returns at the first
dot that differs, and the name MD_MAX72XX gives a wiring is found by a function of its own
(FN-14); `DocuSvg` takes its paint and font by `const&`, the control program a `wxPoint` by
value (FN-7); a few helpers of `control/` and `docu/` are `constexpr` (FN-9); the panel of the
simulator holds its wiring by `std::reference_wrapper`, as it always has one (FN-15);
`SimulatorAvr::TakeByte()` is not `const`, as it writes the frame (FN-11); and `DocuPictures`
reads a file through `SimulatorFile` (FN-13). Left as they are for FN-16, as clang-tidy does not
see them and a test fails on a swap of each: `ControlCommand::MakeDot(column, row)` and the
column and row of `ControlDotPanel`, which `ControlCommandTest` and the order of the line pin;
`DocuSvg::AddArrow(from, to_x, thickness)`, `CountLit(chip, digit)` and `FindLedOfDot(x, y)`, which
`docu/pictures` pins; and `SimulatorWebHost::SendResponse(status, content_type)`, which
`simulator/web` pins for the page and a 404, the page's now as well. `SimulatorWeb::ConvertToHex(value,
digits)` is left too: `SimulatorTest` pins the call that escapes a char, the frame log of the page
only the eye.

### 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,
Expand Down
2 changes: 1 addition & 1 deletion control/ControlDotPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ bool ControlDotPanel::IsLit(uint8_t column, uint8_t row) const {
* Finds the LED at `point`, in pixels of the panel; none for a point in the
* margin or in the gap between two modules, which belongs to no LED.
*/
ControlDotPanel::Dot ControlDotPanel::GetDotAt(wxPoint const& point) const {
ControlDotPanel::Dot ControlDotPanel::GetDotAt(wxPoint point) const {
Dot const none{false, 0, 0};
wxCoord const cell{FromDIP(kCell)};
wxCoord const module_width{(max_matrix::kColumnNumberOfModule * cell) +
Expand Down
2 changes: 1 addition & 1 deletion control/ControlDotPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ControlDotPanel : public wxPanel {
void OnCaptureLost(wxMouseCaptureLostEvent& event);
void PaintDot(uint8_t column, uint8_t row);
MAXMATRIX_NODISCARD bool IsLit(uint8_t column, uint8_t row) const;
MAXMATRIX_NODISCARD Dot GetDotAt(wxPoint const& point) const;
MAXMATRIX_NODISCARD Dot GetDotAt(wxPoint point) const;

public:
/*!
Expand Down
6 changes: 4 additions & 2 deletions control/ControlTcpPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ bool SetNonBlocking(Socket socket) {
#endif

// The socket is kept as an intptr_t, which holds both kinds.
Socket ToSocket(intptr_t socket) { return static_cast<Socket>(socket); }
Length ToLength(size_t length) { return static_cast<Length>(length); }
constexpr Socket ToSocket(intptr_t socket) {
return static_cast<Socket>(socket);
}
constexpr Length ToLength(size_t length) { return static_cast<Length>(length); }

} // namespace

Expand Down
125 changes: 81 additions & 44 deletions docu/DocuPictures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
#include <array>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "Arduino.h"
#include "DocuSvg.h"
#include "Host/SimulatorFile.h"
#include "Max7219.h"
#include "MaxMatrix.h"
#include "Utility/Bit.h"
Expand Down Expand Up @@ -169,6 +169,9 @@ template <typename Type>
using PerDot = std::array<std::array<Type, max_matrix::kRowNumberOfModule>,
kNumberOfColumns>;

/* each picture's file name and its SVG text */
using Pictures = std::vector<std::pair<std::string, std::string>>;

/*! \brief The LED of every dot, if each lit exactly one. */
struct FoundLeds {
bool found; /*!< Whether every dot lit exactly one LED. */
Expand Down Expand Up @@ -261,8 +264,6 @@ constexpr bool IsPatternDot(uint8_t x, uint8_t y) {
* \return True if every dot is lit where it was set.
*/
bool IsUpright(Max7219Wiring const& wiring) {
bool upright{true};

matrix.Clear();
for (uint8_t x{0}; x < kNumberOfColumns; x++) {
for (uint8_t y{0}; y < max_matrix::kRowNumberOfModule; y++) {
Expand All @@ -273,11 +274,28 @@ bool IsUpright(Max7219Wiring const& wiring) {
for (uint8_t x{0}; x < kNumberOfColumns; x++) {
for (uint8_t y{0}; y < max_matrix::kRowNumberOfModule; y++) {
if (mock::chain.IsLit(wiring, x, y) != IsPatternDot(x, y)) {
upright = false;
return false;
}
}
}
return upright;
return true;
}

/*!
* \brief Finds the name MD_MAX72XX gives a wiring, if it gives it one.
*
* \param[in] wiring The wiring.
* \return The name, as the README uses it, or nullptr for a wiring without one.
*/
char const* FindModuleName(Max7219Wiring const& wiring) {
for (auto const& named : kModuleNames) {
if ((named.wiring->digits_as_rows == wiring.digits_as_rows) &&
(named.wiring->reversed_columns == wiring.reversed_columns) &&
(named.wiring->reversed_rows == wiring.reversed_rows)) {
return named.name;
}
}
return nullptr;
}

/*!
Expand All @@ -299,12 +317,9 @@ std::string GetModules() {
modules += ", ";
}
modules += wiring.name;
for (auto const& named : kModuleNames) {
if ((named.wiring->digits_as_rows == wiring.digits_as_rows) &&
(named.wiring->reversed_columns == wiring.reversed_columns) &&
(named.wiring->reversed_rows == wiring.reversed_rows)) {
modules += std::string(" (") + named.name + ")";
}
char const* const name{FindModuleName(wiring)};
if (name != nullptr) {
modules += std::string(" (") + name + ")";
}
}
return modules.empty() ? "no module" : modules;
Expand All @@ -317,7 +332,7 @@ std::string GetModules() {
* \param[in] x The column, as the viewer sees it.
* \return The pixels from the left.
*/
int32_t GetCellX(uint8_t x) {
constexpr int32_t GetCellX(uint8_t x) {
return kLeft +
((x / max_matrix::kColumnNumberOfModule) *
((max_matrix::kColumnNumberOfModule * kCell) + kModuleGap)) +
Expand All @@ -331,7 +346,7 @@ int32_t GetCellX(uint8_t x) {
* \param[in] y The row, as the viewer sees it.
* \return The pixels from the top.
*/
int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); }
constexpr int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); }

/*!
* \brief Returns the width of a picture of the whole panel.
Expand All @@ -340,7 +355,7 @@ int32_t GetCellY(uint8_t y) { return kTop + (y * kCell); }
*
* \return The width in pixels.
*/
uint16_t GetPictureWidth() {
constexpr uint16_t GetPictureWidth() {
return static_cast<uint16_t>(GetCellX(kNumberOfColumns - 1) + kCell + kRight);
}

Expand All @@ -351,7 +366,7 @@ uint16_t GetPictureWidth() {
*
* \return The height in pixels.
*/
uint16_t GetPictureHeight() {
constexpr uint16_t GetPictureHeight() {
return static_cast<uint16_t>(GetCellY(max_matrix::kRowNumberOfModule - 1) +
kCell + kBottom);
}
Expand Down Expand Up @@ -641,29 +656,14 @@ std::string DrawCoordinates() {
return svg.GetDocument();
}

/*!
* \brief Reads a whole file.
*
* \param[in] path The file.
* \return What it holds, or an empty text if it cannot be read, which no
* picture equals.
*/
std::string ReadFile(std::string const& path) {
std::ifstream const file{path};
std::stringstream content;

content << file.rdbuf();
return content.str();
}

/*!
* \brief Draws every picture.
*
* \return Each picture's file name and its SVG text, which is empty if a dot
* did not light exactly one LED.
*/
std::vector<std::pair<std::string, std::string>> DrawPictures() {
std::vector<std::pair<std::string, std::string>> pictures;
Pictures DrawPictures() {
Pictures pictures;

pictures.emplace_back("Coordinates.svg", DrawCoordinates());
// The module types in the order of the model's wirings, whose names the files
Expand All @@ -677,26 +677,59 @@ std::vector<std::pair<std::string, std::string>> DrawPictures() {
}

/*!
* \brief Writes the pictures into a folder, or checks that it holds them.
* \brief Counts the pictures that could not be drawn, and names each.
*
* \param[in] pictures Each picture's file name and its SVG text, empty for one
* that could not be drawn.
* \return How many could not be drawn.
*/
uint8_t CountUndrawn(Pictures const& pictures) {
uint8_t undrawn{0};

for (auto const& picture : pictures) {
if (picture.second.empty()) {
std::cerr << picture.first << ": a dot did not light exactly one LED\n";
undrawn++;
}
}
return undrawn;
}

/*!
* \brief Writes the pictures that were drawn into a folder.
*
* \param[in] pictures Each picture's file name and its SVG text.
* \param[in] folder The folder.
* \param[in] write True to write, false to check.
* \return How many pictures could not be drawn or differ.
* \return How many could not be written.
*/
uint8_t WriteOrCheck(
std::vector<std::pair<std::string, std::string>> const& pictures,
std::string const& folder, bool write) {
uint8_t WritePictures(Pictures const& pictures, std::string const& folder) {
uint8_t failed{0};

for (auto const& picture : pictures) {
std::string const path = folder + "/" + picture.first;
if (picture.second.empty()) {
std::cerr << picture.first << ": a dot did not light exactly one LED\n";
if (!picture.second.empty() && !(std::ofstream{path} << picture.second)) {
std::cerr << path << " cannot be written\n";
failed++;
} else if (write) {
std::ofstream(path) << picture.second;
} else if (ReadFile(path) != picture.second) {
}
}
return failed;
}

/*!
* \brief Checks that a folder holds the pictures that were drawn.
*
* \param[in] pictures Each picture's file name and its SVG text.
* \param[in] folder The folder.
* \return How many differ from the files, or have none.
*/
uint8_t CheckPictures(Pictures const& pictures, std::string const& folder) {
uint8_t failed{0};

for (auto const& picture : pictures) {
std::string const path = folder + "/" + picture.first;
SimulatorFileText const file{SimulatorFile::Read(path.c_str())};
if (!picture.second.empty() &&
(!file.complete || (file.text != picture.second))) {
std::cerr << path
<< " is not what the library draws; run DocuPictures --write\n";
failed++;
Expand Down Expand Up @@ -736,5 +769,9 @@ int main(int argc, char** argv) { // NOLINT(misc-const-correctness): main()
std::cerr << "the matrix does not start on the pins of the examples\n";
return 1;
}
return (WriteOrCheck(DrawPictures(), arguments[2], write) == 0) ? 0 : 1;
Pictures const pictures{DrawPictures()};
uint8_t const undrawn{CountUndrawn(pictures)};
uint8_t const failed{write ? WritePictures(pictures, arguments[2])
: CheckPictures(pictures, arguments[2])};
return ((undrawn == 0) && (failed == 0)) ? 0 : 1;
}
5 changes: 3 additions & 2 deletions docu/DocuSvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ std::string DocuSvg::GetDocument() const {
return document.str();
}

void DocuSvg::AddRect(DocuPoint corner, DocuSize size, DocuPaint paint) {
void DocuSvg::AddRect(DocuPoint corner, DocuSize size, DocuPaint const& paint) {
body_ << " <rect x=\"" << corner.x << "\" y=\"" << corner.y << "\" width=\""
<< size.width << "\" height=\"" << size.height << "\" rx=\""
<< static_cast<uint16_t>(paint.radius) << "\" fill=\"" << paint.fill
<< "\" stroke=\"" << paint.stroke << "\" stroke-width=\"2\"/>\n";
}

void DocuSvg::AddText(DocuPoint at, std::string const& text, DocuFont font) {
void DocuSvg::AddText(DocuPoint at, std::string const& text,
DocuFont const& font) {
body_ << " <text x=\"" << at.x << "\" y=\"" << at.y << "\" font-size=\""
<< static_cast<uint16_t>(font.size) << "\" fill=\"" << font.fill
<< "\" text-anchor=\"" << font.anchor << "\""
Expand Down
4 changes: 2 additions & 2 deletions docu/DocuSvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DocuSvg {
* \param[in] size The width and the height.
* \param[in] paint Its fill, its outline and its corners.
*/
void AddRect(DocuPoint corner, DocuSize size, DocuPaint paint);
void AddRect(DocuPoint corner, DocuSize size, DocuPaint const& paint);

/*!
* \brief Adds a line of text, with its baseline at the point given.
Expand All @@ -96,7 +96,7 @@ class DocuSvg {
* \param[in] font How it is set.
* \pre `text` holds no `&`, `<` or `>`.
*/
void AddText(DocuPoint at, std::string const& text, DocuFont font);
void AddText(DocuPoint at, std::string const& text, DocuFont const& font);

/*!
* \brief Adds a horizontal arrow: a line from `from` to `to_x` at the height
Expand Down
6 changes: 4 additions & 2 deletions simulator/Avr/SimulatorAvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ void SimulatorAvr::ReceiveSerial() {
}

// Adds `value`, the byte on the wire, to the frame as the page shows it; a byte
// outside a frame is an error, as in the Arduino stand-in.
void SimulatorAvr::TakeByte(uint8_t value) const {
// outside a frame is an error, as in the Arduino stand-in. FN-11: not const, as
// it writes the frame the chain receives, which this object follows.
// NOLINTNEXTLINE(readability-make-member-function-const)
void SimulatorAvr::TakeByte(uint8_t value) {
if (chip_select_) {
mock::errors++;
} else {
Expand Down
2 changes: 1 addition & 1 deletion simulator/Avr/SimulatorAvr.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SimulatorAvr {
bool receive_ready_{false};

void ReceiveSerial();
void TakeByte(uint8_t value) const;
void TakeByte(uint8_t value);
/*
* simavr reports a pin or a byte through a C function and a pointer it was
* given, so each of those is a static function that is handed the object. The
Expand Down
Loading