summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/camera/camera.cc
blob: fb1fe2d6fc41aa8e1832cf24e061a142af73d69b (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
// camera.cc
// buzz - Programme du robot Efrei Robotique I1-I2 2003
// Copyright (C) 2003 Nicolas Schodet
//
#include "camera.h"
#include "erreur/erreur.h"
#include "kernel/pbus.h"
#include "config/config.h"
#include "date/date.h"

#include <fcntl.h>
#include <unistd.h>
#include <iostream>	// Debug.
#include <iomanip>	//
#include <fstream>

#define CAM_FILE "/dev/robotcam"

#define CAMERA_DEBUG

// Constructeur.
Camera::Camera ()
{
    // Taille par d�faut.
    m_w = 352; m_h = 288; m_d = 1;
    m_cut = 0;
    // Ouvre le p�riph�rique.
    m_fd = open (CAM_FILE, O_RDONLY);
    if (m_fd == -1)
	throw ErreurFatale ("Impossible d'ouvrir le p�riph�rique de la"
			    " camera.\n");
    // Param�tre la camera.
    sccb_io param;
    Config rc ("rc/camera");
    int addr, data;
    while (!rc.eof ())
      {
	if (rc.isId ("width"))
	  {
	    rc.getId ();
	    m_w = rc.getNum ();
#ifdef CAMERA_DEBUG
	    cout << "camera width " << m_w << endl;
#endif // CAMERA_DEBUG
	  }
	else if (rc.isId ("height"))
	  {
	    rc.getId ();
	    m_h = rc.getNum ();
#ifdef CAMERA_DEBUG
	    cout << "camera height " << m_h << endl;
#endif // CAMERA_DEBUG
	  }
	else if (rc.isId ("depth"))
	  {
	    rc.getId ();
	    m_d = rc.getNum ();
#ifdef CAMERA_DEBUG
	    cout << "camera depth " << m_d << endl;
#endif // CAMERA_DEBUG
	  }
	else if (rc.isId ("cut"))
	  {
	    rc.getId ();
	    m_cut = rc.getNum ();
#ifdef CAMERA_DEBUG
	    cout << "camera cut " << m_cut << endl;
#endif // CAMERA_DEBUG
	  }
	else if (rc.isId ("setup"))
	  {
	    rc.getId ();
	    addr = rc.getNum ();
	    data = rc.getNum ();
	    param.addr = addr; param.data = data;
#ifdef CAMERA_DEBUG
	    cout << "camera sccbwrite 0x" << hex << addr << " 0x" << data <<
		endl;
#endif // CAMERA_DEBUG
	    ioctl (m_fd, CAM_SCCBWRITE, &param);
	  }
	else
	    rc.noId ();
      }
    // Affiche les param�tres.
#ifdef CAMERA_DEBUG
    cout << hex << "camera sccbdump";
    for (addr = 0; addr < 0x50; ++addr)
      {
	if (!(addr % 16)) cout << endl;
	param.addr = addr;
	ioctl (m_fd, CAM_SCCBREAD, &param);
	data = param.data;
	cout << setw (2) << setfill ('0') << data << ' ';

      }
    cout << endl << dec;
#endif // CAMERA_DEBUG
    m_lastRead = 0;
    // Param�tre la taille de frame.
    int hcount = m_w * m_d;
    ioctl (m_fd, CAM_SETHCOUNT, &hcount);
    m_frameSize = m_w * m_h * m_d + m_cut;
    ioctl (m_fd, CAM_SETFRAMESIZE, &m_frameSize);
}

// Destructeur.
Camera::~Camera ()
{
    // Ferme le p�riph�rique de camera.
    close (m_fd);
}

// Lit une image.
int
Camera::read (unsigned char *image)
{
    int r;
    int t = Date::getInstance ().start ();
    // Attention, pas trops vite.
    if (m_lastRead + 100 > t)
	return 0;
    m_lastRead = t;
    // Lit les donn�es sur la camera.
    if (m_cut)
	r = ::read (m_fd, image, m_cut);
    if (!m_cut || r)
	r = ::read (m_fd, image, m_frameSize - m_cut);
#ifdef CAMERA_DEBUG
    cout << "camera read " << r << endl;
#endif // CAMERA_DEBUG
    return r;
}

// Lit une image en RGB.
int
Camera::readRGB (unsigned char *rgb)
{
    int ret;
    int t = Date::getInstance ().start ();
    // Attention, pas trops vite.
    if (m_lastRead + 100 > t)
	return 0;
    m_lastRead = t;
    // Alloue de la m�moire.
    unsigned char *image = new unsigned char[m_frameSize];
    // Lit les donn�es sur la camera.
    ret = ::read (m_fd, image, m_frameSize);
#ifdef CAMERA_DEBUG
    cout << "camera read " << ret << endl;
#endif // CAMERA_DEBUG
    if (!ret) return 0;
    // Positions de d�buts.
    unsigned char *r, *g, *b;
    r = image + m_cut + 2;
    g = image + m_cut + 1;
    b = image + m_cut;
    // D�code les couleurs.
    for (int i = 0; i < m_w * m_h; ++i)
      {
	*rgb++ = *r;
	r += 4;
	*rgb++ = *g;
	g += 4;
	*rgb++ = *b;
	b += 4;
      }
    delete image;
    return ret - m_cut;
}