Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/SerialPort.hpp

Go to the documentation of this file.
00001 #ifndef __J2K__SerialPort_HPP__
00002 #define __J2K__SerialPort_HPP__
00003 
00004 // Maybe optimize the cost of sift down the array
00005 // if too much inefficient...
00006 // By using a wrap up array.
00007 
00008 // If clocked enough fast, then the array shouldn't grow
00009 // higher than 3 or 4 task at a time.
00010 
00011 // Modem Delay from 50 to 250 ms...
00012 #define MODEM_DELAY  100
00013 
00014 // We can stack up to 30 task in the list
00015 // 30 x 50 ms = 1500 ms delay needed to empty the task list !
00016 // Which means more than 30 mouse clicks in 1.5 second !
00017  
00018 #define MAX_SERIAL_TASK 30
00019 
00020 struct SerialTask_t {
00021   int cmd;
00022   int id;
00023   int valid;
00024 };
00025 
00026 class SerialPort {
00027 public:
00028   // Default Constructor
00029   SerialPort() {
00030     // Elan-104 on QNX-2 COM1
00031     modem = open( "//2/dev/ser1", O_RDWR );
00032     Task  = new SerialTask_t[ MAX_SERIAL_TASK ];
00033 
00034     for( int i = 0; i < MAX_SERIAL_TASK; i++ ) {
00035       Task[i].cmd   = 0;
00036       Task[i].id    = 0;
00037       Task[i].valid = 0;
00038     }
00039 
00040     bottom  = 0;
00041     Waiting = 0;
00042 
00043     curr.cmd   = 0;
00044     curr.id    = 0;
00045     curr.valid = 0;
00046 
00047     TrainCmd( 96, -1 );   // Activate Serial Controller
00048   }
00049 
00050   // Destructor
00051   virtual ~SerialPort() {
00052     delete [] Task;
00053     TrainCmd( 97, -1 );   // Deactivate Serial Controller
00054   }
00055 
00056   void TrainCmd( int cmd, int id ) {
00057     buf[0] = cmd;
00058 
00059     if ( id > 0 ) {
00060       buf[1] = id;
00061       write( modem, &buf, 2 );    // Send TrainID
00062     } else {
00063       write( modem, &buf, 1 );    // Don't Send TrainID
00064     }
00065 
00066     Waiting = 1;
00067     delay( MODEM_DELAY );
00068 
00069     Waiting = 0;
00070   }
00071 
00072   void AddTask( int cmd, int id ) {
00073     if ( Waiting == 0 ) {
00074       TrainCmd( cmd, id );
00075     } else if ( bottom >= MAX_SERIAL_TASK ) {
00076       printf("\n*** Error: Reach the end of the array  ***  ");
00077       printf("\n***        Can't Add more Serial Task ! ***\n"); 
00078       return;        
00079     } else {
00080       ++bottom;
00081       Task[ bottom ].cmd   = cmd;
00082       Task[ bottom ].id    = id;
00083       Task[ bottom ].valid = 1;
00084     }
00085   }
00086 
00087   void ExecuteTask() {
00088     if ( bottom < 1 ) return;
00089     if ( Task[0].valid == 0 ) return;
00090 
00091     curr.cmd = Task[0].cmd;
00092     curr.id  = Task[0].id;
00093 
00094     for( int k = 0; k < bottom; k++ ) {
00095       Task[k].cmd   = Task[k+1].cmd;
00096       Task[k].id    = Task[k+1].id;
00097       Task[k].valid = Task[k+1].valid;
00098     }
00099 
00100     Task[bottom].cmd   = 0;
00101     Task[bottom].id    = 0;
00102     Task[botton].valid = 0;
00103 
00104     bottom--;
00105 
00106     curr.valid = 0;
00107     TrainCmd( curr.cmd, curr.id );
00108   }
00109 
00110 private:
00111   int  modem;     // Modem File Descriptor
00112   char buf[3];    // Modem Output String Buffer
00113 
00114   SerialTask_t* Task;
00115   SerialTask_t  curr;
00116   int bottom;
00117   int Waiting;
00118 };
00119 
00120 #endif

Generated on Sun Oct 14 18:46:42 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001