67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
#include <array>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
|
|
#include "adc.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()
|
|
{
|
|
init();
|
|
|
|
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_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_SET);
|
|
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);
|
|
|
|
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(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
|
HAL_Delay(1000);
|
|
}
|
|
|
|
return 0;
|
|
}
|