Skip to content

Commit d46c09d

Browse files
committed
added example for blue-pill, improved example for arduino-nano
1 parent 7e7d2e9 commit d46c09d

File tree

8 files changed

+200
-11
lines changed

8 files changed

+200
-11
lines changed

examples/arduino_nano/encoder_input/main.cpp renamed to examples/arduino_nano/encoder_input_bitbang/main.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
// ----------------------------------------------------------------------------
1111

1212
#include <modm/board.hpp>
13-
#include <modm/driver/encoder/bitbang_encoder_input.hpp>
14-
#include <modm/math/algorithm/prescaler.hpp>
1513
#include <modm/processing/timer.hpp>
14+
#include <modm/math/algorithm/prescaler.hpp>
15+
#include <modm/driver/encoder/bitbang_encoder_input.hpp>
1616

1717
using namespace modm::platform;
1818

1919
// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins)
2020
// The common third pin (usually in the middle) is connected to GND.
21-
// Don't add any resistors or filters. It's all in the MCU and the driver.
2221
modm::BitBangEncoderInput<Board::D11, Board::D12, 4> encoder;
2322

2423
MODM_ISR(TIMER2_COMPA)
@@ -48,22 +47,19 @@ main()
4847
Board::initialize();
4948
LedD13::setOutput();
5049

51-
encoder.connect();
52-
50+
encoder.initialize();
5351
init_Timer2();
5452
enableInterrupts();
5553

5654
int value(0);
5755

5856
modm::ShortPeriodicTimer heartbeat(500ms);
59-
modm::ShortPeriodicTimer outputValue(1000ms);
6057

6158
while (true)
6259
{
63-
if (heartbeat.execute()) Board::LedD13::toggle();
64-
if (outputValue.execute())
65-
{
66-
value += encoder.getIncrement();
60+
if (heartbeat.execute()) {
61+
Board::LedD13::toggle();
62+
value += encoder.getDelta();
6763
MODM_LOG_INFO << "value: " << value << modm::endl;
6864
}
6965
}

examples/arduino_nano/encoder_input/project.xml renamed to examples/arduino_nano/encoder_input_bitbang/project.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<library>
22
<extends>modm:arduino-nano</extends>
33
<options>
4-
<option name="modm:build:build.path">../../../build/arduino_nano/encoder_input</option>
4+
<option name="modm:build:build.path">../../../build/arduino_nano/encoder_input_bitbang</option>
55
</options>
66
<modules>
77
<module>modm:build:scons</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/processing/timer.hpp>
15+
#include <modm/driver/encoder/encoder_input.hpp>
16+
17+
// ----------------------------------------------------------------------------
18+
// Set the log level
19+
#undef MODM_LOG_LEVEL
20+
#define MODM_LOG_LEVEL modm::log::INFO
21+
22+
// Create an IODeviceWrapper around the Uart Peripheral we want to use
23+
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
24+
25+
// Set all four logger streams to use the UART
26+
modm::log::Logger modm::log::debug(loggerDevice);
27+
modm::log::Logger modm::log::info(loggerDevice);
28+
modm::log::Logger modm::log::warning(loggerDevice);
29+
modm::log::Logger modm::log::error(loggerDevice);
30+
31+
int
32+
main()
33+
{
34+
Board::initialize();
35+
36+
Usart2::connect<GpioOutputA2::Tx>();
37+
Usart2::initialize<Board::SystemClock, 115200_Bd>();
38+
39+
// Each Timer can drive one Encoder
40+
// For Timer2 and Timer3 you have 2 Gpio options
41+
// When using one of PB3 or PB4 you also have to disable JTAG debugging during shared pins
42+
43+
// Timer1:
44+
modm::EncoderInput<Timer1, GpioInputA8, GpioInputA9> encoder;
45+
46+
// Timer2:
47+
// modm::EncoderInput<Timer2, GpioInputA0, GpioInputA1> encoder;
48+
// modm::EncoderInput<Timer2, GpioInputA15, GpioInputB3> encoder;
49+
// AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
50+
51+
// Timer3:
52+
// modm::EncoderInput<Timer3, GpioInputA6, GpioInputA7> encoder;
53+
// modm::EncoderInput<Timer3, GpioInputB4, GpioInputB5> encoder;
54+
// Disable JTAG to make PB3, PB4 available as Gpio
55+
// AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
56+
57+
// Timer4:
58+
// modm::EncoderInput<Timer4, GpioInputB6, GpioInputB7> encoder;
59+
60+
encoder.initialize(true);
61+
62+
modm::ShortPeriodicTimer heartbeat(500ms);
63+
64+
while (true)
65+
{
66+
if(heartbeat.execute()) {
67+
Board::LedGreen::toggle();
68+
MODM_LOG_INFO << "Encoder Delta: " << encoder.getDelta() << modm::endl;
69+
MODM_LOG_INFO << "Encoder Absolut: " << encoder.getValue() << modm::endl;
70+
}
71+
}
72+
73+
return 0;
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Replace this with your custom programmer
2+
source [find interface/stlink-v2.cfg]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<library>
2+
<extends>modm:blue-pill-f103</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/blue_pill_f103/encoder_input</option>
5+
<option name="modm:build:openocd.cfg">openocd.cfg</option>
6+
</options>
7+
<modules>
8+
<module>modm:debug</module>
9+
<module>modm:platform:uart:2</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:driver:encoder_input</module>
12+
<module>modm:platform:timer:1</module>
13+
<module>modm:platform:timer:2</module>
14+
<module>modm:platform:timer:3</module>
15+
<module>modm:platform:timer:4</module>
16+
<module>modm:build:scons</module>
17+
</modules>
18+
</library>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/processing/timer.hpp>
15+
#include <modm/driver/encoder/bitbang_encoder_input.hpp>
16+
17+
// ----------------------------------------------------------------------------
18+
// Set the log level
19+
#undef MODM_LOG_LEVEL
20+
#define MODM_LOG_LEVEL modm::log::INFO
21+
22+
// Create an IODeviceWrapper around the Uart Peripheral we want to use
23+
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
24+
25+
// Set all four logger streams to use the UART
26+
modm::log::Logger modm::log::debug(loggerDevice);
27+
modm::log::Logger modm::log::info(loggerDevice);
28+
modm::log::Logger modm::log::warning(loggerDevice);
29+
modm::log::Logger modm::log::error(loggerDevice);
30+
31+
// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins)
32+
// The common third pin (usually in the middle) is connected to GND.
33+
modm::BitBangEncoderInput<GpioInputB6, GpioInputB7, 4, int16_t> encoder;
34+
35+
MODM_ISR(TIM2)
36+
{
37+
Timer2::acknowledgeInterruptFlags(Timer2::InterruptFlag::Update);
38+
encoder.update();
39+
}
40+
41+
void
42+
init_Timer2(const uint16_t period)
43+
{
44+
Timer2::enable();
45+
Timer2::setMode(Timer2::Mode::UpCounter);
46+
47+
Timer2::template setPeriod<Board::SystemClock>(period);
48+
Timer2::enableInterruptVector(true, 10);
49+
Timer2::enableInterrupt(Timer2::Interrupt::Update);
50+
51+
Timer2::applyAndReset();
52+
Timer2::start();
53+
}
54+
55+
int
56+
main()
57+
{
58+
Board::initialize();
59+
60+
Usart2::connect<GpioOutputA2::Tx>();
61+
Usart2::initialize<Board::SystemClock, 115200_Bd>();
62+
63+
encoder.initialize();
64+
init_Timer2(1000); // 1ms period
65+
66+
int value(0);
67+
68+
modm::ShortPeriodicTimer heartbeat(1s);
69+
70+
while (true)
71+
{
72+
if (heartbeat.execute()) {
73+
Board::LedGreen::toggle();
74+
75+
const auto delta = encoder.getDelta();
76+
MODM_LOG_INFO << "Delta: " << delta << modm::endl;
77+
78+
value += delta;
79+
MODM_LOG_INFO << "Encoder Absolut: " << value << modm::endl;
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Replace this with your custom programmer
2+
source [find interface/stlink-v2.cfg]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<library>
2+
<extends>modm:blue-pill-f103</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/blue_pill_f103/encoder_input_bitbang</option>
5+
<option name="modm:build:openocd.cfg">openocd.cfg</option>
6+
</options>
7+
<modules>
8+
<module>modm:debug</module>
9+
<module>modm:platform:uart:2</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:driver:encoder_input.bitbang</module>
12+
<module>modm:platform:timer:2</module>
13+
<module>modm:build:scons</module>
14+
</modules>
15+
</library>

0 commit comments

Comments
 (0)