forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstvdriver.c
1731 lines (1507 loc) · 47.1 KB
/
stvdriver.c
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
#if 0
STV Low Level Driver
Copyright (C) 2006 Markus Wildi, [email protected]
The initial work is based on the program STVremote by Shashikiran Ganesh.
email: gshashikiran_AT_linuxmail_dot_org
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.1 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
#endif
#define _GNU_SOURCE 1
#include <unistd.h>
#include <cstring>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <stdio.h>
#include <ctime>
#include <stdlib.h>
#include "config.h"
#include "stvdriver.h"
#include <libnova/libnova.h>
#ifndef _WIN32
#include <termios.h>
#endif
/* INDI Common Routines/RS232 */
#include "indicom.h"
extern int fd;
extern char tracking_buf[];
struct termios orig_tty_setting; /* old serial port setting to restore on close */
struct termios tty_setting; /* new serial port setting */
#define FALSE 0
#define TRUE 1
/*int tty_read( int fd, char *buf, int nbytes, int timeout, int *nbytes_read) ; */
/*int tty_write( int fd, const char *buffer, int *nbytes_written) ; */
void ISUpdateDisplay(int buffer, int line);
int STV_portWrite(char *buf, int nbytes);
int STV_TXDisplay(void);
int STV_TerminateTXDisplay(void);
int STV_FileStatus(int);
int STV_DownloadComplete(void);
int STV_RequestImage(int compression, int buffer, int x_offset, int y_offset, int *length, int *lines, int image[][320],
IMAGE_INFO *image_info);
int STV_RequestImageData(int compression, int *data, int j, int length, int *values);
int STV_Download(void);
int STV_DownloadAll(void);
int STV_RequestAck(void);
int STV_CheckHeaderSum(unsigned char *buf);
int STV_CheckDataSum(unsigned char *data);
int STV_PrintBuffer(unsigned char *buf, int n);
int STV_PrintBufferAsText(unsigned char *buf, int n);
int STV_CheckAck(unsigned char *buf);
int STV_SendPacket(int cmd, int *data, int n);
int STV_ReceivePacket(unsigned char *buf, int mode);
int STV_DecompressData(unsigned char *data, int *values, int length, int expected_n_values);
int STV_BufferStatus(int buffer);
void STV_PrintBits(unsigned int x, int n);
unsigned int STV_RecombineInt(unsigned char low_byte, unsigned char high_byte);
unsigned int STV_GetBits(unsigned int x, int p, int n);
int STV_MenueSetup(int delay);
int STV_MenueDateTime(int delay);
int STV_MenueCCDTemperature(int delay);
typedef struct
{
double ccd_temperature;
} DISPLAY_INFO;
DISPLAY_INFO di;
/* STV Buttons */
int STV_LRRotaryDecrease(void)
{
int data[] = { LR_ROTARY_DECREASE_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_LRRotaryIncrease(void)
{
int data[] = { LR_ROTARY_INCREASE_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_UDRotaryDecrease(void)
{
int data[] = { UD_ROTARY_DECREASE_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_UDRotaryIncrease(void)
{
int data[] = { UD_ROTARY_INCREASE_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_AKey(void)
{ /* Parameter button */
int data[] = { A_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_BKey(void)
{ /* Value Button */
int data[] = { B_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Setup(void)
{
int data[] = { SETUP_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Interrupt(void)
{
int data[] = { INT_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Focus(void)
{
int data[] = { FOCUS_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Image(void)
{
int data[] = { IMAGE_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Monitor(void)
{
int data[] = { MONITOR_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Calibrate(void)
{
int data[] = { CAL_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Track(void)
{
int data[] = { TRACK_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_Display(void)
{
int data[] = { DISPLAY_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_FileOps(void)
{
int data[] = { FILEOPS_KEY_PATTERN };
return STV_SendPacket(SEND_KEY_PATTERN, data, 1);
}
int STV_RequestImageInfo(int buffer, IMAGE_INFO *image_info)
{
int i;
int length;
int res;
int data[] = { buffer };
unsigned char buf[1024];
unsigned int value[21];
if ((res = STV_BufferStatus(buffer)) != 0)
{
if (res == 1)
{
/*fprintf( stderr," STV_RequestImageInfo buffer empty %d\n", buffer) ; */
return res; /* Buffer is empty */
}
else
{
fprintf(stderr, " STV_RequestImageInfo error %d\n", res);
return res;
}
}
res = STV_SendPacket(REQUEST_IMAGE_INFO, data, 1);
usleep(50000);
res = STV_ReceivePacket(buf, 0);
if (buf[1] != REQUEST_IMAGE_INFO)
{
/*length= buf[3] * 0x100 + buf[2] ; */
/*res= STV_PrintBuffer(buf, 6+ length+ 2) ; */
AGAINI:
if ((res = STV_ReceivePacket(buf, 0)) < 0)
{ /* STV answers with a packet 0x03 sometimes, should be 0x04 */
return -1;
;
}
if (buf[1] != REQUEST_IMAGE_INFO)
{ /* Second try */
fprintf(stderr, "STV_RequestImageInfo: expected REQUEST_IMAGE_INFO, received %d, try again\n", buf[1]);
goto AGAINI;
}
}
length = buf[3] * 0x100 + buf[2];
/* DECODE it */
for (i = 6; i < length; i += 2)
{
value[(i - 6) / 2] = STV_RecombineInt(buf[i], buf[i + 1]);
}
image_info->descriptor = value[0];
/* Decode the descriptor */
/*STV_PrintBits(( unsigned int)value[0], 16) ; */
if ((image_info->descriptor & ID_BITS_MASK) == ID_BITS_10)
{
/* fprintf( stderr, "ADC Resolution: 10 bits\n") ; */
}
else if ((image_info->descriptor & ID_BITS_MASK) == ID_BITS_8)
{
/*fprintf( stderr, "Resolution: 8 bits (focus mode only)\n") ; */
}
if ((image_info->descriptor & ID_UNITS_MASK) == ID_UNITS_INCHES)
{
/*fprintf( stderr, "Units: inch\n") ; */
}
else if ((image_info->descriptor & ID_UNITS_MASK) == ID_UNITS_CM)
{
/*fprintf( stderr, "Unis: cm\n") ; */
}
if ((image_info->descriptor & ID_SCOPE_MASK) == ID_SCOPE_REFRACTOR)
{
/*fprintf( stderr, "Refractor\n") ; */
}
else if ((image_info->descriptor & ID_SCOPE_MASK) == ID_SCOPE_REFLECTOR)
{
/*fprintf( stderr, "Reflector\n") ; */
}
if ((image_info->descriptor & ID_DATETIME_MASK) == ID_DATETIME_VALID)
{
/*fprintf( stderr, "Date and time valid\n") ; */
}
else if ((image_info->descriptor & ID_DATETIME_MASK) == ID_DATETIME_INVALID)
{
/*fprintf( stderr, "Date and time invalid\n") ; */
}
if ((image_info->descriptor & ID_BIN_MASK) == ID_BIN_1X1)
{
image_info->binning = 1;
}
else if ((image_info->descriptor & ID_BIN_MASK) == ID_BIN_2X2)
{
image_info->binning = 2;
}
else if ((image_info->descriptor & ID_BIN_MASK) == ID_BIN_3X3)
{
image_info->binning = 3;
}
if ((image_info->descriptor & ID_PM_MASK) == ID_PM_PM)
{
/*fprintf( stderr, "Time: PM\n") ; */
}
else if ((image_info->descriptor & ID_PM_MASK) == ID_PM_AM)
{
/*fprintf( stderr, "Time: AM\n") ; */
}
/* ToDo: Include that to the fits header */
if ((image_info->descriptor & ID_FILTER_MASK) == ID_FILTER_LUNAR)
{
/*fprintf( stderr, "Filter: Lunar\n") ; */
}
else if ((image_info->descriptor & ID_FILTER_MASK) == ID_FILTER_NP)
{
/*fprintf( stderr, "Filter: clear\n") ; */
}
/* ToDo: Include that to the fits header */
if ((image_info->descriptor & ID_DARKSUB_MASK) == ID_DARKSUB_YES)
{
/*fprintf( stderr, "Drak sub yes\n") ; */
}
else if ((image_info->descriptor & ID_DARKSUB_MASK) == ID_DARKSUB_NO)
{
/*fprintf( stderr, "Dark sub no\n") ; */
}
if ((image_info->descriptor & ID_MOSAIC_MASK) == ID_MOSAIC_NONE)
{
/*fprintf( stderr, "Is NOT a mosaic\n") ; */
}
else if ((image_info->descriptor & ID_MOSAIC_MASK) == ID_MOSAIC_SMALL)
{
/*fprintf( stderr, "Is a small mosaic\n") ; */
}
else if ((image_info->descriptor & ID_MOSAIC_MASK) == ID_MOSAIC_LARGE)
{
/*fprintf( stderr, "Is a large mosaic\n") ; */
}
image_info->height = value[1];
image_info->width = value[2];
image_info->top = value[3];
image_info->left = value[4];
/* Exposure time */
if ((value[5] >= 100) && (value[5] <= 60000))
{
image_info->exposure = ((float)value[5]) * 0.01;
}
else if ((value[5] >= 60001) && (value[5] <= 60999))
{
image_info->exposure = ((float)value[5] - 60000.) * .001;
}
else
{
fprintf(stderr, "Error Exposure time %d\n", value[5]);
image_info->exposure = -1.;
}
image_info->noExposure = value[6];
image_info->analogGain = value[7];
image_info->digitalGain = value[8];
image_info->focalLength = value[9];
image_info->aperture = value[10];
image_info->packedDate = value[11];
image_info->year = STV_GetBits(value[11], 6, 7) + 1999;
image_info->day = STV_GetBits(value[11], 11, 5);
image_info->month = STV_GetBits(value[11], 15, 4);
image_info->packedTime = value[12];
image_info->seconds = STV_GetBits(value[12], 5, 6);
image_info->minutes = STV_GetBits(value[12], 11, 6);
image_info->hours = STV_GetBits(value[12], 15, 4);
if ((image_info->descriptor & ID_PM_PM) > 0)
{
image_info->hours += 12;
}
if ((value[13] & 0x8000) == 0x8000)
{
image_info->ccdTemp = (float)(0xffff - value[13]) / 100.;
}
else
{
image_info->ccdTemp = (float)value[13] / 100.;
}
image_info->siteID = value[14];
image_info->eGain = value[15];
image_info->background = value[16];
image_info->range = value[17];
image_info->pedestal = value[18];
image_info->ccdTop = value[19];
image_info->ccdLeft = value[20];
/* fprintf( stderr, "descriptor:%d 0x%2x 0x%2x\n", image_info->descriptor, buf[6], buf[6+1]) ; */
/* fprintf( stderr, "height:%d\n", image_info->height) ; */
/* fprintf( stderr, "width:%d\n", image_info->width) ; */
/* fprintf( stderr, "top:%d\n", image_info->top) ; */
/* fprintf( stderr, "left:%d\n", image_info->left) ; */
/* fprintf( stderr, "exposure:%f\n", image_info->exposure) ; */
/* fprintf( stderr, "noExposure:%d\n", image_info->noExposure) ; */
/* fprintf( stderr, "analogGain:%d\n", image_info->analogGain) ; */
/* fprintf( stderr, "digitalGain:%d\n", image_info->digitalGain) ; */
/* fprintf( stderr, "focalLength:%d\n", image_info->focalLength) ; */
/* fprintf( stderr, "aperture:%d\n", image_info->aperture) ; */
/* fprintf( stderr, "packedDate:%d\n", image_info->packedDate) ; */
/* fprintf( stderr, "Year:%d\n", image_info->year) ; */
/* fprintf( stderr, "Day:%d\n", image_info->day) ; */
/* fprintf( stderr, "Month:%d\n", image_info->month) ; */
/* fprintf( stderr, "packedTime:%d\n", image_info->packedTime) ; */
/* fprintf( stderr, "Seconds:%d\n", image_info->seconds) ; */
/* fprintf( stderr, "minutes:%d\n", image_info->minutes) ; */
/* fprintf( stderr, "hours:%d\n", image_info->hours) ; */
/* fprintf( stderr, "ccdTemp:%f\n", image_info->ccdTemp) ; */
/* fprintf( stderr, "siteID:%d\n", image_info->siteID) ; */
/* fprintf( stderr, "eGain:%d\n", image_info->eGain) ; */
/* fprintf( stderr, "background:%d\n", image_info->background) ; */
/* fprintf( stderr, "range :%d\n", image_info->range ) ; */
/* fprintf( stderr, "pedestal:%d\n", image_info->pedestal) ; */
/* fprintf( stderr, "ccdTop :%d\n", image_info->ccdTop) ; */
/* fprintf( stderr, "ccdLeft:%d\n", image_info->ccdLeft) ; */
return 0;
}
int STV_RequestImage(int compression, int buffer, int x_offset, int y_offset, int *length, int *lines, int image[][320],
IMAGE_INFO *image_info)
{
int i, j;
int res;
int n_values;
unsigned char buf[0xffff];
int values[1024];
int data[] = { 0, y_offset, *length, buffer }; /* Offset row, Offset line, length, buffer number */
int XOFF;
/* fprintf(stderr, "STV_RequestImage: --------------------------------buffer= %d, %d\n", buffer, lines) ; */
if ((res = STV_RequestImageInfo(buffer, image_info)) != 0)
{ /* Trigger for download */
/* buffer empty */
return res;
}
res = STV_BKey(); /* "press" the STV Value button */
usleep(50000);
res = STV_ReceivePacket(buf, 0);
/* Check the boundaries obtained from the image data versus windowing */
/* Take the smaller boundaries in each direction */
if (x_offset > image_info->height)
{
x_offset = image_info->height;
}
XOFF = image_info->top + x_offset;
if (y_offset > image_info->width)
{
y_offset = image_info->width;
}
data[1] = image_info->left + y_offset;
if (*length > image_info->width - y_offset)
{
*length = image_info->width - y_offset;
}
data[2] = *length;
if (*lines > image_info->height - x_offset)
{
*lines = image_info->height - x_offset;
}
/*fprintf(stderr, "STV_RequestImage: DATA 0=%d, 1=%d, 2=%d, 3=%d, length=%d, lines=%d\n", data[0], data[1], data[2], data[3], *length, *lines) ; */
for (j = 0; j < *lines; j++)
{
data[0] = j + XOFF; /*line */
if ((n_values = STV_RequestImageData(compression, data, j, *length, values)) < 0)
{
fprintf(stderr, "STV_RequestImage: Calling STV_RequestImageData failed %d\n", n_values);
return n_values;
}
else
{
for (i = 0; i < n_values; i++)
{
image[j][i] = values[i];
}
ISUpdateDisplay(buffer, j);
}
}
/* Read the 201th line, see mosaic mode */
/* ToDo: analyze/display the data or use them in the fits header(s) */
data[0] = 200; /*line */
if ((res = STV_RequestImageData(1, data, 200, *length, values)) < 0)
{
return res;
}
else
{
for (i = 0; i < n_values; i++)
{
/* fprintf( stderr, "%d: %d ", i, values[i]) ; */
}
/* fprintf( stderr, "\n") ; */
}
res = STV_DownloadComplete();
ISUpdateDisplay(buffer, -j);
return 0;
}
int STV_RequestImageData(int compression, int *data, int j, int length, int *values)
{
int i;
int res;
int data_length;
int n_values;
unsigned char buf[0xffff];
data_length = -1;
if (compression == ON)
{ /* compressed download */
if ((res = STV_SendPacket(REQUEST_COMPRESSED_IMAGE_DATA, data, 4)) != 0)
{
fprintf(stderr, "STV_RequestImageData: could not write\n");
return -1;
}
AGAINC:
if ((res = STV_ReceivePacket(buf, 0)) > 0)
{
if (buf[1] != REQUEST_COMPRESSED_IMAGE_DATA)
{
if (buf[1] != DISPLAY_ECHO)
{
if (buf[1] != ACK)
{
fprintf(stderr, "STV_RequestImageData: expected REQUEST_COMPRESSED_IMAGE_DATA, received %2x\n",
buf[1]);
}
}
goto AGAINC;
}
data_length = (int)buf[3] * 0x100 + (int)buf[2];
if ((n_values = STV_DecompressData((buf + 6), values, data_length, length)) < 0)
{
n_values = -n_values;
fprintf(stderr, "SEVERE ERROR on Line %d, pixel position=%d\n", j, n_values);
_exit(-1);
}
if (n_values == length)
{
return n_values;
}
else
{
fprintf(stderr, "SEVERE Error: Length not equal, Line: %d, %d != %d, data %2x %2x, values %d, %d\n", j,
n_values, length, buf[6 + length - 2], buf[6 + length - 1], values[n_values - 2],
values[n_values - 1]);
_exit(1);
}
}
else
{
fprintf(stderr, "Error: waiting for data on the serial port at line %d\n", j);
return -1;
}
}
else
{ /* uncompressed download */
if ((res = STV_SendPacket(REQUEST_IMAGE_DATA, data, 4)) == 0)
{
AGAINU:
if ((res = STV_ReceivePacket(buf, 0)) > 0)
{
if (buf[1] != REQUEST_IMAGE_DATA)
{
if (buf[1] != DISPLAY_ECHO)
{
if (buf[1] != ACK)
{
fprintf(stderr, "STV_RequestImageData: expected REQUEST_IMAGE_DATA, received %2x\n",
buf[1]);
}
}
goto AGAINU;
}
data_length = (int)buf[3] * 0x100 + (int)buf[2];
for (i = 0; i < data_length; i += 2)
{
values[i / 2] = STV_RecombineInt(buf[6 + i], buf[7 + i]);
}
return data_length / 2;
/*ISUpdateDisplay( buffer, j) ; */ /* Update the display of the INDI client */
}
else
{
fprintf(stderr, "Error: waiting for data on the serial port at line %d\n", j);
return -1;
}
}
else
{
fprintf(stderr, "STV_RequestImageData: error writing %d\n", res);
}
}
return 0;
}
int STV_DecompressData(unsigned char *data, int *values, int length, int expected_n_values)
{
int i, n_values;
int value;
int base;
base = STV_RecombineInt(data[0], data[1]); /*STV Manual says: MSB then LSB! */
values[0] = base;
i = 2;
n_values = 1;
while (i < length)
{
if (((data[i] & 0xc0)) == 0x80)
{ /*two bytes, first byte: bit 7 set, 6 cleared, 14 bits data */
if ((data[i] & 0x20) == 0x20)
{ /* minus sign set? */
value =
-((int)(((~data[i] & 0x1f)) * 0x100) + (((int)(~data[i + 1])) & 0xff)) - 1; /* value without sign */
}
else
{
value = (int)((data[i] & 0x1f) * 0x100) + (int)(data[i + 1]);
}
base = value + base; /* new base value */
values[n_values++] = base; /*pixel data */
i += 2;
}
else if (((data[i] & 0x80)) == 0)
{ /* one byte: bit 7 clear (7 bits data) */
if ((data[i] & 0x40) == 0x40)
{ /* minus sign set? */
value = -(int)((~(data[i])) & 0x3f) - 1; /* value without sign */
}
else
{
value = (int)(data[i] & 0x3f);
/*fprintf( stderr, "Value 7P: %d, pixel value: %d, length %d, pix=%d\n", value, value+ base, length, n_values) ; */
}
/* Sometimes the STV send a 0 at the end, thats not very likely a pixel to pixel variation */
/* I checked the last decompressed pixel value against the last uncompressed pixel value */
/* with different images - it seems to be ok. */
if ((value == 0) && (n_values == expected_n_values))
{
/*fprintf( stderr, "Ignoring byte %d, Zero difference obtained values: %d\n", i, n_values) ; */
}
else
{
base = value + base;
values[n_values++] = base;
}
i++;
}
else if (((data[i] & 0xc0)) == 0xc0)
{ /*two bytes, values= pixel_n/4 */
/*STV_PrintBits( data[ i], 8) ; */
/*STV_PrintBits( data[ i+ 1], 8) ; */
value = 4 * ((int)((data[i] & 0x3f) * 0x100) + (int)(data[i + 1]));
/*fprintf( stderr, "Value 14P: %d, pixel value: %d, length %d, pix=%d\n", value, value+ base, length, n_values) ; */
base = value;
values[n_values++] = value;
i += 2;
/*return -n_values ; */
}
else
{
fprintf(stderr, "Unknown compression case: %2x, length %d, i=%d\n", data[i], length, i);
return -n_values; /* exit */
}
}
return n_values;
}
int STV_TXDisplay(void)
{
int data[] = { TRUE };
return STV_SendPacket(DISPLAY_ECHO, data, 1);
}
int STV_TerminateTXDisplay(void)
{
int res;
int data[] = { FALSE };
res = STV_SendPacket(DISPLAY_ECHO, data, 1);
/* Despite the manual says so, I not always see an ACK packet */
/* So i flush it for the moment */
tcflush(fd, TCIOFLUSH);
return res;
}
int STV_FileStatus(int status)
{
int data[] = { status };
return STV_SendPacket(FILE_STATUS, data, 1);
}
int STV_DownloadComplete(void)
{
return STV_SendPacket(DOWNLOAD_COMPLETE, NULL, 0);
}
int STV_Download(void)
{
return STV_SendPacket(REQUEST_DOWNLOAD, NULL, 0);
}
int STV_DownloadAll(void)
{
return STV_SendPacket(REQUEST_DOWNLOAD_ALL, NULL, 0);
}
int STV_RequestAck(void)
{
int data[] = { 0x06, 0x06, 0x06 };
/* SBIG manual says contains data, which? */
return STV_SendPacket(REQUEST_ACK, data, 3);
}
int STV_BufferStatus(int buffer)
{
unsigned char buf[1024];
int buffers;
int res;
int val;
/*fprintf(stderr, "STV_BufferStatus entering\n") ; */
usleep(50000);
if ((res = STV_SendPacket(REQUEST_BUFFER_STATUS, NULL, 0)) < 0)
{
fprintf(stderr, "STV_BufferStatus: Error requesting buffer status: %d\n", buffer);
return -1;
}
else
{
/*fprintf( stderr, "STV_BufferStatus %2d\n", buffer) ; */
}
AGAIN:
if ((res = STV_ReceivePacket(buf, 0)) < 0)
{
fprintf(stderr, "STV_BufferStatus: Error reading: %d\n", res);
return -1;
;
}
if (buf[1] == REQUEST_BUFFER_STATUS)
{
buffers = STV_RecombineInt(buf[6], buf[7]) * 0x10000 + STV_RecombineInt(buf[8], buf[9]);
if ((val = STV_GetBits(buffers, buffer, 1)) == 1)
{
res = 0; /* image present */
}
else
{
/*fprintf( stderr, "STV_BufferStatus %2d is empty\n", buffer) ; */
res = 1; /* empty */
}
}
else
{
/* The SBIG manual does not specify an ACK, but it is there (at least sometimes) */
if ((val = STV_CheckAck(buf)) == 0)
{
/*fprintf( stderr, "STV_BufferStatus SAW ACK, reading again\n") ; */
goto AGAIN;
}
/* The SBIG manual does not specify the cmd in the answer */
if (buf[1] != DISPLAY_ECHO)
{
fprintf(stderr, "STV_BufferStatus: unexpected cmd byte received %d\n", buf[1]);
val = STV_RecombineInt(buf[2], buf[3]);
res = STV_PrintBuffer(buf, 6 + val + 2);
return -1;
}
else
{
/* a display packet is silently ignored, there are many of this kind */
fprintf(stderr, "STV_BufferStatus DISPLAY_ECHO received, try again\n");
return -1;
}
}
/* fprintf(stderr, "STV_BufferStatus leaving\n") ; */
return res;
}
/* Low level communication */
/* STV_ReceivePacket n_bytes read */
int STV_ReceivePacket(unsigned char *buf, int mode)
{
int i, j, k;
int n_bytes = 0;
int length = 0;
int res;
int trb = 0;
int pos = 0;
char display1[25];
char display2[25];
j = 0;
tracking_buf[0] = 0;
/*fprintf( stderr,"R") ; */
while (pos < 6)
{
/* Read the header first, calculate length of data */
/* At higher speeds the data arrives in smaller chunks, assembling the packet */
if ((trb = read(fd, (char *)(buf + pos), 1)) == -1)
{
fprintf(stderr, "Error, %s\n", strerror(errno));
}
if (buf[0] == 0xa5)
{
if (pos == 5)
{
pos++;
break;
}
else
{
pos += trb; /* could be zero */
}
}
else
{
/* In tracking mode the STV sends raw data (time, brightnes, centroid x,y). */
/* Under normal operation here appear pieces of a packet. This could happen */
/* on start up or if something goes wrong. */
tracking_buf[j++] = buf[pos];
/*fprintf(stderr, "READ: %d, read %d, pos %d, 0x%2x< %c\n", j, trb, pos, buf[pos], buf[pos]) ; */
}
}
if (j > 0)
{
/* Sometimes the packets are corrupt, e.g. at the very beginning */
/* Or it is tracking information */
tracking_buf[j] = 0;
if (mode == ON)
{ /* Tracking mode */
fprintf(stderr, "%s\n", tracking_buf);
}
else
{
for (k = 0; k < j; k++)
{
if (!(tracking_buf[k] > 29 && tracking_buf[k] < 127))
{
tracking_buf[k] = 0x20; /* simply */
}
}
fprintf(stderr, "Not a packet: length: %d >%s<\n", j, tracking_buf);
}
}
n_bytes = pos;
if ((buf[0] == 0xa5) && (pos == 6))
{ /* Check the sanity of the header part */
if ((res = STV_CheckHeaderSum(buf)) == 0)
{
length = (int)buf[3] * 0x100 + (int)buf[2];
;
if (length > 0)
{
trb = 0;
while (pos < 6 + length + 2)
{ /* header, data, check sum */
/* At higher speeds the data arrives in smaller chunks, assembling the packet */
if ((trb = read(fd, (char *)(buf + pos), (6 + length + 2) - pos)) == -1)
{
fprintf(stderr, "STV_ReceivePacket: Error reading at serial port, %s\n", strerror(errno));
return -1;
}
pos += trb;
}
n_bytes += pos;
/*fprintf(stderr, "STV_ReceivePacket: LEAVING LOOP length %d, to read %d, read %d, pos %d\n", length, (6 + length + 2)- pos, trb, pos) ; */
}
}
else
{
fprintf(stderr, "STV_ReceivePacket: Header check failed\n");
return -1;
}
}
else if (pos > 0)
{
for (i = 0; i < 6; i++)
{
if (buf[i] == 0xa5)
{
fprintf(stderr, "STV_ReceivePacket: pos= %d, saw 0xa5 at %d\n", pos, i);
}
}
return -1;
}
else
{
fprintf(stderr, "STV_ReceivePacket: NO 0xa5 until pos= %d\n", pos);
return -1;
}
if (length > 0)
{ /* Check the sanity of the data */
if ((res = STV_CheckDataSum(buf)) == -1)
{
return -1;
}
}
/* Analyse the display and retrieve the values */
if (buf[1] == DISPLAY_ECHO)
{
for (i = 0; i < 24; i++)
{
display1[i] = buf[i + 6];
if (display1[i] == 0)
{
display1[i] = 0x32;
}
display2[i] = buf[i + 30];
if (display2[i] == 0)
{
display2[i] = 0x32;
}
}
display1[24] = 0;
display2[24] = 0;
/*fprintf(stderr, "STV_ReceivePacket: DISPLAY1 %s<\n", display1) ; */
/*fprintf(stderr, "STV_ReceivePacket: DISPLAY2 %s<\n", display2) ; */
/* CCD temperature */
if ((res = strncmp("CCD Temp.", display2, 9)) == 0)
{
float t;
res = sscanf(display2, "CCD Temp. %f", &t);
di.ccd_temperature = (double)t;
/*fprintf(stderr, "STV_ReceivePacket: Read from DISPLAY2 %g<\n", di.ccd_temperature ) ; */
} /* further values can be stored here */
}
return n_bytes;
}
int STV_CheckHeaderSum(unsigned char *buf)
{
int sum = buf[0] + buf[1] + buf[2] + buf[3]; /* Calculated header sum */
int sumbuf = STV_RecombineInt(buf[4], buf[5]); /* STV packet header sum */
if (buf[0] != 0xa5)
{
fprintf(stderr, "STV_CheckHeaderSum: Wrong start byte, skipping\n");
return -1;
}