graphlcd-base/glcddrivers/port.c
changeset 5 37602e25a04a
parent 4 df6a40031aa5
child 7 aac14789cd1d
equal deleted inserted replaced
4:df6a40031aa5 5:37602e25a04a
    14 #include <stdio.h>
    14 #include <stdio.h>
    15 #include <string.h>
    15 #include <string.h>
    16 #include <syslog.h>
    16 #include <syslog.h>
    17 #include <unistd.h>
    17 #include <unistd.h>
    18 #include <termios.h>
    18 #include <termios.h>
       
    19 #include <signal.h>
    19 #include <sys/io.h>
    20 #include <sys/io.h>
    20 #include <sys/ioctl.h>
    21 #include <sys/ioctl.h>
    21 #include <linux/ppdev.h>
    22 #include <linux/ppdev.h>
    22 #include <linux/parport.h>
    23 #include <linux/parport.h>
    23 
    24 
    24 
    25 
    25 
    26 
    26 #include "port.h"
    27 #include "port.h"
       
    28 
       
    29 
       
    30 
    27 
    31 
    28 namespace GLCD
    32 namespace GLCD
    29 {
    33 {
    30 
    34 
    31 static inline int port_in(int port)
    35 static inline int port_in(int port)
   280 
   284 
   281 cSerialPort::~cSerialPort()
   285 cSerialPort::~cSerialPort()
   282 {
   286 {
   283 }
   287 }
   284 
   288 
   285 int cSerialPort::Open(const char * device)
   289 int cSerialPort::Open(const char * device, void (*FP)(int))
   286 {
   290 {
   287     struct termios options;
   291     struct termios options;
       
   292     struct sigaction saio;
   288 
   293 
   289     fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
   294     fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
   290     if (fd == -1)
   295     if (fd == -1)
   291     {
   296     {
   292         printf("error opening port\n");
   297         printf("error opening port\n");
   293         return -1;
   298         return -1;
   294     }
   299     }
   295     //fcntl(fd, F_SETFL, FNDELAY);
   300 
   296     fcntl(fd, F_SETFL, 0);
   301 
       
   302     // install the signal handler before making the device asynchronous/
       
   303     saio.sa_handler = FP;
       
   304     sigemptyset (&saio.sa_mask);
       
   305     saio.sa_flags = 0;
       
   306     saio.sa_restorer = NULL;
       
   307     sigaction(SIGIO,&saio,NULL);
       
   308     
       
   309     // allow the process to receive SIGIO
       
   310     fcntl(fd, F_SETOWN, getpid());
       
   311     // Make the file descriptor asynchronous 
       
   312     fcntl(fd, F_SETFL, FASYNC);
   297 
   313 
   298     tcgetattr(fd, &options);
   314     tcgetattr(fd, &options);
   299 
   315 
   300     cfsetispeed(&options, B921600);
   316     cfsetispeed(&options, B921600);
   301     cfsetospeed(&options, B921600);
   317     cfsetospeed(&options, B921600);
   326         return -1;
   342         return -1;
   327     close(fd);
   343     close(fd);
   328     return 0;
   344     return 0;
   329 }
   345 }
   330 
   346 
   331 int cSerialPort::ReadData(unsigned char * data)
   347 int cSerialPort::ReadData(unsigned char * data, int num)
   332 {
   348 {
   333     if (fd == -1)
   349     if (fd == -1)
   334         return 0;
   350         return 0;
   335     return read(fd, data, 1);
   351     return read(fd, data, num);
   336 }
   352 }
   337 
   353 
   338 void cSerialPort::WriteData(unsigned char data)
   354 void cSerialPort::WriteData(unsigned char data)
   339 {
   355 {
   340     WriteData(&data, 1);
   356     WriteData(&data, 1);
   345     if (fd == -1)
   361     if (fd == -1)
   346         return;
   362         return;
   347     write(fd, data, length);
   363     write(fd, data, length);
   348 }
   364 }
   349 
   365 
       
   366 
   350 } // end of namespace
   367 } // end of namespace
       
   368