From 3f8b4d0b5a4219158f2bde2858a1f3993f744cea Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 3 May 2009 20:30:37 +0000 Subject: [PATCH] filled out VFD::connect() git-svn-id: svn://anubis/misc/parport-2x20vfd@110 bd8a9e45-a331-0410-811e-c64571078777 --- VFD.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ VFD.h | 6 ++++++ 2 files changed, 74 insertions(+) diff --git a/VFD.cc b/VFD.cc index 8bf1e06..1ea06a4 100644 --- a/VFD.cc +++ b/VFD.cc @@ -1,3 +1,71 @@ +/* + * Author: Josh Holtrop + * Date: 2009-05-03 + * See VFD.h for documentation + */ + +#include /* perror() */ +#include /* usleep() */ +#include +#include +#include +#include +#include +#include +#include +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; + } +} diff --git a/VFD.h b/VFD.h index 9e3f1b3..6cde201 100644 --- a/VFD.h +++ b/VFD.h @@ -27,6 +27,8 @@ #ifndef VFD_H #define VFD_H VFD_H +#include + class VFD { public: @@ -36,8 +38,12 @@ class VFD static const unsigned char E_DISABLE = 1; static const unsigned char RS_COMMAND = 0; static const unsigned char RS_DATA = 1; + VFD(); + bool connect(const std::string & parport = "/dev/parport0"); + void disconnect(); protected: + int m_ppfd; /* parallel port file descriptor */ }; #endif