diff --git a/OIL.md b/OIL.md index 2e648bd..031262b 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 | +| 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 | | 42 | A newer avr-gcc as a cross-check | 41 | done | | | 43 | Bit operations in one class, from the template | 41 | done | #109 | @@ -1233,6 +1233,19 @@ as `lround()` did, which takes its `double` (H-7). Left for the compiler that is container: the Windows half of the serial port declares some variables ahead, which only got their initializers here. +As reviewed for the chapter Classes on 2026-11-17, its review parts C-2, C-3, C-6, C-8, C-11 and +C-13, in the library and the host code. Every class but those of `src/Utility/` began with its +private members; all are in the order of C-2 now, public, protected, private, and in each types +and constants, constructors, functions, data members, the text moved and not changed. A class +that declares a special member declares all six (C-3): the moves are deleted where the copies +were, and `Matrix` and `Batch` delete their default constructor; `SimulatorWebHost`, which held +sockets without a destructor and so kept its port taken until the program ended, closes them +now, which a test checks. The virtual functions of `ControlPort` give a reason C-6 takes, the +cost that does not count on a PC and one class for both kinds of port, not a test's; `MatrixOf` +and the control program's windows say why they derive (C-8). Nothing to change for C-11, as no +member is `mutable`, nor for C-13, as the operators are the bitwise ones of `Byte` and the +comparisons of `Status` and `Result`. + ### 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/ControlDotPanel.h b/control/ControlDotPanel.h index 58aab40..37a1e08 100644 --- a/control/ControlDotPanel.h +++ b/control/ControlDotPanel.h @@ -30,37 +30,10 @@ * simulator's page draws them. What it shows is the last picture it was given, * from SerialControl's answer to `picture`; a click or Clear() changes it at * once, so that the click shows before the next picture does. + * + * Derived from wxPanel, as wxWidgets makes a control of its own (C-8). */ class ControlDotPanel : public wxPanel { - private: - /* 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; - }; - - /* one byte per column, bit 0 the top row, as SerialControl answers picture */ - std::vector columns_; - std::function on_switch_; - /* while the button is held: the state every LED passed takes, and the LED the - * mouse was on last, if it was on one */ - bool painting_{false}; - bool paint_lit_{false}; - bool on_dot_{false}; - uint8_t last_column_{0}; - uint8_t last_row_{0}; - - void OnPaint(wxPaintEvent& event); - void OnLeftDown(wxMouseEvent& event); - void OnMotion(wxMouseEvent& event); - void OnLeftUp(wxMouseEvent& event); - 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 point) const; - public: /*! * \brief Makes the panel with every LED off. @@ -84,6 +57,35 @@ class ControlDotPanel : public wxPanel { /*! \brief Turns every LED off. */ void Clear(); + + private: + /* 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; + }; + + void OnPaint(wxPaintEvent& event); + void OnLeftDown(wxMouseEvent& event); + void OnMotion(wxMouseEvent& event); + void OnLeftUp(wxMouseEvent& event); + 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 point) const; + + /* one byte per column, bit 0 the top row, as SerialControl answers picture */ + std::vector columns_; + std::function on_switch_; + /* while the button is held: the state every LED passed takes, and the LED the + * mouse was on last, if it was on one */ + bool painting_{false}; + bool paint_lit_{false}; + bool on_dot_{false}; + uint8_t last_column_{0}; + uint8_t last_row_{0}; }; #endif // MAXMATRIX_CONTROL_CONTROLDOTPANEL_H_ diff --git a/control/ControlLink.h b/control/ControlLink.h index 327dbc1..f088991 100644 --- a/control/ControlLink.h +++ b/control/ControlLink.h @@ -57,26 +57,6 @@ struct ControlLinkLines { * passed on as a line. */ class ControlLink { - private: - std::reference_wrapper port_; - bool ready_{false}; - /* what has arrived after the last line feed */ - std::string received_; - std::vector picture_; - /* - * Whether an answer to picture is due, and since when; picture_asked_at_ - * holds a time only once the picture has been asked for since Begin(), so - * that the first time it is at once. SerialControl answers in order, so an - * answer to a picture asked for before a command shows the matrix before it, - * and is dropped as stale. - */ - bool picture_asked_{false}; - bool picture_stale_{false}; - bool picture_timed_{false}; - uint32_t picture_asked_at_{0}; - - MAXMATRIX_NODISCARD bool Take(std::string const& line); - public: /*! * \brief Makes a link over `port`, which is not ready until Begin(). @@ -139,6 +119,26 @@ class ControlLink { * \return The lines that have arrived, and whether the port is still open. */ MAXMATRIX_NODISCARD ControlLinkLines Task(); + + private: + MAXMATRIX_NODISCARD bool Take(std::string const& line); + + std::reference_wrapper port_; + bool ready_{false}; + /* what has arrived after the last line feed */ + std::string received_; + std::vector picture_; + /* + * Whether an answer to picture is due, and since when; picture_asked_at_ + * holds a time only once the picture has been asked for since Begin(), so + * that the first time it is at once. SerialControl answers in order, so an + * answer to a picture asked for before a command shows the matrix before it, + * and is dropped as stale. + */ + bool picture_asked_{false}; + bool picture_stale_{false}; + bool picture_timed_{false}; + uint32_t picture_asked_at_{0}; }; #endif // MAXMATRIX_CONTROL_CONTROLLINK_H_ diff --git a/control/ControlPort.h b/control/ControlPort.h index 36c847d..8dc2df8 100644 --- a/control/ControlPort.h +++ b/control/ControlPort.h @@ -22,9 +22,10 @@ * ControlLink needs to know neither. How a port is opened differs, so each kind * has an Open() of its own. * - * Virtual functions, which the library avoids for the RAM of their tables on - * the Uno: the program runs on a PC, and a test hands ControlLink a port of its - * own. + * C-6, virtual functions, which the library avoids for the RAM of their tables + * on the Uno and for calls that cannot be inlined: the program runs on a PC, + * where neither counts, and the window holds a link over each kind of port, as + * one class rather than a template made twice of the same code. */ class ControlPort { public: diff --git a/control/ControlSerialPort.h b/control/ControlSerialPort.h index 7a87550..db78930 100644 --- a/control/ControlSerialPort.h +++ b/control/ControlSerialPort.h @@ -30,15 +30,6 @@ * say `ready`. */ class ControlSerialPort final : public ControlPort { - private: -#ifdef _WIN32 - void* handle_; -#else - int descriptor_{-1}; - - MAXMATRIX_NODISCARD bool Configure(uint32_t baud_rate); -#endif - public: /*! \brief Makes a port that is closed until Open(). */ ControlSerialPort(); @@ -49,6 +40,9 @@ class ControlSerialPort final : public ControlPort { // A copy would close the port of the original a second time. ControlSerialPort(ControlSerialPort const&) = delete; ControlSerialPort& operator=(ControlSerialPort const&) = delete; + // Nor a move, which nothing needs. + ControlSerialPort(ControlSerialPort&&) = delete; + ControlSerialPort& operator=(ControlSerialPort&&) = delete; /*! * \brief Opens the port `name` at `baud_rate`. @@ -78,6 +72,15 @@ class ControlSerialPort final : public ControlPort { bool Write(std::string const& data) override; bool Read(std::string& data) override; void Close() override; + + private: +#ifdef _WIN32 + void* handle_; +#else + int descriptor_{-1}; + + MAXMATRIX_NODISCARD bool Configure(uint32_t baud_rate); +#endif }; #endif // MAXMATRIX_CONTROL_CONTROLSERIALPORT_H_ diff --git a/control/ControlTcpPort.h b/control/ControlTcpPort.h index 91821bf..1b0d458 100644 --- a/control/ControlTcpPort.h +++ b/control/ControlTcpPort.h @@ -26,11 +26,6 @@ * as an end that is gone. */ class ControlTcpPort final : public ControlPort { - private: - // A POSIX descriptor or a Winsock SOCKET, which is an unsigned integer as - // wide as a pointer; -1 for none, which is what INVALID_SOCKET is as well. - intptr_t socket_; - public: /*! \brief Makes a port that is closed until Open(). */ ControlTcpPort(); @@ -41,6 +36,9 @@ class ControlTcpPort final : public ControlPort { // A copy would close the socket of the original a second time. ControlTcpPort(ControlTcpPort const&) = delete; ControlTcpPort& operator=(ControlTcpPort const&) = delete; + // Nor a move, which nothing needs. + ControlTcpPort(ControlTcpPort&&) = delete; + ControlTcpPort& operator=(ControlTcpPort&&) = delete; /*! * \brief Connects to the simulator on localhost. @@ -56,6 +54,11 @@ class ControlTcpPort final : public ControlPort { bool Write(std::string const& data) override; bool Read(std::string& data) override; void Close() override; + + private: + // A POSIX descriptor or a Winsock SOCKET, which is an unsigned integer as + // wide as a pointer; -1 for none, which is what INVALID_SOCKET is as well. + intptr_t socket_; }; #endif // MAXMATRIX_CONTROL_CONTROLTCPPORT_H_ diff --git a/control/ControlWindow.h b/control/ControlWindow.h index d8a5685..5ee80b9 100644 --- a/control/ControlWindow.h +++ b/control/ControlWindow.h @@ -48,34 +48,15 @@ class ControlDotPanel; * that the two agree. * * It holds no logic a test would need: the lines are ControlCommand's, the - * connections ControlLink's, so the window itself is checked by hand. + * connections ControlLink's, so the window itself is checked by hand. Derived + * from wxFrame, as wxWidgets makes a window of its own (C-8). */ class ControlWindow : public wxFrame { - private: - ControlSerialPort serial_port_; - ControlTcpPort tcp_port_; - ControlLink board_link_; - ControlLink simulator_link_; - wxTimer timer_; - std::string last_setting_; - - wxWindow* controls_; - wxTextCtrl* text_{}; - wxStaticText* bytes_left_{}; - wxButton* send_{}; - wxCheckBox* shift_{}; - wxSpinCtrl* space_{}; - wxSlider* intensity_{}; - wxSlider* speed_{}; - ControlDotPanel* dots_{}; - wxComboBox* serial_port_name_{}; - wxButton* board_button_{}; - wxStaticText* board_status_{}; - wxSpinCtrl* tcp_port_number_{}; - wxButton* simulator_button_{}; - wxStaticText* simulator_status_{}; - wxTextCtrl* log_{}; + public: + /*! \brief Makes the window, with nothing connected. */ + ControlWindow(); + private: void MakeMenu(); MAXMATRIX_NODISCARD wxWindow* MakeMatrix(wxWindow* parent); MAXMATRIX_NODISCARD wxWindow* MakeText(wxWindow* parent); @@ -106,9 +87,29 @@ class ControlWindow : public wxFrame { void Log(wxString const& text); MAXMATRIX_NODISCARD static wxString ConvertToText(std::string const& line); - public: - /*! \brief Makes the window, with nothing connected. */ - ControlWindow(); + ControlSerialPort serial_port_; + ControlTcpPort tcp_port_; + ControlLink board_link_; + ControlLink simulator_link_; + wxTimer timer_; + std::string last_setting_; + + wxWindow* controls_; + wxTextCtrl* text_{}; + wxStaticText* bytes_left_{}; + wxButton* send_{}; + wxCheckBox* shift_{}; + wxSpinCtrl* space_{}; + wxSlider* intensity_{}; + wxSlider* speed_{}; + ControlDotPanel* dots_{}; + wxComboBox* serial_port_name_{}; + wxButton* board_button_{}; + wxStaticText* board_status_{}; + wxSpinCtrl* tcp_port_number_{}; + wxButton* simulator_button_{}; + wxStaticText* simulator_status_{}; + wxTextCtrl* log_{}; }; #endif // MAXMATRIX_CONTROL_CONTROLWINDOW_H_ diff --git a/docu/DocuSvg.h b/docu/DocuSvg.h index 0164945..5da2e52 100644 --- a/docu/DocuSvg.h +++ b/docu/DocuSvg.h @@ -50,12 +50,6 @@ struct DocuFont { /*! \brief A picture as SVG text, for the pictures in docu/. */ class DocuSvg { - private: - uint16_t width_; - uint16_t height_; - int32_t top_; - std::ostringstream body_; - public: /*! * \brief Makes an empty picture with a white background. @@ -109,6 +103,12 @@ class DocuSvg { */ void AddArrow(DocuPoint from, int32_t to_x, int32_t thickness, char const* colour); + + private: + uint16_t width_; + uint16_t height_; + int32_t top_; + std::ostringstream body_; }; #endif // MAXMATRIX_DOCU_DOCUSVG_H_ diff --git a/simulator/Avr/SimulatorAvr.h b/simulator/Avr/SimulatorAvr.h index bb78ccd..90b39bd 100644 --- a/simulator/Avr/SimulatorAvr.h +++ b/simulator/Avr/SimulatorAvr.h @@ -36,39 +36,6 @@ constexpr uint32_t kSimulatorAvrCyclesPerMillisecond{kSimulatorAvrFrequency / * time after the other. */ class SimulatorAvr { - private: - avr_t* avr_{nullptr}; - elf_firmware_t firmware_{}; - bool data_in_{false}; - bool clock_{false}; - // high, as the pull-up of a real module keeps it until Init() drives the pin - bool chip_select_{true}; - // The bits of the byte on the wire so far, for the frame the page shows; the - // model has them one by one already. - uint8_t bits_{0}; - uint8_t bit_count_{0}; - // simavr's receive buffer has room. - bool receive_ready_{false}; - - void ReceiveSerial(); - 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 - * pins are followed rather than the port registers: digitalWrite() and - * shiftOut() write the port bit by bit, and only a change is a step on the - * wire. - */ - void ConnectPins(); - void ConnectUart(); - static void OnDataIn(avr_irq_t* irq, uint32_t value, void* object); - static void OnClock(avr_irq_t* irq, uint32_t value, void* object); - static void OnChipSelect(avr_irq_t* irq, uint32_t value, void* object); - static void OnSpiByte(avr_irq_t* irq, uint32_t value, void* object); - static void OnSerialByte(avr_irq_t* irq, uint32_t value, void* object); - static void OnReceiveReady(avr_irq_t* irq, uint32_t value, void* object); - static void OnReceiveFull(avr_irq_t* irq, uint32_t value, void* object); - public: /*! \brief Makes a simulator without an AVR; Load() makes one. */ SimulatorAvr(); @@ -87,6 +54,12 @@ class SimulatorAvr { /*! \brief Not assignable, as it is not copyable. */ SimulatorAvr& operator=(SimulatorAvr const&) = delete; + /*! \brief Not movable: simavr calls back with the object's address. */ + SimulatorAvr(SimulatorAvr&&) = delete; + + /*! \brief Not movable, as it is not copyable. */ + SimulatorAvr& operator=(SimulatorAvr&&) = delete; + /*! * \brief Returns the cycles the AVR runs in `milliseconds`, at its 16 MHz. * @@ -129,6 +102,39 @@ class SimulatorAvr { * \pre Load() has succeeded. */ bool Run(uint32_t milliseconds); + + private: + void ReceiveSerial(); + 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 + * pins are followed rather than the port registers: digitalWrite() and + * shiftOut() write the port bit by bit, and only a change is a step on the + * wire. + */ + void ConnectPins(); + void ConnectUart(); + static void OnDataIn(avr_irq_t* irq, uint32_t value, void* object); + static void OnClock(avr_irq_t* irq, uint32_t value, void* object); + static void OnChipSelect(avr_irq_t* irq, uint32_t value, void* object); + static void OnSpiByte(avr_irq_t* irq, uint32_t value, void* object); + static void OnSerialByte(avr_irq_t* irq, uint32_t value, void* object); + static void OnReceiveReady(avr_irq_t* irq, uint32_t value, void* object); + static void OnReceiveFull(avr_irq_t* irq, uint32_t value, void* object); + + avr_t* avr_{nullptr}; + elf_firmware_t firmware_{}; + bool data_in_{false}; + bool clock_{false}; + // high, as the pull-up of a real module keeps it until Init() drives the pin + bool chip_select_{true}; + // The bits of the byte on the wire so far, for the frame the page shows; the + // model has them one by one already. + uint8_t bits_{0}; + uint8_t bit_count_{0}; + // simavr's receive buffer has room. + bool receive_ready_{false}; }; #endif // MAXMATRIX_SIMULATOR_AVR_SIMULATORAVR_H_ diff --git a/simulator/Host/SimulatorSerialHost.h b/simulator/Host/SimulatorSerialHost.h index 76c8091..658820b 100644 --- a/simulator/Host/SimulatorSerialHost.h +++ b/simulator/Host/SimulatorSerialHost.h @@ -30,13 +30,6 @@ * simulator's tick, as the web host is. */ class SimulatorSerialHost { - private: - int listener_; - int client_; - - void AcceptClient(); - void DropClient(); - public: /*! \brief Makes a host that listens nowhere until Begin(). */ SimulatorSerialHost(); @@ -47,6 +40,9 @@ class SimulatorSerialHost { // A copy would close the sockets of the original a second time. SimulatorSerialHost(SimulatorSerialHost const&) = delete; SimulatorSerialHost& operator=(SimulatorSerialHost const&) = delete; + // Nor a move, which nothing needs. + SimulatorSerialHost(SimulatorSerialHost&&) = delete; + SimulatorSerialHost& operator=(SimulatorSerialHost&&) = delete; /*! * \brief Opens the port on localhost and starts listening. @@ -77,6 +73,13 @@ class SimulatorSerialHost { * \param[in] text The bytes, as the sketch printed them. */ void Send(std::string const& text); + + private: + void AcceptClient(); + void DropClient(); + + int listener_; + int client_; }; #endif // MAXMATRIX_SIMULATOR_HOST_SIMULATORSERIALHOST_H_ diff --git a/simulator/Host/SimulatorWebHost.cpp b/simulator/Host/SimulatorWebHost.cpp index 38febf0..ab9a670 100644 --- a/simulator/Host/SimulatorWebHost.cpp +++ b/simulator/Host/SimulatorWebHost.cpp @@ -44,6 +44,17 @@ SimulatorWebHost::SimulatorWebHost() : listener_{kSimulatorSocketNone} { } } +SimulatorWebHost::~SimulatorWebHost() { + for (int const client : clients_) { + if (client != kSimulatorSocketNone) { + SimulatorSocket::Close(client); + } + } + if (listener_ != kSimulatorSocketNone) { + SimulatorSocket::Close(listener_); + } +} + bool SimulatorWebHost::Begin(uint16_t port, char const* page_path) { page_path_ = page_path; listener_ = SimulatorSocket::OpenListener(port); diff --git a/simulator/Host/SimulatorWebHost.h b/simulator/Host/SimulatorWebHost.h index f6924c1..17afc26 100644 --- a/simulator/Host/SimulatorWebHost.h +++ b/simulator/Host/SimulatorWebHost.h @@ -33,29 +33,19 @@ constexpr uint8_t kSimulatorWebHostMaxClients{4u}; * sketch. */ class SimulatorWebHost { - private: - int listener_; - std::array clients_{}; - std::array pending_; - char const* page_path_{nullptr}; - - void AcceptClients(); - MAXMATRIX_NODISCARD std::vector ReadClient(uint8_t slot); - MAXMATRIX_NODISCARD bool AnswerRequest(int socket, - std::string const& request); - void SendPage(int socket); - MAXMATRIX_NODISCARD uint8_t FindFreeSlot() const; - void DropClient(uint8_t slot); - MAXMATRIX_NODISCARD static std::string ReadRequest(int socket); - MAXMATRIX_NODISCARD static SimulatorFoundText GetPath( - std::string const& request); - static void SendResponse(int socket, char const* status, - char const* content_type, std::string const& body); - public: /*! \brief Makes a host that listens nowhere until Begin(). */ SimulatorWebHost(); + /*! \brief Closes every WebSocket and the port. */ + ~SimulatorWebHost(); + + // A copy would close the sockets of the original a second time. + SimulatorWebHost(SimulatorWebHost const&) = delete; + SimulatorWebHost& operator=(SimulatorWebHost const&) = delete; + SimulatorWebHost(SimulatorWebHost&&) = delete; + SimulatorWebHost& operator=(SimulatorWebHost&&) = delete; + /*! * \brief Opens the port on localhost and starts listening. * @@ -88,6 +78,25 @@ class SimulatorWebHost { * \param[in] text The message. */ void Broadcast(std::string const& text); + + private: + void AcceptClients(); + MAXMATRIX_NODISCARD std::vector ReadClient(uint8_t slot); + MAXMATRIX_NODISCARD bool AnswerRequest(int socket, + std::string const& request); + void SendPage(int socket); + MAXMATRIX_NODISCARD uint8_t FindFreeSlot() const; + void DropClient(uint8_t slot); + MAXMATRIX_NODISCARD static std::string ReadRequest(int socket); + MAXMATRIX_NODISCARD static SimulatorFoundText GetPath( + std::string const& request); + static void SendResponse(int socket, char const* status, + char const* content_type, std::string const& body); + + int listener_; + std::array clients_{}; + std::array pending_; + char const* page_path_{nullptr}; }; #endif // MAXMATRIX_SIMULATOR_HOST_SIMULATORWEBHOST_H_ diff --git a/simulator/Host/SimulatorWebSocket.h b/simulator/Host/SimulatorWebSocket.h index e0fa18e..a5a0541 100644 --- a/simulator/Host/SimulatorWebSocket.h +++ b/simulator/Host/SimulatorWebSocket.h @@ -52,20 +52,6 @@ struct SimulatorWebSocketFrame { * max_matrix::Font. */ class SimulatorWebSocket { - private: - /* the five words of the hash while SHA-1 runs, FIPS 180-4 */ - static constexpr uint8_t kSha1StateWords{5}; - using Sha1State = std::array; - - MAXMATRIX_NODISCARD static SimulatorFoundText GetKey( - std::string const& request); - MAXMATRIX_NODISCARD static std::string PadSha1Message( - std::string const& message); - MAXMATRIX_NODISCARD static Sha1State ProcessSha1Block( - std::string const& padded, size_t offset, Sha1State state); - MAXMATRIX_NODISCARD static uint32_t MixSha1(uint8_t round, uint32_t b, - uint32_t c, uint32_t d); - public: /*! * \brief Builds the answer to a browser's request to open a WebSocket. @@ -135,6 +121,20 @@ class SimulatorWebSocket { */ MAXMATRIX_NODISCARD static std::string ConvertToBase64( std::string const& bytes); + + private: + /* the five words of the hash while SHA-1 runs, FIPS 180-4 */ + static constexpr uint8_t kSha1StateWords{5}; + using Sha1State = std::array; + + MAXMATRIX_NODISCARD static SimulatorFoundText GetKey( + std::string const& request); + MAXMATRIX_NODISCARD static std::string PadSha1Message( + std::string const& message); + MAXMATRIX_NODISCARD static Sha1State ProcessSha1Block( + std::string const& padded, size_t offset, Sha1State state); + MAXMATRIX_NODISCARD static uint32_t MixSha1(uint8_t round, uint32_t b, + uint32_t c, uint32_t d); }; #endif // MAXMATRIX_SIMULATOR_HOST_SIMULATORWEBSOCKET_H_ diff --git a/simulator/SimulatorFrontEnd.h b/simulator/SimulatorFrontEnd.h index 72ee69f..67a2def 100644 --- a/simulator/SimulatorFrontEnd.h +++ b/simulator/SimulatorFrontEnd.h @@ -27,12 +27,6 @@ * page as well, and with `--tcp` to the control program. */ class SimulatorFrontEnd { - private: - SimulatorWeb web_; - SimulatorSerialHost serial_host_; - bool web_on_{false}; - bool terminal_on_{false}; - public: /*! \brief Makes a front end that shows nothing until Begin(). */ SimulatorFrontEnd(); @@ -62,6 +56,12 @@ class SimulatorFrontEnd { * \param[in] now The time of the sketch, in milliseconds. */ void Follow(SimulatorPanel& panel, uint32_t now); + + private: + SimulatorWeb web_; + SimulatorSerialHost serial_host_; + bool web_on_{false}; + bool terminal_on_{false}; }; #endif // MAXMATRIX_SIMULATOR_SIMULATORFRONTEND_H_ diff --git a/simulator/SimulatorOptions.h b/simulator/SimulatorOptions.h index 0370731..6348ebb 100644 --- a/simulator/SimulatorOptions.h +++ b/simulator/SimulatorOptions.h @@ -33,40 +33,6 @@ constexpr uint32_t kSimulatorOptionsMaxRunFor{UINT32_MAX}; /* milliseconds */ * sketch is built into refuses it. */ class SimulatorOptions { - private: - std::string wiring_name_; - bool upside_down_{false}; - bool web_{false}; - uint16_t port_{kSimulatorOptionsDefaultPort}; - uint16_t tcp_port_{0}; - uint32_t run_for_{0}; - char const* expect_file_{nullptr}; - char const* elf_file_{nullptr}; - std::string serial_input_; - - /* a number read from the command line, and whether it is one the option - * takes */ - template - struct Converted { - bool valid; - Number value; - }; - - bool ParseOption(std::vector const& arguments, size_t& index); - bool ParseValue(std::string const& option, char const* value); - MAXMATRIX_NODISCARD static Converted ConvertToPort( - char const* text); - MAXMATRIX_NODISCARD static Converted ConvertToRunFor( - char const* text); - // Keeps `converted` in `option` if it is valid, and says whether it is. - template - static bool Keep(Converted converted, Number& option) { - if (converted.valid) { - option = converted.value; - } - return converted.valid; - } - public: /*! * \brief Makes the options a simulator has without any on its command line. @@ -134,6 +100,40 @@ class SimulatorOptions { * \param[in] program The program's name, `argv[0]`. */ static void PrintUsage(char const* program); + + private: + /* a number read from the command line, and whether it is one the option + * takes */ + template + struct Converted { + bool valid; + Number value; + }; + + bool ParseOption(std::vector const& arguments, size_t& index); + bool ParseValue(std::string const& option, char const* value); + MAXMATRIX_NODISCARD static Converted ConvertToPort( + char const* text); + MAXMATRIX_NODISCARD static Converted ConvertToRunFor( + char const* text); + // Keeps `converted` in `option` if it is valid, and says whether it is. + template + static bool Keep(Converted converted, Number& option) { + if (converted.valid) { + option = converted.value; + } + return converted.valid; + } + + std::string wiring_name_; + bool upside_down_{false}; + bool web_{false}; + uint16_t port_{kSimulatorOptionsDefaultPort}; + uint16_t tcp_port_{0}; + uint32_t run_for_{0}; + char const* expect_file_{nullptr}; + char const* elf_file_{nullptr}; + std::string serial_input_; }; #endif // MAXMATRIX_SIMULATOR_SIMULATOROPTIONS_H_ diff --git a/simulator/SimulatorPanel.h b/simulator/SimulatorPanel.h index d94d4d7..2c44758 100644 --- a/simulator/SimulatorPanel.h +++ b/simulator/SimulatorPanel.h @@ -35,17 +35,6 @@ constexpr uint32_t kSimulatorPanelRedrawInterval{33u}; * depends on what changed on it: a frame, the wiring or the mounting. */ class SimulatorPanel { - private: - std::string wiring_name_; - std::reference_wrapper wiring_; - bool upside_down_{false}; - int32_t drawn_frames_{-1}; - uint32_t last_draw_{0}; - bool draw_requested_{true}; - - MAXMATRIX_NODISCARD static Max7219Wiring const* FindWiring( - std::string const& name); - public: /*! \brief Makes a panel of the default wiring, mounted upright, that has * never been drawn. */ @@ -141,6 +130,17 @@ class SimulatorPanel { * \param[in] now The time, as `millis()` gives it. */ void SetDrawn(uint32_t now); + + private: + MAXMATRIX_NODISCARD static Max7219Wiring const* FindWiring( + std::string const& name); + + std::string wiring_name_; + std::reference_wrapper wiring_; + bool upside_down_{false}; + int32_t drawn_frames_{-1}; + uint32_t last_draw_{0}; + bool draw_requested_{true}; }; #endif // MAXMATRIX_SIMULATOR_SIMULATORPANEL_H_ diff --git a/simulator/Web/SimulatorWeb.h b/simulator/Web/SimulatorWeb.h index 16e7525..9abb31d 100644 --- a/simulator/Web/SimulatorWeb.h +++ b/simulator/Web/SimulatorWeb.h @@ -31,26 +31,6 @@ constexpr size_t kSimulatorWebFrameLogLength{16u}; * Every function that needs the panel is handed it. */ class SimulatorWeb { - private: - // Every function that needs the panel is handed it, as - // max_matrix::internal::Text is, rather than the class keeping a reference: - // the panel belongs to the simulator, and the terminal is handed it too. - - SimulatorWebHost host_; - /* the frames since the page was last sent the panel, as hex pairs, one string - * per frame */ - std::vector frame_log_; - int32_t logged_frames_{0}; - /* what the sketch printed since the page was last sent the panel */ - std::string serial_output_; - - void LogFrame(); - static void Command(SimulatorPanel& panel, std::string const& message); - MAXMATRIX_NODISCARD std::string GetMessage(SimulatorPanel const& panel) const; - MAXMATRIX_NODISCARD std::string GetLog() const; - MAXMATRIX_NODISCARD static std::string GetIntensities( - SimulatorPanel const& panel); - public: /*! \brief Makes a web view that has logged no frame yet. */ SimulatorWeb(); @@ -94,6 +74,26 @@ class SimulatorWeb { */ MAXMATRIX_NODISCARD static std::string ConvertToJsonString( std::string const& text); + + private: + void LogFrame(); + static void Command(SimulatorPanel& panel, std::string const& message); + MAXMATRIX_NODISCARD std::string GetMessage(SimulatorPanel const& panel) const; + MAXMATRIX_NODISCARD std::string GetLog() const; + MAXMATRIX_NODISCARD static std::string GetIntensities( + SimulatorPanel const& panel); + + // Every function that needs the panel is handed it, as + // max_matrix::internal::Text is, rather than the class keeping a reference: + // the panel belongs to the simulator, and the terminal is handed it too. + + SimulatorWebHost host_; + /* the frames since the page was last sent the panel, as hex pairs, one string + * per frame */ + std::vector frame_log_; + int32_t logged_frames_{0}; + /* what the sketch printed since the page was last sent the panel */ + std::string serial_output_; }; #endif // MAXMATRIX_SIMULATOR_WEB_SIMULATORWEB_H_ diff --git a/src/Command/Command.h b/src/Command/Command.h index a6bee21..bddc6af 100644 --- a/src/Command/Command.h +++ b/src/Command/Command.h @@ -73,16 +73,32 @@ class Matrix; * shifts while the next line fills the line buffer. */ class Command { - private: - /* - * As internal::Text is, the class is handed the matrix by every call rather - * than keeping a reference to it. + public: + /*! \brief Makes a reader at the start of an empty line. */ + Command(); + + /*! + * \brief Takes the next char of a command line, and carries the line out and + * answers it at its end. + * + * The chars are collected up to a line feed. A carriage return is skipped, so + * that lines may end in `\\r\\n` as well; an empty line is no command and + * gets no answer. A line too long for the buffer is dropped whole, rather + * than carried out cut short. + * + * \param[in,out] matrix The matrix the command acts on. + * \param[in] c The next char, as `Serial.read()` gives it. + * \param[out] answer Where the answer to a line goes, as a rule `Serial`. + * \return CommandState::kPending while the line goes on or after an empty + * line, CommandState::kDone once it was carried out, or + * CommandState::kAnswered once the answer to `picture` is printed. + * Error::kLineTooLong if the line did not fit and is dropped; + * Error::kUnknownCommand if no command has its name; or why the + * command failed: kNoText, kNotANumber, kOutOfRange or kUnknownChar. */ - array line_; - array shift_text_; - uint8_t line_length_{0}; - bool line_too_long_{false}; + Result Feed(Matrix& matrix, char c, Print& answer); + private: /* * The commands find their parts by their position in line_, a byte, as the * static_assert below holds the line to: walked by a span instead, as T-20 @@ -91,6 +107,7 @@ class Command { * position needs no check against its size. */ static constexpr uint16_t kMaxLineLength{0xFF}; + static_assert(MAXMATRIX_COMMAND_LINE_LENGTH <= kMaxLineLength, "a position in the line has to fit into a byte"); MAXMATRIX_NODISCARD Result Execute(Matrix& matrix, @@ -106,30 +123,14 @@ class Command { MAXMATRIX_NODISCARD Result ConvertToNumber(uint8_t text, uint16_t maximum) const; - public: - /*! \brief Makes a reader at the start of an empty line. */ - Command(); - - /*! - * \brief Takes the next char of a command line, and carries the line out and - * answers it at its end. - * - * The chars are collected up to a line feed. A carriage return is skipped, so - * that lines may end in `\\r\\n` as well; an empty line is no command and - * gets no answer. A line too long for the buffer is dropped whole, rather - * than carried out cut short. - * - * \param[in,out] matrix The matrix the command acts on. - * \param[in] c The next char, as `Serial.read()` gives it. - * \param[out] answer Where the answer to a line goes, as a rule `Serial`. - * \return CommandState::kPending while the line goes on or after an empty - * line, CommandState::kDone once it was carried out, or - * CommandState::kAnswered once the answer to `picture` is printed. - * Error::kLineTooLong if the line did not fit and is dropped; - * Error::kUnknownCommand if no command has its name; or why the - * command failed: kNoText, kNotANumber, kOutOfRange or kUnknownChar. + /* + * As internal::Text is, the class is handed the matrix by every call rather + * than keeping a reference to it. */ - Result Feed(Matrix& matrix, char c, Print& answer); + array line_; + array shift_text_; + uint8_t line_length_{0}; + bool line_too_long_{false}; }; } // namespace max_matrix diff --git a/src/Matrix/Panel.h b/src/Matrix/Panel.h index b6db9ab..80a5f10 100644 --- a/src/Matrix/Panel.h +++ b/src/Matrix/Panel.h @@ -167,62 +167,6 @@ namespace internal { * takes most of its functions over. */ class Panel { - private: - Wired wired_; - ModuleType module_type_{ModuleType::kDr0Cr0Rr1}; - MatrixOrientation matrix_orientation_{MatrixOrientation::k0}; - - /* where a dot lies in the buffer: the buffer column and its bit */ - struct BufferDot { - uint8_t column; - uint8_t bit; - }; - - /* - * Whether a column, a row or a dot lies on the panel, or a column or row - * within a module. Templates, as SetFarDot() checks its sums as int32_t and - * the rest int16_t, which is cheaper on the Uno. - */ - template - MAXMATRIX_NODISCARD bool HasColumn(Coordinate column) const { - return (column >= 0) && (column < GetNumberOfColumns()); - } - template - MAXMATRIX_NODISCARD static constexpr bool HasRow(Coordinate row) { - return (row >= 0) && (row < kRowNumberOfModule); - } - template - MAXMATRIX_NODISCARD bool HasDot(Coordinate column, Coordinate row) const { - return HasColumn(column) && HasRow(row); - } - MAXMATRIX_NODISCARD bool HasModuleColumn(Module module, - uint8_t column) const { - return (module.number < GetNumberOfModules()) && - (column < kColumnNumberOfModule); - } - MAXMATRIX_NODISCARD bool HasModuleRow(Module module, uint8_t row) const { - return (module.number < GetNumberOfModules()) && (row < kRowNumberOfModule); - } - - static Byte ReverseByte(Byte input); - MAXMATRIX_NODISCARD BufferDot ConvertDotToBuffer(uint8_t column, - uint8_t row) const; - MAXMATRIX_NODISCARD Byte GetLine(uint8_t column, uint8_t row, - bool vertical) const; - void SetLine(uint8_t column, uint8_t row, bool vertical, Byte value); - /* - * SetDot() with its point unpacked, inline, before the call: avr-gcc 7.3 - * passes a Point to a function it does not inline as one value of four - * bytes, and every caller that built one paid for it, 88 bytes of flash in - * Basics and 30 in SerialControl. Only SetDot() calls it. - */ - void SetDotAt(int16_t column, int16_t row, bool value); - void SetFarDot(int32_t column, int32_t row, bool value); - void ChangeMapping(MatrixOrientation matrix_orientation, - ModuleType module_type); - Byte GetFill(ShiftFill fill, uint8_t rotated_column, uint8_t kept_column); - void ShiftRows(bool up, ShiftFill fill); - public: /*! * \brief Makes the panel of the chain at these pins, of modules @@ -525,6 +469,62 @@ class Panel { * the top one as it was. */ void ShiftDown(ShiftFill fill = ShiftFill::kZero); + + private: + /* where a dot lies in the buffer: the buffer column and its bit */ + struct BufferDot { + uint8_t column; + uint8_t bit; + }; + + /* + * Whether a column, a row or a dot lies on the panel, or a column or row + * within a module. Templates, as SetFarDot() checks its sums as int32_t and + * the rest int16_t, which is cheaper on the Uno. + */ + template + MAXMATRIX_NODISCARD bool HasColumn(Coordinate column) const { + return (column >= 0) && (column < GetNumberOfColumns()); + } + template + MAXMATRIX_NODISCARD static constexpr bool HasRow(Coordinate row) { + return (row >= 0) && (row < kRowNumberOfModule); + } + template + MAXMATRIX_NODISCARD bool HasDot(Coordinate column, Coordinate row) const { + return HasColumn(column) && HasRow(row); + } + MAXMATRIX_NODISCARD bool HasModuleColumn(Module module, + uint8_t column) const { + return (module.number < GetNumberOfModules()) && + (column < kColumnNumberOfModule); + } + MAXMATRIX_NODISCARD bool HasModuleRow(Module module, uint8_t row) const { + return (module.number < GetNumberOfModules()) && (row < kRowNumberOfModule); + } + + static Byte ReverseByte(Byte input); + MAXMATRIX_NODISCARD BufferDot ConvertDotToBuffer(uint8_t column, + uint8_t row) const; + MAXMATRIX_NODISCARD Byte GetLine(uint8_t column, uint8_t row, + bool vertical) const; + void SetLine(uint8_t column, uint8_t row, bool vertical, Byte value); + /* + * SetDot() with its point unpacked, inline, before the call: avr-gcc 7.3 + * passes a Point to a function it does not inline as one value of four + * bytes, and every caller that built one paid for it, 88 bytes of flash in + * Basics and 30 in SerialControl. Only SetDot() calls it. + */ + void SetDotAt(int16_t column, int16_t row, bool value); + void SetFarDot(int32_t column, int32_t row, bool value); + void ChangeMapping(MatrixOrientation matrix_orientation, + ModuleType module_type); + Byte GetFill(ShiftFill fill, uint8_t rotated_column, uint8_t kept_column); + void ShiftRows(bool up, ShiftFill fill); + + Wired wired_; + ModuleType module_type_{ModuleType::kDr0Cr0Rr1}; + MatrixOrientation matrix_orientation_{MatrixOrientation::k0}; }; } // namespace internal diff --git a/src/Matrix/Wired.h b/src/Matrix/Wired.h index 74d9f61..e7d42b5 100644 --- a/src/Matrix/Wired.h +++ b/src/Matrix/Wired.h @@ -32,19 +32,6 @@ namespace internal { * changed to the chain. */ class Wired { - private: - Chain chain_; - // The sketch's, BufferSize() of the chain's modules long, which says how - // many modules the chain has; MatrixOf brings one. - span matrix_buffer_; - /* - * A frame carries one register of every module, so the same column of all - * modules goes out in one frame. The buffer therefore keeps which columns of - * a module have changed, one bit each, and Send() needs one frame per changed - * column: eight at most for a whole picture, however many calls drew it. - */ - Byte changed_columns_{}; - public: /*! * \brief Makes the matrix of the chain at these pins, and empties its buffer. @@ -120,6 +107,19 @@ class Wired { * nothing goes out when nothing has changed. */ void Send(); + + private: + Chain chain_; + // The sketch's, BufferSize() of the chain's modules long, which says how + // many modules the chain has; MatrixOf brings one. + span matrix_buffer_; + /* + * A frame carries one register of every module, so the same column of all + * modules goes out in one frame. The buffer therefore keeps which columns of + * a module have changed, one bit each, and Send() needs one frame per changed + * column: eight at most for a whole picture, however many calls drew it. + */ + Byte changed_columns_{}; }; } // namespace internal diff --git a/src/Max7219/Chain.h b/src/Max7219/Chain.h index 2379728..979646d 100644 --- a/src/Max7219/Chain.h +++ b/src/Max7219/Chain.h @@ -170,50 +170,6 @@ namespace internal { * pins; bit banging or SPI is chosen with `MAXMATRIX_USE_SPI`. */ class Chain { - private: - /* a pin's port and its bit there, as one, so that the two cannot be mixed up - * between pins or swapped in a call (FN-16) */ - struct PortBit { - uint8_t port; - uint8_t mask; - }; - - uint8_t data_in_pin_; - uint8_t chip_select_pin_; - uint8_t clock_pin_; - /* - * The port and the bit of each pin, looked up once in Init(): digitalWrite() - * looks both up in tables in flash on every call, which made a byte bit - * banged by shiftOut() take about 1500 cycles of the Uno's 16 MHz. The port, - * not its register: the core reads the register's address from flash with a - * statement expression, which decltype() cannot take outside a function, so - * it is read where it is written. Data in and clock only bit bang. - */ - PortBit chip_select_{}; -#if (MAXMATRIX_USE_SPI == STD_OFF) - PortBit data_in_{}; - PortBit clock_{}; -#endif - - /* - * Not const, though they change no member: they change the chain this object - * stands for, and a const Chain that still sends would be a lie. - * transfer() is not static either, though with SPI it uses no member: bit - * banging needs the pins. clang-tidy reports all three, and each definition - * answers it with a NOLINT. - */ - void StartTransmission(); - void StopTransmission(); - void Transfer(max7219::Register address, uint8_t value); -#if (MAXMATRIX_USE_SPI == STD_OFF) - void ShiftOut(uint8_t value); -#endif - static PortBit LookUpPortBit(uint8_t pin); - static void WritePin(PortBit pin, bool high); - // Whether each of the three pins is one of the board's, which Init() checks - // before it takes them. - MAXMATRIX_NODISCARD bool HasBoardPins() const; - public: /*! * \brief Makes the chain at these pins; nothing is sent until Init(). @@ -300,6 +256,50 @@ class Chain { */ void WriteRegisterEach(max7219::Register address, span buffer, uint8_t column); + + private: + /* a pin's port and its bit there, as one, so that the two cannot be mixed up + * between pins or swapped in a call (FN-16) */ + struct PortBit { + uint8_t port; + uint8_t mask; + }; + + /* + * Not const, though they change no member: they change the chain this object + * stands for, and a const Chain that still sends would be a lie. + * transfer() is not static either, though with SPI it uses no member: bit + * banging needs the pins. clang-tidy reports all three, and each definition + * answers it with a NOLINT. + */ + void StartTransmission(); + void StopTransmission(); + void Transfer(max7219::Register address, uint8_t value); +#if (MAXMATRIX_USE_SPI == STD_OFF) + void ShiftOut(uint8_t value); +#endif + static PortBit LookUpPortBit(uint8_t pin); + static void WritePin(PortBit pin, bool high); + // Whether each of the three pins is one of the board's, which Init() checks + // before it takes them. + MAXMATRIX_NODISCARD bool HasBoardPins() const; + + uint8_t data_in_pin_; + uint8_t chip_select_pin_; + uint8_t clock_pin_; + /* + * The port and the bit of each pin, looked up once in Init(): digitalWrite() + * looks both up in tables in flash on every call, which made a byte bit + * banged by shiftOut() take about 1500 cycles of the Uno's 16 MHz. The port, + * not its register: the core reads the register's address from flash with a + * statement expression, which decltype() cannot take outside a function, so + * it is read where it is written. Data in and clock only bit bang. + */ + PortBit chip_select_{}; +#if (MAXMATRIX_USE_SPI == STD_OFF) + PortBit data_in_{}; + PortBit clock_{}; +#endif }; } // namespace internal diff --git a/src/MaxMatrix.h b/src/MaxMatrix.h index 63f293c..638a2b5 100644 --- a/src/MaxMatrix.h +++ b/src/MaxMatrix.h @@ -81,27 +81,6 @@ enum class State : uint8_t { * fails reports its name and the error to ReportError(). */ class Matrix { - private: - internal::Panel panel_; - internal::Text text_; - State state_{State::kNone}; - bool auto_update_{true}; - uint16_t shift_interval_{MAXMATRIX_SHIFT_INTERVAL_INIT_VALUE}; - uint32_t last_shift_{0}; - - // Sends what changed, if auto update is on; every public function that draws - // ends with it. - void UpdateIfAuto() { - if (auto_update_) { - Update(); - } - } - MAXMATRIX_NODISCARD Status UpdateIfAuto(Status status) { - UpdateIfAuto(); - return status; - } - void StartShift(State shift_state); - public: /*! * \brief Makes a matrix on the chain at these pins, in the buffer given, and @@ -132,12 +111,21 @@ class Matrix { /*! \brief Leaves the LEDs as they are. */ ~Matrix() = default; + /*! \brief No matrix without its pins and its buffer. */ + Matrix() = delete; + /*! \brief Not copyable: the copy would draw into the same buffer. */ Matrix(Matrix const&) = delete; /*! \brief Not assignable, as it is not copyable. */ Matrix& operator=(Matrix const&) = delete; + /*! \brief Not movable: a Batch or a Command may hold on to the matrix. */ + Matrix(Matrix&&) = delete; + + /*! \brief Not movable, as it is not copyable. */ + Matrix& operator=(Matrix&&) = delete; + /*! \brief Returns the modules of the chain. */ MAXMATRIX_NODISCARD uint8_t GetNumberOfModules() const { return panel_.GetNumberOfModules(); @@ -643,6 +631,27 @@ class Matrix { panel_.ShiftDown(fill); UpdateIfAuto(); } + + private: + // Sends what changed, if auto update is on; every public function that draws + // ends with it. + void UpdateIfAuto() { + if (auto_update_) { + Update(); + } + } + MAXMATRIX_NODISCARD Status UpdateIfAuto(Status status) { + UpdateIfAuto(); + return status; + } + void StartShift(State shift_state); + + internal::Panel panel_; + internal::Text text_; + State state_{State::kNone}; + bool auto_update_{true}; + uint16_t shift_interval_{MAXMATRIX_SHIFT_INTERVAL_INIT_VALUE}; + uint32_t last_shift_{0}; }; /*! @@ -668,10 +677,6 @@ class Matrix { * that is what Matrix::SetAutoUpdate() and Matrix::Update() are for. */ class Batch { - private: - internal::reference_wrapper matrix_; - bool previous_auto_update_; - public: /*! * \brief Turns auto update of `matrix` off until the end of the block. @@ -687,11 +692,24 @@ class Batch { * on. */ ~Batch() { matrix_.get().SetAutoUpdate(previous_auto_update_); } + /*! \brief No batch without the matrix it holds back. */ + Batch() = delete; + /*! \brief Not copyable: a copy would put auto update back twice. */ Batch(Batch const&) = delete; /*! \brief Not assignable, as it is not copyable. */ Batch& operator=(Batch const&) = delete; + + /*! \brief Not movable: the moved one would put auto update back as well. */ + Batch(Batch&&) = delete; + + /*! \brief Not movable, as it is not copyable. */ + Batch& operator=(Batch&&) = delete; + + private: + internal::reference_wrapper matrix_; + bool previous_auto_update_; }; namespace internal { @@ -716,7 +734,9 @@ class Buffer { // misc-multiple-inheritance, suppressed below: the buffer is a base of its own, // so that it is made before the Matrix it is handed to (internal::Buffer); it // is private and has no virtual function, so that nothing but the constructor -// reaches it. +// reaches it. C-8, as both bases are reused by deriving: the buffer for that +// order, and Matrix, as a MatrixOf is the matrix a sketch hands wherever one is +// taken, Command::Feed() and Batch among them, without its length. /*! * \brief A Matrix with the buffer for a chain of `NumberOfModules` modules. * diff --git a/src/MaxMatrixError.h b/src/MaxMatrixError.h index 62ad2df..fad8d20 100644 --- a/src/MaxMatrixError.h +++ b/src/MaxMatrixError.h @@ -71,11 +71,6 @@ enum class Error : uint8_t { * `E_OK` was 0, so `if(matrix.SetText(...))` was true on an error. */ class Status { - private: - // A sketch reaches the error only through this class, so that it could carry - // more than the enum one day without a sketch changing. - Error error_; - public: /*! * \brief Makes a status of `error`, ok when none is given. @@ -127,6 +122,11 @@ class Status { friend constexpr bool operator!=(Status status, Error error) { return !(status == error); } + + private: + // A sketch reaches the error only through this class, so that it could carry + // more than the enum one day without a sketch changing. + Error error_; }; /*! @@ -142,12 +142,6 @@ class Status { */ template class Result { - private: - // Side by side rather than in a union: a byte and its error still return in - // registers on the AVR, and the value is never left undefined. - ValueType value_; - Error error_; - public: /*! * \brief Makes a result that holds `value`. @@ -205,6 +199,12 @@ class Result { friend constexpr bool operator!=(Result const& result, Error error) { return !(result == error); } + + private: + // Side by side rather than in a union: a byte and its error still return in + // registers on the AVR, and the value is never left undefined. + ValueType value_; + Error error_; }; #if (MAXMATRIX_DEV_ERROR_DETECT == STD_ON) diff --git a/src/Text/Text.h b/src/Text/Text.h index 2852183..f5d787c 100644 --- a/src/Text/Text.h +++ b/src/Text/Text.h @@ -31,43 +31,6 @@ namespace internal { * which Matrix::ShiftTask() makes. */ class Text { - private: - /* - * Every function that draws is given the panel rather than the class keeping - * a reference to it. A reference costs two bytes of RAM, and avr-gcc no - * longer sees through it that the panel is the one inside the global - * Matrix of the sketch: ScrollText was 86 bytes of flash larger with it. - */ - - /* - * The columns of a char shifted in so far, its space included: kShiftIdle - * while no char is shifting, and kShiftStart when one begins. A count rather - * than a state, so the two values are constants, not an enum. - */ - static constexpr uint16_t kShiftIdle{0}; - static constexpr uint16_t kShiftStart{1}; - - /* - * The font every char is drawn in, empty for kDefaultFont: only a - * function that draws a char names the default font then, and a sketch that - * draws none does not link its 614 bytes, as Animation and Sprites did with - * the default font set here. And the char that shifts in, which lies in the - * font. - */ - span font_; - Glyph glyph_{}; - /* 16 bits, as a char and its space take up to 255 + 255 columns */ - uint16_t sprite_shift_counter_{kShiftIdle}; - uint8_t space_between_chars_{MAXMATRIX_SPACE_BETWEEN_CHARS_INIT_VALUE}; - /* the text that shifts in, the position of its next char, and whether it - * starts over after its last char */ - span text_; - size_t position_{0}; - bool repeat_{false}; - - void ReadNextChar(); - static span DefaultFont(); - public: /*! \brief Makes a text with nothing to shift. */ Text(); @@ -194,6 +157,43 @@ class Text { * \return True while the text is still shifting. */ bool StringShiftTask(Panel& panel); + + private: + /* + * Every function that draws is given the panel rather than the class keeping + * a reference to it. A reference costs two bytes of RAM, and avr-gcc no + * longer sees through it that the panel is the one inside the global + * Matrix of the sketch: ScrollText was 86 bytes of flash larger with it. + */ + + /* + * The columns of a char shifted in so far, its space included: kShiftIdle + * while no char is shifting, and kShiftStart when one begins. A count rather + * than a state, so the two values are constants, not an enum. + */ + static constexpr uint16_t kShiftIdle{0}; + static constexpr uint16_t kShiftStart{1}; + + void ReadNextChar(); + static span DefaultFont(); + + /* + * The font every char is drawn in, empty for kDefaultFont: only a + * function that draws a char names the default font then, and a sketch that + * draws none does not link its 614 bytes, as Animation and Sprites did with + * the default font set here. And the char that shifts in, which lies in the + * font. + */ + span font_; + Glyph glyph_{}; + /* 16 bits, as a char and its space take up to 255 + 255 columns */ + uint16_t sprite_shift_counter_{kShiftIdle}; + uint8_t space_between_chars_{MAXMATRIX_SPACE_BETWEEN_CHARS_INIT_VALUE}; + /* the text that shifts in, the position of its next char, and whether it + * starts over after its last char */ + span text_; + size_t position_{0}; + bool repeat_{false}; }; } // namespace internal diff --git a/test/SimulatorWebHostTest.cpp b/test/SimulatorWebHostTest.cpp index 441b697..d2c1b46 100644 --- a/test/SimulatorWebHostTest.cpp +++ b/test/SimulatorWebHostTest.cpp @@ -60,3 +60,15 @@ TEST(SimulatorWebHostTest, TaskDoesNotWaitForAConnectionThatSaysNothing) { close(idle); EXPECT_LT(elapsed.count(), 1000) << "milliseconds in Task()"; } + +/* Bug: the host closed none of its sockets when it was destroyed, so that its + * port stayed taken until the program ended, and a second host on it failed */ +TEST(SimulatorWebHostTest, FreesItsPortWhenDestroyed) { + { + SimulatorWebHost first; + ASSERT_TRUE(first.Begin(kPort, "/dev/null")); + } + SimulatorWebHost second; + + EXPECT_TRUE(second.Begin(kPort, "/dev/null")); +}