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
13 changes: 12 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, #130, #132 |
| 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, #132, #133 |
| 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 @@ -1246,6 +1246,17 @@ and the control program's windows say why they derive (C-8). Nothing to change f
member is `mutable`, nor for C-13, as the operators are the bitwise ones of `Byte` and the
comparisons of `Status` and `Result`.

As reviewed for the chapters Memory and Time, hardware and interrupts on 2026-11-17, their review
parts M-2, M-3, M-4, M-6 and H-1 to H-8. The library holds them: it does not block, takes its
pins as parameters, names its registers, has no interrupt routine, leaves the bitwise operators
to `Bit`, has neither exceptions nor RTTI, and what it writes itself lies in `Utility/` with its
tests. Changed: `--for` of the simulator reads its seconds with integers, where `strtod()` went
through the last `double` of the repository (H-7), which tests at the edges pin; the three texts
whose address the host code takes, the default wiring, SerialControl's `ready` and the AVR's
name, are declared `extern` in their header and defined once (M-4); and the block of `Matrix`
says what RAM it takes on the Uno, 43 bytes with bit banging and 39 with SPI, besides its buffer
(M-6).

### 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: 2 additions & 0 deletions control/ControlLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "ControlCommand.h"
#include "ControlPort.h"

char const kControlLinkReady[] = "ready";

ControlLink::ControlLink(ControlPort& port) : port_{port} {}

void ControlLink::Begin(bool wait_for_ready) {
Expand Down
5 changes: 4 additions & 1 deletion control/ControlLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

/* ControlLink Parameter */
/* what SerialControl says at the end of setup() */
constexpr char kControlLinkReady[] = "ready";
// M-4: defined once, as its address is taken. T-20: a text, which the check
// takes as one only where it is defined with its quotes.
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
extern char const kControlLinkReady[];
/* the longest line kept whole; a sketch that never ends a line is shown in
* pieces of this length */
constexpr size_t kControlLinkMaxLineLength{256u};
Expand Down
2 changes: 2 additions & 0 deletions simulator/Avr/SimulatorAvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "MaxMatrixAttributes.h"
#include "Utility/Bit.h"

char const kSimulatorAvrMcu[] = "atmega328p";

namespace {

/* the Uno's pins 10, 11 and 13 are PB2, PB3 and PB5 */
Expand Down
5 changes: 4 additions & 1 deletion simulator/Avr/SimulatorAvr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
#include "MaxMatrixAttributes.h"

/* SimulatorAvr Parameter */
constexpr char kSimulatorAvrMcu[] = "atmega328p";
// M-4: defined once, as its address is taken. T-20: a text, which the check
// takes as one only where it is defined with its quotes.
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
extern char const kSimulatorAvrMcu[];
constexpr uint32_t kSimulatorAvrFrequency{16000000u};
constexpr uint32_t kSimulatorAvrCyclesPerMillisecond{kSimulatorAvrFrequency /
1000u};
Expand Down
60 changes: 46 additions & 14 deletions simulator/SimulatorOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,36 @@

#include "SimulatorOptions.h"

#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include "SimulatorPanel.h"

namespace {

/* the milliseconds of a second, and the digits of a fraction that say them */
constexpr uint64_t kMillisecondsPerSecond{1000u};
constexpr size_t kMillisecondDigits{3u};
/* the digits of the most whole seconds whose milliseconds a uint32_t holds,
* 4294967, so that a longer number is refused before it is read */
constexpr size_t kMaxSecondDigits{7u};

// Whether `text` is decimal digits and nothing else; an empty one is.
bool IsDigits(std::string const& text) {
return std::all_of(text.begin(), text.end(), [](char c) {
return isdigit(static_cast<unsigned char>(c)) != 0;
});
}

} // namespace

SimulatorOptions::SimulatorOptions()
: wiring_name_(kSimulatorPanelDefaultWiring) {}

Expand Down Expand Up @@ -124,25 +145,36 @@ SimulatorOptions::Converted<uint16_t> SimulatorOptions::ConvertToPort(
}

/*
* Converts the value of --for, the seconds as a decimal number, fractions
* included, and nothing else, into milliseconds. They are not valid if the
* value is no number, or not a single millisecond, or more than the
* milliseconds fit.
* Converts the value of --for, the seconds as decimal digits with a fraction
* after a point, `1.5` or `.5`, into milliseconds; digits of the fraction past
* the third are dropped. They are not valid if the value is anything else, or
* not a single millisecond, or more than the milliseconds fit.
*
* atof(), as before, took "2x" as two seconds, and a negative or a huge number
* reached a cast to uint32_t that is undefined for it.
* reached a cast to uint32_t that is undefined for it; strtod() after it took
* those apart, but went through a double (H-7).
*/
SimulatorOptions::Converted<uint32_t> SimulatorOptions::ConvertToRunFor(
char const* text) {
char* end{nullptr};
double const milliseconds{strtod(text, &end) * 1000};

/* asked as whether it lies in the range, so that NaN, which compares false
* with everything, is refused as well */
bool const in_range{(milliseconds >= 1) &&
(milliseconds <= kSimulatorOptionsMaxRunFor)};
if ((end == text) || (*end != '\0') || !in_range) {
std::string const value{text};
size_t const point{value.find('.')};
bool const has_point{point != std::string::npos};
std::string const whole{value.substr(0, point)};
std::string const fraction{has_point ? value.substr(point + 1)
: std::string{}};
bool const digits_only{IsDigits(whole) && IsDigits(fraction)};
bool const has_digit{!whole.empty() || !fraction.empty()};

if (!digits_only || !has_digit || (whole.size() > kMaxSecondDigits)) {
return Converted<uint32_t>{false, 0};
}
return Converted<uint32_t>{true, static_cast<uint32_t>(milliseconds)};
uint64_t const seconds{whole.empty() ? 0u
: strtoull(whole.c_str(), nullptr, 10)};
std::string const thousandths{
(fraction + "000").substr(0, kMillisecondDigits)};
uint64_t const milliseconds{(seconds * kMillisecondsPerSecond) +
strtoull(thousandths.c_str(), nullptr, 10)};
bool const in_range{(milliseconds >= 1) &&
(milliseconds <= kSimulatorOptionsMaxRunFor)};
return Converted<uint32_t>{in_range, static_cast<uint32_t>(milliseconds)};
}
2 changes: 2 additions & 0 deletions simulator/SimulatorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "Max7219.h"
#include "MaxMatrixConfig.h"

char const kSimulatorPanelDefaultWiring[] = "FC16";

/* the four wirings MD_MAX72XX has names for, beside DR0CR0RR0 ... DR1CR1RR1
* from kMax7219Wirings */
namespace {
Expand Down
5 changes: 4 additions & 1 deletion simulator/SimulatorPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#include "MaxMatrixConfig.h"

/* SimulatorPanel Parameter */
constexpr char kSimulatorPanelDefaultWiring[] = "FC16";
// M-4: defined once, as its address is taken. T-20: a text, which the check
// takes as one only where it is defined with its quotes.
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
extern char const kSimulatorPanelDefaultWiring[];
/* about 30 pictures a second; neither a terminal nor a page keeps up with every
* frame */
constexpr uint32_t kSimulatorPanelRedrawInterval{33u};
Expand Down
3 changes: 3 additions & 0 deletions src/MaxMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ enum class State : uint8_t {
* `loop()` and takes a step only when the shift interval has passed since the
* last one, so that no sketch has to time it.
*
* A Matrix takes 43 bytes of RAM on the Uno with bit banging and 39 with SPI,
* besides its buffer of eight bytes per module: a MatrixOf<4> takes 75 and 71.
*
* \note With `MAXMATRIX_DEV_ERROR_DETECT` at `STD_ON`, every function here that
* fails reports its name and the error to ReportError().
*/
Expand Down
22 changes: 22 additions & 0 deletions test/SimulatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ TEST(SimulatorTest, RunForIsTakenInMilliseconds) {
EXPECT_EQ(options.GetRunFor(), 1500u);
}

/* the seconds as far as a millisecond, a fraction past it dropped, up to the
* most milliseconds a uint32_t holds; as atof() read them, which went for the
* double the coding style forbids (H-7) */
TEST(SimulatorTest, RunForTakesTheMillisecondsOfAFraction) {
SimulatorOptions options;

ASSERT_TRUE(Parse(options, {"--for", "0.001"}));
EXPECT_EQ(options.GetRunFor(), 1u);
ASSERT_TRUE(Parse(options, {"--for", ".5"}));
EXPECT_EQ(options.GetRunFor(), 500u);
ASSERT_TRUE(Parse(options, {"--for", "2."}));
EXPECT_EQ(options.GetRunFor(), 2000u);
ASSERT_TRUE(Parse(options, {"--for", "1.2349"}));
EXPECT_EQ(options.GetRunFor(), 1234u);
ASSERT_TRUE(Parse(options, {"--for", "4294967.295"}));
EXPECT_EQ(options.GetRunFor(), 4294967295u);
EXPECT_FALSE(Parse(options, {"--for", "4294967.296"}));
EXPECT_FALSE(Parse(options, {"--for", "0.0009"}));
EXPECT_FALSE(Parse(options, {"--for", "."}));
EXPECT_FALSE(Parse(options, {"--for", "1.2.3"}));
}

/* Bug: atof() took --for 2x as two seconds, and turned -1 and 1e10 into a value
* a cast to uint32_t is undefined for. */
TEST(SimulatorTest, RunForThatIsNoDurationIsRefused) {
Expand Down