|
| 1 | +#include"simple_logger.hpp" |
| 2 | +#include"sound_controller.hpp" |
| 3 | +#include"sound_on_state_handler.hpp" |
| 4 | +#include"transmission_in_progress_state_handler.hpp" |
| 5 | +#include"transmission_ready_state_handler.hpp" |
| 6 | + |
| 7 | +#include<array> |
| 8 | +#include<iostream> |
| 9 | + |
| 10 | +namespaceco_fsm::morse |
| 11 | +{ |
| 12 | + std::ostream&operator<< (std::ostream& out,const automaton_id item) |
| 13 | + { |
| 14 | +staticconst std::array<constchar*const,1U> texts { |
| 15 | +"morse_fsm", |
| 16 | + }; |
| 17 | + |
| 18 | + out << texts[static_cast<int>(item)]; |
| 19 | +return out; |
| 20 | + } |
| 21 | + |
| 22 | + std::ostream&operator<< (std::ostream& out,const event_id item) |
| 23 | + { |
| 24 | +staticconst std::array<constchar*const,5U> texts { |
| 25 | +"transmit_message","transmit_symbol","transmit_ready","do_beep","beep_done", |
| 26 | + }; |
| 27 | + |
| 28 | + out << texts[static_cast<int>(item)]; |
| 29 | +return out; |
| 30 | + } |
| 31 | + |
| 32 | + std::ostream&operator<< (std::ostream& out,const state_id item) |
| 33 | + { |
| 34 | +staticconst std::array<constchar*const,3U> texts { |
| 35 | +"transmission_ready", |
| 36 | +"transmission_in_progress", |
| 37 | +"sound_on", |
| 38 | + }; |
| 39 | + |
| 40 | + out << texts[static_cast<int>(item)]; |
| 41 | +return out; |
| 42 | + } |
| 43 | + |
| 44 | +voidsetup(FSM& fsm, sound_controller& sound_controller) |
| 45 | + { |
| 46 | +constexpr std::uint8_t wordsPerMinute =12U;// Approximate transmission speed in words per minute. |
| 47 | + |
| 48 | +// Register the state coroutines and give them ids. |
| 49 | + fsm << (coroutine(fsm, transmission_ready_state_handler {}).set_id(state_id::transmission_ready)) |
| 50 | + << (coroutine(fsm, transmission_in_progress_state_handler {wordsPerMinute}).set_id(state_id::transmission_in_progress)) |
| 51 | + << (coroutine(fsm, sound_on_state_handler {sound_controller}).set_id(state_id::sound_on)); |
| 52 | + |
| 53 | +// Configure the transition table in format ("From State", "Event", "To State") |
| 54 | +// Example: event_id::transmit_symbol sent from state_id::transmission_ready state goes to |
| 55 | +// state_id::transmission_in_progress state |
| 56 | + fsm <<FSM::transition(state_id::transmission_ready, event_id::transmit_symbol, state_id::transmission_in_progress) |
| 57 | + <<FSM::transition(state_id::transmission_in_progress, event_id::transmit_ready, state_id::transmission_ready) |
| 58 | + <<FSM::transition(state_id::transmission_in_progress, event_id::do_beep, state_id::sound_on) |
| 59 | + <<FSM::transition(state_id::sound_on, event_id::beep_done, state_id::transmission_in_progress); |
| 60 | + |
| 61 | +// Change to "#if 1" to use live tracing. |
| 62 | +// You can filter the log messages out by running "sudo ./fsm-example-morse 2> /dev/null" |
| 63 | +// The sudo is needed only if LEDs are used (i.e. built with "make linux") |
| 64 | +#if0 |
| 65 | + fsm.set_logger(simple_logger<FSM> {std::cerr}); |
| 66 | +#endif |
| 67 | + |
| 68 | +// Launch the state coroutines and set the initial state. |
| 69 | + fsm.start().go_to(state_id::transmission_ready); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +intmain() |
| 74 | +{ |
| 75 | +usingnamespaceco_fsm::morse; |
| 76 | + sound_controller sound_controller {}; |
| 77 | + FSM fsm {automaton_id::morse_fsm}; |
| 78 | +setup(fsm, sound_controller); |
| 79 | + |
| 80 | +// Make the first event which will start the show. |
| 81 | + Event event {}; |
| 82 | + |
| 83 | +// Send these sentences |
| 84 | +const std::vector<std::string> messages {"Hello World","SOS SOS","Wikipedia the free encyclopedia"}; |
| 85 | +for (constauto& message: messages) |
| 86 | + { |
| 87 | + std::cout <<"Message = '" << message <<"'\n"; |
| 88 | + event.set_message(event_id::transmit_message, message); |
| 89 | + fsm.send_event(std::move(event)); |
| 90 | + } |
| 91 | + |
| 92 | + std::cout <<"\n'" << fsm.id() <<"' is suspended at state '" << fsm.state_id() <<"'\n"; |
| 93 | +return0; |
| 94 | +} |