source: tools/FAD/ddd_for_fad/Functions.cpp@ 271

Last change on this file since 271 was 271, checked in by dneise, 14 years ago
added sclkon & sclkoff command to 'fad'
File size: 18.2 KB
Line 
1
2#include "GUI.h"
3#include "SocketClient.h"
4#include "ViewEvent.h"
5#include "math.h"
6// Quit application when clicking close button on window
7void fad::closeEvent(QCloseEvent *) {
8 qApp->quit();
9}
10
11// +++ Connecting or disconnecting from client +++
12void fad::MakeConnection() {
13
14 if(Socket[0]->state() == QAbstractSocket::ConnectedState) {
15 ManualDisconnect = true;
16 for (int i=0; i<NUM_SOCKETS; i++) Socket[i]->disconnectFromHost();
17 return;
18 }
19
20 for (int i=0; i<NUM_SOCKETS; i++) {
21 Connect->setEnabled(false);
22 Socket[i]->connectToHost(IPAddress->text(),Port->value() + i);
23 Socket[i]->waitForConnected(SOCKET_TIMEOUT);
24 Connect->setEnabled(true);
25
26 if (Socket[i]->state() != QAbstractSocket::ConnectedState) {
27 QMessageBox::warning(this, "fad Message","Could not connect to host.",QMessageBox::Ok);
28 return;
29 }
30 }
31
32 Connect->setText("Disconnect");
33 ConnectAction->setText("Disconnect");
34 Port->setEnabled(false);
35 IPAddress->setEnabled(false);
36 Command->setEnabled(true);
37 ManualDisconnect = false;
38}
39
40// +++ Send command to socket +++
41void fad::SendToSocket() {
42
43 char *Buffer = Command->text().toAscii().data();
44
45 int i;
46 unsigned short CMD_Buffer[512];
47 unsigned char CMD_Str[16];
48 unsigned short CMD_Num = 1;
49 int val, num;
50
51 if (strncmp (Buffer, "r", 2) == 0)
52 {
53 CMD_Buffer[0] = htons (CMD_Start);
54 printf ("# Start Run ->\n");
55 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
56 }
57
58 else if (strncmp (Buffer, "s", 2) == 0)
59 {
60 CMD_Buffer[0] = htons (CMD_Stop);
61 printf ("# Stop Run ->\n");
62 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
63 }
64
65 else if (strncmp (Buffer, "sz", 3) == 0)
66 {
67 CMD_Buffer[0] = 0x0000;
68 printf ("# Send 0x0000 ->\n");
69 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
70 }
71 else if (strncmp (Buffer, "de\n", 2) == 0)
72 {
73 CMD_Buffer[0] = htons (CMD_DENABLE);
74 printf ("# domino wave enabled ->\n");
75 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
76 }
77
78 else if (strncmp (Buffer, "dd\n", 2) == 0)
79 {
80 CMD_Buffer[0] = htons (CMD_DDISABLE);
81 printf ("# domino wave disabled ->\n");
82 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
83 }
84
85 else if (strncmp (Buffer, "dr\n", 2) == 0)
86 {
87 CMD_Buffer[0] = htons (CMD_DWRITE_RUN);
88 printf ("# DWRITE HIGH->\n");
89 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
90 }
91
92 else if (strncmp (Buffer, "ds\n", 2) == 0)
93 {
94 CMD_Buffer[0] = htons (CMD_DWRITE_STOP);
95 printf ("# DWRITE LOW ->\n");
96 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
97 }
98
99 else if (strncmp (Buffer, "t", 2) == 0)
100 {
101 CMD_Buffer[0] = htons (CMD_Trigger);
102 printf ("# Trigger ->\n");
103 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
104 }
105
106 else if (strncmp (Buffer, "tc", 3) == 0)
107 {
108 CMD_Buffer[0] = htons (CMD_Trigger_C);
109 printf ("# Continuous Trigger ->\n");
110 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
111 }
112
113 else if (strncmp (Buffer, "ts", 3) == 0)
114 {
115 CMD_Buffer[0] = htons (CMD_Trigger_S);
116 printf ("# Stop Trigger ->\n");
117 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
118 }
119
120 else if (strncmp (Buffer, "sclkon\n", 6) == 0)
121 {
122 CMD_Buffer[0] = htons (CMD_SCLK_ON);
123 printf ("# SCLK ENABLED ->\n");
124 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
125 }
126 else if (strncmp (Buffer, "sclkoff\n", 7) == 0)
127 {
128 CMD_Buffer[0] = htons (CMD_SCLK_OFF);
129 printf ("# SCLK DISABLED ->\n");
130 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
131 }
132
133
134 // Set ROI value
135 else if (strncmp (Buffer, "sr ", 3) == 0)
136 {
137 CMD_Num = 0;
138 if (sscanf (Buffer, "%3s %i %i", CMD_Str, &num, &val) == 3)
139 {
140 if ((num >= 0) & (num <= MAX_ROINUM) & (val >= 0) & (val <= MAX_ROIVAL))
141 {
142 printf ("# Set ROI %d to %d ->\n", num, val);
143 CMD_Buffer[0] = htons (CMD_Write | (BADDR_ROI + num));
144 CMD_Buffer[1] = htons (val);
145 printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
146 CMD_Num = 2;
147 }
148 else
149 {
150 printf ("Out of range\n");
151 printf ("Usage: sr NUM VAL: Set ROI NUM (0-%d) to VAL (0-%d)\n", MAX_ROINUM, MAX_ROIVAL);
152 }
153 }
154 else
155 {
156 printf ("Usage: sr NUM VAL: Set ROI NUM (0-%d) to VAL (0-%d)\n", MAX_ROINUM, MAX_ROIVAL);
157 }
158 }
159
160 // Set all ROIs
161 else if (strncmp (Buffer, "sra ", 4) == 0)
162 {
163 CMD_Num = 0;
164 if (sscanf (Buffer, "%4s %i", CMD_Str, &val) == 2)
165 {
166 if ((val >= 0) & (val <= MAX_ROIVAL))
167 {
168 printf ("# Set all ROIs to %d ->\n", val);
169 for (i = 0; i < (MAX_ROINUM + 1); i++)
170 {
171 CMD_Buffer[i * 2] = htons (CMD_Write | (BADDR_ROI + i));
172 CMD_Buffer[(i * 2) + 1] = htons (val);
173 CMD_Num += 2;
174 }
175 for (i = 0; i < ((MAX_ROINUM + 1) * 2); i += 8)
176 {
177 printf ("# 0x%.4X 0x%.4X 0x%.4X 0x%.4X 0x%.4X 0x%.4X 0x%.4X 0x%.4X\n",
178 ntohs (CMD_Buffer[i]), ntohs (CMD_Buffer[i + 1]),
179 ntohs (CMD_Buffer[i + 2]), ntohs (CMD_Buffer[i + 3]),
180 ntohs (CMD_Buffer[i + 4]), ntohs (CMD_Buffer[i + 5]),
181 ntohs (CMD_Buffer[i + 6]), ntohs (CMD_Buffer[i + 7]));
182 }
183 }
184 else
185 {
186 printf ("Out of range\n");
187 printf ("Usage: sra VAL: Set all ROIs to VAL (0-%d)\n", MAX_ROIVAL);
188 }
189 }
190 else
191 {
192 printf ("Usage: sra VAL: Set all ROIs to VAL (0-%d)\n", MAX_ROIVAL);
193 }
194 }
195
196 // Set DAC value
197 else if (strncmp (Buffer, "sd ", 3) == 0)
198 {
199 CMD_Num = 0;
200 if (sscanf (Buffer, "%3s %i %i", CMD_Str, &num, &val) == 3)
201 {
202 if ((num >= 0) & (num <= MAX_DACNUM) & (val >= 0) & (val <= MAX_DACVAL))
203 {
204 printf ("# Set DAC %d to %d ->\n", num, val);
205 CMD_Buffer[0] = htons (CMD_Write | (BADDR_DAC + num));
206 CMD_Buffer[1] = htons (val);
207 printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
208 CMD_Num = 2;
209 }
210 else
211 {
212 printf ("Out of range\n");
213 printf ("Usage: sd NUM VAL: Set DAC NUM (0-%d) to VAL (0-%d)\n", MAX_DACNUM, MAX_DACVAL);
214 }
215 }
216 else
217 {
218 printf ("Usage: sd NUM VAL: Set DAC NUM (0-%d) to VAL (0-%d)\n", MAX_DACNUM, MAX_DACVAL);
219 }
220 }
221
222 // Set address to value
223 else if (strncmp (Buffer, "sa ", 3) == 0)
224 {
225 CMD_Num = 0;
226 if (sscanf (Buffer, "%4s %i %i", CMD_Str, &num, &val) == 3)
227 {
228 if ((num >= 0) & (num <= MAX_ADDR) & (val >= 0) & (val <= MAX_VAL))
229 {
230 printf ("# Set ADDR %d to %d ->\n", num, val);
231 CMD_Buffer[0] = htons (CMD_Write | num);
232 CMD_Buffer[1] = htons (val);
233 printf ("# 0x%.4X 0x%.4X\n", ntohs (CMD_Buffer[0]), ntohs (CMD_Buffer[1]));
234 CMD_Num = 2;
235 }
236 else
237 {
238 printf ("Out of range\n");
239 printf ("Usage: sa NUM VAL: Set addr NUM (0-%d) to VAL (0-%d)\n", MAX_ADDR, MAX_VAL);
240 }
241 }
242 else
243 {
244 printf ("Usage: raw NUM VAL: Set addr NUM (0-%d) to VAL (0-%d)\n", MAX_ADDR, MAX_VAL);
245 }
246 }
247
248 // Send value
249 else if (strncmp (Buffer, "sv ", 3) == 0)
250 {
251 CMD_Num = 0;
252 if (sscanf (Buffer, "%4s %i", CMD_Str, &val) == 2)
253 {
254 if ((val >= 0) & (val <= MAX_VAL))
255 {
256 printf ("# Send %d ->\n", val);
257 CMD_Buffer[0] = htons (val);
258 printf ("# 0x%.4X\n", ntohs (CMD_Buffer[0]));
259 CMD_Num = 1;
260 }
261 else
262 {
263 printf ("Out of range\n");
264 printf ("Usage: sv VAL: Send VAL (0-%d)\n", MAX_VAL);
265 }
266 }
267 else
268 {
269 printf ("Usage: sv VAL: Send VAL (0-%d)\n", MAX_VAL);
270 }
271 }
272
273 // Anything else... , print help
274 else
275 {
276 printf ("Commands:\n");
277 printf (" r: Start Run -- TODO\n");
278 printf (" s: Stop Run -- TODO\n");
279 printf (" t: Single Trigger\n\n");
280 printf (" de: DENABLE HIGH\n");
281 printf (" dd: DENABLE LOW\n");
282 printf (" dr: DWRITE HIGH\n");
283 printf (" ds: DWRITE LOW\n\n");
284 printf (" tc: Continuous Trigger\n");
285 printf (" ts: Stop Trigger\n");
286 printf (" sr NUM VAL: Set ROI NUM (0-35) to VAL (0-1024)\n");
287 printf (" sra VAL: Set all ROIs to VAL (0-1024)\n");
288 printf (" sd NUM VAL: Set DAC NUM (0-7) to VAL (0-65535)\n");
289 printf (" q: Quit\n");
290 printf ("\n");
291 printf (" Only for debugging:\n");
292 printf (" sa NUM VAL: Set addr NUM (0-255) to VAL (0-65535)\n");
293 printf (" sv VAL: Send VAL\n");
294 printf (" sz: Send 0x0000\n");
295 printf ("\n");
296
297 CMD_Num = 0;
298 }
299
300
301 // Clear output
302 SocketOutput->clear();
303 for (int i=0; i<NUM_SOCKETS; i++) Data[i].clear();
304
305 // Send commands
306 QByteArray Data((char *) CMD_Buffer, CMD_Num * sizeof (short));
307 printf("Wrote %d bytes\n", (int) Socket[0]->write(Data));
308 Command->clear();
309}
310
311// +++ Read data from socket and display +++
312//
313// Structure interpreation taken from 'ViewEvent.cc' by Kai Warda.
314//
315void fad::ReadFromSocket() {
316
317 QByteArray SocketData;
318
319 // Add data to text display (hex) and to data array
320 for (int Sckt=0; Sckt<NUM_SOCKETS; Sckt++) {
321
322 // Read from socket and accumulate in Data[] arrays
323 SocketData = Socket[Sckt]->readAll();
324 if (SocketData.isEmpty()) continue;
325 Data[Sckt].append(SocketData);
326
327 // Print result
328 printf("Got %d bytes from socket %d\n", (int) SocketData.size(), Sckt);
329 SocketOutput->insertPlainText(SocketData.toHex());
330
331 // Check package length (measured in units of 2 bytes)
332 EVNT_HEADER *Header = (EVNT_HEADER *) Data[Sckt].data();
333 if (Data[Sckt].size() != 2 * ntohs (Header->package_length)) continue;
334
335 // read and print package header
336 printf ("Start Package Flag: 0x%.4X\n", ntohs (Header->start_package_flag));
337 printf ("Package Length: 0x%.4X\n", ntohs (Header->package_length));
338 printf ("Version Number: 0x%.4X\n", ntohs (Header->version_no));
339 printf ("Trigger-ID: 0x%.8X\n", ntohl (Header->trigger_id));
340 printf ("Trigger Type: 0x%.2X\n", Header->trigger_type);
341 printf ("Trigger CRC: 0x%.2X\n", Header->trigger_crc);
342 printf ("Local Trigger-ID: 0x%.8X\n", ntohl (Header->local_trigger_id));
343 printf ("Local Trigger Type: 0x%.2X\n", Header->local_trigger_type);
344 printf ("Local Trigger CRC: 0x%.2X\n", Header->local_trigger_crc);
345 printf ("Board-ID: 0x%.4X\n", ntohs (Header->board_id));
346
347 for (int i = 0; i < 4; i++) {
348 if ((ntohs (Header->drs_temperature[i]) & 0x8000) == 0x8000) {
349 printf ("Temperature %d: %d\n", i, 0xE000 | (ntohs (Header->drs_temperature[i])) >> 3);
350 }
351 else printf ("Temperature %d: %d\n", i, ntohs (Header->drs_temperature[i]) >> 3);
352 }
353
354 // DAC Values, only if version > 0x0101
355 if (ntohs (Header->version_no) > 0x0101) {
356 for (int i = 0; i < 8; i++) printf ("DAC %d: %d\n", i, ntohs (Header->dac[i]));
357 }
358
359 // read channels, no error checking...
360 double *x, *y;
361 CHANNEL *Channel = (CHANNEL *) (Data[Sckt].data() + sizeof(EVNT_HEADER));
362
363 unsigned int drs_index, channel_index;
364
365 for (int i=0; i<NUM_CHANNELS; i++) {
366
367 drs_index = i%4;
368 channel_index = i/4;
369
370 x = new double [ntohs (Channel->channel_roi)];
371 y = new double [ntohs (Channel->channel_roi)];
372 int dummy;
373 printf("ROI: %d\n", ntohs (Channel->channel_roi));
374
375 // print channel header
376 printf ("\nChannel-ID: 0x%.4X\n", ntohs (Channel->channel_id));
377 printf ("Channel Start-Cell: 0x%.4X\n", ntohs (Channel->channel_start_cell));
378 printf ("Channel ROI: 0x%.4X\n", ntohs (Channel->channel_roi));
379
380 // print channel data
381 printf ("Channel Data:\n");
382 unsigned short *ChannelData = (unsigned short *) &Channel->channel_adc_data;
383 for (int j=0; j<ntohs (Channel->channel_roi); j++) {
384 x[j] = j* 50;
385 dummy = ad9238ToInt( (short) ntohs (ChannelData[j]) );
386 y[j] = (double) dummy /2048.0 ;
387 //printf ("%4d: 0x%.4x , 0x%.4x , %.4f \n", j, (short) ntohs (ChannelData[j]), dummy, y[j]);
388
389 }
390
391 printf ("channel %d: DRS[%d] is %d : ChannelCheckBox[%d] is %d\n" , i, drs_index, DRS[drs_index]->isChecked() , channel_index, ChannelCheckBox[channel_index]->isChecked() );
392 if (DRS[drs_index]->isChecked() && ChannelCheckBox[channel_index]->isChecked())
393 {
394 // Plot data
395 Signal[i]->setData(x, y, ntohs(Channel->channel_roi));
396 Signal[i]->show();
397 printf ("plotting\n" );
398 }
399 else
400 {
401 Signal[i]->hide();
402 }
403 delete[] x;
404 delete[] y;
405
406 // Move pointer to next channel
407
408 Channel = (CHANNEL *) ((char *) Channel + (3 + ntohs (Channel->channel_roi)) * sizeof(unsigned short));
409 }
410
411 // Recast to access last entries of event structure
412 unsigned int *Pointer = (unsigned int *) Channel;
413
414 // CRC, only if version > 0x0100
415 if (ntohs (Header->version_no) > 0x0100) {
416 printf ("\nPackage CRC: 0x%.4X\n", ntohs (*(Pointer++)));
417 }
418 // end package flag
419 printf ("\nEnd Package Flag: 0x%.4X\n\n", ntohs (*Pointer));
420
421 // Clear accumulated data array
422 Data[Sckt].clear();
423
424 } // Loop over sockets
425}
426
427// +++ Reset graph axes to autoscale when fully unzoomed +++
428void fad::HandleZoom(const QwtDoubleRect &) {
429 if(Zoomer->zoomRectIndex() == 0) {
430 Graph->setAxisAutoScale(QwtPlot::xBottom);
431 Graph->setAxisAutoScale(QwtPlot::yLeft);
432 }
433}
434
435// +++ Disconnect from socket +++
436void fad::GotDisconnected() {
437 Connect->setText("Connect");
438 ConnectAction->setText("Connect");
439 Port->setEnabled(true);
440 IPAddress->setEnabled(true);
441 Command->setEnabled(false);
442
443 SocketOutput->clear();
444 if(!ManualDisconnect) QMessageBox::warning(this, "fad Message","Socket disconnected, maybe host gone.",QMessageBox::Ok);
445}
446
447
448//------------------------------------------------------------------
449//**************************** All menus ***************************
450//------------------------------------------------------------------
451
452void fad::MenuSave() {
453 QString Filename = QFileDialog::getSaveFileName(this,
454 "Filename of image", ".", "Image files (*.bmp *.jpg *.png *.ppm *.tiff *.xbm *.xpm);;All files (*)");
455 if (Filename.length()>0) {
456 QPixmap Pixmap = QPixmap::grabWidget(Graph);
457 if(!Pixmap.save(Filename)) {
458 QMessageBox::warning(this, "fad Message","Could not write image file.",QMessageBox::Ok);
459 remove(Filename.toAscii().data());
460 }
461 }
462}
463
464void fad::MenuPrint() {
465 QPrinter *Printer = new QPrinter;
466 QPrintDialog *PrintDialog = new QPrintDialog(Printer, this);
467 if (PrintDialog->exec() == QDialog::Accepted) {
468 QPainter Painter(Printer);
469 QPixmap Pixmap = QPixmap::grabWidget(Graph);
470 Painter.drawPixmap(0, 0, Pixmap);
471 }
472 delete Printer; delete PrintDialog;
473}
474
475void fad::MenuSaveASCII() {
476 QString Filename = QFileDialog::getSaveFileName(this,
477 "Filename", ".", "Text files (*.txt *.ascii *.asc);;All files (*)");
478 if (Filename.length()>0) {
479 QFile File(Filename);
480 if (File.open(QFile::WriteOnly | QIODevice::Text | QFile::Truncate)) {
481 QTextStream Stream(&File);
482 for (int Count=0; Count<NUM_CHANNELS; Count++) {
483 for (int i=0; i<Signal[i]->dataSize(); i++) Stream << Signal[i]->y(i) << endl;
484 Stream << endl;
485 }
486 }
487 else {
488 QMessageBox::warning(this, "fad Message","Could not write data to file.",QMessageBox::Ok);
489 }
490 }
491}
492
493/*
494ad9238ToDouble
495input:
496unsigned short code: 13bit ADC code in an 16bit data type
497 unsigned short was chosen, because it is least subject to interpretation
498
499AD9238 is an 12bit ADC which in addition outputs an overflow bit.
500The overflow bit (OVFL) as well as the data bits 11 until 0 (D11..D0) are assembled in
501the unsigned short as follows:
502|bit15|bit14|bit13|bit12|bit11|bit10|bit9 |bit8 |
503| 0 | 0 | 0 |OVFL | D11 | D10 | D9 | D8 |
504
505|bit7 |bit6 |bit5 |bit4 |bit3 |bit2 |bit1 |bit0 |
506| D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
507
508D11..D0 are formatted as twos complement so:
509
5100x000 = 0
511...
5120x7FF = 2047
5130x800 = -2048
514...
5150xFFF = -1
516
517Note:
518taking the overflow bit into account as well as the
519twos complement format.
520This is how overflows are coded:
5210x7FF ist the largest positive 12bit number.
522therefore:
5230x17FF = 0001.0111.1111.1111 = 6143 is the code for a positive overflow
524and
5250x800 is the most negative 12bit number.
526now 0x1800 = 0001.1000.0000.0000 = 6144 is the code for an underflow.
527*/
528
529
530double fad::ad9238ToDouble(unsigned short code){
531 unsigned short upper_three_bit_mask = 0xE000;
532
533 if ((code & upper_three_bit_mask) != 0x0000)
534 // one of the upper three bits was set --> Error
535 {
536 fprintf (stderr, "error in ad9238ToDouble. 1 of 3 upper bits was set. code = 0x%4X" ,code);
537 return nan("");
538 }
539
540 // check for over- and underflow
541 if (code == 0x17FF) // overflow
542 return 2048; // this is the highest signed short + 1, indicating an overflow.
543 if (code == 0x1800) // underflow
544 return -2049; // this is the lowest signed short - 1, indicating an underflow.
545
546 // note: bit shifting operator >> is smart.
547 // when shifting a signed integer type to the right, the
548 // sign bit is expanded:
549 // 0x1800 << 4 = 0x8000 and then
550 // 0x8000 >> 4 = 0xF800, when the type is a signed one.
551 return double( (((short) code) << 4) >> 4);
552}
553
554
555double fad::ad9238ToDouble(short code){
556
557 unsigned short upper_three_bit_mask = 0xE000;
558
559
560 if ((code & upper_three_bit_mask) != 0x0000)
561 // one of the upper three bits was set --> Error
562 {
563 fprintf (stderr, "error in ad9238ToDouble. 1 of 3 upper bits was set. code = 0x%4X" ,code);
564 return nan("");
565 }
566
567 // check for over- and underflow
568 if (code == 0x17FF) // overflow
569 return 2048; // this is the highest signed short + 1, indicating an overflow.
570 if (code == 0x1800) // underflow
571 return -2049; // this is the lowest signed short - 1, indicating an underflow.
572
573 // note: bit shifting operator >> is smart.
574 // when shifting a signed integer type to the right, the
575 // sign bit is expanded:
576 // 0x1800 << 4 = 0x8000 and then
577 // 0x8000 >> 4 = 0xF800, when the type is a signed one.
578 return double( (( code) << 4) >> 4);
579}
580
581int fad::ad9238ToInt(short code){
582
583 unsigned short upper_three_bit_mask = 0xE000;
584
585
586 if ((code & upper_three_bit_mask) != 0x0000)
587 // one of the upper three bits was set --> Error
588 {
589 fprintf (stderr, "error in ad9238ToDouble. 1 of 3 upper bits was set. code = 0x%4X" ,code);
590 return -10000;
591 }
592
593 // check for over- and underflow
594 if (code == 0x17FF) // overflow
595 return 2048; // this is the highest signed short + 1, indicating an overflow.
596 if (code == 0x1800) // underflow
597 return -2049; // this is the lowest signed short - 1, indicating an underflow.
598
599 // note: bit shifting operator >> is smart.
600 // when shifting a signed integer type to the right, the
601 // sign bit is expanded:
602 // 0x1800 << 4 = 0x8000 and then
603 // 0x8000 >> 4 = 0xF800, when the type is a signed one.
604 //return (( code) << 4) >> 4;
605 // this doenst seem to work.
606
607 // check if sign-bit (bit 11) is set
608 if ( (code & 0x800) != 0) { // sign-bit is set -> number is negative
609 code = code | 0xF000; // bits 12..15 are set
610 } else {
611
612 }
613 return code;
614
615}
Note: See TracBrowser for help on using the repository browser.