00001
00018
00019 #include <pthread.h>
00020 #include <stdio.h>
00021
00022
00023 #include "gloveDevice.h"
00024 #include <string>
00025
00026 using std::string;
00027
00028 gloveDevice::gloveDevice()
00029 {
00030
00031 }
00032 gloveDevice::~gloveDevice()
00033 {
00034
00035 }
00036
00037 bool gloveDevice::init(const char *serialDev)
00038 {
00039
00040
00041 pthread_mutexattr_t mutex_attr;
00042 pthread_mutexattr_init(&mutex_attr);
00043 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
00044
00045 pthread_mutex_init(&serial_lock, &mutex_attr);
00046 pthread_mutexattr_destroy(&mutex_attr);
00047
00048
00049 if ((serialId = serial.open_serial(serialDev)) == -1)
00050 {
00051 printf("open_serial() failed.\n");
00052 error = false;
00053 return false;
00054 }
00055
00056
00057 sensorMask_t mask;
00058 error = cmdGetHwSensorMask(&mask);
00059 if (!error)
00060 {
00061 printf("Error: cmdGetHwSensorMask(&mask) failed.\n");
00062 }
00063 error &= cmdSetSwSensorMask(mask);
00064 if (!error)
00065 {
00066 printf("Error: cmdSetSwSensorMask(mask) failed.\n");
00067 }
00068
00069
00070 uchar_t n;
00071 error &= cmdGetNbSensorsTotal(&n);
00072 if (!error)
00073 {
00074 printf("Error: cmdGetNbSensorsTotal(&n) failed.\n");
00075 }
00076 error &= cmdSetNbSensorsEnabled(n);
00077 if (!error)
00078 {
00079 printf("Error: cmdSetNbSensorsEnabled(n) failed.\n");
00080 }
00081
00082
00083 bool enable;
00084 error &= cmdGetGloveStatus(&enable);
00085 if (!error)
00086 {
00087 printf("Error: cmdGetGloveStatus(&enable) failed.\n");
00088 }
00089
00090
00091 error &= cmdGetTimeStamp(&enable);
00092 if (!error)
00093 {
00094 printf("Error: cmdGetTimeStamp(&enable) failed.\n");
00095 }
00096
00097 if (!error)
00098 {
00099 printf("Try to switch off (and on again!) the glove hardware.\n");
00100 }
00101
00102 return error;
00103 }
00104
00105 void gloveDevice::stop()
00106 {
00107 serial.close_serial(serialId);
00108 }
00109
00110 void gloveDevice::lock_serial()
00111 {
00112 pthread_mutex_lock(&serial_lock);
00113
00114 }
00115
00116 void gloveDevice::unlock_serial()
00117 {
00118 pthread_mutex_unlock(&serial_lock);
00119
00120 }
00121
00122 uchar_t gloveDevice::getChar()
00123 {
00124 uchar_t c;
00125 error = serial.read_serial(serialId, &c, 1);
00126 return c;
00127 }
00128
00129 void gloveDevice::getString(vector_t s, int n)
00130 {
00131 error = serial.read_serial(serialId, s, n);
00132 }
00133
00134 void gloveDevice::sendChar(uchar_t c)
00135 {
00136 error = serial.write_serial(serialId, &c, 1);
00137 }
00138
00139 void gloveDevice::sendString(vector_t s, int n)
00140 {
00141 error = serial.write_serial(serialId, s, n);
00142 }
00143
00144 bool gloveDevice::readResponseSet(uchar_t c)
00145 {
00146 return (getChar() == c && getChar() == 0);
00147 }
00148
00149 bool gloveDevice::readResponseSet(uchar_t c1, uchar_t c2)
00150 {
00151 return (getChar() == c1 && getChar() == c2 && getChar() == 0);
00152 }
00153
00154 bool gloveDevice::readNullChar()
00155 {
00156 return (getChar() == 0);
00157 }
00158
00159 bool gloveDevice::readResponseGet(uchar_t c)
00160 {
00161
00162 sendChar('?');
00163 sendChar(c);
00164
00165
00166 return (getChar() == '?' && getChar() == c);
00167 }
00168
00169 bool gloveDevice::cmdSetChar(uchar_t name, uchar_t val)
00170 {
00171 lock_serial();
00172
00173
00174 sendChar(name);
00175
00176
00177 sendChar(val);
00178
00179
00180 bool res = readResponseSet(name);
00181
00182 unlock_serial();
00183 return res;
00184 }
00185
00186 bool gloveDevice::cmdGetChar(uchar_t name, uchar_t *val)
00187 {
00188 bool res = false;
00189 lock_serial();
00190
00191
00192 if (readResponseGet(name))
00193 {
00194 *val = getChar();
00195 res = readNullChar();
00196 }
00197
00198 unlock_serial();
00199 return res;
00200 }
00201
00202 bool gloveDevice::cmdGetBool(uchar_t name, bool *val)
00203 {
00204 uchar_t c;
00205 bool res = cmdGetChar(name, &c);
00206 *val = c;
00207 return res;
00208 }
00209
00210 bool gloveDevice::cmdSetActuate(int n, vector_t index, vector_t values)
00211 {
00212 lock_serial();
00213
00214
00215 sendChar('A');
00216 sendChar(n);
00217
00218
00219 for (int i = 0; i < n; i++)
00220 {
00221 sendChar(index[i]);
00222 sendChar(values[i]);
00223 }
00224
00225
00226 bool res = readResponseSet('A');
00227
00228 unlock_serial();
00229 return res;
00230 }
00231
00232 bool gloveDevice::cmdSetActuateAll(vector_t values)
00233 {
00234 lock_serial();
00235
00236
00237 sendChar('A');
00238 sendChar(255);
00239
00240
00241 sendString(values, 6);
00242
00243
00244 bool res = readResponseSet('A');
00245
00246 unlock_serial();
00247 return res;
00248 }
00249
00250 bool gloveDevice::cmdGetActuateAll(vector_t values)
00251 {
00252 bool res = false;
00253 lock_serial();
00254
00255
00256 if (readResponseGet('A'))
00257 {
00258 getString(values, 6);
00259 res = readNullChar();
00260 }
00261
00262 unlock_serial();
00263 return res;
00264 }
00265
00266 bool gloveDevice::cmdSetBaudRate(uchar_t baudDivide)
00267 {
00268 return cmdSetChar('B', baudDivide);
00269 }
00270
00271 bool gloveDevice::cmdGetBaudRate(uchar_t * baudDivide)
00272 {
00273 return cmdGetChar('B', baudDivide);
00274 }
00275
00276 bool gloveDevice::cmdSetCalibrateAll(vector_t offset, vector_t gain)
00277 {
00278 lock_serial();
00279
00280
00281 sendChar('C');
00282 sendChar('A');
00283
00284
00285 sendString(offset, status.getSensorSize());
00286 sendString(gain, status.getSensorSize());
00287
00288
00289 bool res = readResponseSet('C', 'A');
00290
00291 unlock_serial();
00292 return res;
00293 }
00294
00295 bool gloveDevice::cmdSetCalibrateOffset(uchar_t index, uchar_t offset)
00296 {
00297 lock_serial();
00298
00299
00300 sendChar('C');
00301 sendChar('O');
00302
00303
00304 sendChar(index);
00305 sendChar(offset);
00306
00307
00308 bool res = readResponseSet('C', 'O');
00309
00310 unlock_serial();
00311 return res;
00312 }
00313
00314 bool gloveDevice::cmdSetCalibrateGain(uchar_t index, uchar_t gain)
00315 {
00316 lock_serial();
00317
00318
00319 sendChar('C');
00320 sendChar('G');
00321
00322
00323 sendChar(index);
00324 sendChar(gain);
00325
00326
00327 bool res = readResponseSet('C', 'G');
00328
00329 unlock_serial();
00330 return res;
00331 }
00332
00333 bool gloveDevice::cmdGetCalibrateAll(vector_t offset, vector_t gain)
00334 {
00335 bool res = false;
00336 lock_serial();
00337
00338
00339 if (readResponseGet('C'))
00340 {
00341 getString(offset, status.getSensorSize());
00342 getString(gain, status.getSensorSize());
00343 res = readNullChar();
00344 }
00345
00346 unlock_serial();
00347 return res;
00348 }
00349
00350 bool gloveDevice::cmdSetTimeStamp(bool enable)
00351 {
00352 bool res = false;
00354
00355
00356 if (cmdSetChar('D', enable))
00357 {
00358 status.setTimeStamp(enable);
00359 res = true;
00360 }
00361
00362 return res;
00363 }
00364
00365 bool gloveDevice::cmdGetTimeStamp(bool *enable)
00366 {
00367 bool res = false;
00369
00370
00371 if (cmdGetBool('D', enable))
00372 {
00373 status.setTimeStamp(*enable);
00374 res = true;
00375 }
00376
00377 return res;
00378 }
00379
00380 bool gloveDevice::cmdSetDigitalFilter(bool enable)
00381 {
00382 return cmdSetChar('F', enable);
00383 }
00384
00385 bool gloveDevice::cmdGetDigitalFilter(bool *enable)
00386 {
00387 return cmdGetBool('F', enable);
00388 }
00389
00390 bool gloveDevice::cmdGetSensorValues(vector_t values, uchar_t *n,
00391 statusQuery_t *query, gloveStatusByte_t *gloveStatusByte)
00392 {
00393 bool res = false;
00394 lock_serial();
00395
00396
00397 sendChar('G');
00398
00399
00400 if (getChar() == 'G')
00401 {
00402 *n = status.getSensorSize();
00403 getString(values, status.getSensorSize());
00404 if (status.isTimeStamp())
00405 {
00406 getString(query->timeStamp.raw, 5);
00407 }
00408 if (status.isGloveStatusByte())
00409 gloveStatusByte->raw = getChar();
00410 bool end = false;
00411 do
00412 {
00413 uchar_t c = getChar();
00414 switch (c)
00415 {
00416 case 'e':
00417 {
00418 uchar_t error = getChar();
00419 switch (error)
00420 {
00421 case 's':
00422 query->sample = true;
00423 break;
00424 case 'g':
00425 query->plug = true;
00426 break;
00427 default:
00428 end = true;
00429 break;
00430 }
00431 }; break;
00432 case 0:
00433 res = end = true;
00434 break;
00435 default:
00436 end = true;
00437 break;
00438 }
00439 } while (!end);
00440 }
00441 else
00442 {
00443 printf("Received wrong data from stream\n");
00444 fflush(NULL);
00445 }
00446
00447 unlock_serial();
00448 return res;
00449 }
00450
00451 bool gloveDevice::cmdSetStreamValues(bool enable)
00452 {
00453 lock_serial();
00454
00455 if (enable)
00456 sendChar('S');
00457 else
00458 {
00459 sendChar('^');
00460 sendChar('c');
00461 }
00462
00463 unlock_serial();
00464 return true;
00465 }
00466
00467 bool gloveDevice::cmdGetConnectStatus(statusConnect_t *status)
00468 {
00469 return cmdGetChar('G', &(status->raw));
00470 }
00471
00472 bool gloveDevice::cmdReinitializeCyberGlove()
00473 {
00474 return internalCmd('I');
00475 }
00476
00477 bool gloveDevice::cmdSetControlLight(bool enable)
00478 {
00479 return cmdSetChar('J', enable);
00480 }
00481
00482 bool gloveDevice::cmdGetControlLight(bool *enable)
00483 {
00484 return cmdGetBool('J', enable);
00485 }
00486
00487 bool gloveDevice::cmdGetHwSensorMask(sensorMask_t *mask)
00488 {
00489 bool res = false;
00490 lock_serial();
00491
00492
00493 if (readResponseGet('K'))
00494 {
00495 getString(mask->raw, 3);
00496 res = readNullChar();
00497 }
00498
00499 unlock_serial();
00500 return res;
00501 }
00502
00503 bool gloveDevice::cmdSetWristLight(bool enable)
00504 {
00505 return cmdSetChar('L', enable);
00506 }
00507
00508 bool gloveDevice::cmdGetWristLight(bool *enable)
00509 {
00510 return cmdGetBool('L', enable);
00511 }
00512
00513 bool gloveDevice::cmdSetSwSensorMask(sensorMask_t mask)
00514 {
00515 bool res = false;
00516 lock_serial();
00517
00518
00519 sendChar('M');
00520
00521
00522 sendString(mask.raw, 3);
00523
00524
00525 res = readResponseSet('M');
00526
00527 unlock_serial();
00528 return res;
00529 }
00530
00531 bool gloveDevice::cmdGetSwSensorMask(sensorMask_t *mask)
00532 {
00533 bool res = false;
00534 lock_serial();
00535
00536
00537 if (readResponseGet('M'))
00538 {
00539 getString(mask->raw, 3);
00540 res = readNullChar();
00541 }
00542
00543 unlock_serial();
00544 return res;
00545 }
00546
00547 bool gloveDevice::cmdSetNbSensorsEnabled(uchar_t n)
00548 {
00549 if (cmdSetChar('N', n))
00550 {
00551 status.setSensorSize(n);
00552 return true;
00553 }
00554 return false;
00555 }
00556
00557 bool gloveDevice::cmdGetNbSensorsEnabled(uchar_t * n)
00558 {
00559 if (cmdGetChar('N', n))
00560 {
00561 status.setSensorSize(*n);
00562 return true;
00563 }
00564 return false;
00565 }
00566
00567 bool gloveDevice::cmdSetParameterFlags(parameterFlags_t flags)
00568 {
00569 bool res = false;
00570 lock_serial();
00571
00572
00573 sendChar('P');
00574
00575
00576 sendString(flags.raw, 3);
00577
00578
00579 res = readResponseSet('P');
00580
00581 unlock_serial();
00582 return res;
00583 }
00584
00585 bool gloveDevice::cmdGetParameterFlags(parameterFlags_t *flags)
00586 {
00587 bool res = false;
00588 lock_serial();
00589
00590
00591 if (readResponseGet('P'))
00592 {
00593 getString(flags->raw, 3);
00594 res = readNullChar();
00595 }
00596
00597 unlock_serial();
00598 return res;
00599 }
00600
00601 bool gloveDevice::cmdSetQuantized(bool enable)
00602 {
00603 return cmdSetChar('Q', enable);
00604 }
00605
00606 bool gloveDevice::cmdGetQuantized(bool *enable)
00607 {
00608 return cmdGetBool('Q', enable);
00609 }
00610
00611 bool gloveDevice::cmdGetHand(bool *rightHand)
00612 {
00613 return cmdGetBool('Q', rightHand);
00614 }
00615
00616 bool gloveDevice::cmdGetSensorDummyValues(vector_t values, uchar_t* n,
00617 statusQuery_t* query, gloveStatusByte_t* gloveStatusByte)
00618 {
00619 for (unsigned int i = 0; i < MAX_SENSORS; i++)
00620 {
00621 values[i] = i*3;
00622 }
00623 *n = (unsigned char) MAX_SENSORS;
00624 return true;
00625 }
00626
00627 bool gloveDevice::internalCmd(uchar_t cmd)
00628 {
00629 bool result = false;
00630 lock_serial();
00631
00632
00633 sendChar('^');
00634 sendChar(cmd);
00635
00636
00637 uchar_t res[5];
00638 getString(res, 5);
00639
00640 result = (res[0] == '^' && res[1] == cmd && res[2] == CR && res[3] == LF
00641 && res[4] == 0);
00642
00643 unlock_serial();
00644 return result;
00645 }
00646
00647 bool gloveDevice::cmdRestartFirmware()
00648 {
00649 return internalCmd('R');
00650 }
00651
00652 bool gloveDevice::cmdGetNbSensorsTotal(uchar_t * n)
00653 {
00654 return cmdGetChar('S', n);
00655 }
00656
00657 bool gloveDevice::cmdSetSamplePeriod(samplePeriod_t period)
00658 {
00659 bool res = false;
00660 lock_serial();
00661
00662
00663 sendChar('T');
00664
00665
00666 sendString(period.raw, 4);
00667
00668
00669 res = readResponseSet('T');
00670
00671 unlock_serial();
00672 return res;
00673 }
00674
00675 bool gloveDevice::cmdGetSamplePeriod(samplePeriod_t *period)
00676 {
00677 bool res = false;
00678 lock_serial();
00679
00680
00681 if (readResponseGet('T'))
00682 {
00683 getString(period->raw, 4);
00684 res = readNullChar();
00685 }
00686
00687 unlock_serial();
00688 return res;
00689 }
00690
00691 bool gloveDevice::cmdSetGloveStatus(bool enable)
00692 {
00693 if (cmdSetChar('U', enable))
00694 {
00695 status.setGloveStatusByte(enable);
00696 return true;
00697 }
00698 return false;
00699 }
00700
00701 bool gloveDevice::cmdGetGloveStatus(bool *enable)
00702 {
00703 if (cmdGetBool('U', enable))
00704 {
00705 status.setGloveStatusByte(*enable);
00706 return true;
00707 }
00708 return false;
00709 }
00710
00711 bool gloveDevice::cmdGetGloveVersion(gloveVersion_t *version)
00712 {
00713 bool res = false;
00714 lock_serial();
00715
00716
00717 if (readResponseGet('V'))
00718 {
00719 version->raw[0] = getChar();
00720 version->raw[1] = getChar();
00721 version->raw[2] = getChar();
00722 if (version->raw[2])
00723 {
00724 version->raw[3] = getChar();
00725 if (version->raw[3])
00726 {
00727 res = readNullChar();
00728 }
00729 else
00730 res = true;
00731 }
00732 else
00733 res = true;
00734 }
00735
00736 unlock_serial();
00737 return res;
00738 }
00739
00740 bool gloveDevice::cmdSetWristToggle(bool enable)
00741 {
00742 return cmdSetChar('W', enable);
00743 }
00744
00745 bool gloveDevice::cmdGetWristToggle(bool *enable)
00746 {
00747 return cmdGetBool('W', enable);
00748 }
00749
00750 bool gloveDevice::cmdSetBinarySync(bool enable)
00751 {
00752 return cmdSetChar('W', enable);
00753 }
00754
00755 bool gloveDevice::cmdGetBinarySync(bool *enable)
00756 {
00757 return cmdGetBool('W', enable);
00758 }
00759
00760 bool gloveDevice::cmdGetSensorValuesSync(vector_t values, uchar_t *n,
00761 bool *errorSync, gloveStatusByte_t *gloveStatusByte)
00762 {
00763 bool res = false;
00764 lock_serial();
00765
00766
00767 if (getChar() == 'Y')
00768 {
00769 *n = status.getSensorSize();
00770 getString(values, status.getSensorSize());
00771 if (status.isGloveStatusByte())
00772 gloveStatusByte->raw = getChar();
00773
00774 uchar_t c = getChar();
00775 if (c)
00776 {
00777 *errorSync = c;
00778 res = readNullChar();
00779 }
00780 else
00781 res = true;
00782 }
00783
00784 unlock_serial();
00785 return res;
00786 }
00787
00788 void gloveDevice::showCurrentStatus()
00789 {
00790 printf("\n");
00791 printf("--- Glove Current Status ---\n");
00792 printf("Nb of sensors sampled: %d\n", status.getSensorSize());
00793 printf("Glove Status Byte: %s\n",
00794 status.isGloveStatusByte() ? "on" : "off");
00795 printf("Time-Stamp: %s\n", status.isTimeStamp() ? "on" : "off");
00796 printf("Errors detected: %s\n", error ? "no" : "yes");
00797 printf("\n");
00798 }
00799
00800 #if gloveDevice_test
00801 #include <stdio.h>
00802 int main(int argc, char **argv)
00803 {
00804
00805
00806
00807
00808
00809
00810 fprintf(stderr, "Testing gloveDevice\n");
00811
00812 printf("testing gloveDevice\n");
00813
00814 return 0;
00815 }
00816 #endif