/*
 * roombalib -- Roomba C API, part of RoombaComm
 *
 * http://roombahacking.com/
 *
 * Copyright (C) 2006, Tod E. Kurt, tod@todbot.com
 *
 */


#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h> 
#include <stdint.h>   /* Standard types */
#include <string.h>   /* String function definitions */
#include <unistd.h>   /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */
#include <sys/ioctl.h>

#define DEFAULT_VELOCITY 200
#define COMMANDPAUSE_MILLIS 100

// holds all the per-roomba info
// consider it an opaque blob, please
typedef struct Roomba_struct {
    int fd;
    unsigned char sensor_bytes[26];
    int velocity;
} Roomba;

// set to non-zero to see debugging output
extern int roombadebug;

// given a serial port name, create a Roomba object and return it
// or return NULL on error
Roomba* roomba_init( char* portname );

// frees the memory of the Roomba object created with roomba_init
// will close the serial port if it's open
void roomba_free( Roomba* roomba );

// close the serial port connected to the Roomba
void roomba_close( Roomba* roomba );

// stop the Roomba 
void roomba_stop( Roomba* roomba );

// Move Roomba forward at current velocity
void roomba_go_forward( Roomba* roomba );

// Move Roomba backward at current velocity
void roomba_go_backward( Roomba* roomba );

// Spin Roomba left at current velocity
void roomba_spin_left( Roomba* roomba );

// Spin Roomba right at current velocity
void roomba_spin_right( Roomba* roomba );

// Set current velocity for higher-level movement commands
void roomba_set_velocity( Roomba* roomba, int velocity );

// Get current velocity for higher-level movement commands
int roomba_get_velocity( Roomba* roomba );

// Move Roomba with low-level DRIVE command
void roomba_drive( Roomba* roomba, int velocity, int radius );

// Get the sensor data from the Roomba
// returns -1 on failure
int roomba_read_sensors( Roomba* roomba );

// print existing sensor data nicely
void roomba_print_sensors( Roomba* roomba );

// print existing sensor data as string of hex chars
void roomba_print_raw_sensors( Roomba* roomba );

// utility function
void roomba_delay( int millisecs );
#define roomba_wait roomba_delay

// some simple macros of bit manipulations
#define bump_right(b)           ((b & 0x01)!=0)
#define bump_left(b)            ((b & 0x02)!=0)
#define wheeldrop_right(b)      ((b & 0x04)!=0)
#define wheeldrop_left(b)       ((b & 0x08)!=0)
#define wheeldrop_caster(b)     ((b & 0x10)!=0)

#define motorover_sidebrush(b)  ((b & 0x01)!=0) 
#define motorover_vacuum(b)     ((b & 0x02)!=0) 
#define motorover_mainbrush(b)  ((b & 0x04)!=0) 
#define motorover_driveright(b) ((b & 0x08)!=0) 
#define motorover_driveleft(b)  ((b & 0x10)!=0) 


