1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // SPDX-License-Identifier: MIT
- // SPDX-FileCopyrightText: 2022 Ivan Baidakou
- #include "board.h"
- namespace rl = rotor_light;
- namespace message {
- struct Ping : rl::Message {
- using Message::Message;
- static constexpr auto type_id = __LINE__;
- };
- struct Pong : rl::Message {
- using Message::Message;
- static constexpr auto type_id = __LINE__;
- };
- } // namespace message
- struct Pinger : rl::Actor<2> {
- using Parent = Actor<2>;
- void initialize() override {
- subscribe(&Pinger::on_pong);
- Parent::initialize();
- }
- void advance_start() override {
- Parent::advance_start();
- ping();
- }
- void ping() {
- Board::toggle_led();
- send<rl::ctx::thread, message::Ping>(0, ponger_id);
- }
- void on_pong(message::Pong &) {
- add_event<rl::ctx::thread>(
- 500000, [](void *data) { static_cast<Pinger *>(data)->ping(); }, this);
- }
- rl::ActorId ponger_id;
- };
- struct Ponger : rl::Actor<2> {
- using Parent = Actor<2>;
- void initialize() override {
- subscribe(&Ponger::on_ping);
- Parent::initialize();
- }
- void on_ping(message::Ping &) {
- //
- send<rl::ctx::thread, message::Pong>(0, pinger_id);
- }
- rl::ActorId pinger_id;
- };
- using Supervisor =
- rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Pinger, Ponger>;
- using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
- rl::message::ChangeStateAck,
- message::Ping, message::Pong>;
- using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
- using Planner = rl::Planner<1>; /* upto 1 time event */
- int main(int, char **) {
- Board::init_start();
- Board::enable_timer();
- ROTOR_LIGHT_ENABLE_INTERRUPTS();
- /* allocate */
- Queue queue;
- Planner planner;
- rl::Context context{&queue, &planner, &Board::get_now};
- Supervisor sup;
- /* setup */
- sup.bind(context);
- auto pinger = sup.get_child<0>();
- auto ponger = sup.get_child<1>();
- pinger->ponger_id = ponger->get_id();
- ponger->pinger_id = pinger->get_id();
- /* disable poll timer */
- sup.start(false);
- /* main cycle */
- while (true) {
- sup.process();
- auto next_event_time = planner.next_event();
- if (next_event_time) {
- Board::sleep(next_event_time);
- }
- }
- return 0;
- }
|