AdaptiveBrightness/AdaptiveBrightnessFirmware/Src/main.cpp

72 lines
1.9 KiB
C++
Raw Normal View History

#include <array>
2020-06-28 14:22:46 +02:00
#include <cstdint>
#include <cstdio>
2020-06-28 14:22:46 +02:00
#include "init.h"
2020-07-03 18:34:39 +02:00
#include "light_sensors.hpp"
2020-06-29 00:33:14 +02:00
#include "terminal.hpp"
#include "uart.hpp"
2020-07-03 17:43:52 +02:00
using serial_t = uart::Vcp<>;
2020-07-03 18:14:46 +02:00
static volatile bool g_error = false;
extern "C" void Error_Handler(void)
{
g_error = true;
}
2020-07-03 17:43:52 +02:00
static inline void initializeHardware()
2020-06-28 14:22:46 +02:00
{
2020-07-03 17:43:52 +02:00
serial_t serial;
2020-06-28 14:22:46 +02:00
2020-07-03 18:14:46 +02:00
do {
g_error = false;
init();
serial.init();
if(g_error)
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
} while(g_error);
2020-07-03 17:43:52 +02:00
}
int main()
{
initializeHardware();
2020-06-28 21:22:17 +02:00
2020-07-03 18:14:46 +02:00
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
2020-06-28 14:26:34 +02:00
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
2020-07-03 18:14:46 +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);
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-07-03 17:43:52 +02:00
serial_t serial;
2020-07-03 18:34:39 +02:00
LightSensors lightSensors;
2020-06-28 21:22:17 +02:00
2020-06-28 14:22:46 +02:00
while(true) {
2020-07-03 18:34:39 +02:00
const auto ldrValues = lightSensors.getValues();
2020-06-29 00:33:14 +02:00
std::array<char, 32> printBuffer;
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" : "");
if(bufLen > 0) {
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
2020-06-29 00:33:14 +02:00
serial.txString(printBuffer.data());
serial.flushTx();
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;
}