PlayingMode.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2010 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef PLAYING_MODE_HPP
  11. #define PLAYING_MODE_HPP
  12. #include <iostream>
  13. #include <boost/any.hpp>
  14. #define FUSION_MAX_VECTOR_SIZE 20
  15. #include "Events.hpp"
  16. #include <boost/msm/back/favor_compile_time.hpp>
  17. #include <boost/msm/back/state_machine.hpp>
  18. #include <boost/msm/front/state_machine_def.hpp>
  19. #include <boost/msm/front/euml/euml.hpp>
  20. using namespace std;
  21. namespace msm = boost::msm;
  22. namespace euml = boost::msm::front::euml;
  23. struct PlayingMode_ : public msm::front::state_machine_def<PlayingMode_>
  24. {
  25. //flags
  26. struct NoFastFwd{};
  27. struct Playing : public msm::front::state<default_base_state,msm::front::sm_ptr>
  28. {
  29. template <class Event,class FSM>
  30. void on_entry(Event const&,FSM& )
  31. {
  32. std::cout << "starting: PlayingMode::Playing" << std::endl;
  33. std::cout << "playing song:" << m_fsm->get_current_song() << std::endl;
  34. }
  35. template <class Event,class FSM>
  36. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::Playing" << std::endl;}
  37. void set_sm_ptr(PlayingMode_* pl)
  38. {
  39. m_fsm = pl;
  40. }
  41. private:
  42. PlayingMode_* m_fsm;
  43. };
  44. struct Paused : public msm::front::state<>
  45. {
  46. template <class Event,class FSM>
  47. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::Paused" << std::endl;}
  48. template <class Event,class FSM>
  49. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::Paused" << std::endl;}
  50. };
  51. struct WaitingForNextPrev : public msm::front::state<>
  52. {
  53. template <class Event,class FSM>
  54. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::WaitingForNextPrev" << std::endl;}
  55. template <class Event,class FSM>
  56. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::WaitingForNextPrev" << std::endl;}
  57. };
  58. struct WaitingForEnd : public msm::front::state<>
  59. {
  60. template <class Event,class FSM>
  61. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::WaitingForEnd" << std::endl;}
  62. template <class Event,class FSM>
  63. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::WaitingForEnd" << std::endl;}
  64. };
  65. struct NoForward : public msm::front::state<>
  66. {
  67. template <class Event,class FSM>
  68. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::NoForward" << std::endl;}
  69. template <class Event,class FSM>
  70. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::NoForward" << std::endl;}
  71. };
  72. struct ForwardPressed : public msm::front::state<>
  73. {
  74. template <class Event,class FSM>
  75. void on_entry(Event const&,FSM& )
  76. {
  77. std::cout << "starting: PlayingMode::ForwardPressed," << "start timer" << std::endl;
  78. }
  79. template <class Event,class FSM>
  80. void on_exit(Event const&,FSM& )
  81. {
  82. std::cout << "finishing: PlayingMode::ForwardPressed," << "stop timer" << std::endl;
  83. }
  84. };
  85. struct FastForward : public msm::front::state<>
  86. {
  87. template <class Event,class FSM>
  88. void on_entry(Event const&,FSM& )
  89. {
  90. std::cout << "starting: PlayingMode::FastForward," << "start timer" << std::endl;
  91. }
  92. template <class Event,class FSM>
  93. void on_exit(Event const&,FSM& )
  94. {
  95. std::cout << "finishing: PlayingMode::FastForward," << "stop timer" << std::endl;
  96. }
  97. };
  98. struct StdDisplay : public msm::front::state<>
  99. {
  100. template <class Event,class FSM>
  101. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::StdDisplay" << std::endl;}
  102. template <class Event,class FSM>
  103. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::StdDisplay" << std::endl;}
  104. };
  105. struct SetPosition : public msm::front::state<>
  106. {
  107. typedef mpl::vector1<NoFastFwd> flag_list;
  108. template <class Event,class FSM>
  109. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::SetPosition" << std::endl;}
  110. template <class Event,class FSM>
  111. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::SetPosition" << std::endl;}
  112. };
  113. struct SetMark : public msm::front::state<>
  114. {
  115. template <class Event,class FSM>
  116. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::SetMark" << std::endl;}
  117. template <class Event,class FSM>
  118. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::SetMark" << std::endl;}
  119. };
  120. struct PlayingExit : public msm::front::exit_pseudo_state<EndPlay>
  121. {
  122. template <class Event,class FSM>
  123. void on_entry(Event const&,FSM& ) {std::cout << "starting: PlayingMode::PlayingExit" << std::endl;}
  124. template <class Event,class FSM>
  125. void on_exit(Event const&,FSM& ) {std::cout << "finishing: PlayingMode::PlayingExit" << std::endl;}
  126. };
  127. // transition action methods
  128. struct inc_song_counter : euml::euml_action<inc_song_counter>
  129. {
  130. template <class FSM,class EVT,class SourceState,class TargetState>
  131. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  132. {
  133. if (++fsm.m_SongIndex <= fsm.m_NumberOfSongs )
  134. {
  135. std::cout << "playing song:" << fsm.m_SongIndex << std::endl;
  136. }
  137. else
  138. {
  139. // last song => end playing, next play will start at the beginning
  140. fsm.m_SongIndex = 1;
  141. fsm.process_event(EndPlay());
  142. }
  143. }
  144. };
  145. void select_song(StartSong const& evt)
  146. {
  147. if ((evt.m_Selected>0) && (evt.m_Selected<=m_NumberOfSongs))
  148. {
  149. m_SongIndex = evt.m_Selected;
  150. std::cout << "selecting song:" << m_SongIndex << std::endl;
  151. }
  152. else
  153. {
  154. // play current song
  155. std::cout << "selecting song:" << m_SongIndex << std::endl;
  156. }
  157. }
  158. struct dec_song_counter : euml::euml_action<dec_song_counter>
  159. {
  160. template <class FSM,class EVT,class SourceState,class TargetState>
  161. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  162. {
  163. if (--fsm.m_SongIndex >0 )
  164. {
  165. std::cout << "playing song:" << fsm.m_SongIndex << std::endl;
  166. }
  167. else
  168. {
  169. // before first song => end playing
  170. fsm.m_SongIndex = 1;
  171. fsm.process_event(EndPlay());
  172. }
  173. }
  174. };
  175. struct send_NextSong : euml::euml_action<send_NextSong>
  176. {
  177. template <class FSM,class EVT,class SourceState,class TargetState>
  178. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  179. {
  180. fsm.process_event(NextSong());
  181. }
  182. };
  183. void do_fast_forward(ForwardTimer const&)
  184. {
  185. std::cout << "moving song forward..." << std::endl;
  186. }
  187. // transition guard methods
  188. struct fast_fwd_ok : euml::euml_action<fast_fwd_ok>
  189. {
  190. template <class FSM,class EVT,class SourceState,class TargetState>
  191. bool operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  192. {
  193. // guard accepts only if fast forward is possible (No SetPosition mode)
  194. return !fsm.is_flag_active<NoFastFwd>();
  195. }
  196. };
  197. // initial states / orthogonal zones
  198. typedef mpl::vector5<Playing,WaitingForNextPrev,WaitingForEnd,NoForward,StdDisplay>
  199. initial_state;
  200. typedef PlayingMode_ fsm; // makes transition table cleaner
  201. // Transition table for player
  202. struct transition_table : mpl::vector19<
  203. // Start Event Next Action Guard
  204. // +--------------------+---------------------+--------------------+--------------------------+----------------------+
  205. _row < Playing , PlayPause , Paused >,
  206. _row < Playing , Off , Paused >,
  207. a_row < Playing , StartSong , Playing , &fsm::select_song >,
  208. _row < Paused , PlayPause , Playing >,
  209. msm::front::Row < Playing , SongFinished , Playing , inc_song_counter , msm::front::none >,
  210. a_row < Paused , StartSong , Playing , &fsm::select_song >,
  211. // +--------------------+---------------------+--------------------+--------------------------+----------------------+
  212. msm::front::Row < WaitingForNextPrev , PreviousSong , WaitingForNextPrev , dec_song_counter , msm::front::none >,
  213. msm::front::Row < WaitingForNextPrev , NextSong , WaitingForNextPrev , inc_song_counter , msm::front::none >,
  214. // +--------------------+---------------------+--------------------+--------------------------+----------------------+
  215. _row < WaitingForEnd , EndPlay , PlayingExit >,
  216. // +--------------------+---------------------+--------------------+--------------------------+----------------------+
  217. msm::front::Row < NoForward , EastPressed , ForwardPressed , msm::front::none , fast_fwd_ok >,
  218. msm::front::Row < ForwardPressed , EastReleased , NoForward , send_NextSong , msm::front::none >,
  219. a_row < ForwardPressed , ForwardTimer , FastForward , &fsm::do_fast_forward >,
  220. a_row < FastForward , ForwardTimer , FastForward , &fsm::do_fast_forward >,
  221. _row < FastForward , EastReleased , NoForward >,
  222. // +--------------------+---------------------+---------------------+--------------------------+----------------------+
  223. _row < StdDisplay , PlayingMiddleButton , SetPosition >,
  224. _row < SetPosition , StartSong , StdDisplay >,
  225. _row < SetPosition , PlayingMiddleButton , SetMark >,
  226. _row < SetMark , PlayingMiddleButton , StdDisplay >,
  227. _row < SetMark , StartSong , StdDisplay >
  228. // +--------------------+---------------------+---------------------+--------------------------+----------------------+
  229. > {};
  230. PlayingMode_():
  231. m_SongIndex(1),
  232. // for simplicity we decide there are 5 songs
  233. m_NumberOfSongs(5){}
  234. int get_current_song(){return m_SongIndex;}
  235. int m_SongIndex;
  236. int m_NumberOfSongs;
  237. };
  238. typedef msm::back::state_machine<PlayingMode_> PlayingMode;
  239. #endif // PLAYING_MODE_HPP