skullc-peripherals/Peripherals/Inc/peripherals_encoder.hpp
Erki e9b633e46c
All checks were successful
continuous-integration/drone/push Build is passing
Fix encoder to read and act properly
2021-04-18 17:32:27 +03:00

97 lines
1.6 KiB
C++

/*
* peripherals_encoder.hpp
*
* Created on: Apr 11, 2021
* Author: erki
*/
#ifndef PERIPHERALS_ENCODER_HPP_
#define PERIPHERALS_ENCODER_HPP_
#include <cstdint>
#include <tim.h>
namespace Peripherals
{
class Encoder
{
public:
enum class Dirs : std::uint8_t
{
FORWARD,
BACKWARD
};
Encoder() = delete;
explicit Encoder(TIM_HandleTypeDef* htim, const std::uint32_t channels)
: _htim(htim), _channels(channels)
{}
Encoder(const Encoder&) = delete;
Encoder(Encoder&&) = delete;
Encoder& operator=(const Encoder&) = delete;
Encoder& operator=(Encoder&&) = delete;
void start()
{
HAL_TIM_Base_Start_IT(_htim);
HAL_TIM_Encoder_Start_IT(_htim, _channels);
}
void stop()
{
HAL_TIM_Encoder_Stop_IT(_htim, _channels);
}
void reset()
{
__HAL_TIM_SET_COUNTER(_htim, 0);
_full_revolutions = 0;
}
void setRevolutionTickCount(const std::uint16_t& count)
{
__HAL_TIM_SET_AUTORELOAD(_htim, count);
}
std::uint16_t getCurrentClicks() const
{
const std::uint16_t val = __HAL_TIM_GET_COUNTER(_htim);
return val;
}
std::int32_t getFullRevolutions() const
{
return _full_revolutions;
}
Dirs getDirection() const
{
if (__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim))
return Dirs::BACKWARD;
else
return Dirs::FORWARD;
}
void timerUpdateEvent()
{
if (!__HAL_TIM_IS_TIM_COUNTING_DOWN(_htim))
_full_revolutions++;
else
_full_revolutions--;
}
private:
TIM_HandleTypeDef* _htim;
std::uint32_t _channels;
std::int32_t _full_revolutions = 0;
};
}// namespace Peripherals
#endif /* PERIPHERALS_ENCODER_HPP_ */