add std::optional example

This commit is contained in:
Amar Mahmutbegovic
2024-07-04 01:09:13 +02:00
parent ada1e2d80e
commit 984346bd77
4 changed files with 87 additions and 2 deletions

View File

@@ -21,7 +21,6 @@ template <class T, std::size_t N> struct ring_buffer {
}
};
int main()
{
hal::init();

View File

@@ -0,0 +1,28 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
int main()
{
hal::init();
hal::uart_stm32 uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
printf("std::expected example\r\n");
while(true)
{
}
}

View File

@@ -0,0 +1,58 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <optional>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
struct sensor {
struct data {
int x;
int y;
};
static inline bool ret_val = true;
static std::optional<data> get_data() {
ret_val = !ret_val;
if(ret_val) {
return data{4, 5};
}
else {
return std::nullopt;
}
}
};
int main()
{
hal::init();
hal::uart_stm32 uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
printf("std::optional example\r\n");
auto get_data_from_main = [] () {
auto result = sensor::get_data();
if(result) {
printf("x = %d, y = %d\r\n", (*result).x, (*result).y);
}
else {
printf("No data!\r\n");
}
};
get_data_from_main();
get_data_from_main();
while(true)
{
}
}