File indexing completed on 2024-06-16 11:34:24

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef PORT_H
0012 #define PORT_H
0013 
0014 #include <QStringList>
0015 
0016 class QSerialPort;
0017 
0018 /**
0019 @author David Saxton
0020  */
0021 class Port
0022 {
0023 public:
0024     enum ProbeResult { ExistsAndRW = 1 << 0, ExistsButNotRW = 1 << 1, DoesntExist = 1 << 2 };
0025 
0026     Port();
0027     virtual ~Port();
0028 
0029     /**
0030      * Returns a list of available ports.
0031      * This function just returns the combination of the lists for
0032      * SerialPort::ports and ParallelPort::ports.
0033      */
0034     static QStringList ports();
0035 };
0036 
0037 /**
0038 Abstraction for a serial port, allowing control over individual pins.
0039 
0040 @author David Saxton
0041  */
0042 class SerialPort : public Port
0043 {
0044 public:
0045     SerialPort();
0046     ~SerialPort() override;
0047 
0048     void setBreakEnabled(bool state);
0049     void setDataTerminalReady(bool state);
0050     void setDataSetReady(bool state);
0051     void setRequestToSend(bool state);
0052 
0053     bool getDataCarrierDetectSignal();
0054     bool getSecondaryReceivedDataSignal();
0055     bool getClearToSendSignal();
0056     bool getRingIndicatorSignal();
0057 
0058     /**
0059      * @see Port::ports
0060      */
0061     static QStringList ports();
0062     /**
0063      * Opens the given port.
0064      * @return if the port could be opened.
0065      * @param baudRate The baud rate
0066      */
0067     bool openPort(const QString &port, qint32 baudRate);
0068     /**
0069      * Closes any currently open port.
0070      */
0071     void closePort();
0072 
0073     static bool isAvailable();
0074 
0075 protected:
0076     QSerialPort *m_port;
0077 };
0078 
0079 /**
0080 Abstraction for a parallel port, allowing control over individual pins.
0081 Based loosely on code in the parapin project; see http://parapin.sf.net
0082 
0083 @author David Saxton
0084 */
0085 class ParallelPort : public Port
0086 {
0087 public:
0088     enum Pin {
0089         // Data Register
0090         //   Offset: Base + 0
0091         //   Readable / writable
0092         PIN02 = 1 << 0, // Data 0
0093         PIN03 = 1 << 1, // Data 1
0094         PIN04 = 1 << 2, // Data 2
0095         PIN05 = 1 << 3, // Data 3
0096         PIN06 = 1 << 4, // Data 4
0097         PIN07 = 1 << 5, // Data 5
0098         PIN08 = 1 << 6, // Data 6
0099         PIN09 = 1 << 7, // Data 7
0100         DATA_PINS = PIN02 | PIN03 | PIN04 | PIN05 | PIN06 | PIN07 | PIN08 | PIN09,
0101 
0102         // Status Register
0103         //   Offset: Base + 1
0104         //   Read only
0105         PIN15 = 1 << 11, // Error
0106         PIN13 = 1 << 12, // Online
0107         PIN12 = 1 << 13, // Paper End
0108         PIN10 = 1 << 14, // Ack
0109         PIN11 = 1 << 15, // Busy
0110         STATUS_PINS = PIN15 | PIN13 | PIN12 | PIN10 | PIN11,
0111 
0112         // Control Register
0113         //   Offset: Base + 2
0114         //   Readable / writable
0115         PIN01 = 1 << 16, // Strobe
0116         PIN14 = 1 << 17, // Auto Feed
0117         PIN16 = 1 << 18, // Init
0118         PIN17 = 1 << 19, // Select
0119         CONTROL_PINS = PIN01 | PIN14 | PIN16 | PIN17
0120 
0121         // Pins 18 to 25 are ground
0122     };
0123 
0124     enum Register { Data = 0, Status = 1, Control = 2 };
0125 
0126     /**
0127      * For setting the direction of the Data register or the Control pins.
0128      */
0129     enum Direction { Input = 0, Output = 1 };
0130 
0131     ParallelPort();
0132     ~ParallelPort() override;
0133 
0134     /**
0135      * Opens the given port.
0136      * @return if the port could be opened.
0137      */
0138     bool openPort(const QString &port);
0139     /**
0140      * Closes any currently open port.
0141      */
0142     void closePort();
0143 
0144     // BEGIN Pin-oriented operations
0145     /**
0146      * @param pins A list of ParallelPort::Pin OR'd together.
0147      */
0148     void setPinState(int pins, bool state);
0149     /**
0150      * @return the pin states for the given list of pins.
0151      */
0152     int pinState(int pins);
0153     /**
0154      * Sets the given pins to the given state in the Data register.
0155      */
0156     void setDataState(uchar pins, bool state);
0157     /**
0158      * Sets the given pins to the given state in the Control register.
0159      */
0160     void setControlState(uchar pins, bool state);
0161     // END Pin-oriented operations
0162 
0163     // BEGIN Register-oriented operations
0164     /**
0165      * Reads and stores the value in the given reigsters, and returns it.
0166      */
0167     uchar readFromRegister(Register reg);
0168     /**
0169      * Write the given value to the Data register.
0170      */
0171     void writeToData(uchar value);
0172     /**
0173      * Write the given value to the Control register (any input pins will be
0174      * set to one).
0175      */
0176     void writeToControl(uchar value);
0177     // END Register-oriented operations
0178 
0179     // BEGIN Changing pin directions
0180     /**
0181      * Sets the (input / ouput) direction of the data pins.
0182      */
0183     void setDataDirection(Direction dir);
0184     /**
0185      * Sets the given pins to the given direction.
0186      */
0187     void setControlDirection(int pins, Direction dir);
0188     // END Changing pin directions
0189 
0190     static ProbeResult probe(const QString &port);
0191     /**
0192      * @see Port::ports
0193      */
0194     static QStringList ports();
0195 
0196     static bool isAvailable();
0197 
0198 protected:
0199     /**
0200      * Writes the value to the given register.
0201      */
0202     void writeToRegister(Register reg, uchar value);
0203     void reset();
0204 
0205 #ifdef Q_OS_LINUX
0206     uchar m_reg[3];
0207 
0208     /// Mask of the pins that are currently set as input
0209     int m_inputPins;
0210 
0211     /// Mask of the pins that are currently set as output
0212     int m_outputPins;
0213 
0214     /// File descriptor for the port.
0215     int m_file;
0216 #endif
0217 };
0218 
0219 #endif