2020-06-28 16:23:20 +02:00
|
|
|
#include <array>
|
|
|
|
|
2020-06-28 14:22:46 +02:00
|
|
|
#include <cstdint>
|
2020-06-28 16:23:20 +02:00
|
|
|
#include <cstdio>
|
2020-06-28 14:22:46 +02:00
|
|
|
|
2020-06-28 16:23:20 +02:00
|
|
|
#include "adc.h"
|
2020-06-28 14:22:46 +02:00
|
|
|
#include "init.h"
|
2020-06-29 00:33:14 +02:00
|
|
|
#include "terminal.hpp"
|
|
|
|
#include "uart.hpp"
|
2020-06-28 16:23:20 +02:00
|
|
|
#include "utils.hpp"
|
|
|
|
|
2020-06-28 17:05:29 +02:00
|
|
|
std::array<uint16_t, 3> sampleLightSensors()
|
2020-06-28 16:23:20 +02:00
|
|
|
{
|
|
|
|
std::array<uint16_t, 3> adcValues;
|
|
|
|
|
|
|
|
HAL_ADC_Start(&hadc);
|
|
|
|
|
|
|
|
util::for_constexpr(
|
|
|
|
[&adcValues](const auto& idx) {
|
|
|
|
constexpr auto i = idx.value;
|
2020-06-28 17:05:29 +02:00
|
|
|
HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
|
2020-06-28 16:23:20 +02:00
|
|
|
adcValues[i] = HAL_ADC_GetValue(&hadc);
|
|
|
|
},
|
2020-06-28 17:05:29 +02:00
|
|
|
std::make_index_sequence<adcValues.size()>{});
|
2020-06-28 16:23:20 +02:00
|
|
|
|
|
|
|
HAL_ADC_Stop(&hadc);
|
|
|
|
|
2020-06-28 17:05:29 +02:00
|
|
|
return adcValues;
|
2020-06-28 16:23:20 +02:00
|
|
|
}
|
2020-06-28 14:22:46 +02:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-06-29 00:33:14 +02:00
|
|
|
uart::Vcp<> serial;
|
2020-06-28 14:22:46 +02:00
|
|
|
|
2020-06-29 00:33:14 +02:00
|
|
|
init();
|
|
|
|
serial.init();
|
2020-06-28 21:22:17 +02:00
|
|
|
|
2020-06-28 14:26:34 +02:00
|
|
|
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);
|
|
|
|
|
2020-06-28 21:22:17 +02:00
|
|
|
uint8_t data = 0;
|
|
|
|
|
2020-06-28 14:22:46 +02:00
|
|
|
while(true) {
|
2020-06-29 00:33:14 +02:00
|
|
|
if(serial.rxByte(data)) {
|
|
|
|
serial.txByte(data);
|
2020-06-28 21:22:17 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:05:29 +02:00
|
|
|
const auto ldrValues = sampleLightSensors();
|
|
|
|
|
|
|
|
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
|
|
|
|
2020-06-29 00:33:14 +02:00
|
|
|
std::array<char, 32> printBuffer;
|
2020-06-28 17:05:29 +02:00
|
|
|
|
|
|
|
for(uint8_t i = 0; i < ldrValues.size(); ++i) {
|
2020-06-28 17:15:06 +02:00
|
|
|
const auto ldrID = i + 1;
|
|
|
|
const auto percentage = ldrValues[i] * 100 / 0xFFF;
|
2020-06-29 00:33:14 +02:00
|
|
|
const auto bufLen = std::sprintf(printBuffer.data(), "LDR%d: %04hu - %03d%%\r\n%s", ldrID, ldrValues[i], percentage, (i == 2) ? "\r\n" : "");
|
2020-06-28 17:05:29 +02:00
|
|
|
if(bufLen > 0) {
|
2020-06-29 00:33:14 +02:00
|
|
|
serial.txString(printBuffer.data());
|
|
|
|
serial.flushTx();
|
2020-06-28 17:05:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
2020-06-28 14:26:34 +02:00
|
|
|
HAL_Delay(1000);
|
2020-06-28 14:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|