68 lines
1.9 KiB
C++
68 lines
1.9 KiB
C++
#include <array>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
|
|
#include "adc.h"
|
|
#include "init.h"
|
|
#include "usbd_cdc_if.h"
|
|
#include "utils.hpp"
|
|
|
|
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;
|
|
}
|
|
|
|
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) {
|
|
const auto ldrValues = sampleLightSensors();
|
|
|
|
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
|
|
|
std::array<uint8_t, 32> printBuffer;
|
|
|
|
for(uint8_t i = 0; i < ldrValues.size(); ++i) {
|
|
const auto ldrID = i + 1;
|
|
const auto percentage = ldrValues[i] * 100 / 0xFFF;
|
|
const auto bufLen =
|
|
std::sprintf(reinterpret_cast<char*>(printBuffer.data()), "LDR%d: %04hu - %03d%%\r\n%s", ldrID, ldrValues[i], percentage, (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);
|
|
}
|
|
|
|
return 0;
|
|
}
|