blink-led-uart.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  3. #include "common.h"
  4. #include "rotor-light.hpp"
  5. #include <avr/interrupt.h>
  6. #include <avr/io.h>
  7. #include <avr/sleep.h>
  8. #include <avr/wdt.h>
  9. #include <string.h>
  10. #include <util/delay.h>
  11. #define LED PB5
  12. namespace rl = rotor_light;
  13. namespace message {
  14. struct ChangeInterval : rl::Message {
  15. static constexpr auto type_id = __LINE__;
  16. ChangeInterval(rl::ActorId to, const rl::Duration &delta_)
  17. : Message{to}, delta{delta_} {}
  18. rl::MessageTypeId get_type_id() const override { return type_id; }
  19. rl::Duration delta;
  20. };
  21. struct Notify : rl::Message {
  22. static constexpr auto type_id = __LINE__;
  23. Notify(rl::ActorId to, const char *data_) : Message(to), data{data_} {}
  24. rl::MessageTypeId get_type_id() const override { return type_id; }
  25. const char *data;
  26. };
  27. } // namespace message
  28. struct Blinker : rl::Actor<2> {
  29. using Parent = Actor<2>;
  30. void initialize() override {
  31. next_event = 0;
  32. subscribe(&Blinker::on_change_interval);
  33. Parent::initialize();
  34. }
  35. void advance_start() override {
  36. Parent::advance_start();
  37. next_event = add_event(
  38. delay, [](void *data) { static_cast<Blinker *>(data)->blink(); }, this);
  39. }
  40. void blink() {
  41. if (next_event) {
  42. supervisor->get_planner()->remove_event(next_event, this);
  43. }
  44. PORTB ^= (1 << LED);
  45. send<message::Notify>(0, notifier_id, "blink\r\n");
  46. next_event = add_event(
  47. delay, [](void *data) { static_cast<Blinker *>(data)->blink(); }, this);
  48. }
  49. void on_change_interval(message::ChangeInterval &msg) {
  50. if (delay > msg.delta) {
  51. send<message::Notify>(0, notifier_id, "dec\r\n");
  52. } else {
  53. send<message::Notify>(0, notifier_id, "inc\r\n");
  54. }
  55. delay = msg.delta;
  56. blink();
  57. }
  58. rl::ActorId notifier_id;
  59. rl::Duration delay;
  60. rl::TimePoint next_event;
  61. };
  62. struct Notifier : rl::Actor<2> {
  63. using Parent = Actor<2>;
  64. void initialize() override {
  65. ptr = end = nullptr;
  66. subscribe(&Notifier::on_notify);
  67. Parent::initialize();
  68. }
  69. void advance_start() override {
  70. Parent::advance_start();
  71. send<message::Notify>(0, id, "Ready\n");
  72. }
  73. void on_notify(message::Notify &message) {
  74. if (!ptr) { // prevent overloading
  75. cli();
  76. ptr = message.data;
  77. end = message.data + strlen(message.data);
  78. send_next();
  79. sei();
  80. }
  81. }
  82. void send_next() {
  83. if (end) {
  84. if (ptr == end) {
  85. ptr = nullptr;
  86. end = nullptr;
  87. } else {
  88. // enable empty data buffer interrupt
  89. UCSR0B |= (1 << UDRIE0);
  90. while (!(UCSR0A & (1 << UDRE0)))
  91. ;
  92. // if (!(UCSR0A & (1<<UDRE0))) {
  93. UDR0 = *ptr++;
  94. //}
  95. }
  96. }
  97. }
  98. volatile const char *ptr;
  99. const char *end;
  100. };
  101. static void app_hw_init();
  102. using Supervisor =
  103. rl::Supervisor<rl::SupervisorBase::min_handlers_amount, Blinker, Notifier>;
  104. using Storage =
  105. rl::traits::MessageStorage<rl::message::ChangeState,
  106. rl::message::ChangeStateAck,
  107. message::ChangeInterval, message::Notify>;
  108. using Queue = rl::Queue<Storage, 5>; /* upto 5 messages in 1 queue */
  109. using Planner = rl::Planner<2>; /* upto 2 time events */
  110. Queue queue;
  111. Planner planner;
  112. rl::Context context{&queue, &planner, &get_now};
  113. Supervisor sup;
  114. int main(int, char **) {
  115. app_hw_init();
  116. /* setup */
  117. sup.bind(context);
  118. auto blinker = sup.get_child<0>();
  119. blinker->delay = rl::Duration{1000000};
  120. blinker->notifier_id = sup.get_child<1>()->get_id();
  121. /* let it polls timer */
  122. sup.start(true);
  123. /* main cycle */
  124. sup.process();
  125. return 0;
  126. }
  127. static void app_hw_init() {
  128. // Initialize the application including WDT, PORTB.5 and TIMER0
  129. // We will now disable the watchdog.
  130. // Service the watchdog just to be sure to avoid pending timeout.
  131. wdt_reset();
  132. // Clear WDRF in MCUSR.
  133. MCUSR &= ~(1U << WDRF);
  134. // Write logical one to WDCE and WDE.
  135. // Keep the old prescaler setting to prevent unintentional time-out.
  136. WDTCSR |= (1U << WDCE) | (1U << WDE);
  137. // Turn off the WDT.
  138. WDTCSR = 0x00;
  139. // We will now initialize PORTB.5 to be used as an LED driver port.
  140. // Set PORTB.5 value to low.
  141. PORTB &= ~(1U << PORTB5);
  142. /* initializing PB5 which is connected to port 13 of uno as output*/
  143. DDRB |= (1 << LED);
  144. enable_timer();
  145. // USART
  146. #define BAUD 9600
  147. #include <util/setbaud.h>
  148. UBRR0H = UBRRH_VALUE;
  149. UBRR0L = UBRRL_VALUE;
  150. UCSR0B = (1 << RXEN0) // enable rx
  151. | (1 << TXEN0) // enable tx
  152. | (1 << RXCIE0) // enable rx interrupt
  153. ;
  154. /* Set frame format: 8data, 2stop bit */
  155. UCSR0C = (1 << USBS0) | (3 << UCSZ00);
  156. // Enable all interrupts.
  157. sei();
  158. }
  159. ISR(USART_RX_vect) {
  160. if (UCSR0A & (1 << RXC0)) {
  161. auto c = static_cast<char>(UDR0);
  162. auto blinker = sup.get_child<0>();
  163. const auto &value = blinker->delay;
  164. if (c == '+') {
  165. auto new_value = value + value / 10;
  166. blinker->send<message::ChangeInterval>(0, blinker->get_id(), new_value);
  167. } else if (c == '-') {
  168. auto new_value = value - value / 10;
  169. blinker->send<message::ChangeInterval>(0, blinker->get_id(), new_value);
  170. }
  171. }
  172. }
  173. ISR(USART_UDRE_vect) {
  174. // disable empty data buffer interrupt
  175. UCSR0B &= ~(1 << UDRIE0);
  176. auto notifier = sup.get_child<1>();
  177. notifier->send_next();
  178. }