Subversion Repositories programming

Rev

Rev 340 | Rev 346 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 340 Rev 344
Line 11... Line 11...
11
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
11
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
12
{
12
{
13
    assert (num_floors > 2);
13
    assert (num_floors > 2);
14
    assert (num_elevators > 0);
14
    assert (num_elevators > 0);
15
 
15
 
-
 
16
    // Connect to the dispatcher
-
 
17
    dispatcher.connect (sigc::mem_fun (*this, &Controller::dispatch_handler));
-
 
18
 
-
 
19
    // Create all of the elevators
16
    elevator.reserve (num_elevators);
20
    elevator.reserve (num_elevators);
17
 
21
 
18
    for (int i=0; i<num_elevators; i++)
22
    for (int i=0; i<num_elevators; i++)
19
    {
23
    {
20
        elevator.push_back (new Elevator (num_floors));
24
        elevator.push_back (new Elevator (num_floors));
21
        elevator.at(i) -> set_controller (this);
25
        elevator.at(i) -> set_controller (this);
-
 
26
        elevator.at(i) -> set_dispatcher (&dispatcher);
22
    }
27
    }
23
 
28
 
24
    // Seed the RNG
29
    // Seed the RNG
25
    srandom (time(NULL));
30
    srandom (time(NULL));
26
}
31
}
Line 160... Line 165...
160
    }
165
    }
161
 
166
 
162
    return answer;
167
    return answer;
163
}
168
}
164
 
169
 
165
void Controller::disable_elevator (int elevator_number)
-
 
166
{
-
 
167
}
-
 
168
 
-
 
169
void Controller::enable_elevator (int elevator_number)
-
 
170
{
-
 
171
    // make sure elevator is enabled first
-
 
172
}
-
 
173
 
-
 
174
void Controller::start_all_elevators ()
170
void Controller::start_all_elevators ()
175
{
171
{
176
    int i;
172
    int i;
177
 
173
 
178
    for (i=0; i<num_elevators; i++)
174
    for (i=0; i<num_elevators; i++)
Line 220... Line 216...
220
 
216
 
221
    assert (elevator_num != -1);
217
    assert (elevator_num != -1);
222
 
218
 
223
    pause_all_elevators ();
219
    pause_all_elevators ();
224
 
220
 
225
    /*****************
-
 
226
     * Call the gui
-
 
227
     ****************/
-
 
228
 
-
 
229
    // Open the elevator door
221
    // Open the elevator door
230
    //gui->open_elevator_at (floor, elevator_num);
-
 
231
    gui->open_elevator_at (num_floors-(floor+1), elevator_num);
222
    gui->open_elevator_at (num_floors-(floor+1), elevator_num);
232
 
223
 
233
    // Untoggle all buttons on that floor
224
    // Untoggle all buttons on that floor
234
    if (floor >= 0 && floor <= 8)
225
    if (floor >= 0 && floor <= 8)
235
        gui->unset_up_button (floor);
226
        gui->unset_up_button (floor);
Line 238... Line 229...
238
        gui->unset_down_button (floor-1);
229
        gui->unset_down_button (floor-1);
239
 
230
 
240
    // Get the floors from the user
231
    // Get the floors from the user
241
    gui->get_floors_from_user (elevator_num);
232
    gui->get_floors_from_user (elevator_num);
242
 
233
 
243
    sleep (4); // FIXME
234
    // Close the elevator door
244
 
-
 
245
    gui->close_elevator_at (num_floors-(floor+1), elevator_num);
235
    gui->close_elevator_at (num_floors-(floor+1), elevator_num);
246
 
236
 
247
    unpause_all_elevators ();
237
    unpause_all_elevators ();
248
}
238
}
249
 
239
 
250
void Controller::set_gui (Elevator_Window *gui)
240
void Controller::set_gui (Elevator_Window *gui)
251
{
241
{
252
    this->gui = gui;
242
    this->gui = gui;
253
}
243
}
254
 
244
 
255
void Controller::update_elevator_position (Elevator *e)
245
void Controller::update_elevator_position (Elevator *e, float new_floor)
256
{
246
{
257
    std::ostringstream strstrm;
247
    std::ostringstream strstrm;
258
    int elevator_num = which_elevator_is (e);
248
    int elevator_num = which_elevator_is (e);
259
 
249
 
260
    assert (elevator_num != -1);
250
    assert (elevator_num != -1);
261
 
251
 
-
 
252
    // Create the label
-
 
253
    strstrm << std::setiosflags (std::ios_base::showpoint | std::ios_base::fixed)
262
    strstrm << e->get_current_floor ();
254
            << std::setprecision (1) << new_floor;
263
 
255
 
264
    gui->set_label (elevator_num, strstrm.str());
256
    gui->set_label (elevator_num, strstrm.str());
265
}
257
}
266
 
258
 
-
 
259
void Controller::push_to_gui_queue (Dispatch_Data data)
-
 
260
{
-
 
261
    gui_events.push (data);
-
 
262
}
-
 
263
 
-
 
264
void Controller::dispatch_handler ()
-
 
265
{
-
 
266
    assert (gui_events.empty() == false);
-
 
267
 
-
 
268
    // Pop the first variable off of the queue
-
 
269
    Dispatch_Data temp = gui_events.front ();
-
 
270
    gui_events.pop ();
-
 
271
 
-
 
272
    switch (temp.type)
-
 
273
    {
-
 
274
        case DISPATCH_DATA_STOP_AT_FLOOR:
-
 
275
            stop_at_floor (temp.elev, temp.iarg);
-
 
276
            break;
-
 
277
 
-
 
278
        case DISPATCH_DATA_UPDATE_LABEL:
-
 
279
            update_elevator_position (temp.elev, temp.farg);
-
 
280
            break;
-
 
281
 
-
 
282
        default:
-
 
283
            std::cout << "Bad value in dispatch_handler()" << std::endl;
-
 
284
            break;
-
 
285
    }
-
 
286
}
-
 
287
 
-
 
288
void Controller::push_button_in_elevator (int elevator_num, int floor)
-
 
289
{
-
 
290
    assert (elevator_num < num_elevators);
-
 
291
    assert (elevator_num >= 0);
-
 
292
 
-
 
293
    assert (floor < num_floors);
-
 
294
    assert (floor >= 0);
-
 
295
 
-
 
296
    elevator.at (elevator_num)->push_button (floor);
-
 
297
}
-
 
298