Use Finite State Machine to manage Elevator movement
[cs356-p1-elevator.git] / elevator.hpp
index 1b5c599..df8a228 100644 (file)
 #include "position.hpp"
 #include "stop.hpp"
 
-typedef enum { CLOSED, OPEN } DoorStatus;
 typedef std::list<Stop> StopList;
 
+enum State { STATE_IDLE, STATE_UP, STATE_DOWN, STATE_WAIT, STATE_OPEN_DOOR, STATE_CLOSE_DOOR };
+enum Event { EVT_IDLE, EVT_UP, EVT_DOWN, EVT_WAIT, EVT_OPEN_DOOR, EVT_CLOSE_DOOR };
+
 class Elevator
 {
        public:
                Elevator ();
+               Elevator (int starting_floor);
 
                void stop_at (Stop &stop);
                float distance_from (Position& pos) const;
@@ -27,21 +30,32 @@ class Elevator
                bool is_idle () const;
 
        private:
-               /* Callbacks into the GUI */
-               void open_door () const;
-               void close_door () const;
-               void update_position () const;
+
+               /* State Machine: Helper Functions */
+               Event find_next_event () const;
+
+               /* State Machine: Transition Functions */
+               void transition_move_up ();
+               void transition_move_down ();
+               void transition_move_idle ();
+               void transition_open_door ();
+               void transition_close_door ();
+               void transition_begin_wait ();
+               void transition_continue_wait ();
 
                /* Analyze the list of stops */
                bool currently_at_stop () const;
 
                /* Elevator Status Variables */
-               DoorStatus      door_;
                Direction       direction_;
-               Position        current_position_;
+               Position        position_;
                StopList        stops_;
 
-               const float     ELEVATOR_STEP;
+               /* State Machine */
+               State state_;
+               int wait_;
+
+               static const float ELEVATOR_STEP = 0.1;
 };