|
| 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 | +} |
0 commit comments