Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
a38f6f2eaThis file is pinned to the selected release. Download the original to open it in your design tool.
module;
#include "stm32f1xx_hal.h"
#include <array>
#include <cstddef>
#include <cstdint>
export module Driver.LightSensorDriver;
import Driver.DriverComponent;
import Driver.LightSensorDriverConcept;
export namespace Driver
{
/**
* @class LightSensorDriver
* @brief Zero-CPU-overhead light sensor using ADC with DMA transfers
*
* Interfaces with phototransistor voltage divider via ADC. DMA continuously
* fills buffer without CPU intervention. Readings averaged for stability.
*/
class LightSensorDriver final : public DriverComponent
{
public:
explicit constexpr LightSensorDriver(ADC_HandleTypeDef &_hadc) noexcept
: hadc(_hadc)
{
}
~LightSensorDriver() = default;
LightSensorDriver() = delete;
LightSensorDriver(const LightSensorDriver &) = delete;
LightSensorDriver &operator=(const LightSensorDriver &) = delete;
LightSensorDriver(LightSensorDriver &&) = delete;
LightSensorDriver &operator=(LightSensorDriver &&) = delete;
[[nodiscard]] std::uint32_t getAmbientLightLevel() const noexcept;
// [[nodiscard]] constexpr bool onInit() noexcept { return true; }
[[nodiscard]] bool onStart();
[[nodiscard]] bool onStop();
private:
static constexpr std::size_t ADC_BUFFER_SIZE = 10U;
[[nodiscard]] bool startAdc() noexcept;
[[nodiscard]] bool stopAdc() noexcept;
ADC_HandleTypeDef &hadc;
std::array<std::uint16_t, ADC_BUFFER_SIZE> adcBuffer{};
};
static_assert(Driver::Concepts::LightSensorDriverConcept<LightSensorDriver>,
"LightSensorDriver must satisfy the concept requirements");
} // namespace Driver