From c1eadf4cb172c086cb8846152e0848d65da38536 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 21 Feb 2020 20:38:32 +0100 Subject: [PATCH] Implemented different configs and refactored implementation into Impl class --- adc.hpp | 105 ++++++++++++++++++++++++++++++++++++----------------- config.hpp | 22 +++++++++++ 2 files changed, 94 insertions(+), 33 deletions(-) diff --git a/adc.hpp b/adc.hpp index 0f8adab..859f14d 100644 --- a/adc.hpp +++ b/adc.hpp @@ -1,4 +1,5 @@ -#pragma once +#ifndef ADC_HPP +#define ADC_HPP #include "config.hpp" #include "hardware.hpp" @@ -9,6 +10,30 @@ namespace adc { +namespace detail { + +extern void (*fnAdcIntHandler)(uint16_t); + +class AdcImpl { + private: + using callback_t = void (*)(uint16_t); + + public: + static void init(callback_t callback) + { + detail::fnAdcIntHandler = callback; + } + + static void init() {} + + static uint16_t read() + { + return 0; + } +}; + +} // namespace detail + template class Adc { public: @@ -16,43 +41,57 @@ class Adc { }; template -class Adc { - private: - using callback_t = void (*)(uint16_t); - +class Adc : public detail::AdcImpl { public: static_assert(detail::supports_adc_v, "Pin does not support ADC"); - - Adc() {} - - Adc(callback_t callback) : m_callback(callback) {} - - uint16_t read() - { - return 0; - } - - private: - const callback_t m_callback = nullptr; }; template -class Adc { - private: - using callback_t = void (*)(uint16_t); - - public: - Adc() {} - - Adc(callback_t callback) : m_callback(callback) {} - - uint16_t read() - { - return 0; - } - - private: - const callback_t m_callback = nullptr; +class Adc : public detail::AdcImpl { }; } // namespace adc + +#endif + +////////////////////////////////////////////////////////////////////////// + +#ifdef ADC_INT_VECTOR + +#include + +#include + +namespace adc { +namespace detail { + +#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega328P__) + +void (*fnAdcIntHandler)(uint16_t) = nullptr; + +using reg_ptr_t = volatile uint8_t *; + +template +static inline reg_ptr_t getRegPtr() +{ + return reinterpret_cast(Address); +} + +ISR(ADC_vect) +{ + if (fnAdcIntHandler) { + const auto adcSample = *getRegPtr() | (*getRegPtr() << 8); + fnAdcIntHandler(adcSample); + } +} + +#else +#error "This chip is not supported" +#endif + +} // namespace detail +} // namespace adc + +#undef ADC_INT_VECTORS + +#endif diff --git a/config.hpp b/config.hpp index b3f83c3..4e7d412 100644 --- a/config.hpp +++ b/config.hpp @@ -4,12 +4,16 @@ namespace adc { +namespace detail { + enum class Mode { SINGLE, AUTO, FREE_RUNNING, }; +} // namespace detail + enum class TriggerSource { FREE_RUNNING, ANALOG_COMP, @@ -46,6 +50,24 @@ enum class InputSource { template struct Config { + static constexpr auto MODE = detail::Mode::AUTO; + static constexpr auto TRIGGER_SRC = Mode::SRC; + static constexpr auto VREF = vref; + static constexpr auto PRESCALER = prescaler; +}; + +template +struct Config { + static constexpr auto MODE = detail::Mode::FREE_RUNNING; + static constexpr auto VREF = vref; + static constexpr auto PRESCALER = prescaler; +}; + +template +struct Config { + static constexpr auto MODE = detail::Mode::SINGLE; + static constexpr auto VREF = vref; + static constexpr auto PRESCALER = prescaler; }; } // namespace adc