Skip to content

Commit

Permalink
Add static assertion on param in cyclic synchronous tranmission type
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jan 8, 2020
1 parent 4f1ef7b commit 6bbfffc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions libraries/CanBusSharerLib/PdoProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ class PdoTransmissionType final
{ return type; }

//! Cast input byte to an @ref PdoTransmissionType enumerator.
static constexpr PdoTransmissionType SYNCHRONOUS_CYCLIC_N(std::uint8_t n)
{ return static_cast<transmission_type>(n); }
template<std::uint8_t n>
static constexpr PdoTransmissionType SYNCHRONOUS_CYCLIC_N()
{
// https://stackoverflow.com/a/12109644
static_assert(n >= 0x01 && n <= 0xF0, "Illegal argument."); // [1-240]
return static_cast<transmission_type>(n);
}

private:
transmission_type type = SYNCHRONOUS_ACYCLIC;
Expand Down
2 changes: 1 addition & 1 deletion tests/testCanBusSharerLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ TEST_F(CanBusSharerTest, TransmitPdo)

const unsigned int n = 1;

PdoTransmissionType type = PdoTransmissionType::SYNCHRONOUS_CYCLIC_N(0x04);
PdoTransmissionType type = PdoTransmissionType::SYNCHRONOUS_CYCLIC_N<0x04>();

const std::uint16_t comm = 0x1800 + n - 1;
const std::uint8_t commLSB = comm & 0x00FF;
Expand Down

0 comments on commit 6bbfffc

Please sign in to comment.