WIP add super basic gps handler

This commit is contained in:
2023-02-28 01:34:04 +01:00
parent 9b22bc6acf
commit 8bd5604a3c
5 changed files with 108 additions and 17 deletions

View File

@@ -99,6 +99,18 @@ struct buffer {
return true;
}
template<typename Func_t>
bool execute(const Func_t& executor)
{
const auto lk = commons::lock<commons::mutex>::get(m);
if(!lk)
{
return false;
}
executor(buf);
}
bool print() const
{
const auto lk = commons::lock<commons::mutex>::get(m);
@@ -120,3 +132,62 @@ private:
mutable commons::mutex m;
};
class gps_data
{
public:
void extract_gps_data(auto& buf)
{
buf.execute([this](auto& value) { this->copy_to_own_buf(value); });
}
void copy_to_own_buf(std::span<uint8_t> other)
{
auto view = std::string_view{reinterpret_cast<const char*>(other.data()), other.size()};
const auto it_begin = view.find("$GPGGA");
if(it_begin == view.npos)
{
return;
}
view.remove_prefix(it_begin);
const auto it_end = view.find("\r\n", it_begin);
if(it_end == view.npos)
{
return;
}
view.remove_suffix(view.size() - it_end);
if(view.size() > max_size)
{
return;
}
std::copy(view.begin(), view.end(), current_line.begin());
current_size = std::distance(view.begin(), view.end());
valid = true;
}
bool is_valid() const
{
return valid;
}
void print() const
{
log::info("Current GPS Data:");
uart_interface::write(
{reinterpret_cast<const char *>(current_line.data()), current_size});
log::linebreak();
}
private:
static constexpr uint8_t max_size = 82; //defined by GPS NMEA 0183 protocol
size_t current_size = 0;
std::array<uint8_t, max_size> current_line;
bool valid = false;
};

View File

@@ -59,6 +59,11 @@ class log {
return logger_impl.init();
}
static void linebreak() {
const auto val = std::string_view{"\r\n"};
return logger->log(val);
}
template <typename... Args>
static void error(Args&&... args) {
if (!log_impl_begin(LogLevel::ERROR)) {

View File

@@ -72,9 +72,13 @@ class cmd_holder {
};
static bool updated_main_buf = false;
static buffer<uint8_t, 512> main_buffer;
static bool print_main_buf = false;
static buffer<uint8_t, 512> main_buffer{};
static gps_data gps;
void print_buf(void) { main_buffer.print(); }
void print_gps(void) { gps.print(); }
void print_buf_toggle(void) { print_main_buf = !print_main_buf; }
class cmd_handler {
public:
@@ -135,12 +139,15 @@ class cmd_handler {
void print_help_() const { commands.print_help(); }
private:
static constexpr cmd_holder<6> commands{
static constexpr cmd_holder<8> commands{
std::make_tuple("ver", "Prints current version.", &print_version),
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug),
std::make_tuple("buf", "Prints gps uart buffer", &print_buf),
std::make_tuple("gps", "Prints captured gps data", &print_gps),
std::make_tuple("buf", "Prints uart2 buffer", &print_buf),
std::make_tuple("buft", "toggles continous printing of uart2 buffer",
&print_buf_toggle),
std::make_tuple("help", "Prints available commands", &print_help)};
array_t symbols;
@@ -178,7 +185,6 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (value == '\r') {
cmd_handler::get().queue_execution();
HAL_GPIO_TogglePin(LED_PORT, LED2_PIN);
return;
}
@@ -195,7 +201,6 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
if (main_buffer.copy_from(gps_interface::new_rx_buf, Size)) {
log::debug("Copied to main buff");
updated_main_buf = true;
}
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
@@ -282,6 +287,11 @@ int main(void) {
if (!MX_SPI1_Init()) {
// toggle status led or something
}
HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET);
HAL_Delay(10);
log::debug("SPI1 Initialized.");
if (!gps_interface::init()) {
log::error("UART2 Initialization failed, needed for GPS");
@@ -289,11 +299,6 @@ int main(void) {
log::debug("Uart2 Initialized");
}
HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET);
HAL_Delay(1000);
log::debug("SPI1 Initialized.");
log::debug("Initialization done.");
char OP_Mode = 0x01;
@@ -308,6 +313,8 @@ int main(void) {
// RF95_Init();
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET);
while (1) {
cmd_handler::get().run();
@@ -317,12 +324,20 @@ int main(void) {
// RF95_receive(LoRa_buff);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
HAL_Delay(100);
// HAL_Delay(100);
// if (updated_main_buf) {
// log::debug("Printing main buf");
// updated_main_buf = !main_buffer.print();
// }
if (gps.is_valid()) {
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET);
}
if (updated_main_buf) {
updated_main_buf = false;
gps.extract_gps_data(main_buffer);
if (print_main_buf) {
updated_main_buf = !main_buffer.print();
}
}
// std::string_view msg{reinterpret_cast<char *>(LoRa_buff)};
// log::info("Received Message");

View File

@@ -9,4 +9,4 @@ std::array<uint8_t, 1> uart_interface::new_rx_buf{};
template <>
uint8_t gps_interface::rx_buff = 0;
template <>
std::array<uint8_t, 64> gps_interface::new_rx_buf{};
std::array<uint8_t, 256> gps_interface::new_rx_buf{};

View File

@@ -239,4 +239,4 @@ using uart_interface = uart_handler<UARTTX_PIN, UARTRX_PIN, UART_PORT,
USART1_BASE, UartMode::Interrupt>;
using gps_interface = uart_handler<UART2TX_PIN, UART2RX_PIN, UART2_PORT,
USART2_BASE, UartMode::DMA, 64>;
USART2_BASE, UartMode::DMA, 256>;