1
+ /*
2
+ * Copyright (c) 2021, Thomas Sommer
3
+ *
4
+ * This file is part of the modm project.
5
+ *
6
+ * This Source Code Form is subject to the terms of the Mozilla Public
7
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
8
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
+ */
10
+
11
+ #pragma once
12
+
13
+ #include < modm/platform.hpp>
14
+
15
+ namespace modm
16
+ {
17
+ /* *
18
+ * @brief Gray code decoder using STM32 Timer mode
19
+ *
20
+ * @tparam Timer STM32 General purpose timer
21
+ * @tparam SignalA First modm::platform::Gpio pin that connects to Timer::Ch1
22
+ * @tparam SignalB Second modm::platform::Gpio pin that connects to Timer::Ch2
23
+ */
24
+ template <class Timer , typename SignalA, typename SignalB, uint16_t POSTSCALER = 4 , std::signed_integral ValueType = int8_t >
25
+ class EncoderInput
26
+ {
27
+ public:
28
+ void
29
+ initialize (const bool invert = false , const uint8_t filter = 4 )
30
+ {
31
+ // TODO Pullup optional?
32
+ SignalA::setInput (modm::platform::Gpio::InputType::PullUp);
33
+ SignalB::setInput (modm::platform::Gpio::InputType::PullUp);
34
+ Timer::template connect<typename SignalA::Ch1, typename SignalB::Ch2>();
35
+
36
+ Timer::enable ();
37
+
38
+ Timer::setMode (invert ? Timer::Mode::UpCounter : Timer::Mode::DownCounter, Timer::SlaveMode::Encoder3);
39
+ Timer::setPrescaler (POSTSCALER);
40
+
41
+ if (filter) {
42
+ Timer::configureInputChannel (1 , filter);
43
+ Timer::configureInputChannel (2 , filter);
44
+ }
45
+
46
+ Timer::applyAndReset ();
47
+ Timer::start ();
48
+ last = Timer::getValue ();
49
+ }
50
+
51
+ uint16_t
52
+ getValue ()
53
+ {
54
+ return Timer::getValue ();
55
+ }
56
+
57
+ ValueType
58
+ getIncrement ()
59
+ {
60
+ uint16_t current = Timer::getValue ();
61
+ int16_t delta = current - last;
62
+ last = current;
63
+ return delta;
64
+ }
65
+
66
+
67
+ private:
68
+ uint16_t last = 0 ;
69
+ };
70
+ } // namespace modm
0 commit comments