WIP add super basic gps handler
This commit is contained in:
71
commons.hpp
71
commons.hpp
@@ -99,6 +99,18 @@ struct buffer {
|
|||||||
return true;
|
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
|
bool print() const
|
||||||
{
|
{
|
||||||
const auto lk = commons::lock<commons::mutex>::get(m);
|
const auto lk = commons::lock<commons::mutex>::get(m);
|
||||||
@@ -120,3 +132,62 @@ private:
|
|||||||
mutable commons::mutex m;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,11 @@ class log {
|
|||||||
return logger_impl.init();
|
return logger_impl.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void linebreak() {
|
||||||
|
const auto val = std::string_view{"\r\n"};
|
||||||
|
return logger->log(val);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
static void error(Args&&... args) {
|
static void error(Args&&... args) {
|
||||||
if (!log_impl_begin(LogLevel::ERROR)) {
|
if (!log_impl_begin(LogLevel::ERROR)) {
|
||||||
|
|||||||
45
main.cpp
45
main.cpp
@@ -72,9 +72,13 @@ class cmd_holder {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static bool updated_main_buf = false;
|
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_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 {
|
class cmd_handler {
|
||||||
public:
|
public:
|
||||||
@@ -135,12 +139,15 @@ class cmd_handler {
|
|||||||
void print_help_() const { commands.print_help(); }
|
void print_help_() const { commands.print_help(); }
|
||||||
|
|
||||||
private:
|
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("ver", "Prints current version.", &print_version),
|
||||||
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
|
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
|
||||||
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
|
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
|
||||||
std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug),
|
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)};
|
std::make_tuple("help", "Prints available commands", &print_help)};
|
||||||
|
|
||||||
array_t symbols;
|
array_t symbols;
|
||||||
@@ -178,7 +185,6 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
|||||||
|
|
||||||
if (value == '\r') {
|
if (value == '\r') {
|
||||||
cmd_handler::get().queue_execution();
|
cmd_handler::get().queue_execution();
|
||||||
HAL_GPIO_TogglePin(LED_PORT, LED2_PIN);
|
|
||||||
return;
|
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);
|
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
|
||||||
if (main_buffer.copy_from(gps_interface::new_rx_buf, Size)) {
|
if (main_buffer.copy_from(gps_interface::new_rx_buf, Size)) {
|
||||||
log::debug("Copied to main buff");
|
|
||||||
updated_main_buf = true;
|
updated_main_buf = true;
|
||||||
}
|
}
|
||||||
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
|
||||||
@@ -282,6 +287,11 @@ int main(void) {
|
|||||||
if (!MX_SPI1_Init()) {
|
if (!MX_SPI1_Init()) {
|
||||||
// toggle status led or something
|
// 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()) {
|
if (!gps_interface::init()) {
|
||||||
log::error("UART2 Initialization failed, needed for GPS");
|
log::error("UART2 Initialization failed, needed for GPS");
|
||||||
@@ -289,11 +299,6 @@ int main(void) {
|
|||||||
log::debug("Uart2 Initialized");
|
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.");
|
log::debug("Initialization done.");
|
||||||
|
|
||||||
char OP_Mode = 0x01;
|
char OP_Mode = 0x01;
|
||||||
@@ -308,6 +313,8 @@ int main(void) {
|
|||||||
|
|
||||||
// RF95_Init();
|
// RF95_Init();
|
||||||
|
|
||||||
|
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
cmd_handler::get().run();
|
cmd_handler::get().run();
|
||||||
|
|
||||||
@@ -317,12 +324,20 @@ int main(void) {
|
|||||||
// RF95_receive(LoRa_buff);
|
// RF95_receive(LoRa_buff);
|
||||||
// HAL_Delay(100);
|
// HAL_Delay(100);
|
||||||
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
|
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
|
||||||
HAL_Delay(100);
|
// HAL_Delay(100);
|
||||||
|
|
||||||
// if (updated_main_buf) {
|
if (gps.is_valid()) {
|
||||||
// log::debug("Printing main buf");
|
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET);
|
||||||
// updated_main_buf = !main_buffer.print();
|
}
|
||||||
// }
|
|
||||||
|
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)};
|
// std::string_view msg{reinterpret_cast<char *>(LoRa_buff)};
|
||||||
// log::info("Received Message");
|
// log::info("Received Message");
|
||||||
|
|||||||
@@ -9,4 +9,4 @@ std::array<uint8_t, 1> uart_interface::new_rx_buf{};
|
|||||||
template <>
|
template <>
|
||||||
uint8_t gps_interface::rx_buff = 0;
|
uint8_t gps_interface::rx_buff = 0;
|
||||||
template <>
|
template <>
|
||||||
std::array<uint8_t, 64> gps_interface::new_rx_buf{};
|
std::array<uint8_t, 256> gps_interface::new_rx_buf{};
|
||||||
|
|||||||
@@ -239,4 +239,4 @@ using uart_interface = uart_handler<UARTTX_PIN, UARTRX_PIN, UART_PORT,
|
|||||||
USART1_BASE, UartMode::Interrupt>;
|
USART1_BASE, UartMode::Interrupt>;
|
||||||
|
|
||||||
using gps_interface = uart_handler<UART2TX_PIN, UART2RX_PIN, UART2_PORT,
|
using gps_interface = uart_handler<UART2TX_PIN, UART2RX_PIN, UART2_PORT,
|
||||||
USART2_BASE, UartMode::DMA, 64>;
|
USART2_BASE, UartMode::DMA, 256>;
|
||||||
|
|||||||
Reference in New Issue
Block a user