Fixed NetSend so it can be used in debug mode.
[qt/compvision.git] / SerialReceive.cpp
1 // Receive color pictures from the DSP over serial\r
2 #include "SerialReceive.h"\r
3 \r
4 #include <QMutexLocker>\r
5 \r
6 SerialReceive::SerialReceive(ImagePool *source, ImagePool *destination) : ImageSource(source, destination)\r
7 {\r
8         width=SRWIDTH;\r
9         height=SRHEIGHT;\r
10         upsampling=5;\r
11         \r
12         // set the buffer to black\r
13         int *tmp=(int *)buffer;\r
14         for(int i=0; i<width*height; i++)\r
15         {\r
16                 *(tmp++)=0;\r
17         }\r
18         \r
19         sp.setPort(2);\r
20         sp.setBaudRate(BAUD115200);\r
21         sp.setDataBits(DATA_8);\r
22         sp.setParity(PAR_NONE);\r
23         sp.setStopBits(STOP_1);\r
24         sp.setFlowControl(FLOW_OFF);\r
25         sp.setTimeout(5); // really fast timeout; we'll be back soon enough\r
26         \r
27         QObject::connect(&sp, SIGNAL(dataReady()), this, SLOT(receive()));\r
28         QObject::connect(&sp, SIGNAL(portOpened()), this, SLOT(opened()));\r
29         QObject::connect(&sp, SIGNAL(portClosed()), this, SLOT(closed()));\r
30         \r
31         channel=0;\r
32 }\r
33 \r
34 void SerialReceive::connect()\r
35 {\r
36         sp.openPort();\r
37 }\r
38 \r
39 void SerialReceive::disconnect()\r
40 {\r
41         sp.closePort();\r
42 }\r
43 \r
44 void SerialReceive::opened()\r
45 {\r
46         connected=true;\r
47 }\r
48 \r
49 void SerialReceive::closed()\r
50 {\r
51         connected=false;\r
52 }\r
53 \r
54 bool SerialReceive::isConnected()\r
55 {\r
56         return connected;\r
57 }\r
58 \r
59 // flip the received image about both axes\r
60 void SerialReceive::receive()\r
61 {\r
62         int available=sp.receive(SRCOMBUF, serdat);\r
63         char c;\r
64         char *max=buffer+width*height*4;\r
65         \r
66         for(int pos=0; pos<available; pos++)\r
67         {\r
68                 c=serdat[pos];\r
69                 switch(c)\r
70                 {\r
71                         case 255: // end\r
72                                 channel=0;\r
73                                 if(!sync.available()) sync.release();\r
74                                 break;\r
75                         case 253: // red\r
76                                 channel=buffer+2;\r
77                                 break;\r
78                         case 252: // green\r
79                                 channel=buffer+1;\r
80                                 break;\r
81                         case 251: // blue\r
82                                 channel=buffer+0;\r
83                                 break;\r
84                         case 250: // image value >= 250\r
85                                 c=255;\r
86                         default:\r
87                                 if(buffer<=channel && channel<max)\r
88                                 {\r
89                                         *channel=c;\r
90                                         channel+=4;\r
91                                 }\r
92                                 break;\r
93                 }\r
94         }\r
95 }\r
96 \r
97 void SerialReceive::process()\r
98 {\r
99         sync.acquire();\r
100         \r
101         if(img->isNull() || img->width()!=width*upsampling || img->height()!=height*upsampling || img->format()!=QImage::Format_RGB32)\r
102         {\r
103                 delete(img);\r
104                 img=new QImage(width*upsampling, height*upsampling, QImage::Format_RGB32);\r
105         }\r
106         \r
107         // copy over the buffer\r
108         for(int r=0; r<upsampling; r++) // offset the row\r
109         {\r
110                 for(int c=0; c<upsampling; c++) // offset the column\r
111                 {\r
112                         int *buf=(int *)buffer;\r
113                         int *copy=(int *)(img->bits()) + c + r*width*upsampling;\r
114                         for(int y=0; y<height; y++)\r
115                         {\r
116                                 for(int x=0; x<width; x++)\r
117                                 {\r
118                                         *copy=*(buf++);\r
119                                         copy+=upsampling;\r
120                                 }\r
121                                 copy+=width*upsampling*(upsampling-1);\r
122                         }\r
123                 }\r
124         }\r
125 }\r