blink-led.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Aliaksei Makarau
  3. #include "board.h"
  4. #include <rotor-light.hpp>
  5. namespace rl = rotor_light;
  6. namespace message {
  7. struct BlinkCommand : rl::Message {
  8. static constexpr auto type_id = __LINE__;
  9. using rl::Message::Message;
  10. rl::MessageTypeId get_type_id() const override { return type_id; }
  11. };
  12. } // namespace message
  13. struct Blinker : rl::Actor<2> {
  14. using Parent = Actor<2>;
  15. void initialize() override {
  16. subscribe(&Blinker::on_blink_command);
  17. Parent::initialize();
  18. }
  19. void advance_start() override {
  20. Parent::advance_start();
  21. blink();
  22. }
  23. void blink() {
  24. Board::toggle_led();
  25. add_event(
  26. delay,
  27. [](void *data) {
  28. auto self = static_cast<Blinker *>(data);
  29. self->send<message::BlinkCommand>(0, self->id);
  30. },
  31. this);
  32. }
  33. void on_blink_command(message::BlinkCommand &msg) { blink(); }
  34. rl::Duration delay;
  35. };
  36. using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
  37. rl::message::ChangeStateAck,
  38. message::BlinkCommand>;
  39. using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
  40. using Planner = rl::Planner<2>; /* upto 2 time events */
  41. using Supervisor =
  42. rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Blinker>;
  43. Supervisor sup;
  44. int main(int, char **) {
  45. Board::init_start();
  46. Board::enable_timer();
  47. Board::enable_interrupts();
  48. /* setup */
  49. Queue queue;
  50. Planner planner;
  51. rl::Context context{&queue, &planner, &Board::get_now};
  52. sup.bind(context);
  53. auto blinker = sup.get_child<0>();
  54. blinker->delay = rl::Duration{1};
  55. /* let it polls timer */
  56. sup.start(true);
  57. /* main cycle */
  58. sup.process();
  59. return 0;
  60. }