diff --git a/OIL.md b/OIL.md index 031262b..d3ea620 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, #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 | @@ -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, diff --git a/control/ControlLink.cpp b/control/ControlLink.cpp index c62f37c..0e8dfbb 100644 --- a/control/ControlLink.cpp +++ b/control/ControlLink.cpp @@ -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) { diff --git a/control/ControlLink.h b/control/ControlLink.h index f088991..353482b 100644 --- a/control/ControlLink.h +++ b/control/ControlLink.h @@ -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}; diff --git a/simulator/Avr/SimulatorAvr.cpp b/simulator/Avr/SimulatorAvr.cpp index 83c5d45..ada8554 100644 --- a/simulator/Avr/SimulatorAvr.cpp +++ b/simulator/Avr/SimulatorAvr.cpp @@ -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 */ diff --git a/simulator/Avr/SimulatorAvr.h b/simulator/Avr/SimulatorAvr.h index 90b39bd..e756eaf 100644 --- a/simulator/Avr/SimulatorAvr.h +++ b/simulator/Avr/SimulatorAvr.h @@ -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}; diff --git a/simulator/SimulatorOptions.cpp b/simulator/SimulatorOptions.cpp index a691155..16d6bf5 100644 --- a/simulator/SimulatorOptions.cpp +++ b/simulator/SimulatorOptions.cpp @@ -11,15 +11,36 @@ #include "SimulatorOptions.h" +#include +#include #include #include +#include #include #include #include #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(c)) != 0; + }); +} + +} // namespace + SimulatorOptions::SimulatorOptions() : wiring_name_(kSimulatorPanelDefaultWiring) {} @@ -124,25 +145,36 @@ SimulatorOptions::Converted 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 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{false, 0}; } - return Converted{true, static_cast(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{in_range, static_cast(milliseconds)}; } diff --git a/simulator/SimulatorPanel.cpp b/simulator/SimulatorPanel.cpp index bfca3f3..59366d2 100644 --- a/simulator/SimulatorPanel.cpp +++ b/simulator/SimulatorPanel.cpp @@ -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 { diff --git a/simulator/SimulatorPanel.h b/simulator/SimulatorPanel.h index 2c44758..568119b 100644 --- a/simulator/SimulatorPanel.h +++ b/simulator/SimulatorPanel.h @@ -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}; diff --git a/src/MaxMatrix.h b/src/MaxMatrix.h index 638a2b5..ad76c31 100644 --- a/src/MaxMatrix.h +++ b/src/MaxMatrix.h @@ -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(). */ diff --git a/test/SimulatorTest.cpp b/test/SimulatorTest.cpp index 02b2bae..5ba4095 100644 --- a/test/SimulatorTest.cpp +++ b/test/SimulatorTest.cpp @@ -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) {