Refactor light sensor
This commit is contained in:
parent
29e4c62e6e
commit
5a26169539
44
AdaptiveBrightnessFirmware/Inc/light_sensors.hpp
Normal file
44
AdaptiveBrightnessFirmware/Inc/light_sensors.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "adc.h"
|
||||
#include "utils.hpp"
|
||||
|
||||
class LightSensors {
|
||||
public:
|
||||
static inline const std::array<uint16_t, 3>& getValues()
|
||||
{
|
||||
sampleLightSensors();
|
||||
return m_adcValues;
|
||||
}
|
||||
|
||||
template<size_t Idx>
|
||||
static inline const int16_t& getValue()
|
||||
{
|
||||
static_assert(Idx < m_adcValues.size(), "Invalid light sensor index");
|
||||
|
||||
sampleLightSensors();
|
||||
return m_adcValues[Idx];
|
||||
}
|
||||
|
||||
private:
|
||||
static std::array<uint16_t, 3> m_adcValues;
|
||||
|
||||
static void sampleLightSensors()
|
||||
{
|
||||
HAL_ADC_Start(&hadc);
|
||||
|
||||
util::for_constexpr(
|
||||
[](const auto& idx) {
|
||||
constexpr auto i = idx.value;
|
||||
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
|
||||
m_adcValues[i] = HAL_ADC_GetValue(&hadc);
|
||||
},
|
||||
std::make_index_sequence<m_adcValues.size()>{});
|
||||
|
||||
HAL_ADC_Stop(&hadc);
|
||||
}
|
||||
};
|
@ -74,7 +74,8 @@ Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
|
||||
# C++ sources
|
||||
CXX_SOURCES = \
|
||||
Src/main.cpp \
|
||||
Src/uart_vcp.cpp
|
||||
Src/uart_vcp.cpp \
|
||||
Src/light_sensors.cpp
|
||||
|
||||
# ASM sources
|
||||
ASM_SOURCES = \
|
||||
|
3
AdaptiveBrightnessFirmware/Src/light_sensors.cpp
Normal file
3
AdaptiveBrightnessFirmware/Src/light_sensors.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "light_sensors.hpp"
|
||||
|
||||
std::array<uint16_t, 3> LightSensors::m_adcValues = {};
|
@ -3,33 +3,13 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include "adc.h"
|
||||
#include "init.h"
|
||||
#include "light_sensors.hpp"
|
||||
#include "terminal.hpp"
|
||||
#include "uart.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
using serial_t = uart::Vcp<>;
|
||||
|
||||
std::array<uint16_t, 3> sampleLightSensors()
|
||||
{
|
||||
std::array<uint16_t, 3> adcValues;
|
||||
|
||||
HAL_ADC_Start(&hadc);
|
||||
|
||||
util::for_constexpr(
|
||||
[&adcValues](const auto& idx) {
|
||||
constexpr auto i = idx.value;
|
||||
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
|
||||
adcValues[i] = HAL_ADC_GetValue(&hadc);
|
||||
},
|
||||
std::make_index_sequence<adcValues.size()>{});
|
||||
|
||||
HAL_ADC_Stop(&hadc);
|
||||
|
||||
return adcValues;
|
||||
}
|
||||
|
||||
static volatile bool g_error = false;
|
||||
|
||||
extern "C" void Error_Handler(void)
|
||||
@ -65,9 +45,10 @@ int main()
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
|
||||
serial_t serial;
|
||||
LightSensors lightSensors;
|
||||
|
||||
while(true) {
|
||||
const auto ldrValues = sampleLightSensors();
|
||||
const auto ldrValues = lightSensors.getValues();
|
||||
|
||||
std::array<char, 32> printBuffer;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user