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
16 changes: 15 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, #128, #129 |
| 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, #130 |
| 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 @@ -1219,6 +1219,20 @@ condition of three parts in `CheckFont()` and a sum in that of `CharShiftTask()`
in every variant, every example 8 bytes of flash smaller, Shutdown 12 and SerialControl 22; the
same RAM.

As built for the host code: no assignment or second side effect stands in a condition any more
(T-14): the hosts accept in a loop that declares what it accepted, `ControlLink::Task()` finds
each line end in a statement of its own, and the setup of a listener, of a serial port and of a
WebSocket client is a named step per call; `SimulatorAvr` counts a bit before it compares. A
variable stands where it is first needed (T-13), the length field of a WebSocket frame and the
length of its payload are two, and a chunk is made only after the guard that may return. A
condition or sum of several parts is named steps (T-18): where a frame's payload starts and ends,
a port's text and range, how long a picture is waited for, a module's pitch in the pictures. A
range-`for` names its element's type (T-8), and the counter of lit LEDs in `DocuPictures` is a
`uint16_t`, as all 256 would wrap a byte (T-1). The control program's stroke rounds with integers
as `lround()` did, which takes its `double` (H-7). Left for the compiler that is not in the dev
container: the Windows half of the serial port declares some variables ahead, which only got
their initializers here.

### 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
38 changes: 27 additions & 11 deletions control/ControlDotPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "ControlDotPanel.h"

#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <wx/dcbuffer.h>
Expand All @@ -38,6 +37,23 @@ constexpr int kLed{14};
constexpr int kModuleGap{8};
constexpr int kMargin{6};

/* where a stroke is between two LEDs: `step` of `steps` */
struct StrokeStep {
int16_t step;
int16_t steps;
};

// Returns how far a stroke has come by `at` on its way by `delta`, in LEDs,
// rounded to the nearest, a half away from its start, as lround() rounded it
// before the double went (H-7).
int16_t GetStrokeOffset(int16_t delta, StrokeStep at) {
int32_t const travelled{static_cast<int32_t>(delta) * at.step};
int32_t const half{at.steps / 2};
int32_t const rounded{(travelled >= 0) ? ((travelled + half) / at.steps)
: -((half - travelled) / at.steps)};
return static_cast<int16_t>(rounded);
}

} // namespace

/* ControlDotPanel colours: the red of the common modules, and the board behind
Expand Down Expand Up @@ -90,17 +106,17 @@ void ControlDotPanel::OnPaint(wxPaintEvent& /* event */) {
wxCoord const led{FromDIP(kLed)};
wxCoord const gap{FromDIP(kModuleGap)};
wxCoord const margin{FromDIP(kMargin)};
wxCoord const inset{(cell - led) / 2};

dc.SetBackground(wxBrush(CONTROL_DOT_PANEL_BOARD));
dc.Clear();
dc.SetPen(*wxTRANSPARENT_PEN);
for (uint8_t row{0}; row < max_matrix::kRowNumberOfModule; row++) {
for (uint8_t column{0}; column < kControlNumberOfColumns; column++) {
bool const lit{IsLit(column, row)};
wxCoord const x{margin + (column * cell) +
((column / max_matrix::kColumnNumberOfModule) * gap) +
((cell - led) / 2)};
wxCoord const y{margin + (row * cell) + ((cell - led) / 2)};
wxCoord const gaps{(column / max_matrix::kColumnNumberOfModule) * gap};
wxCoord const x{margin + (column * cell) + gaps + inset};
wxCoord const y{margin + (row * cell) + inset};
if (!IsEnabled()) {
dc.SetBrush(wxBrush(CONTROL_DOT_PANEL_DISABLED));
} else {
Expand Down Expand Up @@ -160,12 +176,12 @@ void ControlDotPanel::OnMotion(wxMouseEvent& event) {
int16_t const steps{
static_cast<int16_t>(std::max(abs(delta_column), abs(delta_row)))};
for (int16_t step{1}; step < steps; step++) {
PaintDot(static_cast<uint8_t>(
last_column_ +
lround(static_cast<double>(delta_column) * step / steps)),
static_cast<uint8_t>(
last_row_ +
lround(static_cast<double>(delta_row) * step / steps)));
StrokeStep const at{step, steps};
auto const column_on_the_way{static_cast<uint8_t>(
last_column_ + GetStrokeOffset(delta_column, at))};
auto const row_on_the_way{
static_cast<uint8_t>(last_row_ + GetStrokeOffset(delta_row, at))};
PaintDot(column_on_the_way, row_on_the_way);
}
}
PaintDot(column, row);
Expand Down
23 changes: 12 additions & 11 deletions control/ControlLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ bool ControlLink::Send(std::string const& line) {
// The time is compared as a difference, so that a clock that runs past 2^32 ms
// keeps working.
void ControlLink::AskForPicture(uint32_t now) {
uint32_t const waited{now - picture_asked_at_};

if (!IsReady()) {
return;
}
if (picture_timed_ &&
(waited < (picture_asked_ ? kControlLinkPictureTimeoutMs
: kControlLinkPictureIntervalMs))) {
uint32_t const waited{now - picture_asked_at_};
uint32_t const wait_for{picture_asked_ ? kControlLinkPictureTimeoutMs
: kControlLinkPictureIntervalMs};
if (picture_timed_ && (waited < wait_for)) {
return;
}
if (!port_.get().Write(ControlCommand::MakePicture() + "\n")) {
Expand All @@ -63,20 +62,22 @@ void ControlLink::AskForPicture(uint32_t now) {
}

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

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

while (((end = received_.find('\n')) != std::string::npos) ||
(received_.size() >= kControlLinkMaxLineLength)) {
size_t const length{(end != std::string::npos) ? end
: kControlLinkMaxLineLength};
for (;;) {
size_t const end{received_.find('\n')};
bool const has_end{end != std::string::npos};
if (!has_end && (received_.size() < kControlLinkMaxLineLength)) {
break;
}
size_t const length{has_end ? end : kControlLinkMaxLineLength};
std::string line = received_.substr(0, length);
received_.erase(0, (end != std::string::npos) ? (end + 1) : length);
received_.erase(0, has_end ? (end + 1) : length);
if (!line.empty() && (line.back() == '\r')) {
line.pop_back();
}
Expand Down
10 changes: 5 additions & 5 deletions control/ControlSerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ bool ControlSerialPort::Write(std::string const& data) {
}

bool ControlSerialPort::Read(std::string& data) {
std::array<char, kControlStreamChunkLength> chunk;
std::array<char, kControlStreamChunkLength> chunk{};
DWORD const size = static_cast<DWORD>(chunk.size());
DWORD length = size;

Expand Down Expand Up @@ -218,17 +218,17 @@ bool ControlSerialPort::Configure(uint32_t baud_rate) {
termios settings{};

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg): POSIX's ioctl()
bool const taken{(ioctl(descriptor_, TIOCEXCL) == 0) &&
(tcgetattr(descriptor_, &settings) == 0)};
bool const taken{ioctl(descriptor_, TIOCEXCL) == 0};
bool const read{taken && (tcgetattr(descriptor_, &settings) == 0)};
cfmakeraw(&settings);
max_matrix::Bit<tcflag_t>::SetBits(settings.c_cflag, kLocalReceive);
max_matrix::Bit<tcflag_t>::ClearBits(settings.c_cflag, kTwoStopsHandshake);
settings.c_cc[VMIN] = 1;
settings.c_cc[VTIME] = 0;
// glibc's cfsetspeed() takes a rate as its number as well as its B constant,
// so no table maps one to the other.
return taken && (cfsetspeed(&settings, baud_rate) == 0) &&
(tcsetattr(descriptor_, TCSANOW, &settings) == 0);
bool const rate_set{read && (cfsetspeed(&settings, baud_rate) == 0)};
return rate_set && (tcsetattr(descriptor_, TCSANOW, &settings) == 0);
}

std::vector<std::string> ControlSerialPort::List() {
Expand Down
3 changes: 2 additions & 1 deletion control/ControlStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class ControlStream {

while (sent < data.size()) {
auto const written{send_some(&data[sent], data.size() - sent)};
bool const full{(written < 0) && would_block()};
if (written > 0) {
sent += static_cast<size_t>(written);
} else if ((written >= 0) || !would_block() || !wait_writable()) {
} else if (!full || !wait_writable()) {
return false;
}
}
Expand Down
5 changes: 2 additions & 3 deletions control/ControlTcpPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ ControlTcpPort::~ControlTcpPort() { Close(); }
* only then switched to return at once, so that Read() never waits.
*/
bool ControlTcpPort::Open(uint16_t port) {
sockaddr_in address{};

Close();
#ifdef _WIN32
WSADATA winsock;
WSADATA winsock{};
if (WSAStartup(MAKEWORD(2, 2), &winsock) != 0) return false;
#endif
socket_ = static_cast<intptr_t>(socket(AF_INET, SOCK_STREAM, 0));
Expand All @@ -110,6 +108,7 @@ bool ControlTcpPort::Open(uint16_t port) {
#endif
return false;
}
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
Expand Down
19 changes: 11 additions & 8 deletions control/ControlWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ constexpr int32_t kLevelSlider{static_cast<int32_t>(
constexpr int32_t kLogText{static_cast<int32_t>(
static_cast<uint32_t>(wxTE_MULTILINE) | wxTE_READONLY)};

// The time in milliseconds of a clock that does not jump, as
// ControlLink::AskForPicture() takes it; it wraps after 49 days, which the link
// takes in its stride, as it only subtracts.
uint32_t GetMilliseconds() {
auto const since_start{std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())};
return static_cast<uint32_t>(since_start.count());
}

} // namespace

/* ControlWindow colours of a connection's state */
Expand Down Expand Up @@ -404,18 +413,13 @@ void ControlWindow::OnListPorts(wxCommandEvent& /* event */) {
* shows for its picture; only that one, as the other would answer for nothing.
*/
void ControlWindow::OnTimer(wxTimerEvent& /* event */) {
uint32_t const now{static_cast<uint32_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count())};

Follow(board_link_, "board");
Follow(simulator_link_, "simulator");
ControlLink* const shown{GetShownLink()};
if (shown == nullptr) {
return;
}
shown->AskForPicture(now);
shown->AskForPicture(GetMilliseconds());
dots_->SetPicture(shown->GetPicture());
}

Expand Down Expand Up @@ -540,8 +544,6 @@ ControlLink* ControlWindow::GetShownLink() {
* only for a text that shifts.
*/
void ControlWindow::ShowState() {
bool const any_ready{board_link_.IsReady() || simulator_link_.IsReady()};

/*
* Enable() and SetForegroundColour() say whether they changed anything, here
* and in ShowBytesLeft(), which the window does not need: it shows the state
Expand Down Expand Up @@ -570,6 +572,7 @@ void ControlWindow::ShowState() {
simulator_link_.IsReady()
? CONTROL_WINDOW_READY
: wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT)));
bool const any_ready{board_link_.IsReady() || simulator_link_.IsReady()};
max_matrix::Discard(controls_->Enable(any_ready));
ShowBytesLeft();
max_matrix::Discard(speed_->Enable(any_ready && shift_->GetValue()));
Expand Down
35 changes: 22 additions & 13 deletions docu/DocuPictures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ constexpr int32_t kCell{28};
constexpr int32_t kPanelHeight{max_matrix::kRowNumberOfModule * kCell};
constexpr int32_t kLed{22};
constexpr int32_t kModuleGap{28};
/* from the left edge of one module to that of the next */
constexpr int32_t kModulePitch{(max_matrix::kColumnNumberOfModule * kCell) +
kModuleGap};
constexpr int32_t kLeft{60};
constexpr int32_t kTop{86};
constexpr int32_t kRight{120};
Expand Down Expand Up @@ -170,7 +173,8 @@ 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>>;
using Picture = std::pair<std::string, std::string>;
using Pictures = std::vector<Picture>;

/*! \brief The LED of every dot, if each lit exactly one. */
struct FoundLeds {
Expand Down Expand Up @@ -208,15 +212,15 @@ LitLeds CountLit(uint8_t chip, uint8_t digit) {
*/
FoundLed FindLed() {
FoundLed found{false, DocuLed{0, 0, 0}};
uint8_t lit{0};
uint16_t lit{0};

for (size_t chip{0}; chip < mock::chain.chips.size(); chip++) {
for (uint8_t digit{0}; digit < Max7219::kNumberOfDigits; digit++) {
LitLeds const of_digit{CountLit(static_cast<uint8_t>(chip), digit)};
if (of_digit.count > 0) {
found.led = of_digit.last;
}
lit = static_cast<uint8_t>(lit + of_digit.count);
lit = static_cast<uint16_t>(lit + of_digit.count);
}
}
found.found = (lit == 1);
Expand Down Expand Up @@ -288,7 +292,7 @@ bool IsUpright(Max7219Wiring const& 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) {
for (ModuleName 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)) {
Expand Down Expand Up @@ -333,9 +337,7 @@ std::string GetModules() {
* \return The pixels from the left.
*/
constexpr int32_t GetCellX(uint8_t x) {
return kLeft +
((x / max_matrix::kColumnNumberOfModule) *
((max_matrix::kColumnNumberOfModule * kCell) + kModuleGap)) +
return kLeft + ((x / max_matrix::kColumnNumberOfModule) * kModulePitch) +
((x % max_matrix::kColumnNumberOfModule) * kCell);
}

Expand Down Expand Up @@ -686,7 +688,7 @@ Pictures DrawPictures() {
uint8_t CountUndrawn(Pictures const& pictures) {
uint8_t undrawn{0};

for (auto const& picture : pictures) {
for (Picture const& picture : pictures) {
if (picture.second.empty()) {
std::cerr << picture.first << ": a dot did not light exactly one LED\n";
undrawn++;
Expand All @@ -705,9 +707,14 @@ uint8_t CountUndrawn(Pictures const& pictures) {
uint8_t WritePictures(Pictures const& pictures, std::string const& folder) {
uint8_t failed{0};

for (auto const& picture : pictures) {
for (Picture const& picture : pictures) {
std::string const path = folder + "/" + picture.first;
if (!picture.second.empty() && !(std::ofstream{path} << picture.second)) {
if (picture.second.empty()) {
continue;
}
std::ofstream file{path};
file << picture.second;
if (!file) {
std::cerr << path << " cannot be written\n";
failed++;
}
Expand All @@ -725,7 +732,7 @@ uint8_t WritePictures(Pictures const& pictures, std::string const& folder) {
uint8_t CheckPictures(Pictures const& pictures, std::string const& folder) {
uint8_t failed{0};

for (auto const& picture : pictures) {
for (Picture const& picture : pictures) {
std::string const path = folder + "/" + picture.first;
SimulatorFileText const file{SimulatorFile::Read(path.c_str())};
if (!picture.second.empty() &&
Expand Down Expand Up @@ -757,8 +764,10 @@ int main(int argc, char** argv) { // NOLINT(misc-const-correctness): main()
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::vector<std::string> const arguments(argv, argv + argc);

if ((arguments.size() != 3) ||
((arguments[1] != "--write") && (arguments[1] != "--check"))) {
bool const mode_and_folder{arguments.size() == 3};
bool const known_mode{mode_and_folder && ((arguments[1] == "--write") ||
(arguments[1] == "--check"))};
if (!known_mode) {
std::cerr << "usage: " << arguments[0] << " --write|--check FOLDER\n";
return 2;
}
Expand Down
3 changes: 2 additions & 1 deletion simulator/Avr/SimulatorAvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ void SimulatorAvr::OnClock(avr_irq_t* irq, uint32_t value, void* object) {
max_matrix::Bit<uint8_t>::ShiftUp(simulator->bits_, 1u)};
simulator->bits_ =
max_matrix::Bit<uint8_t>::WriteBit(shifted, 0, simulator->data_in_);
if (++simulator->bit_count_ == kBitsPerByte) {
simulator->bit_count_++;
if (simulator->bit_count_ == kBitsPerByte) {
simulator->TakeByte(simulator->bits_);
simulator->bit_count_ = 0;
}
Expand Down
Loading