x_pigpio.c
Go to the documentation of this file.
1 /*
2 gcc -Wall -pthread -o x_pigpio x_pigpio.c -lpigpio
3 sudo ./x_pigpio
4 
5 *** WARNING ************************************************
6 * *
7 * All the tests make extensive use of gpio 25 (pin 22). *
8 * Ensure that either nothing or just a LED is connected to *
9 * gpio 25 before running any of the tests. *
10 * *
11 * Some tests are statistical in nature and so may on *
12 * occasion fail. Repeated failures on the same test or *
13 * many failures in a group of tests indicate a problem. *
14 ************************************************************
15 */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <ctype.h>
25 
26 #include "pigpio.h"
27 
28 #define USERDATA 18249013
29 
30 #define GPIO 25
31 
32 void CHECK(int t, int st, int got, int expect, int pc, char *desc)
33 {
34  if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2)))
35  {
36  printf("TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect);
37  }
38  else
39  {
40  fprintf(stderr,
41  "TEST %2d.%-2d FAILED got %d (%s: %d)\n",
42  t, st, got, desc, expect);
43  }
44 }
45 
46 void t0()
47 {
48  printf("\nTesting pigpio C I/F\n");
49 
50  printf("pigpio version %d.\n", gpioVersion());
51 
52  printf("Hardware revision %d.\n", gpioHardwareRevision());
53 }
54 
55 void t1()
56 {
57  int v;
58 
59  printf("Mode/PUD/read/write tests.\n");
60 
62  v = gpioGetMode(GPIO);
63  CHECK(1, 1, v, 0, 0, "set mode, get mode");
64 
66  v = gpioRead(GPIO);
67  CHECK(1, 2, v, 1, 0, "set pull up down, read");
68 
70  v = gpioRead(GPIO);
71  CHECK(1, 3, v, 0, 0, "set pull up down, read");
72 
74  v = gpioGetMode(GPIO);
75  CHECK(1, 4, v, 1, 0, "write, get mode");
76 
77  v = gpioRead(GPIO);
78  CHECK(1, 5, v, 0, 0, "read");
79 
81  gpioDelay(1); /* 1 micro delay to let GPIO reach level reliably */
82  v = gpioRead(GPIO);
83  CHECK(1, 6, v, 1, 0, "write, read");
84 }
85 
87 
88 void t2cb(int gpio, int level, uint32_t tick)
89 {
90  t2_count++;
91 }
92 
93 void t2()
94 {
95  int dc, f, r, rr, oc;
96 
97  printf("PWM dutycycle/range/frequency tests.\n");
98 
99  gpioSetPWMrange(GPIO, 255);
102  CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency");
103 
104  t2_count=0;
105 
107 
108  gpioPWM(GPIO, 0);
110  CHECK(2, 2, dc, 0, 0, "get PWM dutycycle");
111 
112  time_sleep(0.5); /* allow old notifications to flush */
113  oc = t2_count;
114  time_sleep(2);
115  f = t2_count - oc;
116  CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback");
117 
118  gpioPWM(GPIO, 128);
120  CHECK(2, 4, dc, 128, 0, "get PWM dutycycle");
121 
122  oc = t2_count;
123  time_sleep(2);
124  f = t2_count - oc;
125  CHECK(2, 5, f, 40, 5, "set PWM dutycycle, callback");
126 
129  CHECK(2, 6, f, 100, 0, "set/get PWM frequency");
130 
131  oc = t2_count;
132  time_sleep(2);
133  f = t2_count - oc;
134  CHECK(2, 7, f, 400, 1, "callback");
135 
136  gpioSetPWMfrequency(GPIO, 1000);
138  CHECK(2, 8, f, 1000, 0, "set/get PWM frequency");
139 
140  oc = t2_count;
141  time_sleep(2);
142  f = t2_count - oc;
143  CHECK(2, 9, f, 4000, 1, "callback");
144 
145  r = gpioGetPWMrange(GPIO);
146  CHECK(2, 10, r, 255, 0, "get PWM range");
147 
149  CHECK(2, 11, rr, 200, 0, "get PWM real range");
150 
151  gpioSetPWMrange(GPIO, 2000);
152  r = gpioGetPWMrange(GPIO);
153  CHECK(2, 12, r, 2000, 0, "set/get PWM range");
154 
156  CHECK(2, 13, rr, 200, 0, "get PWM real range");
157 
158  gpioPWM(GPIO, 0);
159 }
160 
161 int t3_val;
164 uint32_t t3_tick;
165 float t3_on;
166 float t3_off;
167 
168 void t3cbf(int gpio, int level, uint32_t tick, void *userdata)
169 {
170  static int unreported = 1;
171 
172  uint32_t td;
173  int *val;
174 
175  val = userdata;
176 
177  if (*val != USERDATA)
178  {
179  if (unreported)
180  {
181  fprintf
182  (
183  stderr,
184  "unexpected userdata %d (expected %d)\n",
185  *val, USERDATA
186  );
187  }
188  unreported = 0;
189  }
190 
191  if (t3_reset)
192  {
193  t3_count = 0;
194  t3_on = 0.0;
195  t3_off = 0.0;
196  t3_reset = 0;
197  }
198  else
199  {
200  td = tick - t3_tick;
201 
202  if (level == 0) t3_on += td;
203  else t3_off += td;
204  }
205 
206  t3_count ++;
207  t3_tick = tick;
208 }
209 
210 void t3()
211 {
212  int f, rr;
213 
214  float on, off;
215 
216  int t, v;
217 
218  int pw[3]={500, 1500, 2500};
219  int dc[4]={20, 40, 60, 80};
220 
221  printf("PWM/Servo pulse accuracy tests.\n");
222 
223  t3_val = USERDATA;
224  t3_reset=1;
225  t3_count=0;
226  t3_tick=0;
227  t3_on=0.0;
228  t3_off=0.0;
229 
230  gpioSetAlertFuncEx(GPIO, t3cbf, &t3_val); /* test extended alert */
231 
232  for (t=0; t<3; t++)
233  {
234  gpioServo(GPIO, pw[t]);
236  CHECK(3, t+t+1, v, pw[t], 0, "get servo pulsewidth");
237 
238  time_sleep(1);
239  t3_reset = 1;
240  time_sleep(4);
241  on = t3_on;
242  off = t3_off;
243  CHECK(3, t+t+2, (1E3*(on+off))/on, 2E7/pw[t], 1,
244  "set servo pulsewidth");
245  }
246 
247  gpioServo(GPIO, 0);
248  gpioSetPWMfrequency(GPIO, 1000);
250  CHECK(3, 7, f, 1000, 0, "set/get PWM frequency");
251 
252  rr = gpioSetPWMrange(GPIO, 100);
253  CHECK(3, 8, rr, 200, 0, "set PWM range");
254 
255  for (t=0; t<4; t++)
256  {
257  gpioPWM(GPIO, dc[t]);
259  CHECK(3, t+t+9, v, dc[t], 0, "get PWM dutycycle");
260 
261  time_sleep(1);
262  t3_reset = 1;
263  time_sleep(2);
264  on = t3_on;
265  off = t3_off;
266  CHECK(3, t+t+10, (1E3*on)/(on+off), 1E1*dc[t], 1,
267  "set PWM dutycycle");
268  }
269 
270  gpioPWM(GPIO, 0);
271 }
272 
273 void t4()
274 {
275  int h, e, f, n, s, b, l, seq_ok, toggle_ok;
276  gpioReport_t r;
277  char p[32];
278 
279  printf("Pipe notification tests.\n");
280 
282  gpioPWM(GPIO, 0);
283  gpioSetPWMrange(GPIO, 100);
284 
285  h = gpioNotifyOpen();
286 
287  sprintf(p, "/dev/pigpio%d", h);
288  f = open(p, O_RDONLY);
289 
290  e = gpioNotifyBegin(h, (1<<GPIO));
291  CHECK(4, 1, e, 0, 0, "notify open/begin");
292 
293  gpioPWM(GPIO, 50);
294  time_sleep(4);
295  gpioPWM(GPIO, 0);
296 
297  e = gpioNotifyPause(h);
298  CHECK(4, 2, e, 0, 0, "notify pause");
299 
300  e = gpioNotifyClose(h);
301  CHECK(4, 3, e, 0, 0, "notify close");
302 
303  n = 0;
304  s = 0;
305  l = 0;
306  seq_ok = 1;
307  toggle_ok = 1;
308 
309  while (1)
310  {
311  b = read(f, &r, 12);
312  if (b == 12)
313  {
314  if (s != r.seqno) seq_ok = 0;
315 
316  if (n) if (l != (r.level&(1<<GPIO))) toggle_ok = 0;
317 
318  if (r.level&(1<<GPIO)) l = 0;
319  else l = (1<<GPIO);
320 
321  s++;
322  n++;
323 
324  // printf("%d %d %d %X\n", r.seqno, r.flags, r.tick, r.level);
325  }
326  else break;
327  }
328 
329  close(f);
330 
331  CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok");
332 
333  CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok");
334 
335  CHECK(4, 6, n, 80, 10, "number of notifications");
336 }
337 
339 
340 void t5cbf(int gpio, int level, uint32_t tick)
341 {
342  if (level == 0) t5_count++; /* falling edges */
343 }
344 
345 void t5()
346 {
347  int BAUD=4800;
348 
349  char *TEXT=
350 "\n\
351 Now is the winter of our discontent\n\
352 Made glorious summer by this sun of York;\n\
353 And all the clouds that lour'd upon our house\n\
354 In the deep bosom of the ocean buried.\n\
355 Now are our brows bound with victorious wreaths;\n\
356 Our bruised arms hung up for monuments;\n\
357 Our stern alarums changed to merry meetings,\n\
358 Our dreadful marches to delightful measures.\n\
359 Grim-visaged war hath smooth'd his wrinkled front;\n\
360 And now, instead of mounting barded steeds\n\
361 To fright the souls of fearful adversaries,\n\
362 He capers nimbly in a lady's chamber\n\
363 To the lascivious pleasing of a lute.\n\
364 ";
365 
366  gpioPulse_t wf[] =
367  {
368  {1<<GPIO, 0, 10000},
369  {0, 1<<GPIO, 30000},
370  {1<<GPIO, 0, 60000},
371  {0, 1<<GPIO, 100000},
372  };
373 
374  int e, oc, c, wid;
375 
376  char text[2048];
377 
378  printf("Waveforms & serial read/write tests.\n");
379 
380  t5_count = 0;
381 
383 
385 
386  e = gpioWaveClear();
387  CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear");
388 
389  e = gpioWaveAddGeneric(4, wf);
390  CHECK(5, 2, e, 4, 0, "pulse, wave add generic");
391 
392  wid = gpioWaveCreate();
394  if (e < 14) CHECK(5, 3, e, 9, 0, "wave tx repeat");
395  else CHECK(5, 3, e, 19, 0, "wave tx repeat");
396 
397  oc = t5_count;
398  time_sleep(5);
399  c = t5_count - oc;
400  CHECK(5, 4, c, 50, 1, "callback");
401 
402  e = gpioWaveTxStop();
403  CHECK(5, 5, e, 0, 0, "wave tx stop");
404 
405  /* gpioSerialReadOpen changes the alert function */
406 
407  e = gpioSerialReadOpen(GPIO, BAUD, 8);
408  CHECK(5, 6, e, 0, 0, "serial read open");
409 
410  gpioWaveClear();
411  e = gpioWaveAddSerial(GPIO, BAUD, 8, 2, 5000000, strlen(TEXT), TEXT);
412  CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial");
413 
414  wid = gpioWaveCreate();
416  if (e < 6964) CHECK(5, 8, e, 6811, 0, "wave tx start");
417  else CHECK(5, 8, e, 7116, 0, "wave tx start");
418 
419  CHECK(5, 9, 0, 0, 0, "NOT APPLICABLE");
420 
421  CHECK(5, 10, 0, 0, 0, "NOT APPLICABLE");
422 
423  while (gpioWaveTxBusy()) time_sleep(0.1);
424  time_sleep(0.1);
425  c = gpioSerialRead(GPIO, text, sizeof(text)-1);
426  if (c > 0) text[c] = 0;
427  CHECK(5, 11, strcmp(TEXT, text), 0, 0, "wave tx busy, serial read");
428 
430  CHECK(5, 12, e, 0, 0, "serial read close");
431 
432  c = gpioWaveGetMicros();
433  CHECK(5, 13, c, 6158148, 0, "wave get micros");
434 
435  c = gpioWaveGetHighMicros();
436  CHECK(5, 14, c, 6158148, 0, "wave get high micros");
437 
438  c = gpioWaveGetMaxMicros();
439  CHECK(5, 15, c, 1800000000, 0, "wave get max micros");
440 
441  c = gpioWaveGetPulses();
442  CHECK(5, 16, c, 3405, 0, "wave get pulses");
443 
444  c = gpioWaveGetHighPulses();
445  CHECK(5, 17, c, 3405, 0, "wave get high pulses");
446 
447  c = gpioWaveGetMaxPulses();
448  CHECK(5, 18, c, 12000, 0, "wave get max pulses");
449 
450  c = gpioWaveGetCbs();
451  if (e < 6963) CHECK(5, 19, c, 6810, 0, "wave get cbs");
452  else CHECK(5, 19, c, 7115, 0, "wave get cbs");
453 
454  c = gpioWaveGetHighCbs();
455  if (e < 6963) CHECK(5, 20, c, 6810, 0, "wave get high cbs");
456  else CHECK(5, 20, c, 7115, 0, "wave get high cbs");
457 
458  c = gpioWaveGetMaxCbs();
459  CHECK(5, 21, c, 25016, 0, "wave get max cbs");
460 }
461 
463 int t6_on;
464 uint32_t t6_on_tick;
465 
466 void t6cbf(int gpio, int level, uint32_t tick)
467 {
468  if (level == 1)
469  {
470  t6_on_tick = tick;
471  t6_count++;
472  }
473  else
474  {
475  if (t6_on_tick) t6_on += (tick - t6_on_tick);
476  }
477 }
478 
479 void t6()
480 {
481  int tp, t, p;
482 
483  printf("Trigger tests\n");
484 
486 
487  tp = 0;
488 
489  t6_count=0;
490  t6_on=0;
491  t6_on_tick=0;
492 
494 
495  for (t=0; t<5; t++)
496  {
497  time_sleep(0.1);
498  p = 10 + (t*10);
499  tp += p;
500  gpioTrigger(GPIO, p, 1);
501  }
502 
503  time_sleep(0.2);
504 
505  CHECK(6, 1, t6_count, 5, 0, "gpio trigger count");
506 
507  CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length");
508 }
509 
511 
512 void t7cbf(int gpio, int level, uint32_t tick)
513 {
514  if (level == PI_TIMEOUT) t7_count++;
515 }
516 
517 void t7()
518 {
519  int c, oc;
520 
521  printf("Watchdog tests.\n");
522 
523  t7_count=0;
524 
525  /* type of edge shouldn't matter for watchdogs */
527 
528  gpioSetWatchdog(GPIO, 50); /* 50 ms, 20 per second */
529  time_sleep(0.5);
530  oc = t7_count;
531  time_sleep(2);
532  c = t7_count - oc;
533  CHECK(7, 1, c, 39, 5, "set watchdog on count");
534 
535  gpioSetWatchdog(GPIO, 0); /* 0 switches watchdog off */
536  time_sleep(0.5);
537  oc = t7_count;
538  time_sleep(2);
539  c = t7_count - oc;
540  CHECK(7, 2, c, 0, 1, "set watchdog off count");
541 }
542 
543 void t8()
544 {
545  int v;
546 
547  printf("Bank read/write tests.\n");
548 
549  gpioWrite(GPIO, 0);
550  v = gpioRead_Bits_0_31() & (1<<GPIO);
551  CHECK(8, 1, v, 0, 0, "read bank 1");
552 
553  gpioWrite(GPIO, 1);
554  v = gpioRead_Bits_0_31() & (1<<GPIO);
555  CHECK(8, 2, v, (1<<GPIO), 0, "read bank 1");
556 
558  v = gpioRead(GPIO);
559  CHECK(8, 3, v, 0, 0, "clear bank 1");
560 
562  v = gpioRead(GPIO);
563  CHECK(8, 4, v, 1, 0, "set bank 1");
564 
565  v = gpioRead_Bits_32_53();
566 
567  if (v) v = 0; else v = 1;
568 
569  CHECK(8, 5, v, 0, 0, "read bank 2");
570 
572  CHECK(8, 6, v, 0, 0, "clear bank 2");
573 
574  CHECK(8, 7, 0, 0, 0, "NOT APPLICABLE");
575 
577  CHECK(8, 8, v, 0, 0, "set bank 2");
578 
579  CHECK(8, 9, 0, 0, 0, "NOT APPLICABLE");
580 }
581 
583 
584 void t9cbf(int gpio, int level, uint32_t tick)
585 {
586  if (level == 1) t9_count++;
587 }
588 
589 void t9()
590 {
591  int s, oc, c, e;
592  uint32_t p[10];
593 
594  /*
595  100 loops per second
596  p0 number of loops
597  p1 GPIO
598  */
599  char *script="\
600  ld p9 p0\
601  tag 0\
602  w p1 1\
603  mils 5\
604  w p1 0\
605  mils 5\
606  dcr p9\
607  jp 0";
608 
609  printf("Script store/run/status/stop/delete tests.\n");
610 
611  gpioWrite(GPIO, 0); /* need known state */
612 
613  t9_count = 0;
614 
616 
617  s = gpioStoreScript(script);
618 
619  while (1)
620  {
621  /* loop until script initialised */
622  time_sleep(0.1);
623  e = gpioScriptStatus(s, p);
624  if (e != PI_SCRIPT_INITING) break;
625  }
626 
627  oc = t9_count;
628  p[0] = 99;
629  p[1] = GPIO;
630  gpioRunScript(s, 2, p);
631  time_sleep(2);
632  c = t9_count - oc;
633  CHECK(9, 1, c, 100, 0, "store/run script");
634 
635  oc = t9_count;
636  p[0] = 200;
637  p[1] = GPIO;
638  gpioRunScript(s, 2, p);
639  time_sleep(0.1);
640  while (1)
641  {
642  e = gpioScriptStatus(s, p);
643  if (e != PI_SCRIPT_RUNNING) break;
644  time_sleep(0.5);
645  }
646  c = t9_count - oc;
647  time_sleep(0.1);
648  CHECK(9, 2, c, 201, 0, "run script/script status");
649 
650  oc = t9_count;
651  p[0] = 2000;
652  p[1] = GPIO;
653  gpioRunScript(s, 2, p);
654  time_sleep(0.1);
655  while (1)
656  {
657  e = gpioScriptStatus(s, p);
658  if (e != PI_SCRIPT_RUNNING) break;
659  if (p[9] < 1900) gpioStopScript(s);
660  time_sleep(0.1);
661  }
662  c = t9_count - oc;
663  time_sleep(0.1);
664  CHECK(9, 3, c, 110, 10, "run/stop script/script status");
665 
666  e = gpioDeleteScript(s);
667  CHECK(9, 4, e, 0, 0, "delete script");
668 }
669 
670 void ta()
671 {
672  int h, b, e;
673  char *TEXT;
674  char text[2048];
675 
676  printf("Serial link tests.\n");
677 
678  /* this test needs RXD and TXD to be connected */
679 
680  h = serOpen("/dev/ttyAMA0", 57600, 0);
681 
682  CHECK(10, 1, h, 0, 0, "serial open");
683 
684  b = serRead(h, text, sizeof(text)); /* flush buffer */
685 
686  b = serDataAvailable(h);
687  CHECK(10, 2, b, 0, 0, "serial data available");
688 
689  TEXT = "\
690 To be, or not to be, that is the question-\
691 Whether 'tis Nobler in the mind to suffer\
692 The Slings and Arrows of outrageous Fortune,\
693 Or to take Arms against a Sea of troubles,\
694 ";
695  e = serWrite(h, TEXT, strlen(TEXT));
696  CHECK(10, 3, e, 0, 0, "serial write");
697 
698  e = serWriteByte(h, 0xAA);
699  e = serWriteByte(h, 0x55);
700  e = serWriteByte(h, 0x00);
701  e = serWriteByte(h, 0xFF);
702 
703  CHECK(10, 4, e, 0, 0, "serial write byte");
704 
705  time_sleep(0.1); /* allow time for transmission */
706 
707  b = serDataAvailable(h);
708  CHECK(10, 5, b, strlen(TEXT)+4, 0, "serial data available");
709 
710  b = serRead(h, text, strlen(TEXT));
711  CHECK(10, 6, b, strlen(TEXT), 0, "serial read");
712  if (b >= 0) text[b] = 0;
713  CHECK(10, 7, strcmp(TEXT, text), 0, 0, "serial read");
714 
715  b = serReadByte(h);
716  CHECK(10, 8, b, 0xAA, 0, "serial read byte");
717 
718  b = serReadByte(h);
719  CHECK(10, 9, b, 0x55, 0, "serial read byte");
720 
721  b = serReadByte(h);
722  CHECK(10, 10, b, 0x00, 0, "serial read byte");
723 
724  b = serReadByte(h);
725  CHECK(10, 11, b, 0xFF, 0, "serial read byte");
726 
727  b = serDataAvailable(h);
728  CHECK(10, 12, b, 0, 0, "serial data availabe");
729 
730  e = serClose(h);
731  CHECK(10, 13, e, 0, 0, "serial close");
732 }
733 
734 void tb()
735 {
736  int h, e, b, len;
737  char *exp;
738  char buf[128];
739 
740  printf("SMBus / I2C tests.");
741 
742  /* this test requires an ADXL345 on I2C bus 1 addr 0x53 */
743 
744  h = i2cOpen(1, 0x53, 0);
745  CHECK(11, 1, h, 0, 0, "i2cOpen");
746 
747  e = i2cWriteDevice(h, "\x00", 1); /* move to known register */
748  CHECK(11, 2, e, 0, 0, "i2cWriteDevice");
749 
750  b = i2cReadDevice(h, buf, 1);
751  CHECK(11, 3, b, 1, 0, "i2cReadDevice");
752  CHECK(11, 4, buf[0], 0xE5, 0, "i2cReadDevice");
753 
754  b = i2cReadByte(h);
755  CHECK(11, 5, b, 0xE5, 0, "i2cReadByte");
756 
757  b = i2cReadByteData(h, 0);
758  CHECK(11, 6, b, 0xE5, 0, "i2cReadByteData");
759 
760  b = i2cReadByteData(h, 48);
761  CHECK(11, 7, b, 2, 0, "i2cReadByteData");
762 
763  exp = "\x1D[aBcDeFgHjKM]";
764  len = strlen(exp);
765 
766  e = i2cWriteDevice(h, exp, len);
767  CHECK(11, 8, e, 0, 0, "i2cWriteDevice");
768 
769  e = i2cWriteDevice(h, "\x1D", 1);
770  b = i2cReadDevice(h, buf, len-1);
771  CHECK(11, 9, b, len-1, 0, "i2cReadDevice");
772  CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0, "i2cReadDevice");
773 
774  if (strncmp(buf, exp+1, len-1))
775  printf("got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
776 
777  e = i2cWriteByteData(h, 0x1d, 0xAA);
778  CHECK(11, 11, e, 0, 0, "i2cWriteByteData");
779 
780  b = i2cReadByteData(h, 0x1d);
781  CHECK(11, 12, b, 0xAA, 0, "i2cReadByteData");
782 
783  e = i2cWriteByteData(h, 0x1d, 0x55);
784  CHECK(11, 13, e, 0, 0, "i2cWriteByteData");
785 
786  b = i2cReadByteData(h, 0x1d);
787  CHECK(11, 14, b, 0x55, 0, "i2cReadByteData");
788 
789  exp = "[1234567890#]";
790  len = strlen(exp);
791 
792  e = i2cWriteBlockData(h, 0x1C, exp, len);
793  CHECK(11, 15, e, 0, 0, "i2c writeBlockData");
794 
795  e = i2cWriteDevice(h, "\x1D", 1);
796  b = i2cReadDevice(h, buf, len);
797  CHECK(11, 16, b, len, 0, "i2cReadDevice");
798  CHECK(11, 17, strncmp(buf, exp, len), 0, 0, "i2cReadDevice");
799 
800  if (strncmp(buf, exp, len))
801  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
802 
803  b = i2cReadI2CBlockData(h, 0x1D, buf, len);
804  CHECK(11, 18, b, len, 0, "i2cReadI2CBlockData");
805  CHECK(11, 19, strncmp(buf, exp, len), 0, 0, "i2cReadI2CBlockData");
806 
807  if (strncmp(buf, exp, len))
808  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
809 
810  exp = "(-+=;:,<>!%)";
811  len = strlen(exp);
812 
813  e = i2cWriteI2CBlockData(h, 0x1D, exp, len);
814  CHECK(11, 20, e, 0, 0, "i2cWriteI2CBlockData");
815 
816  b = i2cReadI2CBlockData(h, 0x1D, buf, len);
817  CHECK(11, 21, b, len, 0, "i2cReadI2CBlockData");
818  CHECK(11, 22, strncmp(buf, exp, len), 0, 0, "i2cReadI2CBlockData");
819 
820  if (strncmp(buf, exp, len))
821  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
822 
823  e = i2cClose(h);
824  CHECK(11, 23, e, 0, 0, "i2cClose");
825 }
826 
827 void tc()
828 {
829  int h, x, b, e;
830  char txBuf[8], rxBuf[8];
831 
832  printf("SPI tests.");
833 
834  /* this test requires a MCP3202 on SPI channel 1 */
835 
836  h = spiOpen(1, 50000, 0);
837  CHECK(12, 1, h, 0, 0, "spiOpen");
838 
839  sprintf(txBuf, "\x01\x80");
840 
841  for (x=0; x<5; x++)
842  {
843  b = spiXfer(h, txBuf, rxBuf, 3);
844  CHECK(12, 2, b, 3, 0, "spiXfer");
845  if (b == 3)
846  {
847  time_sleep(1.0);
848  printf("%d ", ((rxBuf[1]&0x0F)*256)|rxBuf[2]);
849  }
850  }
851 
852  e = spiClose(h);
853  CHECK(12, 99, e, 0, 0, "spiClose");
854 }
855 
856 int main(int argc, char *argv[])
857 {
858  int i, t, c, status;
859 
860  char test[64]={0,};
861 
862  if (argc > 1)
863  {
864  t = 0;
865 
866  for (i=0; i<strlen(argv[1]); i++)
867  {
868  c = tolower(argv[1][i]);
869 
870  if (!strchr(test, c))
871  {
872  test[t++] = c;
873  test[t] = 0;
874  }
875  }
876  }
877  else strcat(test, "0123456789");
878 
879  status = gpioInitialise();
880 
881  if (status < 0)
882  {
883  fprintf(stderr, "pigpio initialisation failed.\n");
884  return 1;
885  }
886 
887  if (strchr(test, '0')) t0();
888  if (strchr(test, '1')) t1();
889  if (strchr(test, '2')) t2();
890  if (strchr(test, '3')) t3();
891  if (strchr(test, '4')) t4();
892  if (strchr(test, '5')) t5();
893  if (strchr(test, '6')) t6();
894  if (strchr(test, '7')) t7();
895  if (strchr(test, '8')) t8();
896  if (strchr(test, '9')) t9();
897  if (strchr(test, 'a')) ta();
898  if (strchr(test, 'b')) tb();
899  if (strchr(test, 'c')) tc();
900 
901  gpioTerminate();
902 
903  return 0;
904 }
905 
int gpioWaveGetCbs(void)
Definition: pigpio.c:10176
void t3cbf(int gpio, int level, uint32_t tick, void *userdata)
Definition: x_pigpio.c:168
s
Definition: DHT22.py:257
int gpioServo(unsigned gpio, unsigned val)
Definition: pigpio.c:8978
int i2cReadI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3782
int i2cWriteI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3837
int t6_on
Definition: x_pigpio.c:463
void t3()
Definition: x_pigpio.c:210
int serWriteByte(unsigned handle, unsigned bVal)
Definition: pigpio.c:4909
int gpioWaveGetHighCbs(void)
Definition: pigpio.c:10187
int gpioInitialise(void)
Definition: pigpio.c:8459
int serRead(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:5000
void t8()
Definition: x_pigpio.c:543
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
Definition: pigpio.c:11303
void t2()
Definition: x_pigpio.c:93
void t2cb(int gpio, int level, uint32_t tick)
Definition: x_pigpio.c:88
f
int gpioNotifyBegin(unsigned handle, uint32_t bits)
Definition: pigpio.c:11747
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
void t7cbf(int gpio, int level, uint32_t tick)
Definition: x_pigpio.c:512
int gpioSetPWMrange(unsigned gpio, unsigned range)
Definition: pigpio.c:8807
int gpioSerialRead(unsigned gpio, void *buf, size_t bufSize)
Definition: pigpio.c:11100
int i2cReadByteData(unsigned handle, unsigned reg)
Definition: pigpio.c:3436
int gpioWaveGetHighMicros(void)
Definition: pigpio.c:10121
#define PI_WAVE_MODE_REPEAT
Definition: pigpio.h:669
uint32_t t3_tick
Definition: x_pigpio.c:164
#define PI_WAVE_MODE_ONE_SHOT
Definition: pigpio.h:668
int t3_reset
Definition: x_pigpio.c:162
void t9cbf(int gpio, int level, uint32_t tick)
Definition: x_pigpio.c:584
int gpioGetServoPulsewidth(unsigned gpio)
Definition: pigpio.c:9016
int main(int argc, char *argv[])
Definition: x_pigpio.c:856
int gpioGetMode(unsigned gpio)
Definition: pigpio.c:8641
int gpioWaveTxStop(void)
Definition: pigpio.c:10094
#define GPIO
Definition: x_pigpio.c:30
int gpioWaveAddSerial(unsigned gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *bstr)
Definition: pigpio.c:9111
#define USERDATA
Definition: x_pigpio.c:28
int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
Definition: pigpio.c:8897
int gpioWaveGetMaxCbs(void)
Definition: pigpio.c:10198
int serDataAvailable(unsigned handle)
Definition: pigpio.c:5033
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
uint32_t gpioRead_Bits_32_53(void)
Definition: pigpio.c:12455
int i2cWriteByteData(unsigned handle, unsigned reg, unsigned bVal)
Definition: pigpio.c:3470
#define PI_SCRIPT_RUNNING
Definition: pigpio.h:805
int gpioWaveGetHighPulses(void)
Definition: pigpio.c:10154
int serClose(unsigned handle)
Definition: pigpio.c:4889
void ta()
Definition: x_pigpio.c:670
#define PI_PUD_UP
Definition: pigpio.h:594
int t3_val
Definition: x_pigpio.c:161
int gpioSerialReadClose(unsigned gpio)
Definition: pigpio.c:11145
int gpioStoreScript(char *script)
Definition: pigpio.c:12158
int gpioGetPWMrange(unsigned gpio)
Definition: pigpio.c:8846
int gpioGetPWMdutycycle(unsigned gpio)
Definition: pigpio.c:8776
int t2_count
Definition: x_pigpio.c:86
int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:3884
#define PI_SCRIPT_INITING
Definition: pigpio.h:803
int t5_count
Definition: x_pigpio.c:338
unsigned gpioVersion(void)
Definition: pigpio.c:13227
int gpioWaveGetMaxMicros(void)
Definition: pigpio.c:10132
uint32_t gpioDelay(uint32_t micros)
Definition: pigpio.c:13196
void t4()
Definition: x_pigpio.c:273
int gpioPWM(unsigned gpio, unsigned val)
Definition: pigpio.c:8744
#define PI_INPUT
Definition: pigpio.h:581
void t5cbf(int gpio, int level, uint32_t tick)
Definition: x_pigpio.c:340
void t7()
Definition: x_pigpio.c:517
int gpioWaveGetPulses(void)
Definition: pigpio.c:10143
int r
Definition: DHT22.py:259
int gpioScriptStatus(unsigned script_id, uint32_t *param)
Definition: pigpio.c:12300
int gpioRead(unsigned gpio)
Definition: pigpio.c:8691
uint32_t t6_on_tick
Definition: x_pigpio.c:464
void t0()
Definition: x_pigpio.c:46
int i2cClose(unsigned handle)
Definition: pigpio.c:4017
void t9()
Definition: x_pigpio.c:589
int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
Definition: pigpio.c:11021
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
int gpioNotifyOpen(void)
Definition: pigpio.c:11635
int gpioDeleteScript(unsigned script_id)
Definition: pigpio.c:12354
int gpioWaveGetMaxPulses(void)
Definition: pigpio.c:10165
int status
Definition: pigs.c:57
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
Definition: pigpio.c:1228
int gpioWrite_Bits_0_31_Clear(uint32_t bits)
Definition: pigpio.c:12467
void gpioTerminate(void)
Definition: pigpio.c:8495
int gpioGetPWMfrequency(unsigned gpio)
Definition: pigpio.c:8949
#define PI_OUTPUT
Definition: pigpio.h:582
dc
int serWrite(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:4964
#define PI_LOW
Definition: pigpio.h:572
int t6_count
Definition: x_pigpio.c:462
int gpioWrite(unsigned gpio, unsigned level)
Definition: pigpio.c:8707
unsigned gpioHardwareRevision(void)
Definition: pigpio.c:13259
int t9_count
Definition: x_pigpio.c:582
void t1()
Definition: x_pigpio.c:55
int gpioTrigger(unsigned gpio, unsigned pulseLen, unsigned level)
Definition: pigpio.c:11820
int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
Definition: pigpio.c:4654
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition: pigpio.c:8661
int gpioWrite_Bits_32_53_Clear(uint32_t bits)
Definition: pigpio.c:12481
int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
Definition: pigpio.c:9524
int t3_count
Definition: x_pigpio.c:163
Definition: gpio.h:23
int i2cReadDevice(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:3913
int spiClose(unsigned handle)
Definition: pigpio.c:4714
int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
Definition: pigpio.c:3942
int gpioWrite_Bits_0_31_Set(uint32_t bits)
Definition: pigpio.c:12495
int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpio.c:4776
int i2cWriteBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3684
def td()
Definition: x_pigpio.py:859
void time_sleep(double seconds)
Definition: pigpio.c:8374
void tc()
Definition: x_pigpio.c:827
int gpioNotifyPause(unsigned handle)
Definition: pigpio.c:11771
void t6()
Definition: x_pigpio.c:479
void t5()
Definition: x_pigpio.c:345
int gpioWaveTxBusy(void)
Definition: pigpio.c:10056
int gpioNotifyClose(unsigned handle)
Definition: pigpio.c:11795
uint32_t gpioRead_Bits_0_31(void)
Definition: pigpio.c:12443
int gpioWrite_Bits_32_53_Set(uint32_t bits)
Definition: pigpio.c:12509
int gpioGetPWMrealRange(unsigned gpio)
Definition: pigpio.c:8869
float t3_on
Definition: x_pigpio.c:165
int gpioRunScript(unsigned script_id, unsigned numParam, uint32_t *param)
Definition: pigpio.c:12219
int serReadByte(unsigned handle)
Definition: pigpio.c:4934
#define PI_HIGH
Definition: pigpio.h:573
void CHECK(int t, int st, int got, int expect, int pc, char *desc)
Definition: x_pigpio.c:32
#define PI_TIMEOUT
Definition: pigpio.h:577
int t7_count
Definition: x_pigpio.c:510
int gpioWaveClear(void)
Definition: pigpio.c:9034
#define PI_PUD_DOWN
Definition: pigpio.h:593
int gpioWaveGetMicros(void)
Definition: pigpio.c:10110
uint16_t seqno
Definition: pigpio.h:416
uint32_t level
Definition: pigpio.h:419
void t6cbf(int gpio, int level, uint32_t tick)
Definition: x_pigpio.c:466
int gpioStopScript(unsigned script_id)
Definition: pigpio.c:12325
int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
Definition: pigpio.c:9084
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
Definition: pigpio.c:4800
void tb()
Definition: x_pigpio.c:734
float t3_off
Definition: x_pigpio.c:166
int i2cReadByte(unsigned handle)
Definition: pigpio.c:3368
int gpioWaveCreate(void)
Definition: pigpio.c:9388


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57