Skip to content

Commit

Permalink
Convert to "modern" C++. Resolves #17.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Jan 30, 2021
1 parent b27d92f commit 9479d8e
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 58 deletions.
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ decode KEYWORD2
decode KEYWORD2
decode KEYWORD2
decode KEYWORD2
~HashDecoder KEYWORD2
HashDecoder KEYWORD2
HashDecoder KEYWORD2
HashDecoder KEYWORD2
Expand Down Expand Up @@ -252,6 +253,7 @@ readFlash KEYWORD2
IrSequenceReader KEYWORD2
IrSequenceReader KEYWORD2
IrSequenceReader KEYWORD2
IrSequenceReader KEYWORD2
~IrSequenceReader KEYWORD2
getFrequency KEYWORD2
receive KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion src/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/HashDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand Down
16 changes: 14 additions & 2 deletions src/HashDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/InfraredTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
typedef uint16_t microseconds_t;
//typedef uint32_t microseconds_t;
/** Largest microseconds_t number possible */
constexpr static microseconds_t MICROSECONDS_T_MAX = static_cast<microseconds_t>(-1);
static constexpr microseconds_t MICROSECONDS_T_MAX = static_cast<microseconds_t>(-1);

/**
* Type for durations in milli seconds.
* Using a larger type than 16 bits probably is not sensible.
*/
typedef uint16_t milliseconds_t;
/** Largest milliseconds_t number possible */
constexpr static milliseconds_t MILLISECONDS_T_MAX = static_cast<milliseconds_t>(-1);
static constexpr milliseconds_t MILLISECONDS_T_MAX = static_cast<milliseconds_t>(-1);

/**
* Type for modulation frequency in Hz.
Expand All @@ -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;
17 changes: 11 additions & 6 deletions src/IrDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down Expand Up @@ -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_;
}
Expand Down
13 changes: 10 additions & 3 deletions src/IrReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
};

Expand Down
2 changes: 1 addition & 1 deletion src/IrSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/IrSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/IrSenderPwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/IrSenderPwmSoft.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/IrSequenceReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
};

Expand Down
6 changes: 3 additions & 3 deletions src/IrWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/IrWidgetAggregating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/MultiDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down
6 changes: 3 additions & 3 deletions src/Nec1Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Nec1Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<microseconds_t>(108000UL - sum); i++;
microseconds_t *repeatData = new microseconds_t[repeatLength];
repeatData[0] = 9024U;
repeatData[1] = 2256U;
Expand Down
10 changes: 5 additions & 5 deletions src/Nec1Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 1 addition & 3 deletions src/Pronto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 14 additions & 13 deletions src/Pronto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {};

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Rc5Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
class Rc5Decoder : public IrDecoder {
public:
static const char *format;

/**
* Constructs a Rc5Decoder from an IrReader, containing data.
Expand Down Expand Up @@ -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;
};
Loading

0 comments on commit 9479d8e

Please sign in to comment.