/* * 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; } } void VFD::setPPLines(unsigned char bits) { if (m_ppfd >= 0) { ioctl(m_ppfd, PPWDATA, &bits); } } void VFD::sendNibble(unsigned char rs, unsigned char dat) { setPPLines( (rs << PP_OFFSET_RS) | (E_DISABLE << PP_OFFSET_E) | (RW_WRITE << PP_OFFSET_RW) | ((dat & 0xF) << PP_OFFSET_DATA) ); usleep(1); setPPLines( (rs << PP_OFFSET_RS) | (E_ENABLE << PP_OFFSET_E) | (RW_WRITE << PP_OFFSET_RW) | ((dat & 0xF) << PP_OFFSET_DATA) ); usleep(1); } void VFD::sendByte(unsigned char rs, unsigned char dat) { sendNibble(rs, (dat >> 4) & 0xF); sendNibble(rs, dat & 0xF); }