summaryrefslogtreecommitdiff
path: root/noncore/multimedia/camera/zcameraio.cpp
blob: 51771a54a735185276a0351e56dbe1f8f9621aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**********************************************************************
** Copyright (C) 2002 Michael 'Mickey' Lauer.  All rights reserved.
**
** This file is part of Opie Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "zcameraio.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include <qimage.h>
#include <qdatetime.h>

#include <opie2/odebug.h>

#define SHARPZDC "/dev/sharp_zdc"

ZCameraIO* ZCameraIO::_instance = 0;

ZCameraIO* ZCameraIO::instance()
{
    if ( !ZCameraIO::_instance )
    {
        odebug << "Creating ZCameraIO::_instance" << oendl;
        ZCameraIO::_instance = new ZCameraIO();
    }
    return ZCameraIO::_instance;
}


ZCameraIO::ZCameraIO()
          :_height( 0 ), _width( 0 ), _zoom( 0 ), _rot( 0 ), _readlen( 0 )
{
    _driver = ::open( SHARPZDC, O_RDWR );
    if ( _driver == -1 )
        oerr << "Can't open camera driver: " << strerror(errno) << oendl;
    else
        init();
}


void ZCameraIO::init()
{
    if ( ZCameraIO::_instance )
        ofatal << "Don't create more than one ZCameraIO instances." << oendl;
    else
    {
        setReadMode( STATUS );
    }
}


ZCameraIO::~ZCameraIO()
{
    if ( _driver != -1 )
    {
        setReadMode( 0 );
        ::close( _driver );
    }
}


inline bool ZCameraIO::isOpen() const
{
    return _driver != -1;
}


inline bool ZCameraIO::isShutterPressed()
{
    return _status[0] == 'S';
    clearShutterLatch();
}


inline bool ZCameraIO::isFinderReversed() const
{
    return _status[1] == 'M';
}


inline bool ZCameraIO::isCapturing() const
{
    return _status[2] == 'C';
}


inline bool ZCameraIO::isAvailable() const
{
    return _status[3] == 'A';
}


bool ZCameraIO::setCaptureFrame( int width, int height, int zoom, bool rot )
{
    odebug << "setCaptureFrame( " << width << ", " << height << ", " << zoom << ", " << rot << " )" << oendl;
    char b[100];
    sprintf( b, "%c=%d,%d,%d,%d", rot ? 'R':'S', width, height, zoom, width*2 );
    if ( write( b ) )
    {
        _width = width;
        _height = height;
        _zoom = zoom;
        _rot = rot;
        _readlen = 2 * _width * _height; // camera is fixed @ 16 bits per pixel
        return true;
    }
    return false;
}


void ZCameraIO::setReadMode( int mode )
{
    char b[10];
    sprintf( b, "M=%d", mode );
    write( b, mode <= 9 ? 3 : 4 );
    if ( mode & 1 ) // STATUS bit is set
        read( _status, 4 );
}


void ZCameraIO::clearShutterLatch()
{
    char b = 'B';
    write( &b, 1 );
}


bool ZCameraIO::read( char* b, int len )
{
    #ifndef NO_TIMING
    QTime t;
    t.start();
    #endif
    int rlen = ::read( _driver, b, len );
    #ifndef NO_TIMING
    int time = t.elapsed();
    #else
    int time = -1;
    #endif
    if ( rlen )
        odebug << "read " << rlen << " ('" << b[0] << b[1] << b[2] << b[3] << "') [" << time << " ms] from driver." << oendl;
    else
        odebug << "read nothing from driver." << oendl;
    return rlen == len;
}


bool ZCameraIO::write( char* buf, int len )
{
    if ( !len )
        len = strlen( buf );

    odebug << "writing '" << buf << "' to driver." << oendl;

    return ::write( _driver, buf, len ) == len;
}


bool ZCameraIO::snapshot( QImage* image )
{
    setReadMode( STATUS );

    odebug << "finder reversed = " << isFinderReversed() << oendl;
    odebug << "rotation = " << _rot << oendl;

    if ( _rot ) // Portrait
    {
        setReadMode( IMAGE | isFinderReversed() ? XFLIP | YFLIP : 0 );
    }
    else // Landscape
    {
        setReadMode( IMAGE | XFLIP | YFLIP ); //isFinderReversed() ? 0 : XFLIP );
    }

    char buf[_readlen];
    char* bp = buf;
    unsigned char* p;

    read( bp, _readlen );

    image->create( _width, _height, 16 );
    for ( int i = 0; i < _height; ++i )
    {
        p = image->scanLine( i );
        for ( int j = 0; j < _width; j++ )
        {
            *p = *bp;
            p++;
            bp++;
            *p = *bp;
            p++;
            bp++;
        }
    }

    return true;
}

bool ZCameraIO::snapshot( unsigned char* buf )
{
    setReadMode( IMAGE | XFLIP | YFLIP );

    read( (char*) buf, _readlen );

    /* //TESTCODE
    int fd = open( "/tmp/cam", O_RDONLY );
    if ( ::read( fd, (char*) buf, 76800 ) != 76800 )
        owarn << "Couldn't read image from /dev/sharp_zdc" << oendl;
    // TESTCODE */


    return true;
}