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
|
/*
* Remote Laboratory Instrumentation Server
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (c) 2009 Timothy Pearson
* Raptor Engineering
* http://www.raptorengineeringinc.com
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <allegro.h>
#include "parameters.h"
#include "gpib_functions.h"
#include "gpib/ib.h"
char falpha[1024];
unsigned char scope_raw_screenshot_data[4194304];
unsigned long scopeScreenWidth (char * scopeType) {
if (strcmp("HP54600OS", scopeType) == 0) {
return 512;
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
return 640;
}
else {
return 1;
}
}
unsigned long scopeScreenHeight (char * scopeType) {
if (strcmp("HP54600OS", scopeType) == 0) {
return 280;
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
return 480;
}
else {
return 1;
}
}
unsigned long scopeScreenSize (char * scopeType) {
if (strcmp("HP54600OS", scopeType) == 0) {
return scopeScreenWidth(scopeType)*scopeScreenHeight(scopeType)*3;
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
return scopeScreenWidth(scopeType)*scopeScreenHeight(scopeType)*3;
}
else {
return 1;
}
}
int gpib_read_binblock(int ud, int max_num_bytes, char * scopeType)
{
unsigned char segarray[4194304];
long array_pointer;
long ai;
int x;
int y;
array_pointer = 0;
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Trying to read %i bytes from GPIB device...\n", max_num_bytes);
#endif
ibtmo(ud, T30s);
ibeos(ud, 0x0);
ai = gpib_read_array(ud, max_num_bytes, segarray);
if (ai == -1) {
return 1;
}
else {
if (strcmp("HP54600OS", scopeType) == 0) {
ai = 0;
for (x=0;x<scopeScreenWidth(scopeType);x++) {
for (y=0;y<scopeScreenHeight(scopeType);y++) {
if ((x & 0x7) == 0) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x80) << 0;
if ((x & 0x7) == 1) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x40) << 1;
if ((x & 0x7) == 2) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x20) << 2;
if ((x & 0x7) == 3) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x10) << 3;
if ((x & 0x7) == 4) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x08) << 4;
if ((x & 0x7) == 5) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x04) << 5;
if ((x & 0x7) == 6) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x02) << 6;
if ((x & 0x7) == 7) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = (segarray[((x >> 3)+(y*(560/8)))+17] & 0x01) << 7;
if (scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] == 0x0) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = 255;
if (scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] == 0x80) scope_raw_screenshot_data[y+(x*scopeScreenHeight(scopeType))] = 0;
}
}
}
else {
return 2;
}
}
ibtmo(ud, T10s);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Read %li bytes from GPIB device\n", array_pointer);
#endif
return 0;
}
int gpib_read_array(int ud, int max_num_bytes, unsigned char * segarray)
{
int br;
ibrd(ud, segarray, max_num_bytes-1);
br = ThreadIbcntl();
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Number of bytes read from GPIB device: %li\n", br);
#endif
if ((ThreadIbsta() & ERR) && (br == 0)) {
return -1;
}
return br;
}
int gpib_read_binary(int ud, int max_num_bytes)
{
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Trying to read %i bytes from GPIB device...\n", max_num_bytes);
#endif
//ibrd(ud, scope_raw_screenshot_data, max_num_bytes-1);
system("rm -f /tmp/current_scope_screenshot.bmp");
ibrdf(ud, "/tmp/current_scope_screenshot.bmp");
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Number of bytes read from GPIB device: %li\n", ThreadIbcntl());
#endif
if (ThreadIbsta() & ERR) {
return -1;
}
return 0;
}
int scope_get_screenshot_stage2(char * scopeType, int gpibDevice) {
long bytestosend;
int k;
int m;
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Getting scope screenshot [Stage 2]\n\r");
if (strcmp("HP54600OS", scopeType) == 0) {
if (gpib_read_binblock(gpibDevice, 19768, scopeType) == 0) {
return 0;
}
else {
return 1;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
gpib_read_binary(gpibDevice, scopeScreenSize(scopeType));
BITMAP *bmp;
PALETTE palette;
int bpp;
install_allegro(SYSTEM_AUTODETECT, &errno, atexit);
bmp = load_bmp("/tmp/current_scope_screenshot.bmp", palette);
if (!bmp) {
printf("[WARN] Unable to load screenshot bitmap\n\r");
return 1;
}
set_palette(palette);
bpp = bitmap_color_depth(bmp);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Found BPP of %d\n\r", bpp);
#endif
bytestosend = 0;
for (m=0;m<scopeScreenWidth(scopeType);m++) {
for (k=0;k<scopeScreenHeight(scopeType);k++) {
scope_raw_screenshot_data[bytestosend] = getr_depth(bpp, getpixel(bmp, m, k));
bytestosend = bytestosend + 1;
scope_raw_screenshot_data[bytestosend] = getg_depth(bpp, getpixel(bmp, m, k));
bytestosend = bytestosend + 1;
scope_raw_screenshot_data[bytestosend] = getb_depth(bpp, getpixel(bmp, m, k));
bytestosend = bytestosend + 1;
}
}
destroy_bitmap(bmp);
return 0;
}
}
return 1;
}
int scope_get_screenshot(char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Getting scope screenshot [Stage 1]\n\r");
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"PRINT?");
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"HARDCOPY:FORMAT BMPCOLOR");
if (gpib_write(gpibDevice, falpha) == 0) {
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Wrote: %s\n\r", falpha);
#endif
sprintf(falpha,"HARDCOPY:LAYOUT PORTRAIT");
if (gpib_write(gpibDevice, falpha) == 0) {
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Wrote: %s\n\r", falpha);
#endif
sprintf(falpha,"HARDCOPY:PALETTE HARDCOPY");
if (gpib_write(gpibDevice, falpha) == 0) {
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Wrote: %s\n\r", falpha);
#endif
sprintf(falpha,"HARDCOPY:PORT GPIB");
if (gpib_write(gpibDevice, falpha) == 0) {
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Wrote: %s\n\r", falpha);
#endif
sprintf(falpha,"HARDCOPY START");
if (gpib_write(gpibDevice, falpha) == 0) {
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Wrote: %s\n\r", falpha);
#endif
return 0;
}
}
}
}
}
}
}
return 1;
}
int scope_set_timebase(float desired_timebase, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope timebase to %E\n\r", desired_timebase);
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"TIM:RANG %E", desired_timebase);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"HORIZONTAL:MAIN:SCALE %E", desired_timebase/10);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_volts_div(int desired_channel, float desired_volts, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope volts/div on channel %d to %f\n\r", desired_channel, desired_volts/8);
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"CHAN%d:RANG %E", desired_channel, desired_volts);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"CH%d:SCALE %f", desired_channel, desired_volts/8);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_acquisition(int status, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope run status to %d\n\r", status);
if (strcmp("HP54600OS", scopeType) == 0) {
if (status == 0) {
sprintf(falpha,"STOP");
}
else {
sprintf(falpha,"RUN");
}
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"ACQUIRE:STATE %d", status);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_channel_state(int desired_channel, int status, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting channel %d state to %i\n\r", desired_channel, status);
if (strcmp("HP54600OS", scopeType) == 0) {
if (status == 0) {
sprintf(falpha,"BLAN CHAN%d", desired_channel);
}
else if (status == 1) {
sprintf(falpha,"VIEW CHAN%d", desired_channel);
}
else {
return 2;
}
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
if (status == 0) {
sprintf(falpha,"SELECT:CH%d OFF", desired_channel);
}
else if (status == 1) {
sprintf(falpha,"SELECT:CH%d ON", desired_channel);
}
else {
return 2;
}
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_trigger_channel(int desired_channel, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope trigger channel to %d\n\r", desired_channel);
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"TRIG:SOUR CHAN%d", desired_channel);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"TRIGGER:MAIN:EDGE:SOURCE CH%d", desired_channel);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_trigger_level(float desired_level, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope trigger level to %f\n\r", desired_level);
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"TRIG:LEV %E", desired_level);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"TRIGGER:MAIN:LEVEL %f", desired_level);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
int scope_set_channel_position(int desired_channel, float desired_level, char * scopeType, int gpibDevice) {
if ((strcmp("HP54600OS", scopeType) == 0) || (strcmp("TDS744AOS", scopeType) == 0)) {
printf("[INFO] Setting scope channel %d level to %f\n\r", desired_channel, desired_level);
if (strcmp("HP54600OS", scopeType) == 0) {
sprintf(falpha,"CHAN%d:OFFS %E", desired_channel, desired_level);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
else if (strcmp("TDS744AOS", scopeType) == 0) {
sprintf(falpha,"CH%d:POSITION %f", desired_channel, desired_level);
#ifdef ENABLE_EXTRA_DEBUGGING
printf("[DEBG] Writing: %s\n\r", falpha);
#endif
if (gpib_write(gpibDevice, falpha) == 0) {
return 0;
}
else {
return 2;
}
}
}
else {
return 1;
}
}
|