x_pigpio.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #*** WARNING ************************************************
4 #* *
5 #* All the tests make extensive use of gpio 25 (pin 22). *
6 #* Ensure that either nothing or just a LED is connected to *
7 #* gpio 25 before running any of the tests. *
8 #* *
9 #* Some tests are statistical in nature and so may on *
10 #* occasion fail. Repeated failures on the same test or *
11 #* many failures in a group of tests indicate a problem. *
12 #************************************************************
13 
14 import sys
15 import time
16 import struct
17 
18 import pigpio
19 
20 GPIO=25
21 
22 def STRCMP(r, s):
23 
24  if sys.hexversion > 0x03000000:
25 
26  if type(r) == type(""):
27  r = bytearray(r, 'latin-1')
28 
29  if type(s) == type(""):
30  s = bytearray(s, 'latin-1')
31 
32  if r != s:
33  print(r, s)
34  return 0
35 
36  else:
37  return 1
38 
39 def CHECK(t, st, got, expect, pc, desc):
40  if got >= (((1E2-pc)*expect)/1E2) and got <= (((1E2+pc)*expect)/1E2):
41  print("TEST {:2d}.{:<2d} PASS ({}: {:d})".format(t, st, desc, expect))
42  else:
43  print("TEST {:2d}.{:<2d} FAILED got {:d} ({}: {:d})".
44  format(t, st, got, desc, expect))
45 
46 def t0():
47 
48  print("\nTesting pigpio Python module {}".format(pigpio.VERSION))
49 
50  print("Python {}".format(sys.version.replace("\n", " ")))
51 
52  print("pigpio version {}.".format(pi.get_pigpio_version()))
53 
54  print("Hardware revision {}.".format(pi.get_hardware_revision()))
55 
56 def t1():
57 
58  print("Mode/PUD/read/write tests.")
59 
60  pi.set_mode(GPIO, pigpio.INPUT)
61  v = pi.get_mode(GPIO)
62  CHECK(1, 1, v, 0, 0, "set mode, get mode")
63 
64  pi.set_pull_up_down(GPIO, pigpio.PUD_UP)
65  v = pi.read(GPIO)
66  CHECK(1, 2, v, 1, 0, "set pull up down, read")
67 
68  pi.set_pull_up_down(GPIO, pigpio.PUD_DOWN)
69  v = pi.read(GPIO)
70  CHECK(1, 3, v, 0, 0, "set pull up down, read")
71 
72  pi.write(GPIO, pigpio.LOW)
73  v = pi.get_mode(GPIO)
74  CHECK(1, 4, v, 1, 0, "write, get mode")
75 
76  v = pi.read(GPIO)
77  CHECK(1, 5, v, 0, 0, "read")
78 
79  pi.write(GPIO, pigpio.HIGH)
80  v = pi.read(GPIO)
81  CHECK(1, 6, v, 1, 0, "write, read")
82 
83 t2_count=0
84 
85 def t2cbf(gpio, level, tick):
86  global t2_count
87  t2_count += 1
88 
89 def t2():
90 
91  global t2_count
92 
93  print("PWM dutycycle/range/frequency tests.")
94 
95  pi.set_PWM_range(GPIO, 255)
96  pi.set_PWM_frequency(GPIO,0)
97  f = pi.get_PWM_frequency(GPIO)
98  CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency")
99 
100  t2cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t2cbf)
101 
102  pi.set_PWM_dutycycle(GPIO, 0)
103  dc = pi.get_PWM_dutycycle(GPIO)
104  CHECK(2, 2, dc, 0, 0, "get PWM dutycycle")
105 
106  time.sleep(0.5) # allow old notifications to flush
107  oc = t2_count
108  time.sleep(2)
109  f = t2_count - oc
110  CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback")
111 
112  pi.set_PWM_dutycycle(GPIO, 128)
113  dc = pi.get_PWM_dutycycle(GPIO)
114  CHECK(2, 4, dc, 128, 0, "get PWM dutycycle")
115 
116  time.sleep(1)
117  oc = t2_count
118  time.sleep(2)
119  f = t2_count - oc
120  CHECK(2, 5, f, 40, 10, "set PWM dutycycle, callback")
121 
122  pi.set_PWM_frequency(GPIO,100)
123  f = pi.get_PWM_frequency(GPIO)
124  CHECK(2, 6, f, 100, 0, "set/get PWM frequency")
125 
126  time.sleep(1)
127  oc = t2_count
128  time.sleep(2)
129  f = t2_count - oc
130  CHECK(2, 7, f, 400, 5, "callback")
131 
132  pi.set_PWM_frequency(GPIO,1000)
133  f = pi.get_PWM_frequency(GPIO)
134  CHECK(2, 8, f, 1000, 0, "set/get PWM frequency")
135 
136  time.sleep(1)
137  oc = t2_count
138  time.sleep(2)
139  f = t2_count - oc
140  CHECK(2, 9, f, 4000, 5, "callback")
141 
142  r = pi.get_PWM_range(GPIO)
143  CHECK(2, 10, r, 255, 0, "get PWM range")
144 
145  rr = pi.get_PWM_real_range(GPIO)
146  CHECK(2, 11, rr, 200, 0, "get PWM real range")
147 
148  pi.set_PWM_range(GPIO, 2000)
149  r = pi.get_PWM_range(GPIO)
150  CHECK(2, 12, r, 2000, 0, "set/get PWM range")
151 
152  rr = pi.get_PWM_real_range(GPIO)
153  CHECK(2, 13, rr, 200, 0, "get PWM real range")
154 
155  pi.set_PWM_dutycycle(GPIO, 0)
156 
157  t2cb.cancel()
158 
159 t3_reset=True
160 t3_count=0
161 t3_tick=0
162 t3_on=0.0
163 t3_off=0.0
164 
165 def t3cbf(gpio, level, tick):
166  global t3_reset, t3_count, t3_tick, t3_on, t3_off
167 
168  if t3_reset:
169  t3_count = 0
170  t3_on = 0.0
171  t3_off = 0.0
172  t3_reset = False
173  else:
174  td = pigpio.tickDiff(t3_tick, tick)
175 
176  if level == 0:
177  t3_on += td
178  else:
179  t3_off += td
180 
181  t3_count += 1
182  t3_tick = tick
183 
184 def t3():
185 
186  global t3_reset, t3_count, t3_on, t3_off
187 
188  pw=[500.0, 1500.0, 2500.0]
189  dc=[0.2, 0.4, 0.6, 0.8]
190 
191  print("PWM/Servo pulse accuracy tests.")
192 
193  t3cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t3cbf)
194 
195  t = 0
196  for x in pw:
197  t += 1
198  pi.set_servo_pulsewidth(GPIO, x)
199  v = pi.get_servo_pulsewidth(GPIO)
200  CHECK(3, t, v, int(x), 0, "get servo pulsewidth")
201 
202  t += 1
203  time.sleep(1)
204  t3_reset = True
205  time.sleep(4)
206  c = t3_count
207  on = t3_on
208  off = t3_off
209  CHECK(3, t, int((1E3*(on+off))/on), int(2E7/x), 1, "set servo pulsewidth")
210 
211 
212  pi.set_servo_pulsewidth(GPIO, 0)
213  pi.set_PWM_frequency(GPIO, 1000)
214  f = pi.get_PWM_frequency(GPIO)
215  CHECK(3, 7, f, 1000, 0, "set/get PWM frequency")
216 
217  rr = pi.set_PWM_range(GPIO, 100)
218  CHECK(3, 8, rr, 200, 0, "set PWM range")
219 
220  t = 8
221  for x in dc:
222  t += 1
223  pi.set_PWM_dutycycle(GPIO, x*100)
224  v = pi.get_PWM_dutycycle(GPIO)
225  CHECK(3, t, v, int(x*100), 0, "get PWM dutycycle")
226 
227  t += 1
228  time.sleep(1)
229  t3_reset = True
230  time.sleep(2)
231  c = t3_count
232  on = t3_on
233  off = t3_off
234  CHECK(3, t, int((1E3*on)/(on+off)), int(1E3*x), 1, "set PWM dutycycle")
235 
236  pi.set_PWM_dutycycle(GPIO, 0)
237 
238  t3cb.cancel()
239 
240 def t4():
241 
242  print("Pipe notification tests.")
243 
244  pi.set_PWM_frequency(GPIO, 0)
245  pi.set_PWM_dutycycle(GPIO, 0)
246  pi.set_PWM_range(GPIO, 100)
247 
248  h = pi.notify_open()
249  e = pi.notify_begin(h, (1<<GPIO))
250  CHECK(4, 1, e, 0, 0, "notify open/begin")
251 
252  time.sleep(1)
253 
254  try:
255  f = open("/dev/pigpio"+ str(h), "rb")
256  except IOError:
257  f = None
258 
259  pi.set_PWM_dutycycle(GPIO, 50)
260  time.sleep(4)
261  pi.set_PWM_dutycycle(GPIO, 0)
262 
263  e = pi.notify_pause(h)
264  CHECK(4, 2, e, 0, 0, "notify pause")
265 
266  e = pi.notify_close(h)
267  CHECK(4, 3, e, 0, 0, "notify close")
268 
269  if f is not None:
270 
271  n = 0
272  s = 0
273 
274  seq_ok = 1
275  toggle_ok = 1
276 
277  while True:
278 
279  chunk = f.read(12)
280 
281  if len(chunk) == 12:
282 
283  S, fl, t, v = struct.unpack('HHII', chunk)
284  if s != S:
285  seq_ok = 0
286 
287  L = v & (1<<GPIO)
288 
289  if n:
290  if l != L:
291  toggle_ok = 0
292 
293  if L:
294  l = 0
295  else:
296  l = (1<<GPIO)
297 
298  s += 1
299  n += 1
300 
301  else:
302  break
303 
304  f.close()
305 
306  CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok")
307  CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok")
308  CHECK(4, 6, n, 80, 10, "number of notifications")
309 
310  else:
311 
312  CHECK(4, 4, 0, 0, 0, "NOT APPLICABLE")
313  CHECK(4, 5, 0, 0, 0, "NOT APPLICABLE")
314  CHECK(4, 6, 0, 0, 0, "NOT APPLICABLE")
315 
316 t5_count = 0
317 
318 def t5cbf(gpio, level, tick):
319  global t5_count
320  t5_count += 1
321 
322 def t5():
323  global t5_count
324 
325  BAUD=4800
326 
327  TEXT="""
328 Now is the winter of our discontent
329 Made glorious summer by this sun of York;
330 And all the clouds that lour'd upon our house
331 In the deep bosom of the ocean buried.
332 Now are our brows bound with victorious wreaths;
333 Our bruised arms hung up for monuments;
334 Our stern alarums changed to merry meetings,
335 Our dreadful marches to delightful measures.
336 Grim-visaged war hath smooth'd his wrinkled front;
337 And now, instead of mounting barded steeds
338 To fright the souls of fearful adversaries,
339 He capers nimbly in a lady's chamber
340 To the lascivious pleasing of a lute.
341 """
342 
343  print("Waveforms & bit bang serial read/write tests.")
344 
345  t5cb = pi.callback(GPIO, pigpio.FALLING_EDGE, t5cbf)
346 
347  pi.set_mode(GPIO, pigpio.OUTPUT)
348 
349  e = pi.wave_clear()
350  CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear")
351 
352  wf = []
353 
354  wf.append(pigpio.pulse(1<<GPIO, 0, 10000))
355  wf.append(pigpio.pulse(0, 1<<GPIO, 30000))
356  wf.append(pigpio.pulse(1<<GPIO, 0, 60000))
357  wf.append(pigpio.pulse(0, 1<<GPIO, 100000))
358 
359  e = pi.wave_add_generic(wf)
360  CHECK(5, 2, e, 4, 0, "pulse, wave add generic")
361 
362  wid = pi.wave_create()
363  e = pi.wave_send_repeat(wid)
364  if e < 14:
365  CHECK(5, 3, e, 9, 0, "wave send repeat")
366  else:
367  CHECK(5, 3, e, 19, 0, "wave send repeat")
368 
369  oc = t5_count
370  time.sleep(5)
371  c = t5_count - oc
372  CHECK(5, 4, c, 50, 1, "callback")
373 
374  e = pi.wave_tx_stop()
375  CHECK(5, 5, e, 0, 0, "wave tx stop")
376 
377  e = pi.bb_serial_read_open(GPIO, BAUD)
378  CHECK(5, 6, e, 0, 0, "serial read open")
379 
380  pi.wave_clear()
381  e = pi.wave_add_serial(GPIO, BAUD, TEXT, 5000000)
382  CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial")
383 
384  wid = pi.wave_create()
385  e = pi.wave_send_once(wid)
386  if e < 6964:
387  CHECK(5, 8, e, 6811, 0, "wave send once")
388  else:
389  CHECK(5, 8, e, 7116, 0, "wave send once")
390 
391  oc = t5_count
392  time.sleep(3)
393  c = t5_count - oc
394  CHECK(5, 9, c, 0, 0, "callback")
395 
396  oc = t5_count
397  while pi.wave_tx_busy():
398  time.sleep(0.2)
399  time.sleep(0.2)
400  c = t5_count - oc
401  CHECK(5, 10, c, 1702, 0, "wave tx busy, callback")
402 
403  c, text = pi.bb_serial_read(GPIO)
404  CHECK(5, 11, STRCMP(text, TEXT), True, 0, "wave tx busy, serial read");
405 
406  e = pi.bb_serial_read_close(GPIO)
407  CHECK(5, 12, e, 0, 0, "serial read close")
408 
409  c = pi.wave_get_micros()
410  CHECK(5, 13, c, 6158148, 0, "wave get micros")
411 
412  CHECK(5, 14, 0, 0, 0, "NOT APPLICABLE")
413 
414  c = pi.wave_get_max_micros()
415  CHECK(5, 15, c, 1800000000, 0, "wave get max micros")
416 
417  c = pi.wave_get_pulses()
418  CHECK(5, 16, c, 3405, 0, "wave get pulses")
419 
420  CHECK(5, 17, 0, 0, 0, "NOT APPLICABLE")
421 
422  c = pi.wave_get_max_pulses()
423  CHECK(5, 18, c, 12000, 0, "wave get max pulses")
424 
425  c = pi.wave_get_cbs()
426  if c < 6963:
427  CHECK(5, 19, c, 6810, 0, "wave get cbs")
428  else:
429  CHECK(5, 19, c, 7115, 0, "wave get cbs")
430 
431  CHECK(5, 20, 0, 0, 0, "NOT APPLICABLE")
432 
433  c = pi.wave_get_max_cbs()
434  CHECK(5, 21, c, 25016, 0, "wave get max cbs")
435 
436  e = pi.wave_clear()
437  CHECK(5, 22, e, 0, 0, "wave clear")
438 
439  e = pi.wave_add_generic(wf)
440  CHECK(5, 23, e, 4, 0, "pulse, wave add generic")
441 
442  w1 = pi.wave_create()
443  CHECK(5, 24, w1, 0, 0, "wave create")
444 
445  e = pi.wave_send_repeat(w1)
446  if e < 14:
447  CHECK(5, 25, e, 9, 0, "wave send repeat")
448  else:
449  CHECK(5, 25, e, 19, 0, "wave send repeat")
450 
451  oc = t5_count
452  time.sleep(5)
453  c = t5_count - oc
454  CHECK(5, 26, c, 50, 1, "callback")
455 
456  e = pi.wave_tx_stop()
457  CHECK(5, 27, e, 0, 0, "wave tx stop")
458 
459  e = pi.wave_add_serial(GPIO, BAUD, TEXT, 5000000)
460  CHECK(5, 28, e, 3405, 0, "wave add serial")
461 
462  w2 = pi.wave_create()
463  CHECK(5, 29, w2, 1, 0, "wave create")
464 
465  e = pi.wave_send_once(w2)
466  if e < 6964:
467  CHECK(5, 30, e, 6811, 0, "wave send once")
468  else:
469  CHECK(5, 30, e, 7116, 0, "wave send once")
470 
471  oc = t5_count
472  time.sleep(3)
473  c = t5_count - oc
474  CHECK(5, 31, c, 0, 0, "callback")
475 
476  oc = t5_count
477  while pi.wave_tx_busy():
478  time.sleep(0.2)
479  time.sleep(0.2)
480  c = t5_count - oc
481  CHECK(5, 32, c, 1702, 0, "wave tx busy, callback")
482 
483  e = pi.wave_delete(0)
484  CHECK(5, 33, e, 0, 0, "wave delete")
485 
486  t5cb.cancel()
487 
488 t6_count=0
489 t6_on=0
490 t6_on_tick=None
491 
492 def t6cbf(gpio, level, tick):
493  global t6_count, t6_on, t6_on_tick
494  if level == 1:
495  t6_on_tick = tick
496  t6_count += 1
497  else:
498  if t6_on_tick is not None:
499  t6_on += pigpio.tickDiff(t6_on_tick, tick)
500 
501 def t6():
502  global t6_count, t6_on
503 
504  print("Trigger tests.")
505 
506  pi.write(GPIO, pigpio.LOW)
507 
508  tp = 0
509 
510  t6cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t6cbf)
511 
512  for t in range(5):
513  time.sleep(0.1)
514  p = 10 + (t*10)
515  tp += p;
516  pi.gpio_trigger(GPIO, p, 1)
517 
518  time.sleep(0.5)
519 
520  CHECK(6, 1, t6_count, 5, 0, "gpio trigger count")
521 
522  CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length")
523 
524  t6cb.cancel()
525 
526 t7_count=0
527 
528 def t7cbf(gpio, level, tick):
529  global t7_count
530  if level == pigpio.TIMEOUT:
531  t7_count += 1
532 
533 def t7():
534  global t7_count
535 
536  print("Watchdog tests.")
537 
538  # type of edge shouldn't matter for watchdogs
539  t7cb = pi.callback(GPIO, pigpio.FALLING_EDGE, t7cbf)
540 
541  pi.set_watchdog(GPIO, 50) # 50 ms, 20 per second
542  time.sleep(0.5)
543  oc = t7_count
544  time.sleep(2)
545  c = t7_count - oc
546  CHECK(7, 1, c, 39, 5, "set watchdog on count")
547 
548  pi.set_watchdog(GPIO, 0) # 0 switches watchdog off
549  time.sleep(0.5)
550  oc = t7_count
551  time.sleep(2)
552  c = t7_count - oc
553  CHECK(7, 2, c, 0, 1, "set watchdog off count")
554 
555  t7cb.cancel()
556 
557 def t8():
558  print("Bank read/write tests.")
559 
560  pi.write(GPIO, 0)
561  v = pi.read_bank_1() & (1<<GPIO)
562  CHECK(8, 1, v, 0, 0, "read bank 1")
563 
564  pi.write(GPIO, 1)
565  v = pi.read_bank_1() & (1<<GPIO)
566  CHECK(8, 2, v, (1<<GPIO), 0, "read bank 1")
567 
568  pi.clear_bank_1(1<<GPIO)
569  v = pi.read(GPIO)
570  CHECK(8, 3, v, 0, 0, "clear bank 1")
571 
572  pi.set_bank_1(1<<GPIO)
573  v = pi.read(GPIO)
574  CHECK(8, 4, v, 1, 0, "set bank 1")
575 
576  v = pi.read_bank_2()
577 
578  if v:
579  v = 0
580  else:
581  v = 1
582 
583  CHECK(8, 5, v, 0, 0, "read bank 2")
584 
585  v = pi.clear_bank_2(0)
586  CHECK(8, 6, v, 0, 0, "clear bank 2")
587 
588  pigpio.exceptions = False
589  v = pi.clear_bank_2(0xffffff)
590  pigpio.exceptions = True
591  CHECK(8, 7, v, pigpio.PI_SOME_PERMITTED, 0, "clear bank 2")
592 
593  v = pi.set_bank_2(0)
594  CHECK(8, 8, v, 0, 0, "set bank 2")
595 
596  pigpio.exceptions = False
597  v = pi.set_bank_2(0xffffff)
598  pigpio.exceptions = True
599  CHECK(8, 9, v, pigpio.PI_SOME_PERMITTED, 0, "set bank 2")
600 
602  for check in range(10):
603  time.sleep(0.1)
604  e, p = pi.script_status(s)
605  if e != pigpio.PI_SCRIPT_HALTED:
606  return
607 
608 def t9():
609  print("Script store/run/status/stop/delete tests.")
610 
611  pi.write(GPIO, 0) # need known state
612 
613  # 100 loops per second
614  # p0 number of loops
615  # p1 GPIO
616  script="""
617  ld p9 p0
618  tag 0
619  w p1 1
620  mils 5
621  w p1 0
622  mils 5
623  dcr p9
624  jp 0"""
625 
626  t9cb = pi.callback(GPIO)
627 
628  old_exceptions = pigpio.exceptions
629 
630  pigpio.exceptions = False
631 
632  s = pi.store_script(script)
633 
634  # Ensure the script has finished initing.
635  while True:
636  e, p = pi.script_status(s)
637  if e != pigpio.PI_SCRIPT_INITING:
638  break
639  time.sleep(0.1)
640 
641  oc = t9cb.tally()
642  pi.run_script(s, [99, GPIO])
643 
644  t9waitNotHalted(s)
645 
646  while True:
647  e, p = pi.script_status(s)
648  if e != pigpio.PI_SCRIPT_RUNNING:
649  break
650  time.sleep(0.1)
651  time.sleep(0.2)
652  c = t9cb.tally() - oc
653  CHECK(9, 1, c, 100, 0, "store/run script")
654 
655  oc = t9cb.tally()
656  pi.run_script(s, [200, GPIO])
657 
658  t9waitNotHalted(s)
659 
660  while True:
661  e, p = pi.script_status(s)
662  if e != pigpio.PI_SCRIPT_RUNNING:
663  break
664  time.sleep(0.1)
665  time.sleep(0.2)
666  c = t9cb.tally() - oc
667  CHECK(9, 2, c, 201, 0, "run script/script status")
668 
669  oc = t9cb.tally()
670  pi.run_script(s, [2000, GPIO])
671 
672  t9waitNotHalted(s)
673 
674  while True:
675  e, p = pi.script_status(s)
676  if e != pigpio.PI_SCRIPT_RUNNING:
677  break
678  if p[9] < 1900:
679  pi.stop_script(s)
680  time.sleep(0.1)
681  time.sleep(0.2)
682  c = t9cb.tally() - oc
683  CHECK(9, 3, c, 110, 20, "run/stop script/script status")
684 
685  e = pi.delete_script(s)
686  CHECK(9, 4, e, 0, 0, "delete script")
687 
688  t9cb.cancel()
689 
690  pigpio.exceptions = old_exceptions
691 
692 def ta():
693  print("Serial link tests.")
694 
695  # this test needs RXD and TXD to be connected
696 
697  h = pi.serial_open("/dev/ttyAMA0", 57600)
698  CHECK(10, 1, h>=0, 1, 0, "serial open")
699 
700  (b, s) = pi.serial_read(h, 1000) # flush buffer
701 
702  b = pi.serial_data_available(h)
703  CHECK(10, 2, b, 0, 0, "serial data available")
704 
705  TEXT = """
706 To be, or not to be, that is the question-
707 Whether 'tis Nobler in the mind to suffer
708 The Slings and Arrows of outrageous Fortune,
709 Or to take Arms against a Sea of troubles,
710 """
711  e = pi.serial_write(h, TEXT)
712  CHECK(10, 3, e, 0, 0, "serial write")
713 
714  e = pi.serial_write_byte(h, 0xAA)
715  e = pi.serial_write_byte(h, 0x55)
716  e = pi.serial_write_byte(h, 0x00)
717  e = pi.serial_write_byte(h, 0xFF)
718 
719  CHECK(10, 4, e, 0, 0, "serial write byte")
720 
721  time.sleep(0.1) # allow time for transmission
722 
723  b = pi.serial_data_available(h)
724  CHECK(10, 5, b, len(TEXT)+4, 0, "serial data available")
725 
726  (b, text) = pi.serial_read(h, len(TEXT))
727  CHECK(10, 6, b, len(TEXT), 0, "serial read")
728  CHECK(10, 7, STRCMP(text, TEXT), True, 0, "serial read")
729 
730  b = pi.serial_read_byte(h)
731  CHECK(10, 8, b, 0xAA, 0, "serial read byte")
732 
733  b = pi.serial_read_byte(h)
734  CHECK(10, 9, b, 0x55, 0, "serial read byte")
735 
736  b = pi.serial_read_byte(h)
737  CHECK(10, 10, b, 0x00, 0, "serial read byte")
738 
739  b = pi.serial_read_byte(h)
740  CHECK(10, 11, b, 0xFF, 0, "serial read byte")
741 
742  b = pi.serial_data_available(h)
743  CHECK(10, 12, b, 0, 0, "serial data available")
744 
745  e = pi.serial_close(h)
746  CHECK(10, 13, e, 0, 0, "serial close")
747 
748 def tb():
749  print("SMBus / I2C tests.")
750 
751  # this test requires an ADXL345 on I2C bus 1 addr 0x53
752 
753  h = pi.i2c_open(1, 0x53)
754  CHECK(11, 1, h>=0, 1, 0, "i2c open")
755 
756  e = pi.i2c_write_device(h, "\x00") # move to known register
757  CHECK(11, 2, e, 0, 0, "i2c write device")
758 
759  (b, d) = pi.i2c_read_device(h, 1)
760  CHECK(11, 3, b, 1, 0, "i2c read device")
761  CHECK(11, 4, ord(d), 0xE5, 0, "i2c read device")
762 
763  b = pi.i2c_read_byte(h)
764  CHECK(11, 5, b, 0xE5, 0, "i2c read byte")
765 
766  b = pi.i2c_read_byte_data(h, 0)
767  CHECK(11, 6, b, 0xE5, 0, "i2c read byte data")
768 
769  b = pi.i2c_read_byte_data(h, 48)
770  CHECK(11, 7, b, 2, 0, "i2c read byte data")
771 
772  exp = b"[aB\x08cD\xAAgHj\xFD]"
773 
774  e = pi.i2c_write_device(h, b'\x1D'+ exp)
775  CHECK(11, 8, e, 0, 0, "i2c write device")
776 
777  e = pi.i2c_write_device(h, '\x1D')
778  (b, d) = pi.i2c_read_device(h, 12)
779  CHECK(11, 9, b, 12, 0, "i2c read device")
780  CHECK(11, 10, STRCMP(d, exp), True, 0, "i2c read device")
781 
782  e = pi.i2c_write_byte_data(h, 0x1d, 0xAA)
783  CHECK(11, 11, e, 0, 0, "i2c write byte data")
784 
785  b = pi.i2c_read_byte_data(h, 0x1d)
786  CHECK(11, 12, b, 0xAA, 0, "i2c read byte data")
787 
788  e = pi.i2c_write_byte_data(h, 0x1d, 0x55)
789  CHECK(11, 13, e, 0, 0, "i2c write byte data")
790 
791  b = pi.i2c_read_byte_data(h, 0x1d)
792  CHECK(11, 14, b, 0x55, 0, "i2c read byte data")
793 
794  exp = "[1234567890#]"
795 
796  e = pi.i2c_write_block_data(h, 0x1C, exp)
797  CHECK(11, 15, e, 0, 0, "i2c write block data")
798 
799  e = pi.i2c_write_device(h, '\x1D')
800  (b, d) = pi.i2c_read_device(h, 13)
801  CHECK(11, 16, b, 13, 0, "i2c read device")
802  CHECK(11, 17, STRCMP(d, exp), True, 0, "i2c read device")
803 
804  (b, d) = pi.i2c_read_i2c_block_data(h, 0x1D, 13)
805  CHECK(11, 18, b, 13, 0, "i2c read i2c block data")
806  CHECK(11, 19, STRCMP(d, exp), True, 0, "i2c read i2c block data")
807 
808  expl = [0x01, 0x05, 0x06, 0x07, 0x09, 0x1B, 0x99, 0xAA, 0xBB, 0xCC]
809  exp = "\x01\x05\x06\x07\x09\x1B\x99\xAA\xBB\xCC"
810 
811  e = pi.i2c_write_i2c_block_data(h, 0x1D, expl)
812  CHECK(11, 20, e, 0, 0, "i2c write i2c block data")
813 
814  (b, d) = pi.i2c_read_i2c_block_data(h, 0x1D, 10)
815  CHECK(11, 21, b, 10, 0, "i2c read i2c block data")
816  CHECK(11, 22, STRCMP(d, exp), True, 0, "i2c read i2c block data")
817 
818  e = pi.i2c_close(h)
819  CHECK(11, 23, e, 0, 0, "i2c close")
820 
821 def tca(b, d):
822  if b == 3:
823  c1 = d[1] & 0x0F
824  c2 = d[2]
825  time.sleep(1.0)
826  print((c1*256)+c2)
827 
828 def tc():
829  print("SPI tests.")
830 
831  # this test requires a MCP3202 on SPI channel 1
832 
833  h = pi.spi_open(1, 50000)
834  CHECK(12, 1, h>=0, 1, 0, "spi open")
835 
836  (b, d) = pi.spi_xfer(h, [1,128,0])
837  CHECK(12, 2, b, 3, 0, "spi xfer")
838  tca(b, d)
839 
840  (b, d) = pi.spi_xfer(h, "\x01\x80\x00")
841  CHECK(12, 2, b, 3, 0, "spi xfer")
842  tca(b, d)
843 
844  (b, d) = pi.spi_xfer(h, b"\x01\x80\x00")
845  CHECK(12, 2, b, 3, 0, "spi xfer")
846  tca(b, d)
847 
848  (b, d) = pi.spi_xfer(h, '\x01\x80\x00')
849  CHECK(12, 2, b, 3, 0, "spi xfer")
850  tca(b, d)
851 
852  (b, d) = pi.spi_xfer(h, b'\x01\x80\x00')
853  CHECK(12, 2, b, 3, 0, "spi xfer")
854  tca(b, d)
855 
856  e = pi.spi_close(h)
857  CHECK(12, 99, e, 0, 0, "spi close")
858 
859 def td():
860 
861  print("Wavechains & filter tests.")
862 
863  tdcb = pi.callback(GPIO)
864 
865  pi.set_mode(GPIO, pigpio.OUTPUT)
866 
867  pi.write(GPIO, pigpio.LOW)
868 
869  e = pi.wave_clear()
870  CHECK(13, 1, e, 0, 0, "callback, set mode, wave clear")
871 
872  wf = []
873 
874  wf.append(pigpio.pulse(1<<GPIO, 0, 50))
875  wf.append(pigpio.pulse(0, 1<<GPIO, 70))
876  wf.append(pigpio.pulse(1<<GPIO, 0, 130))
877  wf.append(pigpio.pulse(0, 1<<GPIO, 150))
878  wf.append(pigpio.pulse(1<<GPIO, 0, 90))
879  wf.append(pigpio.pulse(0, 1<<GPIO, 110))
880 
881  e = pi.wave_add_generic(wf)
882  CHECK(13, 2, e, 6, 0, "pulse, wave add generic")
883 
884  wid = pi.wave_create()
885 
886  chain = [
887  255, 0, wid, 255, 1, 128, 0, 255, 2, 0, 8,
888  255, 0, wid, 255, 1, 0, 1, 255, 2, 0, 4,
889  255, 0, wid, 255, 1, 0, 2]
890 
891  e = pi.set_glitch_filter(GPIO, 0)
892  CHECK(13, 3, e, 0, 0, "clear glitch filter")
893 
894  e = pi.set_noise_filter(GPIO, 0, 0)
895  CHECK(13, 4, e, 0, 0, "clear noise filter")
896 
897  tdcb.reset_tally()
898  e = pi.wave_chain(chain)
899  CHECK(13, 5, e, 0, 0, "wave chain")
900  while pi.wave_tx_busy():
901  time.sleep(0.1)
902  time.sleep(0.3)
903  tally = tdcb.tally()
904  CHECK(13, 6, tally, 2688, 2, "wave chain, tally")
905 
906  pi.set_glitch_filter(GPIO, 80)
907  tdcb.reset_tally()
908  pi.wave_chain(chain)
909  while pi.wave_tx_busy():
910  time.sleep(0.1)
911  time.sleep(0.3)
912  tally = tdcb.tally()
913  CHECK(13, 7, tally, 1792, 2, "glitch filter, wave chain, tally")
914 
915  pi.set_glitch_filter(GPIO, 120)
916  tdcb.reset_tally()
917  pi.wave_chain(chain)
918  while pi.wave_tx_busy():
919  time.sleep(0.1)
920  time.sleep(0.2)
921  tally = tdcb.tally()
922  CHECK(13, 8, tally, 896, 2, "glitch filter, wave chain, tally")
923 
924  pi.set_glitch_filter(GPIO, 140)
925  tdcb.reset_tally()
926  pi.wave_chain(chain)
927  while pi.wave_tx_busy():
928  time.sleep(0.1)
929  time.sleep(0.2)
930  tally = tdcb.tally()
931  CHECK(13, 9, tally, 0, 0, "glitch filter, wave chain, tally")
932 
933  pi.set_glitch_filter(GPIO, 0)
934 
935  pi.wave_chain(chain)
936  pi.set_noise_filter(GPIO, 1000, 150000)
937  tdcb.reset_tally()
938  while pi.wave_tx_busy():
939  time.sleep(0.1)
940  time.sleep(0.2)
941  tally = tdcb.tally()
942  CHECK(13, 10, tally, 1500, 2, "noise filter, wave chain, tally")
943 
944  pi.wave_chain(chain)
945  pi.set_noise_filter(GPIO, 2000, 150000)
946  tdcb.reset_tally()
947  while pi.wave_tx_busy():
948  time.sleep(0.1)
949  time.sleep(0.2)
950  tally = tdcb.tally()
951  CHECK(13, 11, tally, 750, 2, "noise filter, wave chain, tally")
952 
953  pi.wave_chain(chain)
954  pi.set_noise_filter(GPIO, 3000, 5000)
955  tdcb.reset_tally()
956  while pi.wave_tx_busy():
957  time.sleep(0.1)
958  time.sleep(0.2)
959  tally = tdcb.tally()
960  CHECK(13, 12, tally, 0, 2, "noise filter, wave chain, tally")
961 
962  pi.set_noise_filter(GPIO, 0, 0)
963 
964  e = pi.wave_delete(wid)
965  CHECK(13, 13, e, 0, 0, "wave delete")
966 
967  tdcb.cancel()
968 
969 if len(sys.argv) > 1:
970  tests = ""
971  for C in sys.argv[1]:
972  c = C.lower()
973  if c not in tests:
974  tests += c
975 
976 else:
977  tests = "0123456789d"
978 
979 pi = pigpio.pi()
980 
981 if pi.connected:
982  print("Connected to pigpio daemon.")
983 
984  if '0' in tests: t0()
985  if '1' in tests: t1()
986  if '2' in tests: t2()
987  if '3' in tests: t3()
988  if '4' in tests: t4()
989  if '5' in tests: t5()
990  if '6' in tests: t6()
991  if '7' in tests: t7()
992  if '8' in tests: t8()
993  if '9' in tests: t9()
994  if 'a' in tests: ta()
995  if 'b' in tests: tb()
996  if 'c' in tests: tc()
997  if 'd' in tests: td()
998 
999 pi.stop()
1000 
def t3()
Definition: x_pigpio.py:184
def tb()
Definition: x_pigpio.py:748
def t7cbf(gpio, level, tick)
Definition: x_pigpio.py:528
def t4()
Definition: x_pigpio.py:240
def t9waitNotHalted(s)
Definition: x_pigpio.py:601
def CHECK(t, st, got, expect, pc, desc)
Definition: x_pigpio.py:39
def t6()
Definition: x_pigpio.py:501
def tc()
Definition: x_pigpio.py:828
def t0()
Definition: x_pigpio.py:46
def STRCMP(r, s)
Definition: x_pigpio.py:22
def ta()
Definition: x_pigpio.py:692
def tca(b, d)
Definition: x_pigpio.py:821
def t2()
Definition: x_pigpio.py:89
def t3cbf(gpio, level, tick)
Definition: x_pigpio.py:165
def t5()
Definition: x_pigpio.py:322
def t5cbf(gpio, level, tick)
Definition: x_pigpio.py:318
def tickDiff(t1, t2)
Definition: pigpio.py:911
def t7()
Definition: x_pigpio.py:533
def t9()
Definition: x_pigpio.py:608
def td()
Definition: x_pigpio.py:859
def t8()
Definition: x_pigpio.py:557
def t2cbf(gpio, level, tick)
Definition: x_pigpio.py:85
def t1()
Definition: x_pigpio.py:56
def t6cbf(gpio, level, tick)
Definition: x_pigpio.py:492


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