/* ORPP Motion Controller board sensor test 2005 Jason Hunt huntjas2@msu.edu File: orpp.h */ #ifndef ORPP_H #define ORPP_H #include typedef unsigned char byte; // Create byte datatype for clarity typedef unsigned int word; // Create word datatype /* Different parameter types for the control command structure */ enum param_type {param_none, param_digit, param_hex, param_word}; /* Each IO command is store in one of these structures */ typedef struct control_command_type { char name[32]; char command_char; byte num_device; byte num_param; byte num_ret; param_type param; param_type ret; byte inital_value; }; #define NUM_CONTROL 8 #define MAX_DEVICE 6 // These are the controls that this api provides access to // they need to match in order and length, there will indexs into commands[] enum control_type {orpp_led, orpp_digital_out, orpp_servo, orpp_opencollector, orpp_pwm, orpp_analog, orpp_digital_in, orpp_radio}; // Two connections types currently enum connection_type {orpp_connection_serial, orpp_connection_network}; /* Define all of the commands */ control_command_type const commands[] = // Display name char,max, first param, second param,inital value {{"Led", 'l', 2, 1, 0, param_digit, param_none, 0x00}, {"Digital Out", 'd', 6, 1, 0, param_digit, param_none, 0x00}, {"Servo Pos", 's', 6, 2, 0, param_hex, param_none, 0x80}, {"Open Collector",'o', 2, 1, 0, param_digit, param_none, 0x00}, {"PWM", 'p', 2, 2, 0, param_hex, param_none, 0x00}, {"Analog", 'a', 5, 0, 4, param_none, param_word, 0x00}, {"Digital In", 'i', 5, 0, 1, param_none, param_digit, 0x00}, {"Radio", 'r', 2, 0, 2, param_none, param_hex, 0x00}}; /* Definition of the orpp class */ class orpp_class { public: /* constructor / destructor */ orpp_class(void ); // Create object from serial port orpp_class(char *serial_port); // Create object from serial port orpp_class::orpp_class(char *host, unsigned int the_port); // Create from network conection ~orpp_class(void); /* Communications */ void connect(void); // Connect to serial or network void set_connection(char *serial_port); // Create object from serial port void set_connection(char *host, unsigned int the_port); // Create from network conection /* Setting outputs */ void set(control_type control, byte param1, byte param2); void set_next(control_type control, byte param1, byte param2); void set_all( void ); // Send all of the output values at once /* Getting inputs */ word get(control_type c, byte num); // Get control value word get_last(control_type c, byte num); // Return last known value void get_all( void ); // Get all analog and digital inputs at once /* Modifing values */ void add(control_type c, byte num, int delta); // Add an integer to a control void toggle(control_type c, byte num); // Toggle a control /* Other */ void clear( void ); // Reset to boot values void set_error_function(void (*f_ptr)(char *) ) { error_function = f_ptr; }; void set_message_function(void (*f_ptr)(char *) ) { message_function = f_ptr; }; bool get_comm_ok(void) { return (comm_ok); }; // returns true if connunications is working private: void error(char *); // Called when a serious error has occored void message(char *); // Called for non error related messages void connect_serial(void); // Connect to serial void connect_network(void); // Connect to network void send_data(byte *packet, unsigned int size); // Send data packet void send_data(byte c); // Send a char to the board void send_data_hex(byte c); // Send byte to the board in hex format char read_data(void); byte read_data_hex(void); // Get data from board word read_data_word(void); // Last known states of all inputs and outputs unsigned int states[NUM_CONTROL][MAX_DEVICE]; connection_type connection; HANDLE comm; // Handle to the serial port SOCKET server_socket; char serial_port_name[64]; char host_name[128]; unsigned int network_port; bool comm_ok; // False if there was a communication error void (*error_function)(char *); void (*message_function)(char *); }; #endif