From 9479d8e3e8b95acf843a4662a2f3ba03888caa4c Mon Sep 17 00:00:00 2001 From: Bengt Martensson Date: Sat, 30 Jan 2021 20:02:35 +0100 Subject: [PATCH] Convert to "modern" C++. Resolves #17. --- keywords.txt | 2 ++ src/Board.h | 2 +- src/HashDecoder.cpp | 6 +++--- src/HashDecoder.h | 16 ++++++++++++++-- src/InfraredTypes.h | 8 ++++---- src/IrDecoder.h | 17 +++++++++++------ src/IrReader.h | 13 ++++++++++--- src/IrSender.cpp | 2 +- src/IrSender.h | 9 +++++++++ src/IrSenderPwm.h | 2 +- src/IrSenderPwmSoft.h | 2 +- src/IrSequenceReader.h | 3 +++ src/IrWidget.h | 6 +++--- src/IrWidgetAggregating.cpp | 2 +- src/MultiDecoder.h | 8 ++++++++ src/Nec1Decoder.h | 6 +++--- src/Nec1Renderer.cpp | 2 +- src/Nec1Renderer.h | 10 +++++----- src/Pronto.cpp | 4 +--- src/Pronto.h | 27 ++++++++++++++------------- src/Rc5Decoder.h | 2 +- src/Rc5Renderer.h | 10 +++++----- src/boards/Esp32.h | 2 +- 23 files changed, 103 insertions(+), 58 deletions(-) diff --git a/keywords.txt b/keywords.txt index 09243fb..0c55be5 100644 --- a/keywords.txt +++ b/keywords.txt @@ -104,6 +104,7 @@ decode KEYWORD2 decode KEYWORD2 decode KEYWORD2 decode KEYWORD2 +~HashDecoder KEYWORD2 HashDecoder KEYWORD2 HashDecoder KEYWORD2 HashDecoder KEYWORD2 @@ -252,6 +253,7 @@ readFlash KEYWORD2 IrSequenceReader KEYWORD2 IrSequenceReader KEYWORD2 IrSequenceReader KEYWORD2 +IrSequenceReader KEYWORD2 ~IrSequenceReader KEYWORD2 getFrequency KEYWORD2 receive KEYWORD2 diff --git a/src/Board.h b/src/Board.h index 937bfeb..c5064c3 100644 --- a/src/Board.h +++ b/src/Board.h @@ -18,7 +18,7 @@ this program. If not, see http://www.gnu.org/licenses/. /** * This class serves as an HAL (Hardware Abstraction Layer). * All access to the hardware should go through this class. - * (In particular, using digital[Read,Write] and ::pinMode is prohibited + * (In particular, using \c digital[Read,Write] and \c \::pinMode is prohibited * (exception: code that runs exclusively on the host). * * It is a singleton class (since there is only one board), instantiated diff --git a/src/HashDecoder.cpp b/src/HashDecoder.cpp index c084a65..d4781db 100644 --- a/src/HashDecoder.cpp +++ b/src/HashDecoder.cpp @@ -2,7 +2,7 @@ const char *HashDecoder::format = "%0x"; -const unsigned int offset = 2; +constexpr unsigned int offset = 2; uint32_t HashDecoder::compare(microseconds_t oldVal, microseconds_t newVal) { return @@ -17,7 +17,7 @@ bool HashDecoder::tryDecode(const IrReader& irCapturer, Stream& stream) { } void HashDecoder::decode(const microseconds_t* data, size_t length) { - if (length < 4) + if (length < minMeaningfulLength) return; for (unsigned int i = 0; i < length - offset - 1; i++) { @@ -30,7 +30,7 @@ void HashDecoder::decode(const microseconds_t* data, size_t length) { void HashDecoder::decode(const IrReader& irReader) { size_t length = irReader.getDataLength(); - if (length < 4) + if (length < minMeaningfulLength) return; for (unsigned int i = 0; i < length - offset - 1; i++) { diff --git a/src/HashDecoder.h b/src/HashDecoder.h index c011fa4..89ef464 100644 --- a/src/HashDecoder.h +++ b/src/HashDecoder.h @@ -39,10 +39,22 @@ class HashDecoder : public IrDecoder { } static const char *format; - static const uint32_t FNVprime = 16777619U; - static const uint32_t FNVoffsetBasis = 2166136261U; + static constexpr uint32_t FNVprime = 16777619UL; + static constexpr uint32_t FNVoffsetBasis = 2166136261UL; + static constexpr unsigned int minMeaningfulLength = 4U; public: + +#ifndef DOXYGEN + HashDecoder() = delete; + HashDecoder(const HashDecoder&) = delete; + HashDecoder(HashDecoder&&) = delete; + HashDecoder& operator=(const HashDecoder&) = delete; + HashDecoder& operator=(HashDecoder&&) = delete; +#endif // ! DOXYGEN + + virtual ~HashDecoder() {} + /** * Constructs a HashDecoder from an IrReader, containing data. * @param irReader IrReader with data, i.e. with isReady() true. diff --git a/src/InfraredTypes.h b/src/InfraredTypes.h index 0d5c14b..3b22484 100644 --- a/src/InfraredTypes.h +++ b/src/InfraredTypes.h @@ -15,7 +15,7 @@ typedef uint16_t microseconds_t; //typedef uint32_t microseconds_t; /** Largest microseconds_t number possible */ -constexpr static microseconds_t MICROSECONDS_T_MAX = static_cast(-1); +static constexpr microseconds_t MICROSECONDS_T_MAX = static_cast(-1); /** * Type for durations in milli seconds. @@ -23,7 +23,7 @@ constexpr static microseconds_t MICROSECONDS_T_MAX = static_cast */ typedef uint16_t milliseconds_t; /** Largest milliseconds_t number possible */ -constexpr static milliseconds_t MILLISECONDS_T_MAX = static_cast(-1); +static constexpr milliseconds_t MILLISECONDS_T_MAX = static_cast(-1); /** * Type for modulation frequency in Hz. @@ -40,6 +40,6 @@ typedef int8_t dutycycle_t; */ typedef uint8_t pin_t; /** Symbolic name for an invalid pin number */ -const pin_t invalidPin = 255; +static constexpr pin_t invalidPin = 255; /** Largest pin_t number possible */ -constexpr static pin_t PIN_T_MAX = 255; +static constexpr pin_t PIN_T_MAX = 255; diff --git a/src/IrDecoder.h b/src/IrDecoder.h index 2c93128..6c7ab23 100644 --- a/src/IrDecoder.h +++ b/src/IrDecoder.h @@ -7,9 +7,14 @@ */ class IrDecoder { public: - IrDecoder() { - valid = false; - } + IrDecoder() {} + +#ifndef DOXYGEN + IrDecoder(const IrDecoder&) = delete; + IrDecoder(IrDecoder&&) = delete; + IrDecoder& operator=(const IrDecoder&) = delete; + IrDecoder& operator=(IrDecoder&&) = delete; +#endif // ! DOXYGEN virtual ~IrDecoder() {} @@ -39,11 +44,11 @@ class IrDecoder { } private: - const static uint32_t endingMin = 20000U; - bool valid; + constexpr static uint32_t endingMin = 20000U; + bool valid = false; protected: - static const int invalid = -1; + constexpr static int invalid = -1; void setValid(bool valid_) { valid = valid_; } diff --git a/src/IrReader.h b/src/IrReader.h index ca39aea..ef76125 100644 --- a/src/IrReader.h +++ b/src/IrReader.h @@ -30,9 +30,9 @@ this program. If not, see http://www.gnu.org/licenses/. class IrReader { public: // Defaults - static const milliseconds_t defaultBeginningTimeout = 2000U; - static const milliseconds_t defaultEndingTimeout = 30U; - static const size_t defaultCaptureLength = 100U; + static constexpr milliseconds_t defaultBeginningTimeout = 2000U; + static constexpr milliseconds_t defaultEndingTimeout = 30U; + static constexpr size_t defaultCaptureLength = 100U; protected: milliseconds_t beginningTimeout; @@ -60,6 +60,13 @@ class IrReader { IrReader() { } +#ifndef DOXYGEN + IrReader(const IrReader&) = delete; + IrReader(IrReader&&) = delete; + IrReader& operator=(const IrReader&) = delete; + IrReader& operator=(IrReader&&) = delete; +#endif // ! DOXYGEN + virtual ~IrReader() { }; diff --git a/src/IrSender.cpp b/src/IrSender.cpp index 41f3bb4..c0446f9 100644 --- a/src/IrSender.cpp +++ b/src/IrSender.cpp @@ -72,7 +72,7 @@ void IrSender::send(const IrSequence& irSequence, frequency_t frequency, dutycyc #else microseconds_t delay = irSequence.getDurations()[i]; #endif - if (i & 1) + if (i & 1U) sendSpace(delay); else sendMark(delay); diff --git a/src/IrSender.h b/src/IrSender.h index 109a099..bcf977f 100644 --- a/src/IrSender.h +++ b/src/IrSender.h @@ -48,12 +48,21 @@ class IrSender { virtual void sendMark(microseconds_t time) = 0; public: +#ifndef DOXYGEN + IrSender() = delete; + IrSender(const IrSender&) = delete; + IrSender(IrSender&&) = delete; + IrSender& operator=(const IrSender&) = delete; + IrSender& operator=(IrSender&&) = delete; +#endif // ! DOXYGEN + virtual ~IrSender(); /** * Sends an IrSequence with the prescribed frequency * @param irSequence * @param frequency frequency in Hz + * @param dutyCycle */ virtual void send(const IrSequence& irSequence, frequency_t frequency = IrSignal::defaultFrequency, dutycycle_t dutyCycle = Board::defaultDutyCycle); diff --git a/src/IrSenderPwm.h b/src/IrSenderPwm.h index 62433ff..c8b4ea7 100644 --- a/src/IrSenderPwm.h +++ b/src/IrSenderPwm.h @@ -28,7 +28,7 @@ this program. If not, see http://www.gnu.org/licenses/. */ class IrSenderPwm : public IrSender { private: - static const unsigned int defaultDutyCycle = 50U; + static constexpr unsigned int defaultDutyCycle = 50U; static IrSenderPwm *instance; protected: diff --git a/src/IrSenderPwmSoft.h b/src/IrSenderPwmSoft.h index b7c657e..1282188 100644 --- a/src/IrSenderPwmSoft.h +++ b/src/IrSenderPwmSoft.h @@ -31,7 +31,7 @@ class IrSenderPwmSoft : public IrSenderPwm { virtual ~IrSenderPwmSoft() {} void enable(frequency_t hz, dutycycle_t dutyCycle = Board::defaultDutyCycle); void sendMark(microseconds_t time); - static const unsigned int PULSE_CORRECTION = 3U; + static constexpr unsigned int PULSE_CORRECTION = 3U; virtual void sleepMicros(microseconds_t us) = 0; virtual void sleepUntilMicros(uint32_t terminateTime) = 0; diff --git a/src/IrSequenceReader.h b/src/IrSequenceReader.h index c959ccc..67d3168 100644 --- a/src/IrSequenceReader.h +++ b/src/IrSequenceReader.h @@ -18,6 +18,9 @@ class IrSequenceReader : public IrReader { IrSequenceReader(const IrSequenceReader& orig) : IrReader(),irSequence(orig.irSequence) { }; + IrSequenceReader(IrSequenceReader&& orig) : IrReader(),irSequence(orig.irSequence) { + }; + IrSequenceReader(const IrSequence& irSequence_) : IrReader(),irSequence(irSequence_) { }; diff --git a/src/IrWidget.h b/src/IrWidget.h index b6e2ff5..3aca5fa 100644 --- a/src/IrWidget.h +++ b/src/IrWidget.h @@ -43,7 +43,7 @@ see also here: */ class IrWidget : public IrReader { public: - static const int16_t defaultMarkExcess = 0; + static constexpr int16_t defaultMarkExcess = 0; protected: frequency_t frequency; @@ -127,7 +127,7 @@ class IrWidget : public IrReader { typedef uint8_t ovlBitsDataType; #endif - static const uint8_t RANGE_EXTENSION_BITS = 4; // factor for upper measurement range = 2^(RANGE_EXTENSION_BITS+1) + static constexpr uint8_t RANGE_EXTENSION_BITS = 4; // factor for upper measurement range = 2^(RANGE_EXTENSION_BITS+1) ovlBitsDataType endingTimeout; // = _BV(RANGE_EXTENSION_BITS) - 1; @@ -197,7 +197,7 @@ class IrWidget : public IrReader { } uint16_t *captureData; //[bufSize]; // the buffer where the catured data is stored uint16_t captureCount; // number of values stored in captureData - static const uint8_t sampleSize = 2; + static constexpr uint8_t sampleSize = 2; virtual uint32_t unpackTimeVal(uint32_t val) const = 0; diff --git a/src/IrWidgetAggregating.cpp b/src/IrWidgetAggregating.cpp index abb0f18..6fb9c78 100644 --- a/src/IrWidgetAggregating.cpp +++ b/src/IrWidgetAggregating.cpp @@ -7,7 +7,7 @@ #if HAS_INPUT_CAPTURE -static const frequency_t min_frequency = 20000U; +static constexpr frequency_t min_frequency = 20000U; IrWidgetAggregating::IrWidgetAggregating(size_t captureLength, bool pullup, diff --git a/src/MultiDecoder.h b/src/MultiDecoder.h index cc6d8b4..0734f6f 100644 --- a/src/MultiDecoder.h +++ b/src/MultiDecoder.h @@ -35,6 +35,14 @@ class MultiDecoder : public IrDecoder { */ MultiDecoder(const IrReader &irReader); +#ifndef DOXYGEN + MultiDecoder() = delete; + MultiDecoder(const MultiDecoder&) = delete; + MultiDecoder(MultiDecoder&&) = delete; + MultiDecoder& operator=(const MultiDecoder&) = delete; + MultiDecoder& operator=(MultiDecoder&&) = delete; +#endif // ! DOXYGEN + virtual ~MultiDecoder() { } diff --git a/src/Nec1Decoder.h b/src/Nec1Decoder.h index 49fe903..1974054 100644 --- a/src/Nec1Decoder.h +++ b/src/Nec1Decoder.h @@ -8,9 +8,9 @@ */ class Nec1Decoder : public IrDecoder { private: - static const microseconds_t timebase = 564; - static const microseconds_t timebaseUpper = 650; - static const microseconds_t timebaseLower = 450; + static constexpr microseconds_t timebase = 564U; + static constexpr microseconds_t timebaseUpper = 650U; + static constexpr microseconds_t timebaseLower = 450U; // NOTE: use a signed type to be able to return the value invalid. int F; diff --git a/src/Nec1Renderer.cpp b/src/Nec1Renderer.cpp index 119e87e..3340f92 100644 --- a/src/Nec1Renderer.cpp +++ b/src/Nec1Renderer.cpp @@ -17,7 +17,7 @@ const IrSignal *Nec1Renderer::newIrSignal(unsigned int D, unsigned int S, unsign lsbByte(introData, i, sum, F); lsbByte(introData, i, sum, 255U-F); introData[i] = 564U; i++; - introData[i] = (microseconds_t) (108000U - sum); i++; + introData[i] = static_cast(108000UL - sum); i++; microseconds_t *repeatData = new microseconds_t[repeatLength]; repeatData[0] = 9024U; repeatData[1] = 2256U; diff --git a/src/Nec1Renderer.h b/src/Nec1Renderer.h index be74bc3..4ccad6a 100644 --- a/src/Nec1Renderer.h +++ b/src/Nec1Renderer.h @@ -5,14 +5,14 @@ /** * A static class consisting of two functions that generate IrSignal-s from the NEC1 protocol parameters. * The NEC1 protocol is given in IRP notation as - * {38.4k,564}<1,-1|1,-3>(16,-8,D:8,S:8,F:8,~F:8,1,^108m,(16,-4,1,^108m)*) [D:0..255,S:0..255=255-D,F:0..255] + * \c {38.4k,564}<1,-1|1,-3>(16,-8,D:8,S:8,F:8,~F:8,1,^108m,(16,-4,1,^108m)*) [D:0..255,S:0..255=255-D,F:0..255] */ class Nec1Renderer { private: - static const frequency_t frequency = 38400U; - static const dutycycle_t dutyCycle = 33U; - static const size_t introLength = 68U; - static const size_t repeatLength = 4U; + static constexpr frequency_t frequency = 38400UL; + static constexpr dutycycle_t dutyCycle = 33U; + static constexpr size_t introLength = 68U; + static constexpr size_t repeatLength = 4U; public: diff --git a/src/Pronto.cpp b/src/Pronto.cpp index 2abc59b..1f6ddba 100644 --- a/src/Pronto.cpp +++ b/src/Pronto.cpp @@ -23,13 +23,11 @@ IrSignal *Pronto::parse(const uint16_t *data, size_t size) { IrSequence *intro = mkSequence(data + numbersInPreamble, introPairs, timebase); IrSequence *repeat = mkSequence(data + numbersInPreamble + 2*introPairs, repetitionPairs, timebase); - IrSequence *ending = new IrSequence(); - IrSignal *code = new IrSignal(*intro, *repeat, *ending, frequency, IrSignal::noDutyCycle); + IrSignal *code = new IrSignal(*intro, *repeat, frequency); delete intro; delete repeat; - delete ending; return code; } diff --git a/src/Pronto.h b/src/Pronto.h index fbe27d3..4ddf5e8 100644 --- a/src/Pronto.h +++ b/src/Pronto.h @@ -6,10 +6,11 @@ * Note: Unless you have "infinitely much" memory, it is a very bad idea to put Pronto Hex in your source files. * Better is to use, for example IrScruitinizer to convert the signals offline, * and put the converted version in your source files instead. - * - * This class presently does not use floating point arithmetics. */ +// Maintainer note: +// Make sure that this class does not use floating point arithmetics. + #pragma once #include "InfraredTypes.h" @@ -18,16 +19,16 @@ class Pronto { private: - static const unsigned int bitsInHexadecimal = 4U; - static const unsigned int digitsInProntoNumber = 4U; - static const unsigned int numbersInPreamble = 4U; - static const unsigned int hexMask = 0xFU; - static const uint16_t learnedToken = 0x0000U; - static const uint16_t learnedNonModulatedToken = 0x0100U; - static const uint32_t referenceFrequency = 4145146UL; - static const uint16_t fallbackFrequencyCode = 0x0040U; // To use with frequency = 0; - static const frequency_t fallbackFrequency = 64767U; // To use with frequency = 0; - static const uint32_t microsecondsInSeconds = 1000000UL; + static constexpr unsigned int bitsInHexadecimal = 4U; + static constexpr unsigned int digitsInProntoNumber = 4U; + static constexpr unsigned int numbersInPreamble = 4U; + static constexpr unsigned int hexMask = 0xFU; + static constexpr uint16_t learnedToken = 0x0000U; + static constexpr uint16_t learnedNonModulatedToken = 0x0100U; + static constexpr uint32_t referenceFrequency = 4145146UL; + static constexpr uint16_t fallbackFrequencyCode = 0x0040U; // To use with frequency = 0; + static constexpr frequency_t fallbackFrequency = 64767U; // To use with frequency = 0; + static constexpr uint32_t microsecondsInSeconds = 1000000UL; Pronto() {}; @@ -89,7 +90,7 @@ class Pronto { * @param str Text string containing a Pronto form signal. * @return IrSignal */ - static IrSignal *parse_PF(const uint_farptr_t ptr); + static IrSignal *parse_PF(const uint_farptr_t str); static IrSignal *parse_PF(const char * ptr); diff --git a/src/Rc5Decoder.h b/src/Rc5Decoder.h index 3b2877e..132afde 100644 --- a/src/Rc5Decoder.h +++ b/src/Rc5Decoder.h @@ -8,7 +8,6 @@ */ class Rc5Decoder : public IrDecoder { public: - static const char *format; /** * Constructs a Rc5Decoder from an IrReader, containing data. @@ -74,4 +73,5 @@ class Rc5Decoder : public IrDecoder { static Length decodeDuration(microseconds_t t); static unsigned int decodeFlashGap(microseconds_t flash, microseconds_t gap); + static const char *format; }; diff --git a/src/Rc5Renderer.h b/src/Rc5Renderer.h index adb6070..6828ad3 100644 --- a/src/Rc5Renderer.h +++ b/src/Rc5Renderer.h @@ -4,13 +4,13 @@ /** * A static class consisting of two functions that generate IrSignal-s from the RC5 protocol parameters. * The RC5 protocol is given in IRP notation as - * {36k,msb,889}<1,-1|-1,1>((1:1,~F:1:6,T:1,D:5,F:6,^114m)+,T=1-T)[T@:0..1=0,D:0..31,F:0..127] + * \c {36k,msb,889}<1,-1|-1,1>((1:1,~F:1:6,T:1,D:5,F:6,^114m)+,T=1-T)[T\@:0..1=0,D:0..31,F:0..127] */ class Rc5Renderer { private: - static const frequency_t frequency = 36000U; - static const size_t introLength = 0U; - static const size_t endingLength = 0U; + static constexpr frequency_t frequency = 36000UL; + static constexpr size_t introLength = 0U; + static constexpr size_t endingLength = 0U; public: /** @@ -33,7 +33,7 @@ class Rc5Renderer { private: Rc5Renderer(); - static const microseconds_t timebase = 889; + static constexpr microseconds_t timebase = 889U; static void emit(unsigned int t, unsigned int& index, int& pending, microseconds_t *repeat); static void emitMsb(unsigned int x, unsigned int length, unsigned int& index, int& pending, microseconds_t *repeat); diff --git a/src/boards/Esp32.h b/src/boards/Esp32.h index a0275cb..2a0a5ca 100644 --- a/src/boards/Esp32.h +++ b/src/boards/Esp32.h @@ -16,7 +16,7 @@ this program. If not, see http://www.gnu.org/licenses/. */ /** - * @file Esp.h + * @file Esp32.h * * @brief Hardware dependent definitions for Esp32 boards. * Based upon https://github.com/z3t0/Arduino-IRremote/pull/540/files