Use Finite State Machine to manage Elevator movement
[cs356-p1-elevator.git] / elevator.hpp
1 /*
2  * CS356 Project 01 -- Elevator Simulator
3  *
4  * Elevator Class Specification
5  */
6
7 #ifndef ELEVATOR_HPP
8 #define ELEVATOR_HPP
9
10 #include <iostream>
11 #include <list>
12 #include "direction.hpp"
13 #include "position.hpp"
14 #include "stop.hpp"
15
16 typedef std::list<Stop> StopList;
17
18 enum State { STATE_IDLE, STATE_UP, STATE_DOWN, STATE_WAIT, STATE_OPEN_DOOR, STATE_CLOSE_DOOR };
19 enum Event { EVT_IDLE, EVT_UP, EVT_DOWN, EVT_WAIT, EVT_OPEN_DOOR, EVT_CLOSE_DOOR };
20
21 class Elevator
22 {
23         public:
24                 Elevator ();
25                 Elevator (int starting_floor);
26
27                 void stop_at (Stop &stop);
28                 float distance_from (Position& pos) const;
29                 void move ();
30                 bool is_idle () const;
31
32         private:
33
34                 /* State Machine: Helper Functions */
35                 Event find_next_event () const;
36
37                 /* State Machine: Transition Functions */
38                 void transition_move_up ();
39                 void transition_move_down ();
40                 void transition_move_idle ();
41                 void transition_open_door ();
42                 void transition_close_door ();
43                 void transition_begin_wait ();
44                 void transition_continue_wait ();
45
46                 /* Analyze the list of stops */
47                 bool currently_at_stop () const;
48
49                 /* Elevator Status Variables */
50                 Direction       direction_;
51                 Position        position_;
52                 StopList        stops_;
53
54                 /* State Machine */
55                 State state_;
56                 int wait_;
57
58                 static const float ELEVATOR_STEP = 0.1;
59 };
60
61
62 #endif /* ELEVATOR_HPP */
63
64 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */