Main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2002-2006 Andreas Huber Doenni
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //////////////////////////////////////////////////////////////////////////////
  6. //////////////////////////////////////////////////////////////////////////////
  7. // This program shows how a state machine can be spread over several
  8. // translation units if necessary. The inner workings of a digital camera are
  9. // modeled, the corresponding state chart looks as follows:
  10. //
  11. // ---------------------------
  12. // | |
  13. // | NotShooting |
  14. // | |
  15. // | ------------- |<---O
  16. // | O--->| Idle | | --------------
  17. // | ------------- | EvShutterHalf | |
  18. // | | ^ |------------------>| Shooting |
  19. // | EvConfig | | EvConfig | | |
  20. // | v | | EvShutterRelease | |
  21. // | ------------- |<------------------| |
  22. // | | Configuring | | | |
  23. // | ------------- | --------------
  24. // ---------------------------
  25. //
  26. // The states Configuring and Shooting will contain a large amount of logic,
  27. // so they are implemented in their own translation units. This way one team
  28. // could implement the Configuring mode while the other would work on the
  29. // Shooting mode. Once the above state chart is implemented, the teams could
  30. // work completely independently of each other.
  31. #include "Precompiled.hpp"
  32. #include "Camera.hpp"
  33. #include <iostream>
  34. //////////////////////////////////////////////////////////////////////////////
  35. char GetKey()
  36. {
  37. char key;
  38. std::cin >> key;
  39. return key;
  40. }
  41. //////////////////////////////////////////////////////////////////////////////
  42. int main()
  43. {
  44. std::cout << "Boost.Statechart Camera example\n\n";
  45. std::cout << "h<CR>: Press shutter half-way\n";
  46. std::cout << "f<CR>: Press shutter fully\n";
  47. std::cout << "r<CR>: Release shutter\n";
  48. std::cout << "c<CR>: Enter/exit configuration\n";
  49. std::cout << "e<CR>: Exits the program\n\n";
  50. std::cout << "You may chain commands, e.g. hfr<CR> first presses the shutter half-way,\n";
  51. std::cout << "fully and then releases it.\n\n";
  52. Camera myCamera;
  53. myCamera.initiate();
  54. char key = GetKey();
  55. while ( key != 'e' )
  56. {
  57. switch( key )
  58. {
  59. case 'h':
  60. {
  61. myCamera.process_event( EvShutterHalf() );
  62. }
  63. break;
  64. case 'f':
  65. {
  66. myCamera.process_event( EvShutterFull() );
  67. }
  68. break;
  69. case 'r':
  70. {
  71. myCamera.process_event( EvShutterRelease() );
  72. }
  73. break;
  74. case 'c':
  75. {
  76. myCamera.process_event( EvConfig() );
  77. }
  78. break;
  79. default:
  80. {
  81. std::cout << "Invalid key!\n";
  82. }
  83. break;
  84. }
  85. key = GetKey();
  86. }
  87. return 0;
  88. }