summaryrefslogtreecommitdiff
path: root/camlibs/dimera/mesalib.c
blob: 8a39ab3a95ab42869f6457ca434ecf7623528795 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
/*
* Copyright 2000-2001, Brian Beattie <beattie@aracnet.com>, et. al.
*
*	This software was created with the help of proprietary
*      information belonging to StarDot Technologies
*
* Permission to copy and use and modify this file is granted
* provided the above notices are retained, intact.
*
* Revision History:
*	See CVS log for later revisions
*
*	2001/03/10 - Port to gphoto2				  	  Dan
*
*	2000/09/06 - Modified by Gregg Berg <gberg@covconn.net>		  GDB
*		* Rewrite mesa_read_row() to use mesa_image_arg		  GDB
*		* Add checksum to mesa_read_row()			  GDB
*		* Add return with number of bytes to mesa_read_image()	  GDB
*									  GDB
* $Id$
*/
#include "config.h"

#include "mesalib.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>

#include <gphoto2/gphoto2.h>
#include "gphoto2-endian.h"

#define GP_MODULE "dimera"
#define debuglog(e) GP_DEBUG( "%s", (e))

#ifdef CONVERT_PIXELS
static const uint16_t	pixelTable[256] = {
	0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007,
	0x008, 0x009, 0x00A, 0x00B, 0x00C, 0x00D, 0x00E, 0x00F,
	0x010, 0x011, 0x012, 0x013, 0x014, 0x015, 0x016, 0x017,
	0x018, 0x019, 0x01A, 0x01B, 0x01C, 0x01D, 0x01E, 0x01F,
	0x020, 0x021, 0x022, 0x023, 0x024, 0x025, 0x026, 0x027,
	0x028, 0x029, 0x02A, 0x02B, 0x02C, 0x02D, 0x02E, 0x02F,
	0x030, 0x031, 0x032, 0x033, 0x034, 0x035, 0x036, 0x037,
	0x038, 0x039, 0x03A, 0x03B, 0x03C, 0x03D, 0x03E, 0x03F,
	0x040, 0x041, 0x042, 0x043, 0x044, 0x045, 0x046, 0x047,

	0x048, 0x049, 0x04A, 0x04B, 0x04C, 0x04E, 0x04F, 0x050,
	0x051, 0x052, 0x053, 0x054, 0x055, 0x056, 0x057, 0x058,
	0x059, 0x05A, 0x05B, 0x05C, 0x05D, 0x05E, 0x05F, 0x060,
	0x061, 0x062, 0x063, 0x064, 0x066, 0x067, 0x068, 0x069,
	0x06A, 0x06C, 0x06D, 0x06E, 0x06F, 0x070, 0x072, 0x073,
	0x074, 0x075, 0x076, 0x077, 0x078, 0x079, 0x07A, 0x07B,
	0x07C, 0x07E, 0x07F, 0x081, 0x082, 0x084, 0x085, 0x087,

	0x088, 0x08A, 0x08B, 0x08C, 0x08E, 0x08F, 0x090, 0x092,
	0x093, 0x094, 0x096, 0x098, 0x09A, 0x09C, 0x09E, 0x0A0,
	0x0A2, 0x0A3, 0x0A5, 0x0A6, 0x0A8, 0x0A9, 0x0AB, 0x0AC,
	0x0AE, 0x0B0, 0x0B2, 0x0B4, 0x0B6, 0x0B8, 0x0BA, 0x0BC,

	0x0BE, 0x0C0, 0x0C2, 0x0C4, 0x0C6, 0x0C8, 0x0CA, 0x0CD,
	0x0CF, 0x0D2, 0x0D5, 0x0D8, 0x0DB, 0x0DE, 0x0E0, 0x0E2,
	0x0E5, 0x0E7, 0x0EA, 0x0EC, 0x0EE, 0x0F1, 0x0F3, 0x0F6,
	0x0FC, 0x102, 0x106, 0x10A, 0x10E, 0x111, 0x114, 0x117,

	0x11A, 0x11D, 0x120, 0x123, 0x126, 0x12C, 0x132, 0x138,
	0x13E, 0x142, 0x146, 0x14A, 0x150, 0x156, 0x15A, 0x15E,
	0x162, 0x166, 0x16A, 0x16E, 0x17A, 0x180, 0x186, 0x18C,
	0x192, 0x19E, 0x1AA, 0x1B0, 0x1B6, 0x1BA, 0x1BE, 0x1C2,

	0x1C8, 0x1CE, 0x1DA, 0x1E6, 0x1F2, 0x1FE, 0x204, 0x20A,
	0x216, 0x222, 0x22E, 0x23A, 0x240, 0x246, 0x24C, 0x252,

	0x25E, 0x26A, 0x276, 0x282, 0x28E, 0x2A6, 0x2B2, 0x2BE,
	0x2D6, 0x2EE, 0x2FA, 0x312, 0x336, 0x34E, 0x35e, 0x366
};
#endif

/*
 * return the difference in tenths of seconds, between t1 and t2.
 */
static long
timediff( struct timeval *t1, struct timeval *t2 )
{
	long	t;

	t = (t1->tv_sec - t2->tv_sec) * 10;
	t += (t1->tv_usec - t2->tv_usec) / 100000;
	return t;
}

/* flush input.  Wait for timeout deci-seconds to pass without input */

void
mesa_flush( GPPort *port, int timeout )
{
	uint8_t	b[256];
	struct timeval	start, now;

	gettimeofday( &start, NULL );

	/* Clear any pending input bytes */
	gp_port_flush(port, 0);

	/* Wait for silence */
	do {
		if ( gp_port_read( port, (char *)b, sizeof( b ) ) > 0 )
			/* reset timer */
			gettimeofday( &start, NULL );
		gettimeofday( &now, NULL );
	} while ( timeout > timediff( &now, &start ) );
	return;
}

/* Read exactly this number of bytes from the port within the given timeouts */
int
mesa_read( GPPort *port, uint8_t *b, int s, int timeout2, int timeout1 )
{
	int		n = 0;
	int		r, t;
	struct timeval	start, now;

	t = timeout1 ? timeout1 : timeout2;	/* set first byte timeout */
	gettimeofday( &start, NULL );

	do
	{
		/* limit reads to 1k segment */
		r = gp_port_read( port, (char *)&b[n], s>1024?1024:s );
		if ( r > 0 )
		{
			n += r;
			s -= r;
			gettimeofday( &start, NULL );
			t = timeout2;
		}
		gettimeofday( &now, NULL );
	} while ( s > 0 && t > timediff( &now, &start ) );
	return n;
}

/* Send a command to the camera and read the acknowledgement byte */
int
mesa_send_command( GPPort *port, uint8_t *cmd, int n, int ackTimeout )
{
	uint8_t	c;

	CHECK (gp_port_write( port, (char *)cmd, n ));

	if ( mesa_read( port, &c, 1, ackTimeout, 0 ) != 1 )
	{
		debuglog("mesa_send_command: timeout");
		return GP_ERROR_TIMEOUT;
	}
	if ( c != CMD_ACK )
	{
		debuglog("mesa_send_command: error response");
		return GP_ERROR_CORRUPTED_DATA;
	}
	return GP_OK;
}

/* Open the serial port and configure it */
int
mesa_port_open( GPPort *port )
{
	GPPortSettings settings;

	debuglog("mesa_port_open()");
	gp_port_set_timeout(port, 5000);

	gp_port_get_settings(port, &settings);

	settings.serial.speed   = 115200;
	settings.serial.bits    = 8;
	settings.serial.parity  = 0;
	settings.serial.stopbits= 1;

	return gp_port_set_settings(port, settings);
}

/* Close the serial port (now done by gphoto2 itself) */
int
mesa_port_close( GPPort *port )
{
	return GP_OK;
}

/* reset camera */
int
mesa_reset( GPPort *port )
{
	return gp_port_send_break( port, 1 );
}

/* set camera serial port speed, then our local port speed */
int
mesa_set_speed( GPPort *port, int speed )
{
	uint8_t	b[2];
	gp_port_settings settings;

	if (speed == 0)
		speed = 115200;		/* use default speed */

	GP_DEBUG( 
	 "mesa_set_speed: speed %d", speed);

	b[0] = SET_SPEED;

	switch( speed )
	{
	case 9600:
		b[1] = 1;
		break;
	case 14400:
		b[1] = 2;
		break;
	case 19200:
		b[1] = 3;
		break;
	case 38400:
		b[1] = 4;
		break;
	case 57600:
		b[1] = 5;
		break;
	case 76800:
		b[1] = 6;
		break;
	case 115200:
		b[1] = 7;
		break;
	case 230400:
		b[1] = 8;
		break;
	case 460800:
		b[1] = 9;
		break;
	default:
		return GP_ERROR_BAD_PARAMETERS;
	}
	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	gp_port_get_settings(port, &settings);
	settings.serial.speed = speed;
	return gp_port_set_settings(port, settings);
}

/* get camera version number */
int
mesa_version( GPPort *port, char *version_string)
{
	uint8_t		b;
	uint8_t		r[3];
	int		i;
	char		*v = version_string;

	b = MESA_VERSION;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	if ( ( i = mesa_read( port, r, sizeof( r ), 10, 0 ) ) != sizeof( r ) )
	{
		return GP_ERROR_TIMEOUT;
	}

	GP_DEBUG( 
	 "mesa_version: %02x:%02x:%02x\n", r[0], r[1], r[2] );
	sprintf(v, "%2x.%02x%c", r[1], r[0], r[2]);
	/* highest byte must be < MESA_VERSION_SZ */
	return GP_OK;
}

/* test camera serial port transmit */
int
mesa_transmit_test( GPPort *port )
{
	uint8_t		b;
	uint8_t		r[256];
	unsigned int	i;

	b = XMIT_TEST;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	if ( mesa_read( port, r, sizeof( r ), 10, 0 ) != sizeof( r ) )
	{
		return GP_ERROR_TIMEOUT;
	}

	for ( i = 0; i < 256; i++ )
	{
		if ( r[i] != i )
			return GP_ERROR_CORRUPTED_DATA;
	}
	return GP_OK;
}

/* test camera ram */
int
mesa_ram_test( GPPort *port )
{
	uint8_t	b;
	uint8_t	r;

	b = RAM_TEST;

	CHECK (mesa_send_command( port, &b, 1, 100 ));

	if ( mesa_read( port, &r, 1, 10, 0 ) != 1 )
	{
		return GP_ERROR_TIMEOUT;
	}

	return (int)r;
}

/*
 * Read image row from camera.
 * ********************************************************************   GDB
 * *   The following contradicted by traces from my Win98 twain driver*	  GDB
 * ********************************************************************   GDB
 * The image is stored in the camera as 504 row (0 - 503), the first 8 and the
 * last 2 are blank. 
 * The rows are 18 dummy pixels, 2 blanks, 646 pixels, 13 bytes garbage,
 * 2 bytes blanks
 *
 * start  - offset into row;
 * send   - bytes to send;
 * skip   - bytes to skip;
 * repeat - # repeats of send skip
 * 640 bytes start = 28, send = 2, skip = 0, repeat = 320
 * 320 bytes start = 28, send = 2, skip = 2, repeat = 160
 *
 * return number of bytes read, or < 0 if an error was detected.
 *									  GDB
 * ********************************************************************   GDB
 *									  GDB
 * 640 bytes start = 30, send = 4, skip = 0, repeat = 160		  GDB
 * 320 bytes start = 30, send = 4, skip = 0, repeat = 80		  GDB
 *									  GDB
 * return value	>0 number of bytes read					  GDB
 *		 GP_ERROR_TIMEOUT read time-out
 *		 GP_ERROR_BAD_PARAMETERS bad values
 *		 GP_ERROR command failure
 *		 GP_ERROR_CORRUPTED_DATA checksum failure
 */

int									/*GDB*/
mesa_read_row( GPPort *port, uint8_t *r, struct mesa_image_arg *s )	/*GDB*/
{
	uint8_t		b[9];
	unsigned int	bytes, i;					/*GDB*/
	uint8_t		checksum = 0;					/*GDB*/

	if ( (bytes = s->send * s->repeat) > 680 )			/*GDB*/
	{
		return GP_ERROR_BAD_PARAMETERS;
	}

	b[0] = SEND_RAM;
	htole16a(&b[1], s->row);
	htole16a(&b[3], s->start);
	b[5] = s->send;
	b[6] = s->skip;
	htole16a(&b[7], s->repeat);

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

/*	return ( mesa_read( port, r, n, 10, 0 ));			  GDB*/
	if ( mesa_read( port, r, bytes, 10, 0 ) != bytes )		/*GDB*/
	{								/*GDB*/
		return GP_ERROR_TIMEOUT;
	}								/*GDB*/
									/*GDB*/
	if ( mesa_read( port, b, 1, 10, 0 ) != 1) 			/*GDB*/
	{								/*GDB*/
		return GP_ERROR_TIMEOUT;
	}								/*GDB*/
									/*GDB*/
	for (i = 0; i < bytes; i++ )					/*GDB*/
	{								/*GDB*/
		checksum += r[i];					/*GDB*/
	}								/*GDB*/
									/*GDB*/
	if ( checksum != b[0] )						/*GDB*/
	{								/*GDB*/
		return GP_ERROR_CORRUPTED_DATA;
	}								/*GDB*/
									/*GDB*/
	return bytes;							/*GDB*/
}

/*
 * Snaps a full resolution image, with no flash, or shutter click
 *
 * exposure is in units of 1/50000 seconds
 */
int
mesa_snap_image( GPPort *port, uint16_t exposure )
{
	uint8_t		b[3];
	int		timeout;

	if ( exposure )
		timeout = (exposure/50000) + 10;
	else
		timeout = 10;

	b[0] = SNAP_IMAGE;
	htole16a(&b[1], exposure);
	return mesa_send_command( port, b, sizeof( b ), timeout );
}

/* return black levels, overwrites image memory */
int
mesa_black_levels( GPPort *port, uint8_t r[2] )
{
	uint8_t	b;

	b = SND_BLACK;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	return mesa_read( port, r, 2, 10, 0 );
}

/*
 * snap a view finder image, optionally return image in buffer.
 *
 * if hi_res is true, image is 128x96 packed 4 bits per pixel (6144 bytes).
 * else               image is 64x48 packed 4 bits per pixel (1536 bytes).
 *
 * the image can be full scale, 0, zoom 2x 1, zoom 4x, 2, and for
 * low res images, 8x, 3.
 *
 * row and column may be offset 0-197 and 0-223 for zoom. 
 *
 * exposure is * 1/50000 seconds.
 *
 * download specifies how, of if thge image is downloaded.
 * 0-47		returns one 32 byte (64 pixel) row.
 * 128-223	returns on 64 byte row.
 * 249		returns all odd rows of a hi-res image 1536 bytes.
 * 250		returns all even rows of a hi-res image 1536 bytes.
 * 251		returns complete hi res image 6144 bytes.
 * 252		returns nothing.
 * 253		returns all odd rows of low res image 768 bytes.
 * 254		returns all even rows of low res image 768 bytes.
 * 255		returns entire image of 1536 bytes.
 *
 */
int
mesa_snap_view( GPPort *port, uint8_t *r, unsigned int hi_res,
	unsigned int zoom, unsigned int row, unsigned int col, uint16_t exposure,
	uint8_t download )
{
	unsigned int	timeout, i;
	unsigned int	bytes = 0;
	uint8_t		b[7], cksum;

	if ( download <= 47 )
		bytes = 32;
	else if ( download >= 48 && download <= 127 )
		return GP_ERROR_BAD_PARAMETERS;
	else if ( download >= 128 && download <= 223 )
		bytes = 64;
	else if ( download >= 224 && download <= 248 )
		return GP_ERROR_BAD_PARAMETERS;
	else if ( download == 249 || download == 250 )
		bytes = 1536;
	else if ( download == 251 ) 
		bytes = 6144;
	else if ( download == 252 )
		bytes = 0;
	else if ( download == 253 || download == 254 )
		bytes = 768;
	else /* if ( download == 255 ) */
		bytes = 1536;

	if ( bytes != 0 && r == NULL )
	{
		return GP_ERROR_BAD_PARAMETERS;
	}

	if ( exposure )
		timeout = (exposure/50000) + 10;
	else
		timeout = 10;

	b[0] = SNAP_VIEW;
	b[1] = (zoom&3)+(hi_res?0x80:0);
	b[2] = row;
	b[3] = col;
	htole16a(&b[4], exposure);
	b[6] = download;

	CHECK (mesa_send_command( port, b, sizeof( b ), timeout ));

	if ( bytes != 0 )
	{
		if ( mesa_read( port, r, bytes, 10, 0 ) != bytes )
		{
			return GP_ERROR_TIMEOUT;
		}

		if ( mesa_read( port, b, 1, 10, 0 ) != 1 )
		{
			return GP_ERROR_TIMEOUT;
		}

		for ( i = 0, cksum = 0; i < bytes; i++ )
		{
			cksum += r[i];
		}
		if ( cksum != b[0] )
		{
			return GP_ERROR_CORRUPTED_DATA;
		}
	}
	
	return bytes;
}

/*
 * tell camera to insert extra stop bits between bytes.
 * slows down transmission without using a slower speed.
 */
int
mesa_set_stopbits( GPPort *port, unsigned int bits )
{
	uint8_t	b[2];

	b[0] = XTRA_STP_BIT;
	b[1] = bits;

	return mesa_send_command( port, b, sizeof( b ), 10 );
}

/* download viewfinder image ( see mesa_snap_view ) */
int
mesa_download_view( GPPort *port, uint8_t *r, uint8_t download )
{
	unsigned int	bytes, i;
	uint8_t		b[2], cksum;

	if ( download <= 47 )
		bytes = 32;
	else if ( download >= 48 && download <= 127 )
		return GP_ERROR_BAD_PARAMETERS;
	else if ( download >= 128 && download <= 223 )
		bytes = 64;
	else if ( download >= 224 && download <= 248 )
		return GP_ERROR_BAD_PARAMETERS;
	else if ( download == 249 )
		bytes = 1536;
	else if ( download == 250 || download == 251 ) 
		bytes = 768;
	else if ( download == 252 )
		bytes = 0;
	else if ( download == 253 )
		bytes = 6144;
	else /* if ( download == 254 || download == 255 ) */
		bytes = 1536;

	if ( bytes != 0 && r == NULL )
	{
		return GP_ERROR_BAD_PARAMETERS;
	}

	b[0] = SND_VIEW;
	b[1] = download;

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	if ( bytes != 0 )
	{
		if ( mesa_read( port, r, bytes, 10, 0 ) != bytes )
		{
			return GP_ERROR_TIMEOUT;
		}

		if ( mesa_read( port, b, 1, 10, 0 ) != 1 )
		{
			return GP_ERROR_TIMEOUT;
		}

		for ( i = 0, cksum = 0; i < bytes; i++ )
		{
			cksum += r[i];
		}
		if ( cksum != b[0] )
			return GP_ERROR_CORRUPTED_DATA;
	}
	
	return bytes;
}

/*
 * take a picture, with flash and shutter click
 */
int
mesa_snap_picture( GPPort *port, uint16_t exposure )
{
	unsigned int	timeout;
	uint8_t		b[3];

	if ( exposure )
		timeout = (exposure/50000) + 10;
	else
		timeout = 10;

	b[0] = SNAP_PICTURE;
	htole16a(&b[1], exposure);

	return mesa_send_command( port, b, sizeof( b ), timeout );
}

/* Get the camera chipset identification */
int
mesa_send_id( GPPort *port, struct mesa_id *id )
{
	uint8_t		b, r[4];

	b = SND_ID;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	if ( mesa_read( port, r, sizeof( r ), 10, 0 ) != sizeof( r ) )
	{
		return GP_ERROR_TIMEOUT;
	}

	id->man = r[0] + ((r[1]&15)<<8);
	id->ver = r[1]>>4;
	id->year = 1996 + r[2];
	id->week = r[3];

	return GP_OK;
}

/*
 * check for a camera, or a modem, send AT, if the response is 0x21,
 * it is a camera, if it is "AT" it's a modem.
 *
 * returns	 GP_OK = camera
 *		 GP_ERROR_MODEL_NOT_FOUND = modem
 *		 other = error
 */
int
mesa_modem_check( GPPort *port )
{
	uint8_t 	b[3];

	b[0] = 'A';
	b[1] = 'T';
	b[2] = '\r';

	CHECK (gp_port_write( port, (char *)b, sizeof( b ) ));

	/* Expect at least one byte */
	if ( mesa_read( port, b, 1, 5, 0 ) < 1 )
	{
		return GP_ERROR_TIMEOUT;
	}

	if ( b[0] == CMD_ACK )
		return GP_OK;

	/* Anything past this point results in an error */
	if ( mesa_read( port, b+1, sizeof(b) - 1, 2, 2 ) == sizeof(b) - 1 )
	{
		if ( b[0] == 'A' && b[1] == 'T' )
		{
			mesa_flush( port, 10 );
			return GP_ERROR_MODEL_NOT_FOUND;
		}
	}

	mesa_flush( port, 10 );
	return GP_ERROR;
}

/*
 * mesa_read_image	- returns all or part of an image in the supplied buffer
 * 	uint8_t		*r, buffer to return image in.
 *	struct mesa_image_arg	*i, download specifier.
 *
 *	return value	>0 number of bytes read
 *			GP_ERROR_TIMEOUT read time-out
 *			GP_ERROR command failure
 *			GP_ERROR_CORRUPTED_DATA checksum failure
 */

int
mesa_read_image( GPPort *port, uint8_t *r, struct mesa_image_arg *s )
{
	uint8_t		b[14];
	unsigned long	bytes, i;
	uint8_t		checksum = 0;

	bytes = s->row_cnt * (s->repeat * s->send);

	b[0]  = SND_IMAGE;
	htole16a(&b[1], s->row);
	htole16a(&b[3], s->start);
	b[5]  = s->send;
	b[6]  = s->skip;
	htole16a(&b[7], s->repeat);
	b[9]  = s->row_cnt;
	b[10] = s->inc1;
	b[11] = s->inc2;
	b[12] = s->inc3;
	b[13] = s->inc4;

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	if ( mesa_read( port, r, bytes, 10, 0 ) != bytes )
	{
		return GP_ERROR_TIMEOUT;
	}

	if ( mesa_read( port, b, 1, 10, 0 ) != 1 )
	{
		return GP_ERROR_TIMEOUT;
	}

	for ( i = 0; i < bytes; i++ )
	{
		checksum += r[ i ];
	}

	if ( checksum != b[0] )
	{
		return GP_ERROR_CORRUPTED_DATA;
	}

	return bytes;
}

/*
 * test serial link, send six bytes
 *
 * returns	GP_OK - success
 *		GP_ERROR_TIMEOUT - no data received
 *		GP_ERROR - command failed (no or bad ack)
 *		GP_ERROR_CORRUPTED_DATA - returned bytes do not match
 */
int
mesa_recv_test( GPPort *port, uint8_t r[6] )
{
	uint8_t		b[7];
	int		i;
	
	b[0] = RCV_TEST;
	memcpy( &b[1], r, 6 );

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	if ( mesa_read( port, r, 6, 10, 0 ) != 6 )
	{
		return GP_ERROR_TIMEOUT;
	}

	for ( i = 0; i < 6; i++ )
		if ( r[i] != b[i+1] )
			return GP_ERROR_CORRUPTED_DATA;
	return GP_OK;
}

/* Return the number of images in the EEPROM */
int
mesa_get_image_count( GPPort *port )
{
	uint8_t		b;
	uint8_t		r[2];

	b = IMAGE_CNT;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	if ( mesa_read( port, r, sizeof( r ), 10, 0 ) != sizeof( r ) )
	{
		return GP_ERROR_TIMEOUT;
	}

	return le16atoh(r);
}

/* load image from eeprom, into ram */
int
mesa_load_image( GPPort *port, int image )
{
	uint8_t		b[3];

	b[0] = LD_IMAGE;
	htole16a(&b[1], image);

	return mesa_send_command( port, b, sizeof( b ), 1000 );
}

/*
 * Read EEPROM information
 * Bytes 10..16 of info correspond to the Device ID field, bytes 45..51
 */
int
mesa_eeprom_info( GPPort *port, int long_read, uint8_t info[MESA_EEPROM_SZ] )
{
	uint8_t		b;

	b = EEPROM_INFO;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	return mesa_read( port, info, long_read ? 49 : 33, 10, 0 );
}

/*
 *
 */

int32_t
mesa_read_thumbnail( GPPort *port, int picture, uint8_t *image )
{
	uint8_t		b[3], checksum = 0;
	uint32_t	bytes;
	unsigned int	standard_res, i;

	b[0] = SND_THUMB;
	htole16a(&b[1], picture);

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	if ( mesa_read( port, b, 3, 10, 0 ) != 3 )
	{
		return GP_ERROR_TIMEOUT;
	}

	checksum = b[0] + b[1] + b[2];

	standard_res = ((b[2] & 128) != 0);
	bytes = b[0] + (b[1]<<8) + ((b[2] & 127)<<16);

	if ( mesa_read( port, image, MESA_THUMB_SZ, 10, 0 ) != MESA_THUMB_SZ )
	{
		return GP_ERROR_TIMEOUT;
	}

	if ( mesa_read( port, b, 1, 10, 0 ) != 1 )
	{
		return GP_ERROR_TIMEOUT;
	}

	for ( i = 0; i < MESA_THUMB_SZ; i++ )
	{
		checksum += image[i];
	}

	if ( checksum != b[0] )
		return GP_ERROR_CORRUPTED_DATA;

	return (bytes + standard_res ? 0x1000000 : 0);
}

/* Return the camera's feature bits */
int
mesa_read_features( GPPort *port, struct mesa_feature *f )
{
	uint8_t		b;

	b = FEATURES;

	CHECK (mesa_send_command( port, &b, 1, 10 ));

	return ( mesa_read( port, (uint8_t *)f, sizeof( *f ), 10, 0 ) );
}

/* return percentage battery level */
int
mesa_battery_check( GPPort *port )
{
	struct mesa_feature	f;
	int			l, r, rc;

	if ( (rc = mesa_read_features( port, &f )) != sizeof( f ))
	{
		return rc;
	}

	if ( (f.feature_bits_hi & BAT_VALID) == 0 )
		return GP_ERROR_MODEL_NOT_FOUND;

	/* (df) Does BAT_DIGITAL need to be checked here? */

	if ( ( l =  f.battery_level - f.battery_zero ) < 0 )
		l = 0;

	r = f.battery_full - f.battery_zero;

	return ( (l*100)/r );
}

int32_t
mesa_read_image_info( GPPort *port, int i, struct mesa_image_info *info )
{
	uint8_t		b[3], r[3];
	int32_t		bytes;
	int32_t		standard_res;

	b[0] = SND_IMG_INFO;
	htole16a(&b[1], i);

	CHECK (mesa_send_command( port, b, sizeof( b ), 10 ));

	if ( mesa_read( port, r, sizeof( r ), 10, 0 ) != sizeof( r ) )
	{
		return GP_ERROR_TIMEOUT;
	}

	standard_res = ((r[2] & 128) != 0);
	bytes = r[0] + (r[1]<<8) + ((r[2] & 127)<<16);

	if ( info != NULL )
	{
		info->standard_res = standard_res;
		info->num_bytes = bytes;
	}

	return standard_res;
}

#ifdef CONVERT_PIXELS
/*
 * return and image with raw pixels expanded.
 */
uint16_t *
mesa_get_image( GPPort *port, int image )
{
	static uint8_t			buffer[640*480L];
	static struct mesa_image_info	info;
	static struct mesa_image_arg	ia;
	uint8_t				*b = buffer;
	uint16_t			*rbuffer;
	int				r, res;
	unsigned long			size, i;

	if ( image != RAM_IMAGE_NUM )
	{
		if ( mesa_load_image( port, image ) != GP_OK )
		{
			mesa_flush( port, 100 );
			return NULL;
		}

		if ( mesa_read_image_info( port, image, &info ) < 0 )
		{
			mesa_flush( port, 100 );
			return NULL;
		}
		if ( info.standard_res )
		{
			res = 1;
			size = 320*240;
		} else {
			res = 0;
			size = 640*480;
		}
	} else {
		res = 0;
		size = 640*480;
	}
	rbuffer = (uint16_t *)malloc( size*(sizeof (uint16_t)) );
	if ( rbuffer == NULL )
		return NULL;

	ia.start = 28;
	ia.send = 4;
	ia.skip = 0;
	ia.repeat = (res ? 80 : 160);
	ia.row_cnt = 40;
	ia.inc1 = 1;
	ia.inc2 = 128;
	ia.inc3 = ia.inc4 = 0;

	for ( ia.row = 4; ia.row < (res ? 244 : 484) ; ia.row += ia.row_cnt )
	{

		if ( (r = mesa_read_image( port, b, &ia )) < 0 )
		{
			free( rbuffer );
			return NULL;
		}
		b += r;
	}

	for ( i = 0; i < size; i++ )
	{
		*rbuffer++ = pixelTable[*b++];
	}

	return rbuffer;
}
#else
/*
 * return and raw image retransmit on errors
 */
uint8_t *
mesa_get_image( GPPort *port, int image )
{
	static struct mesa_image_info	info;
	static struct mesa_image_arg	ia;
	uint8_t				*rbuffer, *b;
	int				r, res, retry;
	unsigned long			size;

	if ( image != RAM_IMAGE_NUM )
	{
		if ( mesa_load_image( port, image ) < 0 )
		{
			mesa_flush( port, 100 );
			return NULL;
		}

		if ( mesa_read_image_info( port, image, &info ) < 0 )
		{
			mesa_flush( port, 100 );
			return NULL;
		}
		if ( info.standard_res )
		{
			res = 1;
			size = 320*240;
		} else {
			res = 0;
			size = 640*480;
		}
	} else {
		res = 0;
		size = 640*480;
	}
	rbuffer = (uint8_t *)malloc( size );
	if ( rbuffer == NULL )
		return NULL;
	b = rbuffer;

	ia.start = 28;
	ia.send = 4;
	ia.skip = 0;
	ia.repeat = (res ? 80 : 160);
	ia.row_cnt = 40;
	ia.inc1 = 1;
	ia.inc2 = 128;
	ia.inc3 = ia.inc4 = 0;

	for ( ia.row = 4; ia.row < (res ? 244 : 484) ; ia.row += ia.row_cnt )
	{

		for ( retry = 10;; )
		{
			if ( (r = mesa_read_image( port, b, &ia )) > 0)
				break;

				/* if checksum error count retries */
			if ( r == -2  && --retry > 0)
				continue;	/* not exceeded, try again */

				/* error, exit */
			free( rbuffer );
			return NULL;

		}
		b += r;
	}

	return rbuffer;
}
#endif