port.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #ifndef PORT_H
00012 #define PORT_H
00013 
00014 #include <qstringlist.h>
00015 
00016 #include <termios.h>
00017 
00021 class Port
00022 {
00023         public:
00024                 enum ProbeResult
00025                 {
00026                         ExistsAndRW             = 1 << 0,
00027                         ExistsButNotRW  = 1 << 1,
00028                         DoesntExist             = 1 << 2
00029                 };
00030                 
00031                 Port();
00032                 virtual ~Port();
00033                 
00039                 static QStringList ports( unsigned probeResult );
00040 };
00041 
00042 
00048 class SerialPort : public Port
00049 {
00050         public:
00051                 enum Pin
00052                 {
00053                         CD              = 1, // Carrier detect
00054                         RD              = 2, // Recieved data
00055                         TD              = 3, // Transmitted data
00056                         DTR             = 4, // Data terminal ready
00057                         GND             = 5, // Signal ground
00058                         DSR             = 6, // Data set ready
00059                         RTS             = 7, // Request to send
00060                         CTS             = 8, // Clear to send
00061                         RI              = 9, // Ring indicator
00062                 };
00063                 
00064                 SerialPort();
00065                 ~SerialPort();
00066                 
00070                 void setPinState( Pin pin, bool state );
00071                 bool pinState( Pin pin );
00072                 
00073                 static ProbeResult probe( const QString & port );
00077                 static QStringList ports( unsigned probeResult );
00083                 bool openPort( const QString & port, speed_t baudRate );
00087                 void closePort();
00088                 
00089         protected:
00091                 termios m_previousState;
00092                 
00094                 int m_file;
00095 };
00096 
00097 
00104 class ParallelPort : public Port
00105 {
00106         public:
00107                 enum Pin
00108                 {
00109                         // Data Register
00110                         //   Offset: Base + 0
00111                         //   Readable / writable
00112                         PIN02 = 1 << 0,         // Data 0
00113                         PIN03 = 1 << 1,         // Data 1
00114                         PIN04 = 1 << 2,         // Data 2
00115                         PIN05 = 1 << 3,         // Data 3
00116                         PIN06 = 1 << 4,         // Data 4
00117                         PIN07 = 1 << 5,         // Data 5
00118                         PIN08 = 1 << 6,         // Data 6
00119                         PIN09 = 1 << 7,         // Data 7
00120                         DATA_PINS = PIN02 | PIN03 | PIN04 | PIN05 | PIN06
00121                                          | PIN07 | PIN08 | PIN09,
00122                         
00123                         // Status Register
00124                         //   Offset: Base + 1
00125                         //   Read only
00126                         PIN15 = 1 << 11,        // Error
00127                         PIN13 = 1 << 12,        // Online
00128                         PIN12 = 1 << 13,        // Paper End
00129                         PIN10 = 1 << 14,        // Ack
00130                         PIN11 = 1 << 15,        // Busy
00131                         STATUS_PINS = PIN15 | PIN13 | PIN12 | PIN10 | PIN11,
00132                         
00133                         // Control Register
00134                         //   Offset: Base + 2
00135                         //   Readable / writable
00136                         PIN01 = 1 << 16,        // Strobe
00137                         PIN14 = 1 << 17,        // Auto Feed
00138                         PIN16 = 1 << 18,        // Init
00139                         PIN17 = 1 << 19,        // Select
00140                         CONTROL_PINS = PIN01 | PIN14 | PIN16 | PIN17,
00141                         
00142                         
00143                         // Pins 18 to 25 are ground
00144                 };
00145                 
00146                 enum Register
00147                 {
00148                         Data    = 0,
00149                         Status  = 1,
00150                         Control = 2,
00151                 };
00152                 
00156                 enum Direction
00157                 {
00158                         Input = 0,
00159                         Output = 1,
00160                 };
00161                 
00162                 ParallelPort();
00163                 ~ParallelPort();
00164                 
00169                 bool openPort( const QString & port );
00173                 void closePort();
00174                 
00175                 //BEGIN Pin-oriented operations
00179                 void setPinState( int pins, bool state );
00183                 int pinState( int pins );
00187                 void setDataState( uchar pins, bool state );
00191                 void setControlState( uchar pins, bool state );
00192                 //END Pin-oriented operations
00193                 
00194                 
00195                 //BEGIN Register-oriented operations
00199                 uchar readFromRegister( Register reg );
00203                 void writeToData( uchar value );
00208                 void writeToControl( uchar value );
00209                 //END Register-oriented operations
00210                 
00211                 
00212                 //BEGIN Changing pin directions
00216                 void setDataDirection( Direction dir );
00220                 void setControlDirection( int pins, Direction dir );
00221                 //END Changing pin directions
00222                 
00223                 static ProbeResult probe( const QString & port );
00227                 static QStringList ports( unsigned probeResult );
00228                 
00229         protected:
00233                 void writeToRegister( Register reg, uchar value );
00234                 void reset();
00235                 
00236                 uchar m_reg[3];
00237                 
00239                 int m_inputPins;
00240                 
00242                 int m_outputPins;
00243                 
00245                 int m_file;
00246 };
00247 
00248 #endif

Generated on Tue May 8 17:05:32 2007 for KTechLab by  doxygen 1.5.1