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
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ on the Uno.
Host code whose failures no sketch sees, the simulator and `DocuPictures`, returns `bool`, `true`
for success: each of its functions fails for one reason, which its `\return` names beside `false`.
`Error` has no words for a port that is taken, and `std::optional` is C++17, while the
simulator is built as `gnu++11` with the library.
simulator is built as `gnu++11` with the library. A function that finds or reads something
returns it, and whether it found it, in a small struct of its own, as `SimulatorFoundText` or
`ControlPicture`, never through a pointer (FN-8); a buffer that grows over many calls, as what a
port has read, is a reference, as is an object a function draws on (FN-15).

#### Layout of a source file

Expand Down
11 changes: 10 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 |
| 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 |
| 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 @@ -1171,6 +1171,15 @@ makes each line from its fields in one place, the front end says a port is taken
the seven `avr_irq_register_notify()` of `SimulatorAvr`, one call of simavr each, which a function
of two numbers side by side, the port and the bit, would only make swappable.

As built for FN-8 and FN-15 in the host code: no function writes its result through a pointer.
What one finds or reads it returns, with whether it did in a small struct of its own where both
are needed, as the WebSocket's frames and key, the web host's path, the command line's numbers,
the control program's picture, lines and LED under the mouse, and the LEDs `DocuPictures` looks
up; what the hosts receive they return, and the caller appends. A buffer that grows over many
calls, as what a port has read, the index into the command line and the labels and picture
`DocuPictures` draws on are references. `SimulatorRun::SetUp()` keeps its two references, as it
sets up the objects the tick reaches through a plain function pointer.

### 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
18 changes: 9 additions & 9 deletions control/ControlCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@ std::string ControlCommand::MakePicture() { return "picture"; }

// Two hex digits per column, upper case as SerialControl prints them; lower
// case is taken as well.
bool ControlCommand::DecodePicture(std::string const& line,
std::vector<uint8_t>* columns) {
ControlPicture ControlCommand::DecodePicture(std::string const& line) {
static std::string const kName = "picture=";
ControlPicture picture{false, std::vector<uint8_t>{}};

if (line.compare(0, kName.size(), kName) != 0) {
return false;
return picture;
}
columns->clear();
picture.answers_picture = true;
if (line.size() !=
(kName.size() + (2u * static_cast<size_t>(kControlNumberOfColumns)))) {
return true;
return picture;
}
for (size_t i{kName.size()}; i < line.size(); i += 2) {
std::array<char, 3> const digits{{line[i], line[i + 1], '\0'}};
if ((isxdigit(static_cast<unsigned char>(digits[0])) == 0) ||
(isxdigit(static_cast<unsigned char>(digits[1])) == 0)) {
columns->clear();
return true;
picture.columns.clear();
return picture;
}
columns->push_back(
picture.columns.push_back(
static_cast<uint8_t>(strtoul(digits.data(), nullptr, kHexBase)));
}
return true;
return picture;
}

int32_t ControlCommand::GetBytesLeft(std::string const& line) {
Expand Down
21 changes: 14 additions & 7 deletions control/ControlCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>
#include <vector>

#include "MaxMatrixAttributes.h"
#include "MaxMatrixConfig.h"

/* ControlCommand Parameter */
Expand All @@ -26,6 +27,16 @@ constexpr uint8_t kControlNumberOfModules{4};
constexpr uint8_t kControlNumberOfColumns{kControlNumberOfModules *
max_matrix::kColumnNumberOfModule};

/*! \brief A line SerialControl answered with, as
* ControlCommand::DecodePicture() reads it. */
struct ControlPicture {
bool answers_picture; /*!< Whether the line answers `picture`. */
/*! One byte per column as the viewer sees it, from the left, bit 0 the top
* row; empty if the picture is not one of `kControlNumberOfColumns` columns
* in hex, as from a sketch built for another chain. */
std::vector<uint8_t> columns;
};

/*!
* \brief The command lines of SerialControl, as the control program sends them,
* and the answer to `picture`.
Expand Down Expand Up @@ -102,14 +113,10 @@ class ControlCommand {
* \brief Decodes the answer to `picture` into the columns of the panel.
*
* \param[in] line A line SerialControl answered with, without its line end.
* \param[out] columns One byte per column as the viewer sees it, from the
* left, bit 0 the top row; empty if the picture is not
* one of `kControlNumberOfColumns` columns in hex, as
* from a sketch built for another chain.
* \return True if `line` answers `picture`, false for any other line.
* \return Whether `line` answers `picture`, and the columns if it does.
*/
static bool DecodePicture(std::string const& line,
std::vector<uint8_t>* columns);
MAXMATRIX_NODISCARD static ControlPicture DecodePicture(
std::string const& line);

/*!
* \brief Returns whether SerialControl takes `line` whole.
Expand Down
26 changes: 12 additions & 14 deletions control/ControlDotPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void ControlDotPanel::OnPaint(wxPaintEvent& /* event */) {
* outside the panel.
*/
void ControlDotPanel::OnLeftDown(wxMouseEvent& event) {
Dot dot{};
Dot const dot{GetDotAt(event.GetPosition())};

if (!GetDotAt(event.GetPosition(), &dot)) {
if (!dot.found) {
return;
}
uint8_t const column{dot.column};
Expand All @@ -143,12 +143,11 @@ void ControlDotPanel::OnLeftDown(wxMouseEvent& event) {
* starts again at the next LED without a line to it.
*/
void ControlDotPanel::OnMotion(wxMouseEvent& event) {
Dot dot{};

if (!painting_ || !event.LeftIsDown()) {
return;
}
if (!GetDotAt(event.GetPosition(), &dot)) {
Dot const dot{GetDotAt(event.GetPosition())};
if (!dot.found) {
on_dot_ = false;
return;
}
Expand Down Expand Up @@ -202,11 +201,11 @@ bool ControlDotPanel::IsLit(uint8_t column, uint8_t row) const {
}

/*
* Finds the LED at `point`, in pixels of the panel, and gives it in `dot`.
* Returns false for a point in the margin or in the gap between two modules,
* which belongs to no LED.
* 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.
*/
bool ControlDotPanel::GetDotAt(wxPoint const& point, Dot* dot) const {
ControlDotPanel::Dot ControlDotPanel::GetDotAt(wxPoint const& point) const {
Dot const none{false, 0, 0};
wxCoord const cell{FromDIP(kCell)};
wxCoord const module_width{(max_matrix::kColumnNumberOfModule * cell) +
FromDIP(kModuleGap)};
Expand All @@ -216,17 +215,16 @@ bool ControlDotPanel::GetDotAt(wxPoint const& point, Dot* dot) const {

if ((x < 0) || (y < 0) ||
(in_module >= (max_matrix::kColumnNumberOfModule * cell))) {
return false;
return none;
}
wxCoord const found_column{
(x / module_width * max_matrix::kColumnNumberOfModule) +
(in_module / cell)};
wxCoord const found_row{y / cell};
if ((found_column >= kControlNumberOfColumns) ||
(found_row >= max_matrix::kRowNumberOfModule)) {
return false;
return none;
}
dot->column = static_cast<uint8_t>(found_column);
dot->row = static_cast<uint8_t>(found_row);
return true;
return Dot{true, static_cast<uint8_t>(found_column),
static_cast<uint8_t>(found_row)};
}
7 changes: 4 additions & 3 deletions control/ControlDotPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
*/
class ControlDotPanel : public wxPanel {
private:
/* an LED by its column and row, as GetDotAt() finds it: named members rather
* than two pointers to bytes, which a call could swap (FN-16) */
/* the LED under a point, as GetDotAt() finds it, if there is one: named
* members, which a call cannot swap (FN-16) */
struct Dot {
bool found;
uint8_t column;
uint8_t row;
};
Expand All @@ -58,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;
bool GetDotAt(wxPoint const& point, Dot* dot) const;
MAXMATRIX_NODISCARD Dot GetDotAt(wxPoint const& point) const;

public:
/*!
Expand Down
35 changes: 19 additions & 16 deletions control/ControlLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ void ControlLink::AskForPicture(uint32_t now) {
picture_asked_at_ = now;
}

bool ControlLink::Task(std::vector<std::string>* lines) {
ControlLinkLines ControlLink::Task() {
size_t end{0};
ControlLinkLines read{false, std::vector<std::string>{}};

if (!port_.get().IsOpen()) {
return false;
return read;
}
bool const open{port_.get().Read(&received_)};
read.open = port_.get().Read(received_);

while (((end = received_.find('\n')) != std::string::npos) ||
(received_.size() >= kControlLinkMaxLineLength)) {
Expand All @@ -79,30 +80,32 @@ bool ControlLink::Task(std::vector<std::string>* lines) {
if (!line.empty() && (line.back() == '\r')) {
line.pop_back();
}
Take(std::move(line), lines);
if (Take(line)) {
read.lines.push_back(std::move(line));
}
}
return open;
return read;
}

/*
* Takes one line from SerialControl. A board that says ready has started again,
* by its reset button say, and answers no picture asked for before; it is asked
* again at the next interval. An answer to picture is never passed on, even one
* that cannot be decoded, as it would fill the log twenty times a second.
* Takes one line from SerialControl, and returns whether it is one to pass on.
* A board that says ready has started again, by its reset button say, and
* answers no picture asked for before; it is asked again at the next interval.
* An answer to picture is never passed on, even one that cannot be decoded, as
* it would fill the log twenty times a second.
*/
void ControlLink::Take(std::string line, std::vector<std::string>* lines) {
std::vector<uint8_t> picture;

bool ControlLink::Take(std::string const& line) {
if (line == kControlLinkReady) {
ready_ = true;
picture_asked_ = false;
}
if (!ControlCommand::DecodePicture(line, &picture)) {
lines->push_back(std::move(line));
return;
ControlPicture picture{ControlCommand::DecodePicture(line)};
if (!picture.answers_picture) {
return true;
}
if (picture_asked_ && !picture_stale_) {
picture_ = std::move(picture);
picture_ = std::move(picture.columns);
}
picture_asked_ = false;
return false;
}
19 changes: 12 additions & 7 deletions control/ControlLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ constexpr uint32_t kControlLinkPictureIntervalMs{50u};
* asked for again */
constexpr uint32_t kControlLinkPictureTimeoutMs{1000u};

/*! \brief What ControlLink::Task() read. */
struct ControlLinkLines {
/*! Whether the port is still open, or closed now or gone at its other end. */
bool open;
/*! Every whole line that has arrived, without its line end, but an answer to
* `picture`, which ControlLink::GetPicture() returns. */
std::vector<std::string> lines;
};

/*!
* \brief The lines between the control program and one SerialControl, on a
* board or in the simulator.
Expand Down Expand Up @@ -66,7 +75,7 @@ class ControlLink {
bool picture_timed_{false};
uint32_t picture_asked_at_{0};

void Take(std::string line, std::vector<std::string>* lines);
bool Take(std::string const& line);

public:
/*!
Expand Down Expand Up @@ -127,13 +136,9 @@ class ControlLink {
*
* Call it often, from a timer: it never waits.
*
* \param[out] lines Every whole line that has arrived is appended, without
* its line end, but an answer to `picture`, which
* GetPicture() returns.
* \return True while the port is open, or false once it is closed or its
* other end is gone.
* \return The lines that have arrived, and whether the port is still open.
*/
bool Task(std::vector<std::string>* lines);
MAXMATRIX_NODISCARD ControlLinkLines Task();
};

#endif // MAXMATRIX_CONTROL_CONTROLLINK_H_
2 changes: 1 addition & 1 deletion control/ControlPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ControlPort {
* \return True while the port is open, or false once the other end is gone:
* the cable pulled, the simulator stopped. The port is closed then.
*/
virtual bool Read(std::string* data) = 0;
virtual bool Read(std::string& data) = 0;

/*! \brief Closes the port, if it is open. */
virtual void Close() = 0;
Expand Down
8 changes: 4 additions & 4 deletions control/ControlSerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool ControlSerialPort::Write(std::string const& data) {
return true;
}

bool ControlSerialPort::Read(std::string* data) {
bool ControlSerialPort::Read(std::string& data) {
std::array<char, kControlStreamChunkLength> chunk;
DWORD const size = static_cast<DWORD>(chunk.size());
DWORD length = size;
Expand All @@ -151,7 +151,7 @@ bool ControlSerialPort::Read(std::string* data) {
Close();
return false;
}
data->append(chunk.data(), length);
data.append(chunk.data(), length);
}
return true;
}
Expand Down Expand Up @@ -261,13 +261,13 @@ bool ControlSerialPort::Write(std::string const& data) {
return sent;
}

bool ControlSerialPort::Read(std::string* data) {
bool ControlSerialPort::Read(std::string& data) {
if (!IsOpen()) {
return false;
}
int const descriptor{descriptor_};
bool const open{ControlStream::ReceiveAvailable(
*data,
data,
[descriptor](char* bytes, size_t length) {
return read(descriptor, bytes, length);
},
Expand Down
2 changes: 1 addition & 1 deletion control/ControlSerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ControlSerialPort final : public ControlPort {

MAXMATRIX_NODISCARD bool IsOpen() const override;
bool Write(std::string const& data) override;
bool Read(std::string* data) override;
bool Read(std::string& data) override;
void Close() override;
};

Expand Down
4 changes: 2 additions & 2 deletions control/ControlTcpPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ bool ControlTcpPort::Write(std::string const& data) {
return sent;
}

bool ControlTcpPort::Read(std::string* data) {
bool ControlTcpPort::Read(std::string& data) {
if (!IsOpen()) {
return false;
}
Socket const connection{ToSocket(socket_)};
bool const open{ControlStream::ReceiveAvailable(
*data,
data,
[connection](char* bytes, size_t length) {
return recv(connection, bytes, ToLength(length), 0);
},
Expand Down
2 changes: 1 addition & 1 deletion control/ControlTcpPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ControlTcpPort final : public ControlPort {

MAXMATRIX_NODISCARD bool IsOpen() const override;
bool Write(std::string const& data) override;
bool Read(std::string* data) override;
bool Read(std::string& data) override;
void Close() override;
};

Expand Down
Loading