WIP3
This commit is contained in:
parent
4d897ad5c6
commit
66461af1e4
@ -10,5 +10,20 @@ target_include_directories(peripherals
|
||||
$<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
|
||||
skullc_install_packages(skullc peripherals ${SKULLC_VERSION})
|
||||
|
||||
@ -10,6 +10,14 @@
|
||||
|
||||
#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
|
||||
{
|
||||
|
||||
@ -20,7 +28,7 @@ enum class ButtonPress : std::uint32_t
|
||||
LONG_PRESS
|
||||
};
|
||||
|
||||
template<typename G, typename H>
|
||||
template<Hal::Gpio G, Hal::StaticHal H>
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
@ -70,6 +78,23 @@ public:
|
||||
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:
|
||||
bool was_pressed_ = false;
|
||||
std::uint32_t time_pressed_down_ = 0;
|
||||
|
||||
@ -40,4 +40,9 @@ concept RegisterInterface = requires(T t, RegName reg, std::uint8_t* data, const
|
||||
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
|
||||
|
||||
@ -14,6 +14,10 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
#ifdef SKULLC_WITH_CORO
|
||||
#include "skullc/coro/peripheral_awaiters.hpp"
|
||||
#endif
|
||||
|
||||
#define USE_DELAY_US
|
||||
|
||||
namespace Peripherals
|
||||
@ -92,6 +96,13 @@ struct Gpio
|
||||
void toggle() { HAL_GPIO_TogglePin(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) \
|
||||
|
||||
@ -4,11 +4,11 @@
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "skullc/coro/task.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/sleep.hpp"
|
||||
#include "skullc/coro/task.hpp"
|
||||
#include "skullc/coro/this_coro.hpp"
|
||||
|
||||
#include <semaphore>
|
||||
|
||||
@ -43,7 +43,7 @@ skullc::coro::Task<> test_semaphore_coro(T* semaphore, const int expected)
|
||||
co_return;
|
||||
}
|
||||
|
||||
}
|
||||
}// namespace
|
||||
|
||||
TEST_CASE("Scheduler runs coroutines.", "[coro]")
|
||||
{
|
||||
@ -97,8 +97,7 @@ TEST_CASE("Scheduler runs coroutines.", "[coro]")
|
||||
|
||||
scheduler.start_tasks(
|
||||
test_semaphore_coro(&semaphore, 0),
|
||||
test_semaphore_coro(&counting_semaphore, 1)
|
||||
);
|
||||
test_semaphore_coro(&counting_semaphore, 1));
|
||||
scheduler.loop(0);
|
||||
|
||||
REQUIRE(test_coro_called == 0);
|
||||
@ -120,8 +119,7 @@ TEST_CASE("Scheduler runs coroutines.", "[coro]")
|
||||
{
|
||||
scheduler.start_tasks(
|
||||
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);
|
||||
@ -169,7 +167,7 @@ skullc::coro::Task<> await_pin_edge(TestGpio* pin, const Peripherals::Hal::Edge
|
||||
co_return;
|
||||
}
|
||||
|
||||
}
|
||||
}// namespace
|
||||
|
||||
TEST_CASE("Peripheral awaiters work.", "[coro],[awaiters]")
|
||||
{
|
||||
|
||||
@ -10,5 +10,10 @@ target_include_directories(coro
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
target_compile_definitions(coro
|
||||
INTERFACE
|
||||
SKULLC_WITH_CORO
|
||||
)
|
||||
|
||||
## INSTALL
|
||||
skullc_install_packages(skullc coro ${SKULLC_VERSION})
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
|
||||
#include "peripherals_hal_concepts.hpp"
|
||||
|
||||
#include "skullc/coro/this_coro.hpp"
|
||||
#include "skullc/coro/scheduler.hpp"
|
||||
#include "skullc/coro/this_coro.hpp"
|
||||
|
||||
namespace skullc::coro
|
||||
{
|
||||
@ -20,14 +20,8 @@ struct PinEdgeAwaiter : TaskPoller
|
||||
{
|
||||
PinEdgeAwaiter() = delete;
|
||||
explicit PinEdgeAwaiter(G* pin, const Peripherals::Hal::Edge& edge)
|
||||
: pin(pin)
|
||||
, awaited_edge(edge)
|
||||
{
|
||||
if (awaited_edge == Peripherals::Hal::Edge::Rising)
|
||||
previous_is_set = false;
|
||||
else
|
||||
previous_is_set = true;
|
||||
}
|
||||
: pin(pin), awaited_edge(edge), previous_is_set(pin->read())
|
||||
{}
|
||||
|
||||
~PinEdgeAwaiter() override
|
||||
{
|
||||
@ -83,4 +77,4 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}// namespace skullc::coro
|
||||
|
||||
@ -9,8 +9,8 @@
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "skullc/coro/task.hpp"
|
||||
#include "skullc/coro/scheduler_base.hpp"
|
||||
#include "skullc/coro/task.hpp"
|
||||
|
||||
namespace skullc::coro
|
||||
{
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <cstdint>
|
||||
|
||||
@ -28,4 +29,4 @@ public:
|
||||
virtual void add_poller(TaskPoller* poller) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
}// namespace skullc::coro
|
||||
@ -56,4 +56,4 @@ auto await_semaphore(T& semaphore)
|
||||
return Awaitable{&semaphore};
|
||||
}
|
||||
|
||||
}
|
||||
}// namespace skullc::coro
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
#include "skullc/coro/scheduler.hpp"
|
||||
#include "skullc/coro/this_coro.hpp"
|
||||
|
||||
#include "peripherals_hal_concepts.hpp"
|
||||
|
||||
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()};
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user