josh 3f8b4d0b5a filled out VFD::connect()
git-svn-id: svn://anubis/misc/parport-2x20vfd@110 bd8a9e45-a331-0410-811e-c64571078777
2009-05-03 20:30:37 +00:00

72 lines
1.3 KiB
C++

/*
* Author: Josh Holtrop
* Date: 2009-05-03
* See VFD.h for documentation
*/
#include <stdio.h> /* perror() */
#include <unistd.h> /* usleep() */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <iostream>
using namespace std;
#include "VFD.h"
VFD::VFD()
{
m_ppfd = -1;
}
bool VFD::connect(const std::string & parport)
{
const int mode = IEEE1284_MODE_BYTE;
const int direction = 0;
if ( (m_ppfd = open(parport.c_str(), O_RDWR)) < 0)
{
cerr << "Error opening '" << parport << "'";
perror("");
}
else if (ioctl(m_ppfd, PPCLAIM, NULL))
{
perror("Could not claim parallel port");
}
else if (ioctl(m_ppfd, PPSETMODE, &mode))
{
perror("Could not set parallel port mode");
ioctl(m_ppfd, PPRELEASE);
}
else if (ioctl(m_ppfd, PPDATADIR, &direction))
{
perror("Could not set parallel port direction");
ioctl(m_ppfd, PPRELEASE);
}
else
{
return true;
}
if (m_ppfd >= 0)
{
close(m_ppfd);
m_ppfd = -1;
}
return false;
}
void VFD::disconnect()
{
if (m_ppfd >= 0)
{
ioctl(m_ppfd, PPRELEASE);
close(m_ppfd);
m_ppfd = -1;
}
}