You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.4KB

  1. /* MousePlugin
  2. * Copyleft (C) 2002 Dan Bethell <dan@pawfal.org>
  3. * Dave Griffiths <dave@pawfal.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include "scratch.h"
  20. #define BAUDRATE B1200
  21. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  22. #define FALSE 0
  23. #define TRUE 1
  24. int c, res;
  25. int count, total;
  26. volatile int STOP=FALSE;
  27. int fd; // the serial port device
  28. struct termios oldtio,newtio;
  29. unsigned char buf[3];
  30. char *MODEMDEVICE;
  31. scratch::scratch(char *dev)
  32. {
  33. openSerialPort(dev);
  34. pthread_mutex_init( &mutex, NULL);
  35. pthread_create( &scratch_thread, NULL, (void *(*)(void *))listen, this);
  36. }
  37. scratch::~scratch()
  38. {
  39. closeSerialPort();
  40. }
  41. void scratch::setData(char c)
  42. {
  43. pthread_mutex_lock( &mutex );
  44. data = c;
  45. pthread_mutex_unlock( &mutex );
  46. }
  47. char scratch::getData()
  48. {
  49. char result;
  50. pthread_mutex_lock( &mutex );
  51. result = data;
  52. pthread_mutex_unlock( &mutex );
  53. return result;
  54. }
  55. void scratch::stop()
  56. {
  57. pthread_mutex_lock( &mutex );
  58. STOP=TRUE;
  59. pthread_mutex_unlock( &mutex );
  60. }
  61. void openSerialPort(char *dev)
  62. {
  63. char *MODEMDEVICE = dev;
  64. fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  65. if (fd <0)
  66. {
  67. perror(MODEMDEVICE); exit(-1);
  68. }
  69. tcgetattr(fd,&oldtio); /* save current port settings */
  70. bzero(&newtio, sizeof(newtio));
  71. newtio.c_cflag = BAUDRATE | CS7 | CLOCAL | CREAD;
  72. newtio.c_iflag = IGNPAR;
  73. newtio.c_oflag = 0;
  74. /* set input mode (non-canonical, no echo,...) */
  75. newtio.c_lflag = 0;
  76. newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
  77. newtio.c_cc[VMIN] = 3; /* blocking read until 5 chars received */
  78. tcflush(fd, TCIFLUSH);
  79. tcsetattr(fd,TCSANOW,&newtio);
  80. }
  81. void closeSerialPort()
  82. {
  83. tcsetattr(fd,TCSANOW,&oldtio); // reset serial port settings
  84. close(fd); // close pipe to serial port
  85. }
  86. void listen(scratch *scr)
  87. {
  88. while (STOP==FALSE)
  89. { /* loop for input */
  90. res = read(fd,buf,3); /* returns after 3 chars have been input */
  91. if (buf[0]>>6&1)
  92. {
  93. char c;
  94. c = ((buf[0]&12)<<4)|(buf[2]&63);
  95. if (c!=0x0) {
  96. scr->setData(c);
  97. // some random logging stuff
  98. /*
  99. total += (int)c;
  100. count ++;
  101. if (count%20==0) {
  102. FILE *ptr = fopen("/tmp/EYEMUSH.log", "a+");
  103. fprintf(ptr, "EYEMUSH LOG:\tScratch\n");
  104. fclose(ptr);
  105. }
  106. */
  107. }
  108. }
  109. else
  110. {
  111. read(fd,buf,1);
  112. }
  113. }
  114. }