#ifndef OF_SERIAL_H
#define OF_SERIAL_H

#include "ofConstants.h"

#if defined( TARGET_OSX ) || defined( TARGET_LINUX )
	#include <termios.h>
	#include <sys/ioctl.h>
	#include <getopt.h>
#else
	#include <Winbase.h>
	#include <tchar.h>
	#include <iostream>
	#include <string.h>
#endif

#include <stdio.h>    
#include <stdlib.h>
#include <string.h>  
#include <fcntl.h>    
#include <errno.h>    
#include <ctype.h>
#include <dirent.h>


//----------------------------------------------------------------------
class ofSerial{

	public:	
			ofSerial();
			bool			serialSetup(string portName, int baudrate);
			bool			serialSetup(int deviceNumber, int baudrate);
			int				serialWrite(string str);
			string			serialRead(int length);
			bool			serialWriteByte(unsigned char singleByte);
			unsigned char	serialReadByte();
			
	protected:
			bool 	bInited;

		#ifdef TARGET_WIN32
			HANDLE  	hComm;		// the handle to the serial port pc
		#else
			int 		fd;			// the handle to the serial port mac
		#endif

};

//----------------------------------------------------------------------




// this serial code contains small portions of the following code-examples:
// ---------------------------------------------------
// http://todbot.com/arduino/host/arduino-serial/arduino-serial.c
// web.mac.com/miked13/iWeb/Arduino/Serial%20Write_files/main.cpp
// www.racer.nl/docs/libraries/qlib/qserial.htm
// ---------------------------------------------------

// notes:
// ----------------------------
// when calling serialSetup("....") you need to pass in
// the name of the com port the device is attached to
// for example, on a mac, it might look like:
//
// 		ofSerialSetup("/dev/tty.usbserial-3B1", 9600)
//
// and on a pc, it might look like:
//
// 		ofSerialSetup("COM4", 9600)
//
// if you are using an arduino board, for example,
// you should check what ports you device is on in the
// arduino program

// to do:
// ----------------------------
// a) 	support blocking / non-blocking
// b) 	support numChars available type functions
// c)   can we reduce the number of includes here?)
 

// ----------------------------
// (also useful, might be this serial example - worth checking out:
// http://web.mit.edu/kvogt/Public/osrc/src/
// if has evolved ways of dealing with blocking
// and non-blocking instances)
// ----------------------------


#endif
