Implement sampling ADC and printing over VSP
This commit is contained in:
parent
f678273def
commit
e6205458c5
21
AdaptiveBrightnessFirmware/Inc/utils.hpp
Normal file
21
AdaptiveBrightnessFirmware/Inc/utils.hpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
|
||||||
|
template<typename Fn, size_t... Ints>
|
||||||
|
auto for_constexpr(Fn&& func, std::index_sequence<Ints...>)
|
||||||
|
{
|
||||||
|
if constexpr(std::is_void_v<std::invoke_result_t<Fn, std::integral_constant<size_t, 0>>>) {
|
||||||
|
(func(std::integral_constant<size_t, Ints>{}), ...);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if((func(std::integral_constant<size_t, Ints>{}) && ...))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace util
|
@ -1,6 +1,34 @@
|
|||||||
#include <cstdint>
|
#include <array>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "adc.h"
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
|
#include "usbd_cdc_if.h"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
template<size_t SensorID>
|
||||||
|
uint16_t readLightSensor()
|
||||||
|
{
|
||||||
|
static_assert(SensorID >= 0 && SensorID < 3, "Invalid light sensor ID");
|
||||||
|
|
||||||
|
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, 100);
|
||||||
|
adcValues[i] = HAL_ADC_GetValue(&hadc);
|
||||||
|
},
|
||||||
|
std::make_index_sequence<3>{});
|
||||||
|
|
||||||
|
HAL_ADC_Stop(&hadc);
|
||||||
|
|
||||||
|
return adcValues[SensorID];
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -17,17 +45,21 @@ int main()
|
|||||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
// HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||||
HAL_Delay(1000);
|
|
||||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
|
||||||
|
|
||||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
|
util::for_constexpr(
|
||||||
HAL_Delay(1000);
|
[](const auto& idx) {
|
||||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
constexpr auto i = idx.value;
|
||||||
|
const auto ldrVal = readLightSensor<i>();
|
||||||
|
std::array<uint8_t, 32> printBuffer;
|
||||||
|
const auto bufLen = std::sprintf(reinterpret_cast<char*>(printBuffer.data()), "LDR%d: %d\r\n%s", i + 1, ldrVal, (i == 2) ? "\r\n" : "");
|
||||||
|
if(bufLen > 0)
|
||||||
|
CDC_Transmit_FS(printBuffer.data(), bufLen);
|
||||||
|
},
|
||||||
|
std::make_index_sequence<3>{});
|
||||||
|
|
||||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
// HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
||||||
HAL_Delay(1000);
|
HAL_Delay(1000);
|
||||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user