blink-led.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  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. };
  11. } // namespace message
  12. struct Blinker : rl::Actor<2> {
  13. using Parent = Actor<2>;
  14. void initialize() override {
  15. subscribe(&Blinker::on_blink_command);
  16. Parent::initialize();
  17. }
  18. void advance_start() override {
  19. Parent::advance_start();
  20. blink();
  21. }
  22. void blink() {
  23. Board::toggle_led();
  24. add_event<rl::ctx::thread>(
  25. delay,
  26. [](void *data) {
  27. auto self = static_cast<Blinker *>(data);
  28. self->send<rl::ctx::thread, message::BlinkCommand>(0, self->id);
  29. },
  30. this);
  31. }
  32. void on_blink_command(message::BlinkCommand &msg) { blink(); }
  33. rl::Duration delay;
  34. };
  35. using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
  36. rl::message::ChangeStateAck,
  37. message::BlinkCommand>;
  38. using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
  39. using Planner = rl::Planner<2>; /* upto 2 time events */
  40. using Supervisor =
  41. rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Blinker>;
  42. Supervisor sup;
  43. int main(int, char **) {
  44. Board::init_start();
  45. Board::enable_timer();
  46. ROTOR_LIGHT_ENABLE_INTERRUPTS();
  47. /* setup */
  48. Queue queue;
  49. Planner planner;
  50. rl::Context context{&queue, &planner, &Board::get_now};
  51. sup.bind(context);
  52. auto blinker = sup.get_child<0>();
  53. blinker->delay = rl::Duration{250000};
  54. /* let it polls timer */
  55. sup.start(true);
  56. /* main cycle */
  57. sup.process();
  58. return 0;
  59. }