ping-pong-poll.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 korifey
  3. #include "gpio.h"
  4. #include "main.h"
  5. #include "rotor-light.hpp"
  6. extern "C" void SystemClock_Config(void);
  7. extern "C" int64_t get_current_time(void);
  8. namespace rl = rotor_light;
  9. namespace message {
  10. struct Ping : rl::Message {
  11. using Message::Message;
  12. static constexpr auto type_id = __LINE__;
  13. rl::MessageTypeId get_type_id() const override { return type_id; }
  14. };
  15. struct Pong : rl::Message {
  16. using Message::Message;
  17. static constexpr auto type_id = __LINE__;
  18. rl::MessageTypeId get_type_id() const override { return type_id; }
  19. };
  20. } // namespace message
  21. struct Pinger : rl::Actor<2> {
  22. using Parent = Actor<2>;
  23. void initialize() override {
  24. subscribe(&Pinger::on_pong);
  25. Parent::initialize();
  26. }
  27. void advance_start() override {
  28. Parent::advance_start();
  29. ping();
  30. }
  31. void ping() { send<message::Ping>(0, ponger_id); }
  32. void on_pong(message::Pong &) {
  33. LL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
  34. add_event(
  35. 500, [](void *data) { static_cast<Pinger *>(data)->ping(); }, this);
  36. }
  37. rl::ActorId ponger_id;
  38. };
  39. struct Ponger : rl::Actor<2> {
  40. using Parent = Actor<2>;
  41. void initialize() override {
  42. subscribe(&Ponger::on_ping);
  43. Parent::initialize();
  44. }
  45. void on_ping(message::Ping &) {
  46. LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
  47. add_event(
  48. 200, [](void *data) { static_cast<Ponger *>(data)->reply(); }, this);
  49. }
  50. void reply() { send<message::Pong>(0, pinger_id); }
  51. rl::ActorId pinger_id;
  52. };
  53. using Supervisor =
  54. rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Pinger, Ponger>;
  55. using Storage = rl::traits::MessageStorage<rl::message::ChangeState,
  56. rl::message::ChangeStateAck,
  57. message::Ping, message::Pong>;
  58. using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
  59. using Planner = rl::Planner<2>; /* upto 1 time event */
  60. int main(int, char **) {
  61. /* Reset of all peripherals, Initializes the Flash interface and the Systick.
  62. */
  63. LL_APB4_GRP1_EnableClock(LL_APB4_GRP1_PERIPH_SYSCFG);
  64. /* System interrupt init*/
  65. NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  66. /* Update the time base */
  67. /* Configure the system clock */
  68. SystemClock_Config();
  69. MX_GPIO_Init();
  70. SysTick_Config(SystemCoreClock / 1000);
  71. /* allocate */
  72. Queue queue;
  73. Planner planner;
  74. rl::Context context{&queue, &planner, &get_current_time};
  75. Supervisor sup;
  76. /* setup */
  77. sup.bind(context);
  78. auto pinger = sup.get_child<0>();
  79. auto ponger = sup.get_child<1>();
  80. pinger->ponger_id = ponger->get_id();
  81. ponger->pinger_id = pinger->get_id();
  82. /* let it polls timer */
  83. sup.start(true);
  84. /* main cycle */
  85. sup.process();
  86. return 0;
  87. }