Subversion Repositories programming

Rev

Rev 340 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * elevator.h
 *
 * Header file for the Elevator class.
 *
 * Copyright 2006, Ira W. Snyder (devel@irasnyder.com)
 ******************************************************************************/

#ifndef ELEVATOR_H
#define ELEVATOR_H

#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <assert.h>
#include <vector>
#include <string>

#include <glibmm.h>

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>

#include "dispatch_data.h"
#include "controller.h"

const int MOVE_UP   = 0;
const int MOVE_DOWN = 1;
const int IDLE      = 2;

const float ELEVATOR_TIME_MOVE_AMOUNT = 0.1;
const int   ELEVATOR_TIME_DELAY_USEC  = 500000;
const int   ELEVATOR_TIME_PAUSE_USEC  = 50000;

class Controller;

class Elevator
{
    public:
        Elevator (int num_floors);
        ~Elevator ();

        // Service Functions
        int floors_above_current ();
        int floors_below_current ();
        void push_button (int floor);
        bool button_is_pushed (int floor);

        // Getters and Setters
        int get_direction ();
        float get_current_floor ();
        void set_controller (Controller *c);
        void set_dispatcher (Glib::Dispatcher *d);

        // Thread Functions
        void thread_start ();
        void thread_stop ();
        void thread_pause ();
        void thread_unpause ();

    private:
        // Private Functions
        bool near_a_floor ();
        bool near_floor (int floor);
        bool has_floors_to_stop_at ();
        bool should_stop_at_current_floor ();
        void stop_at_floor (int in_direction);

        // Thread Functions
        void run_elevator_logic ();
        static void thread_entry (const Elevator *me);

        // Private Variables
        int num_floors;
        float current_floor;
        int direction;
        std::vector<bool> stop_at_floors;
        Controller *controller;

        // Dispatcher
        Glib::Dispatcher *dispatcher;

        // Thread Variable
        bool terminate;
        bool pause;

};

#endif // ELEVATOR_H