Skip to content

Commit

Permalink
Renamed modules: :encoder_[input/output].bitbang
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Mar 17, 2021
1 parent e0ffa71 commit 47098b9
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/dlr/scons-build-tools
Submodule scons-build-tools added at 6c35aa
76 changes: 76 additions & 0 deletions src/modm/driver/encoder/encoder_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2021, Thomas Sommer
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_ENCODER_HPP
#define MODM_ENCODER_HPP

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

namespace modm
{
/**
* \brief Encoder
*
* \ingroup modm_ui_encoder
* \author 2021 Thomas Sommer
*/

/// @ingroup modm_driver_encoder
struct encoder
{
typedef enum Division
{
ONE,
TWO,
FOUR
} Division_t;
};

template<typename PH_A, typename PH_B, encoder::Division_t DIVISION,
std::signed_integral UnderlyingType = int8_t>
class Encoder
{
using Phases = modm::platform::SoftwareGpioPort<PH_A, PH_B>;

public:
Encoder() : enc_delta(0), raw_last(0){};

inline void
connect()
{
Phases::setInput(::Gpio::InputType::PullUp);

// Tare power-on state
modm::delay(10us);
UnderlyingType raw = 0;
auto read = Phases::read();
if (read & 0b10) raw = 3;
if (read & 0b01) raw ^= 1;
raw_last = raw;
}

// Call @1kHz for manual movement
inline void
update();

UnderlyingType
getValue();

private:
UnderlyingType enc_delta;
UnderlyingType raw_last;
};
} // namespace modm

#include "encoder_input_impl.hpp"

#endif // MODM_ENCODER_HPP
32 changes: 32 additions & 0 deletions src/modm/driver/encoder/encoder_input.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019, Thomas Sommer
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":driver:encoder_input.bitbang"
module.description = """
# Quadrature Encoder Input
This driver decodes a AB (incremental) encoder signal.
Ported from code created by Peter Dannegger:
https://www.mikrocontroller.net/articles/Drehgeber.
"""


def prepare(module, options):
module.depends(":architecture:atomic")
return True


def build(env):
env.outbasepath = "modm/src/modm/driver/encoder"
env.copy("encoder_input.hpp")
env.copy("encoder_input_impl.hpp")
43 changes: 43 additions & 0 deletions src/modm/driver/encoder/encoder_input_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef MODM_ENCODER_HPP
#error "Don't include this file directly, use 'encoder_input.hpp' instead!"
#endif

template<typename PH_A, typename PH_B, modm::encoder::Division_t DIVISION, std::signed_integral UnderlyingType>
inline void
modm::Encoder<PH_A, PH_B, DIVISION, UnderlyingType>::update()
{
const auto read = Phases::read();

UnderlyingType raw = 0;
if (read & 0b10) raw = 3;
if (read & 0b01) raw ^= 1; // convert gray to binary

const auto diff = raw_last - raw;
if (diff & 1)
{
raw_last = raw; // store new as next last
enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
}
}

template<typename PH_A, typename PH_B, modm::encoder::Division_t DIVISION, std::signed_integral UnderlyingType>
UnderlyingType
modm::Encoder<PH_A, PH_B, DIVISION, UnderlyingType>::getValue()
{
::modm::atomic::Lock _;
UnderlyingType val = enc_delta; // counts since last call
switch (DIVISION)
{
case encoder::Division::ONE:
enc_delta = 0;
break;
case encoder::Division::TWO:
enc_delta = val & 1;
val >>= 1;
break;
case encoder::Division::FOUR:
enc_delta = val & 3;
val >>= 2;
}
return val;
}
2 changes: 1 addition & 1 deletion src/modm/driver/encoder/encoder_output.lb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def init(module):
module.name = ":driver:encoder.output"
module.name = ":driver:encoder_output.bitbang"
module.description = """\
# Quadrature Encoder Output
Expand Down

0 comments on commit 47098b9

Please sign in to comment.