gloveDevice.cc
Go to the documentation of this file.
1 
18 /* system includes */
19 #include <pthread.h>
20 #include <stdio.h>
21 
22 /* my includes */
23 #include "gloveDevice.h"
24 #include <string>
25 
26 using std::string;
27 
29 {
30  //create object for serial connection
31 }
33 {
34  //destroy object for serial connection
35 }
36 
37 bool gloveDevice::init(const char *serialDev)
38 {
39 
40  // create mutex
41  pthread_mutexattr_t mutex_attr;
42  pthread_mutexattr_init(&mutex_attr);
43  pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
44 
45  pthread_mutex_init(&serial_lock, &mutex_attr);
46  pthread_mutexattr_destroy(&mutex_attr);
47 
48  // init serial
49  if ((serialId = serial.open_serial(serialDev)) == -1)
50  {
51  printf("open_serial() failed.\n");
52  error = false;
53  return false;
54  }
55 
56  // set SW mask as HW mask
57  sensorMask_t mask;
58  error = cmdGetHwSensorMask(&mask);
59  if (!error)
60  {
61  printf("Error: cmdGetHwSensorMask(&mask) failed.\n");
62  }
63  error &= cmdSetSwSensorMask(mask);
64  if (!error)
65  {
66  printf("Error: cmdSetSwSensorMask(mask) failed.\n");
67  }
68 
69  // get sensor size
70  uchar_t n;
72  if (!error)
73  {
74  printf("Error: cmdGetNbSensorsTotal(&n) failed.\n");
75  }
77  if (!error)
78  {
79  printf("Error: cmdSetNbSensorsEnabled(n) failed.\n");
80  }
81 
82  // get glove status switch
83  bool enable;
84  error &= cmdGetGloveStatus(&enable);
85  if (!error)
86  {
87  printf("Error: cmdGetGloveStatus(&enable) failed.\n");
88  }
89 
90  // get timestamp switch
91  error &= cmdGetTimeStamp(&enable);
92  if (!error)
93  {
94  printf("Error: cmdGetTimeStamp(&enable) failed.\n");
95  }
96 
97  if (!error)
98  {
99  printf("Try to switch off (and on again!) the glove hardware.\n");
100  }
101 
102  return error;
103 }
104 
106 {
108 }
109 
111 {
112  pthread_mutex_lock(&serial_lock);
113 
114 }
115 
117 {
118  pthread_mutex_unlock(&serial_lock);
119 
120 }
121 
123 {
124  uchar_t c;
125  error = serial.read_serial(serialId, &c, 1);
126  return c;
127 }
128 
130 {
131  error = serial.read_serial(serialId, s, n);
132 }
133 
135 {
136  error = serial.write_serial(serialId, &c, 1);
137 }
138 
140 {
142 }
143 
145 {
146  return (getChar() == c && getChar() == 0);
147 }
148 
150 {
151  return (getChar() == c1 && getChar() == c2 && getChar() == 0);
152 }
153 
155 {
156  return (getChar() == 0);
157 }
158 
160 {
161  // send command get
162  sendChar('?');
163  sendChar(c);
164 
165  // read response
166  return (getChar() == '?' && getChar() == c);
167 }
168 
170 {
171  lock_serial();
172 
173  // send cmd
174  sendChar(name);
175 
176  // send value
177  sendChar(val);
178 
179  // read response
180  bool res = readResponseSet(name);
181 
182  unlock_serial();
183  return res;
184 }
185 
187 {
188  bool res = false;
189  lock_serial();
190 
191  // read response
192  if (readResponseGet(name))
193  {
194  *val = getChar();
195  res = readNullChar();
196  }
197 
198  unlock_serial();
199  return res;
200 }
201 
202 bool gloveDevice::cmdGetBool(uchar_t name, bool *val)
203 {
204  uchar_t c;
205  bool res = cmdGetChar(name, &c);
206  *val = c;
207  return res;
208 }
209 
210 bool gloveDevice::cmdSetActuate(int n, vector_t index, vector_t values)
211 {
212  lock_serial();
213 
214  // send cmd
215  sendChar('A');
216  sendChar(n);
217 
218  // send values
219  for (int i = 0; i < n; i++)
220  {
221  sendChar(index[i]);
222  sendChar(values[i]);
223  }
224 
225  // read response
226  bool res = readResponseSet('A');
227 
228  unlock_serial();
229  return res;
230 }
231 
233 {
234  lock_serial();
235 
236  // send cmd A
237  sendChar('A');
238  sendChar(255);
239 
240  // send values
241  sendString(values, 6);
242 
243  // read response
244  bool res = readResponseSet('A');
245 
246  unlock_serial();
247  return res;
248 }
249 
251 {
252  bool res = false;
253  lock_serial();
254 
255  // read response
256  if (readResponseGet('A'))
257  {
258  getString(values, 6);
259  res = readNullChar();
260  }
261 
262  unlock_serial();
263  return res;
264 }
265 
267 {
268  return cmdSetChar('B', baudDivide);
269 }
270 
272 {
273  return cmdGetChar('B', baudDivide);
274 }
275 
277 {
278  lock_serial();
279 
280  // send cmd
281  sendChar('C');
282  sendChar('A');
283 
284  // send values
285  sendString(offset, status.getSensorSize());
287 
288  // read response
289  bool res = readResponseSet('C', 'A');
290 
291  unlock_serial();
292  return res;
293 }
294 
296 {
297  lock_serial();
298 
299  // send cmd
300  sendChar('C');
301  sendChar('O');
302 
303  // send values
304  sendChar(index);
305  sendChar(offset);
306 
307  // read response
308  bool res = readResponseSet('C', 'O');
309 
310  unlock_serial();
311  return res;
312 }
313 
315 {
316  lock_serial();
317 
318  // send cmd
319  sendChar('C');
320  sendChar('G');
321 
322  // send values
323  sendChar(index);
324  sendChar(gain);
325 
326  // read response
327  bool res = readResponseSet('C', 'G');
328 
329  unlock_serial();
330  return res;
331 }
332 
334 {
335  bool res = false;
336  lock_serial();
337 
338  // read response
339  if (readResponseGet('C'))
340  {
341  getString(offset, status.getSensorSize());
342  getString(gain, status.getSensorSize());
343  res = readNullChar();
344  }
345 
346  unlock_serial();
347  return res;
348 }
349 
351 {
352  bool res = false;
354  //lock_serial();
355 
356  if (cmdSetChar('D', enable))
357  {
358  status.setTimeStamp(enable);
359  res = true;
360  }
361  //unlock_serial();
362  return res;
363 }
364 
366 {
367  bool res = false;
369  //lock_serial();
370 
371  if (cmdGetBool('D', enable))
372  {
373  status.setTimeStamp(*enable);
374  res = true;
375  }
376  //unlock_serial();
377  return res;
378 }
379 
381 {
382  return cmdSetChar('F', enable);
383 }
384 
386 {
387  return cmdGetBool('F', enable);
388 }
389 
391  statusQuery_t *query, gloveStatusByte_t *gloveStatusByte)
392 {
393  bool res = false;
394  lock_serial();
395 
396  //send cmd
397  sendChar('G');
398 
399  // read response
400  if (getChar() == 'G')
401  {
402  *n = status.getSensorSize();
403  getString(values, status.getSensorSize());
404  if (status.isTimeStamp())
405  {
406  getString(query->timeStamp.raw, 5);
407  }
409  gloveStatusByte->raw = getChar();
410  bool end = false;
411  do
412  {
413  uchar_t c = getChar();
414  switch (c)
415  {
416  case 'e':
417  {
418  uchar_t error = getChar();
419  switch (error)
420  {
421  case 's':
422  query->sample = true;
423  break;
424  case 'g':
425  query->plug = true;
426  break;
427  default:
428  end = true;
429  break;
430  }
431  }; break;
432  case 0:
433  res = end = true;
434  break;
435  default:
436  end = true;
437  break;
438  }
439  } while (!end);
440  }
441  else
442  {
443  printf("Received wrong data from stream\n");
444  fflush(NULL);
445  }
446 
447  unlock_serial();
448  return res;
449 }
450 
452 {
453  lock_serial();
454 
455  if (enable)
456  sendChar('S');
457  else
458  {
459  sendChar('^');
460  sendChar('c');
461  }
462 
463  unlock_serial();
464  return true;
465 }
466 
468 {
469  return cmdGetChar('G', &(status->raw));
470 }
471 
473 {
474  return internalCmd('I');
475 }
476 
478 {
479  return cmdSetChar('J', enable);
480 }
481 
483 {
484  return cmdGetBool('J', enable);
485 }
486 
488 {
489  bool res = false;
490  lock_serial();
491 
492  // read response
493  if (readResponseGet('K'))
494  {
495  getString(mask->raw, 3);
496  res = readNullChar();
497  }
498 
499  unlock_serial();
500  return res;
501 }
502 
504 {
505  return cmdSetChar('L', enable);
506 }
507 
509 {
510  return cmdGetBool('L', enable);
511 }
512 
514 {
515  bool res = false;
516  lock_serial();
517 
518  // send cmd
519  sendChar('M');
520 
521  // send values
522  sendString(mask.raw, 3);
523 
524  // read response
525  res = readResponseSet('M');
526 
527  unlock_serial();
528  return res;
529 }
530 
532 {
533  bool res = false;
534  lock_serial();
535 
536  // read response
537  if (readResponseGet('M'))
538  {
539  getString(mask->raw, 3);
540  res = readNullChar();
541  }
542 
543  unlock_serial();
544  return res;
545 }
546 
548 {
549  if (cmdSetChar('N', n))
550  {
552  return true;
553  }
554  return false;
555 }
556 
558 {
559  if (cmdGetChar('N', n))
560  {
561  status.setSensorSize(*n);
562  return true;
563  }
564  return false;
565 }
566 
568 {
569  bool res = false;
570  lock_serial();
571 
572  // send cmd
573  sendChar('P');
574 
575  // send values
576  sendString(flags.raw, 3);
577 
578  // read response
579  res = readResponseSet('P');
580 
581  unlock_serial();
582  return res;
583 }
584 
586 {
587  bool res = false;
588  lock_serial();
589 
590  // read response
591  if (readResponseGet('P'))
592  {
593  getString(flags->raw, 3);
594  res = readNullChar();
595  }
596 
597  unlock_serial();
598  return res;
599 }
600 
602 {
603  return cmdSetChar('Q', enable);
604 }
605 
607 {
608  return cmdGetBool('Q', enable);
609 }
610 
611 bool gloveDevice::cmdGetHand(bool *rightHand)
612 {
613  return cmdGetBool('Q', rightHand);
614 }
615 
617  statusQuery_t* query, gloveStatusByte_t* gloveStatusByte)
618 {
619  for (unsigned int i = 0; i < MAX_SENSORS; i++)
620  {
621  values[i] = i*3;
622  }
623  *n = (unsigned char) MAX_SENSORS;
624  return true;
625 }
626 
628 {
629  bool result = false;
630  lock_serial();
631 
632  // send cmd
633  sendChar('^');
634  sendChar(cmd);
635 
636  // read response
637  uchar_t res[5];
638  getString(res, 5);
639 
640  result = (res[0] == '^' && res[1] == cmd && res[2] == CR && res[3] == LF
641  && res[4] == 0);
642 
643  unlock_serial();
644  return result;
645 }
646 
648 {
649  return internalCmd('R');
650 }
651 
653 {
654  return cmdGetChar('S', n);
655 }
656 
658 {
659  bool res = false;
660  lock_serial();
661 
662  // send cmd
663  sendChar('T');
664 
665  // send values
666  sendString(period.raw, 4);
667 
668  // read response
669  res = readResponseSet('T');
670 
671  unlock_serial();
672  return res;
673 }
674 
676 {
677  bool res = false;
678  lock_serial();
679 
680  // read response
681  if (readResponseGet('T'))
682  {
683  getString(period->raw, 4);
684  res = readNullChar();
685  }
686 
687  unlock_serial();
688  return res;
689 }
690 
692 {
693  if (cmdSetChar('U', enable))
694  {
695  status.setGloveStatusByte(enable);
696  return true;
697  }
698  return false;
699 }
700 
702 {
703  if (cmdGetBool('U', enable))
704  {
705  status.setGloveStatusByte(*enable);
706  return true;
707  }
708  return false;
709 }
710 
712 {
713  bool res = false;
714  lock_serial();
715 
716  // read response
717  if (readResponseGet('V'))
718  {
719  version->raw[0] = getChar();
720  version->raw[1] = getChar();
721  version->raw[2] = getChar();
722  if (version->raw[2])
723  {
724  version->raw[3] = getChar();
725  if (version->raw[3])
726  {
727  res = readNullChar();
728  }
729  else
730  res = true;
731  }
732  else
733  res = true;
734  }
735 
736  unlock_serial();
737  return res;
738 }
739 
741 {
742  return cmdSetChar('W', enable);
743 }
744 
746 {
747  return cmdGetBool('W', enable);
748 }
749 
751 {
752  return cmdSetChar('W', enable);
753 }
754 
756 {
757  return cmdGetBool('W', enable);
758 }
759 
761  bool *errorSync, gloveStatusByte_t *gloveStatusByte)
762 {
763  bool res = false;
764  lock_serial();
765 
766  // read response
767  if (getChar() == 'Y')
768  {
769  *n = status.getSensorSize();
770  getString(values, status.getSensorSize());
772  gloveStatusByte->raw = getChar();
773 
774  uchar_t c = getChar();
775  if (c)
776  {
777  *errorSync = c;
778  res = readNullChar();
779  }
780  else
781  res = true;
782  }
783 
784  unlock_serial();
785  return res;
786 }
787 
789 {
790  printf("\n");
791  printf("--- Glove Current Status ---\n");
792  printf("Nb of sensors sampled: %d\n", status.getSensorSize());
793  printf("Glove Status Byte: %s\n",
794  status.isGloveStatusByte() ? "on" : "off");
795  printf("Time-Stamp: %s\n", status.isTimeStamp() ? "on" : "off");
796  printf("Errors detected: %s\n", error ? "no" : "yes");
797  printf("\n");
798 }
799 
800 #if gloveDevice_test
801 #include <stdio.h>
802 int main(int argc, char **argv)
803 {
804  // This is a module-test block. You can put code here that tests
805  // just the contents of this C file, and build it by saying
806  // make gloveDevice_test
807  // Then, run the resulting executable (gloveDevice_test).
808  // If it works as expected, the module is probably correct. ;-)
809 
810  fprintf(stderr, "Testing gloveDevice\n");
811 
812  printf("testing gloveDevice\n");
813 
814  return 0;
815 }
816 #endif /* gloveDevice_test */
uchar_t raw[3]
Definition: gloveTypes.h:104
bool cmdSetCalibrateGain(uchar_t index, uchar_t gain)
Definition: gloveDevice.cc:314
bool cmdGetWristToggle(bool *enable)
Definition: gloveDevice.cc:745
bool cmdSetWristToggle(bool enable)
Definition: gloveDevice.cc:740
bool cmdSetParameterFlags(parameterFlags_t flags)
Definition: gloveDevice.cc:567
bool readResponseGet(uchar_t c)
Definition: gloveDevice.cc:159
bool cmdGetConnectStatus(statusConnect_t *status)
Definition: gloveDevice.cc:467
bool cmdGetSensorValuesSync(vector_t values, uchar_t *n, bool *errorSync, gloveStatusByte_t *gloveStatusByte)
Definition: gloveDevice.cc:760
bool cmdSetControlLight(bool enable)
Definition: gloveDevice.cc:477
bool isGloveStatusByte()
Definition: gloveStatus.cc:48
uchar_t raw[4]
Definition: gloveTypes.h:95
bool cmdSetSamplePeriod(samplePeriod_t period)
Definition: gloveDevice.cc:657
long write_serial(int tty_fd, unsigned char *p_buffer, long nb_byte)
Definition: serial.cc:141
void unlock_serial()
Definition: gloveDevice.cc:116
bool cmdGetCalibrateAll(vector_t offset, vector_t gain)
Definition: gloveDevice.cc:333
uchar_t raw[5]
Definition: gloveTypes.h:32
void close_serial(int tty_fd)
Definition: serial.cc:220
bool cmdGetChar(uchar_t name, uchar_t *val)
Definition: gloveDevice.cc:186
bool cmdGetSensorDummyValues(vector_t values, uchar_t *n, statusQuery_t *query, gloveStatusByte_t *gloveStatusByte)
Definition: gloveDevice.cc:616
void setGloveStatusByte(bool activate)
Definition: gloveStatus.cc:43
bool cmdSetActuateAll(vector_t values)
Definition: gloveDevice.cc:232
void setSensorSize(uchar_t n)
Definition: gloveStatus.cc:38
bool cmdGetWristLight(bool *enable)
Definition: gloveDevice.cc:508
unsigned char uchar_t
Definition: gloveTypes.h:28
bool isTimeStamp()
Definition: gloveStatus.cc:58
uchar_t getChar()
Definition: gloveDevice.cc:122
bool cmdGetSensorValues(vector_t values, uchar_t *n, statusQuery_t *query, gloveStatusByte_t *gloveStatusByte)
Definition: gloveDevice.cc:390
bool cmdSetGloveStatus(bool enable)
Definition: gloveDevice.cc:691
gloveStatus status
Definition: gloveDevice.h:147
bool cmdGetActuateAll(vector_t values)
Definition: gloveDevice.cc:250
bool cmdGetHand(bool *rightHand)
Definition: gloveDevice.cc:611
void lock_serial()
Definition: gloveDevice.cc:110
void getString(vector_t s, int n)
Definition: gloveDevice.cc:129
uchar_t raw[4]
Definition: gloveTypes.h:65
int main(int argc, char **argv)
bool cmdSetActuate(int n, vector_t index, vector_t values)
Definition: gloveDevice.cc:210
bool cmdGetQuantized(bool *enable)
Definition: gloveDevice.cc:606
bool cmdSetNbSensorsEnabled(uchar_t n)
Definition: gloveDevice.cc:547
SerialDevice serial
Definition: gloveDevice.h:148
bool readResponseSet(uchar_t c)
Definition: gloveDevice.cc:144
bool cmdSetChar(uchar_t name, uchar_t val)
Definition: gloveDevice.cc:169
bool cmdSetWristLight(bool enable)
Definition: gloveDevice.cc:503
uchar_t raw[3]
Definition: gloveTypes.h:60
bool cmdSetQuantized(bool enable)
Definition: gloveDevice.cc:601
bool internalCmd(uchar_t cmd)
Definition: gloveDevice.cc:627
#define LF
Definition: gloveDevice.h:37
bool cmdSetTimeStamp(bool enable)
Definition: gloveDevice.cc:350
bool readNullChar()
Definition: gloveDevice.cc:154
bool cmdReinitializeCyberGlove()
Definition: gloveDevice.cc:472
bool cmdGetTimeStamp(bool *enable)
Definition: gloveDevice.cc:365
bool cmdSetCalibrateAll(vector_t offset, vector_t gain)
Definition: gloveDevice.cc:276
bool cmdGetGloveStatus(bool *enable)
Definition: gloveDevice.cc:701
bool cmdGetNbSensorsEnabled(uchar_t *n)
Definition: gloveDevice.cc:557
bool cmdGetBaudRate(uchar_t *baudDivide)
Definition: gloveDevice.cc:271
bool cmdSetStreamValues(bool enable)
Definition: gloveDevice.cc:451
unsigned char * vector_t
Definition: gloveTypes.h:27
bool init(const char *serialDev)
Definition: gloveDevice.cc:37
bool cmdGetBinarySync(bool *enable)
Definition: gloveDevice.cc:755
#define CR
Definition: gloveDevice.h:36
bool cmdGetControlLight(bool *enable)
Definition: gloveDevice.cc:482
void showCurrentStatus()
Definition: gloveDevice.cc:788
bool cmdRestartFirmware()
Definition: gloveDevice.cc:647
bool cmdGetParameterFlags(parameterFlags_t *flags)
Definition: gloveDevice.cc:585
bool cmdGetGloveVersion(gloveVersion_t *version)
Definition: gloveDevice.cc:711
void setTimeStamp(bool activate)
Definition: gloveStatus.cc:53
bool cmdGetDigitalFilter(bool *enable)
Definition: gloveDevice.cc:385
bool cmdSetBinarySync(bool enable)
Definition: gloveDevice.cc:750
bool cmdSetCalibrateOffset(uchar_t index, uchar_t offset)
Definition: gloveDevice.cc:295
bool cmdGetHwSensorMask(sensorMask_t *mask)
Definition: gloveDevice.cc:487
int open_serial(const char *p_tty_name)
Definition: serial.cc:53
uchar_t getSensorSize()
Definition: gloveStatus.cc:33
#define MAX_SENSORS
Definition: gloveDevice.h:38
long read_serial(int tty_fd, unsigned char *p_buffer, long nb_bytes_max)
Definition: serial.cc:172
pthread_mutex_t serial_lock
Definition: gloveDevice.h:150
void sendChar(uchar_t)
Definition: gloveDevice.cc:134
bool cmdGetNbSensorsTotal(uchar_t *n)
Definition: gloveDevice.cc:652
bool cmdSetSwSensorMask(sensorMask_t mask)
Definition: gloveDevice.cc:513
timeStamp_t timeStamp
Definition: gloveTypes.h:77
bool cmdGetSamplePeriod(samplePeriod_t *period)
Definition: gloveDevice.cc:675
bool cmdGetBool(uchar_t name, bool *val)
Definition: gloveDevice.cc:202
void sendString(vector_t s, int n)
Definition: gloveDevice.cc:139
bool cmdGetSwSensorMask(sensorMask_t *mask)
Definition: gloveDevice.cc:531
bool cmdSetBaudRate(uchar_t baudDivide)
Definition: gloveDevice.cc:266
bool cmdSetDigitalFilter(bool enable)
Definition: gloveDevice.cc:380


asr_cyberglove_lib
Author(s): Heller Florian, Meißner Pascal, Nguyen Trung, Yi Xie
autogenerated on Mon Jun 10 2019 12:40:38