]> rtime.felk.cvut.cz Git - sojka/tekpic.git/blob - src/osccommthread.cpp
Added support for PCX images. TDS224 doesn't have TIFF.
[sojka/tekpic.git] / src / osccommthread.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Michal Sojka   *
3  *   sojkam1@fel.cvut.cz   *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "osccommthread.h"
21 #include <termios.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <poll.h>
25 #include <iomanip>
26 #include <iostream>
27 using namespace std;
28
29 ostream& operator<<(ostream& s, OscImage& im)
30 {
31         int i;
32         ios::fmtflags f;
33         
34         s << "Size: " << im.size() << " Data " << (void *)im.data() << ": ";
35         f = s.flags();
36         s << hex;
37         for (i=0; i<(im.size() > 32 ? 32 : im.size()); i++)
38                   s << setw(2) << setfill('0') << (unsigned int)(unsigned char)im.at(i) << ' ';
39         s.flags(f);
40         if (im.size() >= i) s << "...";
41         return s;
42 }
43
44 OscCommThread::OscCommThread( const string &tty)
45 {
46         m_fd = open(tty.c_str(), O_RDWR);
47         if (m_fd<0) {
48                 perror(tty.c_str());
49                 exit(1);
50         }                                
51         m_img = new OscImage;
52
53         struct termios ts;
54
55         /* Flush input and output queues. */
56         if (tcflush(m_fd, TCIOFLUSH) != 0) {
57                 cerr << "Error in tcflush" << endl;
58                 exit(1);
59         }
60
61         /* Fetch the current terminal parameters. */
62         if (tcgetattr(m_fd, &ts) != 0) {
63                 cerr << "Error in tcfgetattr" << endl;
64                 exit(1);
65         }
66
67         /* Sets hardware control flags:                              */
68         /* 8 data bits                                               */
69         /* Enable receiver                                           */
70         /* Ignore CD (local connection)                              */
71         ts.c_cflag = CS8 | CREAD | CLOCAL;
72         //ts.c_cflag |= CRTSCTS; /* CCTS_OFLOW | CRTS_IFLOW */
73         ts.c_iflag = 0;
74         ts.c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
75         ts.c_lflag = 0;
76
77         cfsetispeed (&ts, B19200);
78         cfsetospeed (&ts, B19200);
79         
80         /* Sets the new terminal parameters. */
81         if (tcsetattr(m_fd, TCSANOW, &ts) != 0) {
82                 cerr << "Error in tcsetattr" << endl;
83                 exit(1);
84         }
85 }
86
87
88 OscCommThread::~OscCommThread()
89 {}
90
91 /*!
92     \fn OscCommThread::run()
93  */
94 void OscCommThread::run()
95 {
96         char buf[100];
97         int n;
98         struct pollfd pfd;
99         int ret;
100         
101         pfd.fd = m_fd;
102         pfd.events = POLLIN;
103         
104         while (1) {
105                 ret = poll(&pfd, 1, 1000/*ms*/);
106                 if (ret>0) {
107                         if (m_img->size() == 0) newImage();
108                         n = read(m_fd, buf, sizeof(buf)-1);
109                         QByteArray ba = QByteArray::fromRawData(buf, n);
110                         m_img->append(ba);
111                         bytesReceived( m_img->size() );
112                 } else if (ret==0) {
113                         if (m_img->size()) {
114                                 processImage();
115                         }
116                 }
117         }
118 }
119
120 void OscCommThread::processImage()
121 {
122         imageAvailable( m_img );
123         m_img = new OscImage;
124 }