From 5e1806216bc503f6fc4cbbb9586fca851b3331c9 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Sat, 13 Oct 2007 14:39:39 -0700 Subject: [PATCH] Add the hacks to fix disabling of buttons Signed-off-by: Ira W. Snyder --- elevator.cpp | 37 ++++++++++++++++++++++++++++++------- elevator.hpp | 3 +++ elevatorcontroller.cpp | 11 +++++++++++ elevatorcontroller.hpp | 3 +++ elevatorgui.cpp | 4 ++++ stop.cpp | 3 ++- 6 files changed, 53 insertions(+), 8 deletions(-) diff --git a/elevator.cpp b/elevator.cpp index 92d66d2..038bc29 100644 --- a/elevator.cpp +++ b/elevator.cpp @@ -78,6 +78,9 @@ void Elevator::stop_at (Stop &stop) { StopList::iterator it; + if (stop.getDirection() == IDLE) + throw bad_direction(); +#if 0 /* If this is an "ALL" Stop, it supercedes all others */ if (stop.getDirection() == ALL) { @@ -85,6 +88,7 @@ void Elevator::stop_at (Stop &stop) stops_.push_back (stop); return; } +#endif /* Check if this stop already exists. If so, just leave */ for (it = stops_.begin(); it != stops_.end(); it++) @@ -166,28 +170,36 @@ void Elevator::transition_open_door () ++stops_below; } + std::cout << "Elevator: " << number_ << " stopping in direction=" << direction_ << std::endl; + + /* Always unpress the in-elevator button for this stop, as well + * as clear the stop. This is like letting people off at a floor. */ + gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, ALL)); + + /* If we are going to switch direction, clear all stops here, * regardless of direction. * * Otherwise, just clear this stop */ if (direction_ == UP && stops_above == 0) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } else if (direction_ == DOWN && stops_below == 0) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } else if (direction_ == IDLE) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } @@ -195,7 +207,6 @@ void Elevator::transition_open_door () { stops_.remove (Stop(position_, direction_)); gui_unpress_call_button ((int)position_, direction_); - gui_unpress_request_button (number_, (int)position_); } // TODO: Call into the GUI to open the door @@ -530,4 +541,16 @@ int Elevator::getLoad () const return stops_.size (); } +bool Elevator::willStopAt (int floor, Direction direction) const +{ + Stop s (floor, direction); + StopList::const_iterator it; + + for (it=stops_.begin(); it != stops_.end(); it++) + if (*it == s) + return true; + + return false; +} + /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/elevator.hpp b/elevator.hpp index 36371a1..0a73480 100644 --- a/elevator.hpp +++ b/elevator.hpp @@ -13,6 +13,8 @@ #include "position.hpp" #include "stop.hpp" +class bad_direction { }; + typedef std::list StopList; enum State { STATE_IDLE, STATE_UP, STATE_DOWN, STATE_WAIT, STATE_OPEN_DOOR, STATE_CLOSE_DOOR }; @@ -30,6 +32,7 @@ class Elevator void move (); bool is_idle () const; int getLoad () const; + bool willStopAt (int floor, Direction direction) const; private: diff --git a/elevatorcontroller.cpp b/elevatorcontroller.cpp index 79418ba..e250cf9 100644 --- a/elevatorcontroller.cpp +++ b/elevatorcontroller.cpp @@ -197,4 +197,15 @@ void ElevatorController::move_elevators () it->move(); } +bool ElevatorController::oneElevatorWillStillStopAt (int floor, Direction direction) const +{ + ElevatorList::const_iterator it; + + for (it=elevators_.begin(); it!=elevators_.end(); it++) + if (it->willStopAt (floor, direction)) + return true; + + return false; +} + /* vim: set ts=4 sts=4 sw=4 noet tw=112: */ diff --git a/elevatorcontroller.hpp b/elevatorcontroller.hpp index 75a5094..46026b4 100644 --- a/elevatorcontroller.hpp +++ b/elevatorcontroller.hpp @@ -31,6 +31,9 @@ class ElevatorController void elevator_request (int elevator_number, int floor); void move_elevators (); + /* true if at least one Elevator still stopping here */ + bool oneElevatorWillStillStopAt (int floor, Direction direction) const; + private: int number_of_floors_; int number_of_elevators_; diff --git a/elevatorgui.cpp b/elevatorgui.cpp index 3db7c3d..f18a334 100644 --- a/elevatorgui.cpp +++ b/elevatorgui.cpp @@ -247,6 +247,10 @@ void ElevatorGUI::gui_unpress_call_button (int floor, Direction direction) { CallButtonVector::iterator it; + /* If there is still an elevator coming, don't turn it off! */ + if (ec_.oneElevatorWillStillStopAt (floor, direction)) + return; + for (it=call_buttons_.begin(); it!=call_buttons_.end(); it++) if ((*it)->getFloorNumber() == floor && (*it)->getDirection() == direction) (*it)->set_active (false); diff --git a/stop.cpp b/stop.cpp index d4cb7f7..54f0f4f 100644 --- a/stop.cpp +++ b/stop.cpp @@ -12,8 +12,10 @@ bool Stop::operator== (const Stop& rhs) const if (rhs.position_ != position_) return false; +#if 0 if (direction_ == ALL || rhs.direction_ == ALL) return true; +#endif return (rhs.direction_ == direction_); } @@ -67,5 +69,4 @@ std::ostream& operator<< (std::ostream& os, const Stop& rhs) return os; } - /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ -- 2.25.1