WIP3
Some checks failed
CI & Unit Tests / Unit-Tests (push) Failing after 13s
CI & Unit Tests / Docs (push) Successful in 10s

This commit is contained in:
Erki 2025-02-06 21:27:51 +02:00
parent 4d897ad5c6
commit 66461af1e4
11 changed files with 92 additions and 30 deletions

View File

@ -10,5 +10,20 @@ target_include_directories(peripherals
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )
if (DEFINED SKULLC_USE_HAL_ST)
set(PERIPHERALS_DEFINITIONS SKULLC_USE_HAL_ST)
endif ()
if (DEFINED SKULLC_USE_HAL_ESP)
set(PERIPHERALS_DEFINITIONS SKULLC_USE_HAL_ESP)
endif ()
if (DEFINED SKULLC_WITH_CORO)
list(APPEND PERIPHERALS_DEFINITIONS SKULLC_WITH_CORO)
endif ()
target_compile_definitions(peripherals
INTERFACE
${PERIPHERALS_DEFINITIONS}
)
## INSTALL ## INSTALL
skullc_install_packages(skullc peripherals ${SKULLC_VERSION}) skullc_install_packages(skullc peripherals ${SKULLC_VERSION})

View File

@ -10,6 +10,14 @@
#include <cstdint> #include <cstdint>
#include "peripherals_hal_concepts.hpp"
#ifdef SKULLC_WITH_CORO
#include "skullc/coro/sleep.hpp"
#include "skullc/coro/task.hpp"
#include "skullc/coro/this_coro.hpp"
#endif
namespace Peripherals namespace Peripherals
{ {
@ -20,7 +28,7 @@ enum class ButtonPress : std::uint32_t
LONG_PRESS LONG_PRESS
}; };
template<typename G, typename H> template<Hal::Gpio G, Hal::StaticHal H>
class Button class Button
{ {
public: public:
@ -70,6 +78,23 @@ public:
return current_state_; return current_state_;
} }
#ifdef SKULLC_WITH_CORO
skullc::coro::Task<ButtonPress> await_press()
{
ButtonPress result = ButtonPress::NOT_PRESSED;
while (result == ButtonPress::NOT_PRESSED)
{
update();
result = getState();
if (result == ButtonPress::NOT_PRESSED)
co_await skullc::coro::sleep(std::chrono::milliseconds(hal::getMillis()), std::chrono::milliseconds(1));
}
co_return result;
}
#endif
private: private:
bool was_pressed_ = false; bool was_pressed_ = false;
std::uint32_t time_pressed_down_ = 0; std::uint32_t time_pressed_down_ = 0;

View File

@ -40,4 +40,9 @@ concept RegisterInterface = requires(T t, RegName reg, std::uint8_t* data, const
t.readRegisterMultibyte(reg, data, len, std::uint32_t{}); t.readRegisterMultibyte(reg, data, len, std::uint32_t{});
}; };
template<typename T>
concept StaticHal = requires() {
{ T::getMillis() } -> std::same_as<std::uint32_t>;
};
}// namespace Peripherals::Hal }// namespace Peripherals::Hal

View File

@ -14,6 +14,10 @@
#include <array> #include <array>
#ifdef SKULLC_WITH_CORO
#include "skullc/coro/peripheral_awaiters.hpp"
#endif
#define USE_DELAY_US #define USE_DELAY_US
namespace Peripherals namespace Peripherals
@ -92,6 +96,13 @@ struct Gpio
void toggle() { HAL_GPIO_TogglePin(port, pin); } void toggle() { HAL_GPIO_TogglePin(port, pin); }
bool read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); } bool read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); }
#ifdef SKULLC_WITH_CORO
auto await_edge(const Edge& edge)
{
return skullc::coro::PinEdgeAwaiter(this, edge);
}
#endif
}; };
#define CREATE_GPIO(name) \ #define CREATE_GPIO(name) \

View File

@ -4,11 +4,11 @@
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include "skullc/coro/task.hpp"
#include "skullc/coro/scheduler.hpp" #include "skullc/coro/scheduler.hpp"
#include "skullc/coro/this_coro.hpp"
#include "skullc/coro/sleep.hpp"
#include "skullc/coro/semaphore.hpp" #include "skullc/coro/semaphore.hpp"
#include "skullc/coro/sleep.hpp"
#include "skullc/coro/task.hpp"
#include "skullc/coro/this_coro.hpp"
#include <semaphore> #include <semaphore>
@ -43,7 +43,7 @@ skullc::coro::Task<> test_semaphore_coro(T* semaphore, const int expected)
co_return; co_return;
} }
} }// namespace
TEST_CASE("Scheduler runs coroutines.", "[coro]") TEST_CASE("Scheduler runs coroutines.", "[coro]")
{ {
@ -97,8 +97,7 @@ TEST_CASE("Scheduler runs coroutines.", "[coro]")
scheduler.start_tasks( scheduler.start_tasks(
test_semaphore_coro(&semaphore, 0), test_semaphore_coro(&semaphore, 0),
test_semaphore_coro(&counting_semaphore, 1) test_semaphore_coro(&counting_semaphore, 1));
);
scheduler.loop(0); scheduler.loop(0);
REQUIRE(test_coro_called == 0); REQUIRE(test_coro_called == 0);
@ -120,8 +119,7 @@ TEST_CASE("Scheduler runs coroutines.", "[coro]")
{ {
scheduler.start_tasks( scheduler.start_tasks(
test_sleepy_coro(0, std::chrono::milliseconds{2}), test_sleepy_coro(0, std::chrono::milliseconds{2}),
test_sleepy_coro(1, std::chrono::milliseconds{2}) test_sleepy_coro(1, std::chrono::milliseconds{2}));
);
scheduler.loop(0); scheduler.loop(0);
scheduler.loop(0); scheduler.loop(0);
@ -169,7 +167,7 @@ skullc::coro::Task<> await_pin_edge(TestGpio* pin, const Peripherals::Hal::Edge
co_return; co_return;
} }
} }// namespace
TEST_CASE("Peripheral awaiters work.", "[coro],[awaiters]") TEST_CASE("Peripheral awaiters work.", "[coro],[awaiters]")
{ {

View File

@ -10,5 +10,10 @@ target_include_directories(coro
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )
target_compile_definitions(coro
INTERFACE
SKULLC_WITH_CORO
)
## INSTALL ## INSTALL
skullc_install_packages(skullc coro ${SKULLC_VERSION}) skullc_install_packages(skullc coro ${SKULLC_VERSION})

View File

@ -9,8 +9,8 @@
#include "peripherals_hal_concepts.hpp" #include "peripherals_hal_concepts.hpp"
#include "skullc/coro/this_coro.hpp"
#include "skullc/coro/scheduler.hpp" #include "skullc/coro/scheduler.hpp"
#include "skullc/coro/this_coro.hpp"
namespace skullc::coro namespace skullc::coro
{ {
@ -20,14 +20,8 @@ struct PinEdgeAwaiter : TaskPoller
{ {
PinEdgeAwaiter() = delete; PinEdgeAwaiter() = delete;
explicit PinEdgeAwaiter(G* pin, const Peripherals::Hal::Edge& edge) explicit PinEdgeAwaiter(G* pin, const Peripherals::Hal::Edge& edge)
: pin(pin) : pin(pin), awaited_edge(edge), previous_is_set(pin->read())
, awaited_edge(edge) {}
{
if (awaited_edge == Peripherals::Hal::Edge::Rising)
previous_is_set = false;
else
previous_is_set = true;
}
~PinEdgeAwaiter() override ~PinEdgeAwaiter() override
{ {
@ -83,4 +77,4 @@ private:
} }
}; };
} }// namespace skullc::coro

View File

@ -9,8 +9,8 @@
#include <cstdint> #include <cstdint>
#include <utility> #include <utility>
#include "skullc/coro/task.hpp"
#include "skullc/coro/scheduler_base.hpp" #include "skullc/coro/scheduler_base.hpp"
#include "skullc/coro/task.hpp"
namespace skullc::coro namespace skullc::coro
{ {

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <chrono>
#include <coroutine> #include <coroutine>
#include <cstdint> #include <cstdint>
@ -28,4 +29,4 @@ public:
virtual void add_poller(TaskPoller* poller) = 0; virtual void add_poller(TaskPoller* poller) = 0;
}; };
} }// namespace skullc::coro

View File

@ -56,4 +56,4 @@ auto await_semaphore(T& semaphore)
return Awaitable{&semaphore}; return Awaitable{&semaphore};
} }
} }// namespace skullc::coro

View File

@ -7,6 +7,8 @@
#include "skullc/coro/scheduler.hpp" #include "skullc/coro/scheduler.hpp"
#include "skullc/coro/this_coro.hpp" #include "skullc/coro/this_coro.hpp"
#include "peripherals_hal_concepts.hpp"
namespace skullc::coro namespace skullc::coro
{ {
@ -48,4 +50,10 @@ inline auto sleep(const std::chrono::duration<uint32_t, std::milli>& time_now, c
return detail::TimeAwaitable{time_now.count() + duration.count()}; return detail::TimeAwaitable{time_now.count() + duration.count()};
} }
template<Peripherals::Hal::StaticHal Hal>
auto sleep(const std::chrono::duration<uint32_t, std::milli>& duration)
{
return detail::TimeAwaitable{Hal::getMillis() + duration.count()};
}
}// namespace skullc::coro }// namespace skullc::coro