summaryrefslogtreecommitdiff
path: root/noncore/multimedia/camera/zcameraio.cpp
Unidiff
Diffstat (limited to 'noncore/multimedia/camera/zcameraio.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/camera/zcameraio.cpp58
1 files changed, 34 insertions, 24 deletions
diff --git a/noncore/multimedia/camera/zcameraio.cpp b/noncore/multimedia/camera/zcameraio.cpp
index 7343dca..56b2e13 100644
--- a/noncore/multimedia/camera/zcameraio.cpp
+++ b/noncore/multimedia/camera/zcameraio.cpp
@@ -15,66 +15,72 @@
15 15
16#include "zcameraio.h" 16#include "zcameraio.h"
17 17
18#include <sys/types.h> 18#include <sys/types.h>
19#include <sys/stat.h> 19#include <sys/stat.h>
20#include <errno.h> 20#include <errno.h>
21#include <string.h> 21#include <string.h>
22#include <fcntl.h> 22#include <fcntl.h>
23#include <unistd.h> 23#include <unistd.h>
24#include <stdio.h> 24#include <stdio.h>
25 25
26#include <qimage.h> 26#include <qimage.h>
27#include <qdatetime.h>
27 28
28#include <opie2/odebug.h> 29#include <opie2/odebug.h>
29 30
31#define SHARPZDC "/dev/sharp_zdc"
32
30ZCameraIO* ZCameraIO::_instance = 0; 33ZCameraIO* ZCameraIO::_instance = 0;
31 34
32ZCameraIO* ZCameraIO::instance() 35ZCameraIO* ZCameraIO::instance()
33{ 36{
34 if ( !ZCameraIO::_instance ) 37 if ( !ZCameraIO::_instance )
35 { 38 {
36 odebug << "Creating ZCameraIO::_instance" << oendl; 39 odebug << "Creating ZCameraIO::_instance" << oendl;
37 ZCameraIO::_instance = new ZCameraIO(); 40 ZCameraIO::_instance = new ZCameraIO();
38 } 41 }
39 return ZCameraIO::_instance; 42 return ZCameraIO::_instance;
40} 43}
41 44
42 45
43ZCameraIO::ZCameraIO() 46ZCameraIO::ZCameraIO()
44 :_height( 0 ), _width( 0 ), _zoom( 0 ), _rot( 0 ), _readlen( 0 ) 47 :_height( 0 ), _width( 0 ), _zoom( 0 ), _rot( 0 ), _readlen( 0 )
45{ 48{
46 _driver = ::open( "/dev/sharp_zdc", O_RDWR ); 49 _driver = ::open( SHARPZDC, O_RDWR );
47 if ( _driver == -1 ) 50 if ( _driver == -1 )
48 oerr << "Can't open camera driver: " << strerror(errno) << oendl; 51 oerr << "Can't open camera driver: " << strerror(errno) << oendl;
49 else 52 else
50 init(); 53 init();
51} 54}
52 55
53 56
54void ZCameraIO::init() 57void ZCameraIO::init()
55{ 58{
56 if ( ZCameraIO::_instance ) 59 if ( ZCameraIO::_instance )
57 ofatal << "Don't create more than one ZCameraIO instances." << oendl; 60 ofatal << "Don't create more than one ZCameraIO instances." << oendl;
58 else 61 else
59 { 62 {
60 setReadMode( STATUS ); 63 setReadMode( STATUS );
61 } 64 }
62} 65}
63 66
64 67
65ZCameraIO::~ZCameraIO() 68ZCameraIO::~ZCameraIO()
66{ 69{
67 if ( _driver != -1 ) 70 if ( _driver != -1 )
71 {
72 setReadMode( 0 );
68 ::close( _driver ); 73 ::close( _driver );
74 }
69} 75}
70 76
71 77
72inline bool ZCameraIO::isOpen() const 78inline bool ZCameraIO::isOpen() const
73{ 79{
74 return _driver != -1; 80 return _driver != -1;
75} 81}
76 82
77 83
78inline bool ZCameraIO::isShutterPressed() 84inline bool ZCameraIO::isShutterPressed()
79{ 85{
80 return _status[0] == 'S'; 86 return _status[0] == 'S';
@@ -98,109 +104,113 @@ inline bool ZCameraIO::isAvailable() const
98{ 104{
99 return _status[3] == 'A'; 105 return _status[3] == 'A';
100} 106}
101 107
102 108
103bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot ) 109bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot )
104{ 110{
105 char b[100]; 111 char b[100];
106 sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 ); 112 sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 );
107 if ( write( b ) ) 113 if ( write( b ) )
108 { 114 {
109 _width = width; 115 _width = width;
110 _height = _height; 116 _height = height;
111 _zoom = zoom; 117 _zoom = zoom;
112 _rot = rot; 118 _rot = rot;
113 _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel 119 _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel
114 return true; 120 return true;
115 } 121 }
116 return false; 122 return false;
117} 123}
118 124
119 125
120void ZCameraIO::setReadMode( int mode ) 126void ZCameraIO::setReadMode( int mode )
121{ 127{
122 char b[4]; 128 char b[10];
123 sprintf( b, "M=%d", mode ); 129 sprintf( b, "M=%d", mode );
124 write( b, mode <= 9 ? 3 : 4 ); 130 write( b, mode <= 9 ? 3 : 4 );
125 if ( mode & 1 ) // STATUS bit is set 131 if ( mode & 1 ) // STATUS bit is set
126 read( _status, 4 ); 132 read( _status, 4 );
127} 133}
128 134
129 135
130void ZCameraIO::clearShutterLatch() 136void ZCameraIO::clearShutterLatch()
131{ 137{
132 char b = 'B'; 138 char b = 'B';
133 write( &b, 1 ); 139 write( &b, 1 );
134} 140}
135 141
136 142
137bool ZCameraIO::read( char* b, int len ) 143bool ZCameraIO::read( char* b, int len )
138{ 144{
145 #ifndef NO_TIMING
146 QTime t;
147 t.start();
148 #endif
139 int rlen = ::read( _driver, b, len ); 149 int rlen = ::read( _driver, b, len );
140 odebug << "read " << rlen << " from driver." << oendl; 150 #ifndef NO_TIMING
151 int time = t.elapsed();
152 #else
153 int time = -1;
154 #endif
155 if ( rlen )
156 odebug << "read " << rlen << " ('" << b[0] << b[1] << b[2] << b[3] << "') [" << time << " ms] from driver." << oendl;
157 else
158 odebug << "read nothing from driver." << oendl;
141 return rlen == len; 159 return rlen == len;
142} 160}
143 161
144 162
145bool ZCameraIO::write( char* buf, int len ) 163bool ZCameraIO::write( char* buf, int len )
146{ 164{
147 if ( !len ) 165 if ( !len )
148 len = strlen( buf ); 166 len = strlen( buf );
149 167
150 odebug << "writing '" << buf << "' to driver." << oendl; 168 odebug << "writing '" << buf << "' to driver." << oendl;
151 169
152 return ::write( _driver, buf, len ) == len; 170 return ::write( _driver, buf, len ) == len;
153} 171}
154 172
155 173
156bool ZCameraIO::snapshot( QImage* image ) 174bool ZCameraIO::snapshot( QImage* image )
157{ 175{
158 /* 176 setReadMode( IMAGE | XFLIP | YFLIP );
159
160 char buf[76800]; 177 char buf[76800];
161 178 char* bp = buf;
162 write( _driver, "M=13", 4 );
163 write( _driver, "R=240,160,256,480", 17 );
164 write( _driver, "M=12", 4 );
165
166 int result = read( _driver, &buf, sizeof buf );
167
168 return result == sizeof buf;
169
170 */
171
172 unsigned char buf[76800];
173 unsigned char* bp = buf;
174 unsigned char* p; 179 unsigned char* p;
175 180
176 int fd = open( "/tmp/cam", O_RDONLY ); 181 read( bp, _readlen );
177 if ( ::read( fd, buf, sizeof buf ) != sizeof buf )
178 owarn << "Couldn't read image from /dev/sharp_zdc" << oendl;
179 182
180 image->create( 240, 160, 16 ); 183 image->create( 240, 160, 16 );
181 for ( int i = 0; i < 160; ++i ) 184 for ( int i = 0; i < 160; ++i )
182 { 185 {
183 p = image->scanLine( i ); 186 p = image->scanLine( i );
184 for ( int j = 0; j < 240; j++ ) 187 for ( int j = 0; j < 240; j++ )
185 { 188 {
186 *p = *bp; 189 *p = *bp;
187 p++; 190 p++;
188 bp++; 191 bp++;
189 *p = *bp; 192 *p = *bp;
190 p++; 193 p++;
191 bp++; 194 bp++;
192 } 195 }
193 } 196 }
194 197
195 return true; 198 return true;
196} 199}
197 200
198bool ZCameraIO::snapshot( uchar* buf ) 201bool ZCameraIO::snapshot( unsigned char* buf )
199{ 202{
203 setReadMode( IMAGE | XFLIP | YFLIP );
204
205 read( (char*) buf, _readlen );
206
207 /* //TESTCODE
200 int fd = open( "/tmp/cam", O_RDONLY ); 208 int fd = open( "/tmp/cam", O_RDONLY );
201 if ( ::read( fd, buf, 76800 ) != 76800 ) 209 if ( ::read( fd, (char*) buf, 76800 ) != 76800 )
202 owarn << "Couldn't read image from /dev/sharp_zdc" << oendl; 210 owarn << "Couldn't read image from /dev/sharp_zdc" << oendl;
211 // TESTCODE */
212
203 213
204 return true; 214 return true;
205} 215}
206 216