Sample all ADC channels and add transmit indicator LED
This commit is contained in:
parent
a5c15cd746
commit
67aa37561e
@ -8,11 +8,8 @@
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "utils.hpp"
|
||||
|
||||
template<size_t SensorID>
|
||||
uint16_t readLightSensor()
|
||||
std::array<uint16_t, 3> sampleLightSensors()
|
||||
{
|
||||
static_assert(SensorID >= 0 && SensorID < 3, "Invalid light sensor ID");
|
||||
|
||||
std::array<uint16_t, 3> adcValues;
|
||||
|
||||
HAL_ADC_Start(&hadc);
|
||||
@ -20,14 +17,14 @@ uint16_t readLightSensor()
|
||||
util::for_constexpr(
|
||||
[&adcValues](const auto& idx) {
|
||||
constexpr auto i = idx.value;
|
||||
HAL_ADC_PollForConversion(&hadc, 100);
|
||||
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
|
||||
adcValues[i] = HAL_ADC_GetValue(&hadc);
|
||||
},
|
||||
std::make_index_sequence<3>{});
|
||||
std::make_index_sequence<adcValues.size()>{});
|
||||
|
||||
HAL_ADC_Stop(&hadc);
|
||||
|
||||
return adcValues[SensorID];
|
||||
return adcValues;
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -45,20 +42,21 @@ int main()
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
|
||||
while(true) {
|
||||
// HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||
const auto ldrValues = sampleLightSensors();
|
||||
|
||||
util::for_constexpr(
|
||||
[](const auto& idx) {
|
||||
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);
|
||||
std::array<uint8_t, 32> printBuffer;
|
||||
|
||||
for(uint8_t i = 0; i < ldrValues.size(); ++i) {
|
||||
const auto bufLen = std::sprintf(reinterpret_cast<char*>(printBuffer.data()), "LDR%d: %hu\r\n%s", i + 1, ldrValues[i], (i == 2) ? "\r\n" : "");
|
||||
if(bufLen > 0) {
|
||||
while(CDC_Transmit_FS(printBuffer.data(), bufLen) == USBD_BUSY)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_Delay(1000);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user