|
4 | 4 | // SPDX-License-Identifier: MIT |
5 | 5 | // Author: Sergei Shirokov <[email protected]> |
6 | 6 |
|
| 7 | +#include "application.hpp" |
| 8 | + |
| 9 | +#include <cetl/pf17/cetlpf.hpp> |
7 | 10 | #include <libcyphal/application/node.hpp> |
| 11 | +#include <libcyphal/executor.hpp> |
| 12 | +#include <libcyphal/types.hpp> |
8 | 13 |
|
| 14 | +#include <algorithm> |
| 15 | +#include <chrono> |
9 | 16 | #include <iostream> |
10 | 17 |
|
| 18 | +using namespace std::chrono_literals; |
| 19 | + |
| 20 | +using Callback = libcyphal::IExecutor::Callback; |
| 21 | + |
11 | 22 | int main() |
12 | 23 | { |
13 | | - const std::string str{"LibCyphal demo."}; |
14 | | - std::cout << str << "\n"; |
| 24 | + Application application; |
| 25 | + auto& executor = application.executor(); |
| 26 | + |
| 27 | + std::cout << "LibCyphal demo." << "\n"; |
| 28 | + |
| 29 | + auto cb = executor.registerCallback([](auto&) { |
| 30 | + // |
| 31 | + std::cout << "Callback fired." << "\n"; |
| 32 | + }); |
| 33 | + cb.schedule(Callback::Schedule::Repeat{executor.now(), libcyphal::Duration{1s}}); |
| 34 | + |
| 35 | + // Main loop. |
| 36 | + // |
| 37 | + libcyphal::Duration worst_lateness{0}; |
| 38 | + const libcyphal::TimePoint deadline = executor.now() + 4s; |
| 39 | + std::cout << "-----------\nRunning..." << std::endl; // NOLINT |
| 40 | + // |
| 41 | + while (executor.now() < deadline) |
| 42 | + { |
| 43 | + const auto spin_result = executor.spinOnce(); |
| 44 | + worst_lateness = std::max(worst_lateness, spin_result.worst_lateness); |
| 45 | + |
| 46 | + libcyphal::Duration timeout{1s}; // awake at least once per second |
| 47 | + if (spin_result.next_exec_time.has_value()) |
| 48 | + { |
| 49 | + timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); |
| 50 | + } |
| 51 | + (void) executor.pollAwaitableResourcesFor(cetl::make_optional(timeout)); |
| 52 | + } |
| 53 | + // |
| 54 | + std::cout << "Done.\n-----------\nRun Stats:\n"; |
| 55 | + std::cout << " worst_callback_lateness=" << worst_lateness.count() << "us\n"; |
| 56 | + |
15 | 57 | return 0; |
16 | 58 | } |
0 commit comments