ping-pong-poll.cpp 2.8 KB

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