summaryrefslogtreecommitdiff
path: root/noncore/multimedia/camera/lib/zcameraio.cpp
Unidiff
Diffstat (limited to 'noncore/multimedia/camera/lib/zcameraio.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/camera/lib/zcameraio.cpp311
1 files changed, 311 insertions, 0 deletions
diff --git a/noncore/multimedia/camera/lib/zcameraio.cpp b/noncore/multimedia/camera/lib/zcameraio.cpp
new file mode 100644
index 0000000..c940b45
--- a/dev/null
+++ b/noncore/multimedia/camera/lib/zcameraio.cpp
@@ -0,0 +1,311 @@
1/**********************************************************************
2** Copyright (C) 2002 Michael 'Mickey' Lauer. All rights reserved.
3**
4** This file is part of Opie Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14**********************************************************************/
15
16#include "zcameraio.h"
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <errno.h>
21#include <string.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <stdio.h>
25
26#include <qimage.h>
27#include <qdatetime.h>
28
29#include <opie2/odebug.h>
30
31#define SHARPZDC "/dev/sharp_zdc"
32
33ZCameraIO* ZCameraIO::_instance = 0;
34
35ZCameraIO* ZCameraIO::instance()
36{
37 if ( !ZCameraIO::_instance )
38 {
39 odebug << "Creating ZCameraIO::_instance" << oendl;
40 ZCameraIO::_instance = new ZCameraIO();
41 }
42 return ZCameraIO::_instance;
43}
44
45
46ZCameraIO::ZCameraIO()
47 : _pressed( false ), _height( 0 ), _width( 0 ), _zoom( 0 ),
48 _flip( -1 ), _rot( 0 ), _readlen( 0 )
49
50{
51 _driver = ::open( SHARPZDC, O_RDWR );
52 if ( _driver == -1 )
53 oerr << "Can't open camera driver: " << strerror(errno) << oendl;
54 else
55 init();
56}
57
58
59void ZCameraIO::init()
60{
61 if ( ZCameraIO::_instance )
62 ofatal << "Don't create more than one ZCameraIO instances." << oendl;
63 else
64 {
65 _timer = new QTime();
66 setReadMode( STATUS );
67 }
68}
69
70
71ZCameraIO::~ZCameraIO()
72{
73 if ( _driver != -1 )
74 {
75 setReadMode( 0 );
76 ::close( _driver );
77 }
78}
79
80
81bool ZCameraIO::isOpen() const
82{
83 return _driver != -1;
84}
85
86
87bool ZCameraIO::isShutterPressed()
88{
89 if ( _status[0] == 'S' )
90 {
91 if ( !_pressed ) // wasn't pressed before, but is now!
92 {
93 _pressed = true;
94 _timer->start();
95 return true;
96 }
97
98 if ( _timer->elapsed() > 2000 ) // the press is pretty old now
99 {
100 clearShutterLatch();
101 _status[0] = 's';
102 _pressed = false;
103 }
104 }
105
106 return false;
107}
108
109
110bool ZCameraIO::isFinderReversed() const
111{
112 return _status[1] == 'M';
113}
114
115
116bool ZCameraIO::isCapturing() const
117{
118 return _status[2] == 'C';
119}
120
121
122bool ZCameraIO::isAvailable() const
123{
124 return _status[3] == 'A';
125}
126
127
128bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot )
129{
130 odebug << "setCaptureFrame( " << width << ", " << height << ", " << zoom << ", " << rot << " )" << oendl;
131 char b[100];
132 sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 );
133 if ( write( b ) )
134 {
135 _width = width;
136 _height = height;
137 _zoom = zoom;
138 _rot = rot;
139 _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel
140 return true;
141 }
142 return false;
143}
144
145
146bool ZCameraIO::setZoom( int zoom )
147{
148 return setCaptureFrame( _width, _height, zoom*256, _rot );
149}
150
151
152void ZCameraIO::setReadMode( int mode )
153{
154 char b[10];
155 sprintf( b, "M=%d", mode );
156 write( b, mode <= 9 ? 3 : 4 );
157 if ( mode & STATUS ) // STATUS bit is set
158 {
159 read( _status, 4 );
160 if ( isShutterPressed() )
161 {
162 emit shutterClicked();
163 }
164 }
165}
166
167
168void ZCameraIO::setFlip( int flip )
169{
170 _flip = flip;
171}
172
173
174void ZCameraIO::clearShutterLatch()
175{
176 write( "B", 1 );
177}
178
179
180bool ZCameraIO::read( char* b, int len )
181{
182 #ifndef NO_TIMING
183 QTime t;
184 t.start();
185 #endif
186 int rlen = ::read( _driver, b, len );
187 #ifndef NO_TIMING
188 int time = t.elapsed();
189 #else
190 int time = -1;
191 #endif
192 if ( rlen )
193 odebug << "read " << rlen << " ('" << b[0] << b[1] << b[2] << b[3] << "') [" << time << " ms] from driver." << oendl;
194 else
195 odebug << "read nothing from driver." << oendl;
196 return rlen == len;
197}
198
199
200bool ZCameraIO::write( char* buf, int len )
201{
202 if ( !len )
203 len = strlen( buf );
204
205 odebug << "writing '" << buf << "' to driver." << oendl;
206
207 return ::write( _driver, buf, len ) == len;
208}
209
210
211bool ZCameraIO::snapshot( QImage* image )
212{
213 setReadMode( STATUS );
214
215 odebug << "finder reversed = " << isFinderReversed() << oendl;
216 odebug << "rotation = " << _rot << oendl;
217
218 int readmode;
219 if ( _flip == -1 ) // AUTO
220 {
221 if ( _rot ) // Portrait
222 {
223 readmode = IMAGE | isFinderReversed() ? XFLIP | YFLIP : 0;
224 }
225 else // Landscape
226 {
227 readmode = IMAGE | XFLIP | YFLIP;
228 }
229 }
230 else // OVERRIDE
231 {
232 readmode = IMAGE | _flip;
233 }
234
235 setReadMode( readmode );
236
237 char buf[_readlen];
238 char* bp = buf;
239 unsigned char* p;
240
241 read( bp, _readlen );
242
243 image->create( _width, _height, 16 );
244 for ( int i = 0; i < _height; ++i )
245 {
246 p = image->scanLine( i );
247 for ( int j = 0; j < _width; j++ )
248 {
249 *p = *bp;
250 p++;
251 bp++;
252 *p = *bp;
253 p++;
254 bp++;
255 }
256 }
257
258 return true;
259}
260
261
262bool ZCameraIO::snapshot( unsigned char* buf )
263{
264 setReadMode( STATUS );
265
266 odebug << "finder reversed = " << isFinderReversed() << oendl;
267 odebug << "rotation = " << _rot << oendl;
268
269 int readmode;
270 if ( _flip == -1 ) // AUTO
271 {
272 if ( _rot ) // Portrait
273 {
274 readmode = IMAGE | isFinderReversed() ? XFLIP | YFLIP : 0;
275 }
276 else // Landscape
277 {
278 readmode = IMAGE | XFLIP | YFLIP;
279 }
280 }
281 else // OVERRIDE
282 {
283 readmode = IMAGE | _flip;
284 }
285
286 setReadMode( readmode );
287 read( (char*) buf, _readlen );
288
289}
290
291
292void ZCameraIO::captureFrame( int w, int h, int zoom, QImage* image )
293{
294 int pw = _width;
295 int ph = _height;
296 setCaptureFrame( w, h, zoom*256, _rot );
297 snapshot( image );
298 setCaptureFrame( pw, ph, _zoom, _rot );
299}
300
301
302void ZCameraIO::captureFrame( int w, int h, int zoom, unsigned char* buf )
303{
304 //FIXME: this is too slow
305 int pw = _width;
306 int ph = _height;
307 setCaptureFrame( w, h, zoom*256, _rot );
308 snapshot( buf );
309 setCaptureFrame( pw, ph, _zoom, _rot );
310}
311