summaryrefslogtreecommitdiff
path: root/camlibs/largan/lmini/largan.c
blob: 2f3e87f5f50ab7382e4f9692938eafac03fc29e5 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* largan.c
 *
 * Copyright 2001 Lutz Mueller <lutz@users.sourceforge.net>
 * Copyright 2002 Hubert Figuiere <hfiguiere@teaser.fr>
 * Code largely borrowed to lmini-0.1 by Steve O Connor
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

/*
 * Currently only largan lmini is supported
 */

#define _DEFAULT_SOURCE

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <gphoto2/gphoto2-library.h>
#include <gphoto2/gphoto2-result.h>

#include "lmini.h"


#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (GETTEXT_PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

#define TIMEOUT 1500

/* internal functions prototypes */
static uint8_t convert_name_to_index (const char * name);

int
camera_id (CameraText *id)
{
	strcpy(id->text, "largan-lmini");

	return (GP_OK);
}


static const struct largan_cameras {
	char * name;
	unsigned short idVendor;
	unsigned short idProduct;
	char hasSerial;
} largan_cameras [] = {
	{ "Largan:Lmini", 0, 0, 1 },
/* these are other largan camera. I'm not sure they use the same
 * protocol */
/*	{ "Largan:Chameleon Slim", 0, 0, 0 },
	{ "Largan:Chameleon Mega", 0, 0, 0 },
	{ "Largan:Chameleon", 0, 0, 0 },
	{ "Largan:Easy 800", 0, 0, 1 }, */
	{ NULL, 0, 0, 0 }
};

int
camera_abilities (CameraAbilitiesList *list)
{
	CameraAbilities a;
	int i;

	for (i = 0; largan_cameras[i].name; i++) {
		memset(&a, 0, sizeof(a));
		strcpy(a.model, largan_cameras[i].name);
		a.status = GP_DRIVER_STATUS_EXPERIMENTAL;
		if (largan_cameras[i].hasSerial) {
			a.port |= GP_PORT_SERIAL;
		}
		if (largan_cameras[i].idVendor && largan_cameras[i].idProduct) {
			a.port |= GP_PORT_USB;
		}
		if (largan_cameras[i].hasSerial) {
			a.speed[0] = 4800;
			a.speed[1] = 9600;
			a.speed[2] = 19200;
			a.speed[3] = 38400;
			a.speed[4] = 0;

		}
		a.operations        = 	GP_OPERATION_CAPTURE_IMAGE;
		a.file_operations   = 	GP_FILE_OPERATION_DELETE |
					GP_FILE_OPERATION_PREVIEW;
		a.folder_operations = 	GP_FOLDER_OPERATION_NONE;

		if (a.port) {
			/* only append if port is defined.*/
			gp_abilities_list_append(list, a);
		}
	}
	return (GP_OK);
}

static int
camera_exit (Camera *camera, GPContext *context)
{
	return (GP_OK);
}

static int
get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileType type, CameraFile *file, void *data,
	       GPContext *context)
{
	Camera *camera = data;

	/*
	 * Get the file from the camera. Use gp_file_set_mime_type,
	 * gp_file_set_data_and_size, etc.
	 */
	int ret;
	largan_pict_info *pict;
	uint8_t index;	/* the index of the file to fetch */
	largan_pict_type pict_type;

	index = convert_name_to_index (filename);

	switch (type) {
	case GP_FILE_TYPE_NORMAL:
		pict_type = LARGAN_PICT;
		break;
	case GP_FILE_TYPE_PREVIEW:
		pict_type = LARGAN_THUMBNAIL;
		break;
	default:
		return GP_ERROR_NOT_SUPPORTED;
	}

	pict = largan_pict_new ();
	ret = largan_get_pict (camera, pict_type, (uint8_t)index, pict);
	if (ret == GP_OK) {
		gp_file_append (file, pict->data , pict->data_size);
		if (pict->type == LARGAN_THUMBNAIL) {
			gp_file_set_mime_type (file, GP_MIME_BMP);
		} else {
			gp_file_set_mime_type (file, GP_MIME_JPEG);
		}
	}

	largan_pict_free (pict);

	return ret;
}


static uint8_t
convert_name_to_index (const char * name)
{
	long index;
	/* extract the index from the file name */
	char * myFile = strdup (name);
	*strstr (myFile, ".jpg") = 0; /* strip the extension */
	index = strtol(myFile, (char **)NULL, 10);
	/* TODO check index bounds */
	free (myFile);

	return (uint8_t)index;
}


static int
delete_file_func (CameraFilesystem *fs, const char *folder,
		  const char *filename, void *data, GPContext *context)
{
	Camera *camera = data;
	int index;

	/* Delete one file from the camera.
	(only the last file can be deleted)*/

	index = convert_name_to_index (filename);
	return largan_erase (camera, index);
}


static int
delete_all_func (CameraFilesystem *fs, const char *folder, void *data,
		 GPContext *context)
{
	Camera *camera = data;
	/*
	 * Delete all files in the given folder. If your camera doesn't have
	 * such a functionality, just don't implement this function.
	 */

	return largan_erase (camera, 0xff);
}


static int
camera_capture (Camera *camera, CameraCaptureType type, CameraFilePath *path,
		GPContext *context)
{
	return largan_capture (camera);
}

static int
camera_about (Camera *camera, CameraText *about, GPContext *context)
{
	strcpy (about->text, _("Largan driver\n"
			       "Hubert Figuiere <hfiguiere@teaser.fr>\n"));

	return (GP_OK);
}


static int
file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
		void *data, GPContext *context)
{
	Camera *camera = data;
	int num, i;
	char name[32];

	num = largan_get_num_pict (camera);
	if (num < 0) {
		/* error */
		return num;
	}
	/* List your files here */
	for (i = 1; i <= num; i++) {
		snprintf (name, sizeof(name), "%08d.jpg", i);
		gp_list_append (list, name, NULL);
	}

	return (GP_OK);
}

static CameraFilesystemFuncs fsfuncs = {
	.file_list_func = file_list_func,
	.get_file_func = get_file_func,
	.del_file_func = delete_file_func,
	.delete_all_func = delete_all_func,
};

int
camera_init (Camera *camera, GPContext *context)
{
	int ret;
	GPPortSettings settings;

        /* First, set up all the function pointers */
        camera->functions->exit                 = camera_exit;
        camera->functions->capture              = camera_capture;
        camera->functions->about                = camera_about;

	/* Now, tell the filesystem where to get lists, files and info */
	gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
	/*
	 * The port is already provided with camera->port (and
	 * already open). You just have to use functions like
	 * gp_port_timeout_set, gp_port_settings_get, gp_port_settings_set.
	 */
	ret = gp_port_get_settings (camera->port, &settings);
	if (ret < 0)
		return (ret);
	switch (camera->port->type) {
	case GP_PORT_SERIAL:

		/* Remember the selected speed */

		settings.serial.speed    = 19200;/* initial speed is 19200 */
		settings.serial.bits     = 8;
		settings.serial.parity   = 0;
		settings.serial.stopbits = 1;
		ret = gp_port_set_timeout (camera->port, TIMEOUT);
		if (ret < 0) {
			return (ret);
		}
		break;
	case GP_PORT_USB:
		/* TODO check with a USB camera.... */
		settings.usb.inep       = 0x82;
		settings.usb.outep      = 0x01;
		settings.usb.config     = 1;
		settings.usb.interface  = 0;
		settings.usb.altsetting = 0;
		break;
	default:
		return (GP_ERROR_UNKNOWN_PORT);
	}

	ret = gp_port_set_settings (camera->port, settings);
	if (ret < 0)
		return (ret);

	/*
	 * Once you have configured the port, you should check if a
	 * connection to the camera can be established.
	 */
	return largan_open (camera);
}