-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed modules: :encoder_[input/output].bitbang
- Loading branch information
Showing
5 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
Submodule scons-build-tools
added at
6c35aa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters