//---------------------------------------------------------------------------
#ifdef WIN32
#include "stdafx.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef _QNX
#include <i86.h>
#include "Syslog.h"
#endif
#include "PicServo.h"

//---------------------------------------------------------------------------
// Global variables
//---------------------------------------------------------------------------
int CPicServo::SimpleMsgBox(char *msgstr)
{
#ifdef WIN32
return MessageBox(NULL, msgstr,"",MB_TASKMODAL | MB_SETFOREGROUND);
#endif
#ifdef _QNX
 Syslog::write(msgstr);
 return True;
#endif
}

//---------------------------------------------------------------------------
void CPicServo::ErrorPrinting(int f)
{
printerrors = f;
}

//---------------------------------------------------------------------------
int CPicServo::ErrorMsgBox(char *msgstr)
{
#ifdef _QNX
if (printerrors) 
  Syslog::write(msgstr);
return True; 
#endif
}

//---------------------------------------------------------------------------
//Opens "COM1:" thru "COM4:", returns a handle to be used by other
//SIO operations.  Sets up read and write timeouts.
//*** Add parameter for baud rate ***
#ifdef _QNX
void CPicServo::SioOpen(char *portName, unsigned int baudrate)
{
  ComPort = new SerialDevice(portName);
  if (ComPort == (SerialDevice *)NULL) {
    Syslog::write("SioOpen(): unable to open port %s\n", portName);
    return;
  }

  if (ComPort->setLineFormat(baudrate, 8, 1, "NONE") != OK) {
    Syslog::write("SioOpen() - unable to set port parameters\n");
  }
}
#endif

#ifdef _QNX
BOOL CPicServo::SioChangeBaud(unsigned int baudrate)
{
  if (ComPort->setLineFormat(baudrate,8,1,"NONE") != OK)
    return False;
  else
    return True;
}
#endif
//---------------------------------------------------------------------------
//Write out N chars to the comport, returns only after chas have been sent
//return 0 on failure, non-zero on success
#ifdef _QNX
BOOL CPicServo::SioPutChars(char *stuff, int n)
{
  delay(1);
  if (ComPort->write(stuff,n) != n) {
    return False;
  } else {
    return True;
  }
}
#endif

//---------------------------------------------------------------------------
//Read n chars into the array stuff (not null terminated)
//Function returns the number of chars actually read.
#ifdef _QNX
DWORD CPicServo::SioGetChars(char *stuff, int n)
{
  tcdrain(ComPort->getFd());
  delay(1);
//  delay(60);
  return ComPort->readNChars(stuff,n,150);
}
#endif

//---------------------------------------------------------------------------
//Returns the number of chars in a port's input buffer
#ifdef _QNX
DWORD CPicServo::SioTest()
{
  return ComPort->nRecvdBytes();
}
#endif

//---------------------------------------------------------------------------
//Purge all chars from the input buffer
#ifdef _QNX
BOOL CPicServo::SioClrInbuf()
{
  ComPort->clearPort();
  return True;
}
#endif

//---------------------------------------------------------------------------
//Close a previously opened COM port
#ifdef _QNX
BOOL CPicServo::SioClose()
{
  delete ComPort;
  ComPort = INVALID_HANDLE_VALUE;
  return True;
}
#endif
//---------------------------------------------------------------------------



