Skip to content

Commit

Permalink
static_assert 2check if DIVISION fits UnderlyingType
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Mar 20, 2021
1 parent 0a9471a commit 0b91ea0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
32 changes: 0 additions & 32 deletions src/modm/driver/encoder/encoder.lb

This file was deleted.

25 changes: 12 additions & 13 deletions src/modm/driver/encoder/encoder_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/
// ----------------------------------------------------------------------------

#ifndef MODM_ENCODER_HPP
#define MODM_ENCODER_HPP
#ifndef MODM_ENCODER_INPUT_HPP
#define MODM_ENCODER_INPUT_HPP

#include <modm/platform.hpp>
#include <bit>
#include <modm/platform.hpp>
#include <type_traits>

namespace modm
Expand All @@ -36,16 +36,15 @@ namespace modm
* @tparam DIVISION Resolution: How many Singal toggles to count one increment.
* @tparam UnderlyingType Must be signed integer
*/
template<
typename SignalA,
typename SignalB,
uint8_t DIVISION = 4,
std::signed_integral UnderlyingType = int8_t
>
template<typename SignalA, typename SignalB, uint8_t DIVISION = 4,
std::signed_integral UnderlyingType = int8_t>
class BitBangEncoderInput
{
static_assert(std::popcount(DIVISION) == 1, "DIVISION must be an integer to basis 2 and not 0.");

static_assert(std::popcount(DIVISION) == 1,
"DIVISION must be an integer to basis 2 and not 0. F.e. 1, 2, 4, 8, ...");
static_assert(DIVISION <= std::numeric_limits<UnderlyingType>::max(),
"UnderlyingType is to small for DIVISION.");

using Phases = modm::platform::SoftwareGpioPort<SignalA, SignalB>;

public:
Expand All @@ -58,8 +57,8 @@ class BitBangEncoderInput
Phases::setInput(::Gpio::InputType::PullUp);

// Tare power-on state
modm::delay(10us);
UnderlyingType raw = 0;
modm::delay(10us);
auto read = Phases::read();
if (read & 0b10) raw = 3;
if (read & 0b01) raw ^= 1;
Expand All @@ -81,4 +80,4 @@ class BitBangEncoderInput

#include "encoder_input_impl.hpp"

#endif // MODM_ENCODER_HPP
#endif // MODM_ENCODER_INPUT_HPP
2 changes: 1 addition & 1 deletion src/modm/driver/encoder/encoder_input_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef MODM_ENCODER_HPP
#ifndef MODM_ENCODER_INPUT_IMPL_HPP
#error "Don't include this file directly, use 'encoder_input.hpp' instead!"
#endif

Expand Down

0 comments on commit 0b91ea0

Please sign in to comment.