x_pigpio.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #*** WARNING ************************************************
00004 #*                                                          *
00005 #* All the tests make extensive use of gpio 25 (pin 22).    *
00006 #* Ensure that either nothing or just a LED is connected to *
00007 #* gpio 25 before running any of the tests.                 *
00008 #*                                                          *
00009 #* Some tests are statistical in nature and so may on       *
00010 #* occasion fail.  Repeated failures on the same test or    *
00011 #* many failures in a group of tests indicate a problem.    *
00012 #************************************************************
00013 
00014 import sys
00015 import time
00016 import struct
00017 
00018 import pigpio
00019 
00020 GPIO=25
00021 
00022 def STRCMP(r, s):
00023 
00024    if sys.hexversion > 0x03000000:
00025 
00026       if type(r) == type(""):
00027          r = bytearray(r, 'latin-1')
00028 
00029       if type(s) == type(""):
00030          s = bytearray(s, 'latin-1')
00031 
00032    if r != s:
00033       print(r, s)
00034       return 0
00035 
00036    else:
00037       return 1
00038 
00039 def CHECK(t, st, got, expect, pc, desc):
00040    if got >= (((1E2-pc)*expect)/1E2) and got <= (((1E2+pc)*expect)/1E2):
00041       print("TEST {:2d}.{:<2d} PASS ({}: {:d})".format(t, st, desc, expect))
00042    else:
00043       print("TEST {:2d}.{:<2d} FAILED got {:d} ({}: {:d})".
00044          format(t, st, got, desc, expect))
00045 
00046 def t0():
00047 
00048    print("\nTesting pigpio Python module {}".format(pigpio.VERSION))
00049 
00050    print("Python {}".format(sys.version.replace("\n", " ")))
00051 
00052    print("pigpio version {}.".format(pi.get_pigpio_version()))
00053 
00054    print("Hardware revision {}.".format(pi.get_hardware_revision()))
00055 
00056 def t1():
00057 
00058    print("Mode/PUD/read/write tests.")
00059 
00060    pi.set_mode(GPIO, pigpio.INPUT)
00061    v = pi.get_mode(GPIO)
00062    CHECK(1, 1, v, 0, 0, "set mode, get mode")
00063 
00064    pi.set_pull_up_down(GPIO, pigpio.PUD_UP)
00065    v = pi.read(GPIO)
00066    CHECK(1, 2, v, 1, 0, "set pull up down, read")
00067 
00068    pi.set_pull_up_down(GPIO, pigpio.PUD_DOWN)
00069    v = pi.read(GPIO)
00070    CHECK(1, 3, v, 0, 0, "set pull up down, read")
00071 
00072    pi.write(GPIO, pigpio.LOW)
00073    v = pi.get_mode(GPIO)
00074    CHECK(1, 4, v, 1, 0, "write, get mode")
00075 
00076    v = pi.read(GPIO)
00077    CHECK(1, 5, v, 0, 0, "read")
00078 
00079    pi.write(GPIO, pigpio.HIGH)
00080    v = pi.read(GPIO)
00081    CHECK(1, 6, v, 1, 0, "write, read")
00082 
00083 t2_count=0
00084 
00085 def t2cbf(gpio, level, tick):
00086    global t2_count
00087    t2_count += 1
00088 
00089 def t2():
00090 
00091    global t2_count
00092 
00093    print("PWM dutycycle/range/frequency tests.")
00094 
00095    pi.set_PWM_range(GPIO, 255)
00096    pi.set_PWM_frequency(GPIO,0)
00097    f = pi.get_PWM_frequency(GPIO)
00098    CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency")
00099 
00100    t2cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t2cbf)
00101 
00102    pi.set_PWM_dutycycle(GPIO, 0)
00103    dc = pi.get_PWM_dutycycle(GPIO)
00104    CHECK(2, 2, dc, 0, 0, "get PWM dutycycle")
00105 
00106    time.sleep(0.5) # allow old notifications to flush
00107    oc = t2_count
00108    time.sleep(2)
00109    f = t2_count - oc
00110    CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback")
00111 
00112    pi.set_PWM_dutycycle(GPIO, 128)
00113    dc = pi.get_PWM_dutycycle(GPIO)
00114    CHECK(2, 4, dc, 128, 0, "get PWM dutycycle")
00115 
00116    time.sleep(1)
00117    oc = t2_count
00118    time.sleep(2)
00119    f = t2_count - oc
00120    CHECK(2, 5, f, 40, 10, "set PWM dutycycle, callback")
00121 
00122    pi.set_PWM_frequency(GPIO,100)
00123    f = pi.get_PWM_frequency(GPIO)
00124    CHECK(2, 6, f, 100, 0, "set/get PWM frequency")
00125 
00126    time.sleep(1)
00127    oc = t2_count
00128    time.sleep(2)
00129    f = t2_count - oc
00130    CHECK(2, 7, f, 400, 5, "callback")
00131 
00132    pi.set_PWM_frequency(GPIO,1000)
00133    f = pi.get_PWM_frequency(GPIO)
00134    CHECK(2, 8, f, 1000, 0, "set/get PWM frequency")
00135 
00136    time.sleep(1)
00137    oc = t2_count
00138    time.sleep(2)
00139    f = t2_count - oc
00140    CHECK(2, 9, f, 4000, 5, "callback")
00141 
00142    r = pi.get_PWM_range(GPIO)
00143    CHECK(2, 10, r, 255, 0, "get PWM range")
00144 
00145    rr = pi.get_PWM_real_range(GPIO)
00146    CHECK(2, 11, rr, 200, 0, "get PWM real range")
00147 
00148    pi.set_PWM_range(GPIO, 2000)
00149    r = pi.get_PWM_range(GPIO)
00150    CHECK(2, 12, r, 2000, 0, "set/get PWM range")
00151 
00152    rr = pi.get_PWM_real_range(GPIO)
00153    CHECK(2, 13, rr, 200, 0, "get PWM real range")
00154 
00155    pi.set_PWM_dutycycle(GPIO, 0)
00156 
00157    t2cb.cancel()
00158 
00159 t3_reset=True
00160 t3_count=0
00161 t3_tick=0
00162 t3_on=0.0
00163 t3_off=0.0
00164 
00165 def t3cbf(gpio, level, tick):
00166    global t3_reset, t3_count, t3_tick, t3_on, t3_off
00167 
00168    if t3_reset:
00169       t3_count = 0
00170       t3_on = 0.0
00171       t3_off = 0.0
00172       t3_reset = False
00173    else:
00174       td = pigpio.tickDiff(t3_tick, tick)
00175 
00176       if level == 0:
00177          t3_on += td
00178       else:
00179          t3_off += td
00180 
00181    t3_count += 1
00182    t3_tick = tick
00183 
00184 def t3():
00185 
00186    global t3_reset, t3_count, t3_on, t3_off
00187 
00188    pw=[500.0, 1500.0, 2500.0]
00189    dc=[0.2, 0.4, 0.6, 0.8]
00190 
00191    print("PWM/Servo pulse accuracy tests.")
00192 
00193    t3cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t3cbf)
00194 
00195    t = 0
00196    for x in pw:
00197       t += 1
00198       pi.set_servo_pulsewidth(GPIO, x)
00199       v = pi.get_servo_pulsewidth(GPIO)
00200       CHECK(3, t, v, int(x), 0, "get servo pulsewidth")
00201 
00202       t += 1
00203       time.sleep(1)
00204       t3_reset = True
00205       time.sleep(4)
00206       c = t3_count
00207       on = t3_on
00208       off = t3_off
00209       CHECK(3, t, int((1E3*(on+off))/on), int(2E7/x), 1, "set servo pulsewidth")
00210 
00211 
00212    pi.set_servo_pulsewidth(GPIO, 0)
00213    pi.set_PWM_frequency(GPIO, 1000)
00214    f = pi.get_PWM_frequency(GPIO)
00215    CHECK(3, 7, f, 1000, 0, "set/get PWM frequency")
00216 
00217    rr = pi.set_PWM_range(GPIO, 100)
00218    CHECK(3, 8, rr, 200, 0, "set PWM range")
00219 
00220    t = 8
00221    for x in dc:
00222       t += 1
00223       pi.set_PWM_dutycycle(GPIO, x*100)
00224       v = pi.get_PWM_dutycycle(GPIO)
00225       CHECK(3, t, v, int(x*100), 0, "get PWM dutycycle")
00226 
00227       t += 1
00228       time.sleep(1)
00229       t3_reset = True
00230       time.sleep(2)
00231       c = t3_count
00232       on = t3_on
00233       off = t3_off
00234       CHECK(3, t, int((1E3*on)/(on+off)), int(1E3*x), 1, "set PWM dutycycle")
00235 
00236    pi.set_PWM_dutycycle(GPIO, 0)
00237 
00238    t3cb.cancel()
00239 
00240 def t4():
00241 
00242    print("Pipe notification tests.")
00243 
00244    pi.set_PWM_frequency(GPIO, 0)
00245    pi.set_PWM_dutycycle(GPIO, 0)
00246    pi.set_PWM_range(GPIO, 100)
00247 
00248    h = pi.notify_open()
00249    e = pi.notify_begin(h, (1<<GPIO))
00250    CHECK(4, 1, e, 0, 0, "notify open/begin")
00251 
00252    time.sleep(1)
00253 
00254    try:
00255       f = open("/dev/pigpio"+ str(h), "rb")
00256    except IOError:
00257       f = None
00258 
00259    pi.set_PWM_dutycycle(GPIO, 50)
00260    time.sleep(4)
00261    pi.set_PWM_dutycycle(GPIO, 0)
00262 
00263    e = pi.notify_pause(h)
00264    CHECK(4, 2, e, 0, 0, "notify pause")
00265 
00266    e = pi.notify_close(h)
00267    CHECK(4, 3, e, 0, 0, "notify close")
00268 
00269    if f is not None:
00270 
00271       n = 0
00272       s = 0
00273 
00274       seq_ok = 1
00275       toggle_ok = 1
00276 
00277       while True:
00278 
00279          chunk = f.read(12)
00280 
00281          if len(chunk) == 12:
00282 
00283             S, fl, t, v = struct.unpack('HHII', chunk)
00284             if s != S:
00285                seq_ok = 0
00286 
00287             L = v & (1<<GPIO)
00288 
00289             if n:
00290                if l != L:
00291                   toggle_ok = 0
00292 
00293             if L:
00294                l = 0
00295             else:
00296                l = (1<<GPIO)
00297            
00298             s += 1
00299             n += 1
00300 
00301          else:
00302             break
00303 
00304       f.close()
00305 
00306       CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok")
00307       CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok")
00308       CHECK(4, 6, n, 80, 10, "number of notifications")
00309 
00310    else:
00311 
00312       CHECK(4, 4, 0, 0, 0, "NOT APPLICABLE")
00313       CHECK(4, 5, 0, 0, 0, "NOT APPLICABLE")
00314       CHECK(4, 6, 0, 0, 0, "NOT APPLICABLE")
00315 
00316 t5_count = 0
00317 
00318 def t5cbf(gpio, level, tick):
00319    global t5_count
00320    t5_count += 1
00321 
00322 def t5():
00323    global t5_count
00324 
00325    BAUD=4800
00326 
00327    TEXT="""
00328 Now is the winter of our discontent
00329 Made glorious summer by this sun of York;
00330 And all the clouds that lour'd upon our house
00331 In the deep bosom of the ocean buried.
00332 Now are our brows bound with victorious wreaths;
00333 Our bruised arms hung up for monuments;
00334 Our stern alarums changed to merry meetings,
00335 Our dreadful marches to delightful measures.
00336 Grim-visaged war hath smooth'd his wrinkled front;
00337 And now, instead of mounting barded steeds
00338 To fright the souls of fearful adversaries,
00339 He capers nimbly in a lady's chamber
00340 To the lascivious pleasing of a lute.
00341 """
00342 
00343    print("Waveforms & bit bang serial read/write tests.")
00344 
00345    t5cb = pi.callback(GPIO, pigpio.FALLING_EDGE, t5cbf)
00346 
00347    pi.set_mode(GPIO, pigpio.OUTPUT)
00348 
00349    e = pi.wave_clear()
00350    CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear")
00351 
00352    wf = []
00353 
00354    wf.append(pigpio.pulse(1<<GPIO, 0,  10000))
00355    wf.append(pigpio.pulse(0, 1<<GPIO,  30000))
00356    wf.append(pigpio.pulse(1<<GPIO, 0,  60000))
00357    wf.append(pigpio.pulse(0, 1<<GPIO, 100000))
00358 
00359    e = pi.wave_add_generic(wf)
00360    CHECK(5, 2, e, 4, 0, "pulse, wave add generic")
00361 
00362    wid = pi.wave_create()
00363    e = pi.wave_send_repeat(wid)
00364    if e < 14:
00365       CHECK(5, 3, e, 9, 0, "wave send repeat")
00366    else:
00367       CHECK(5, 3, e, 19, 0, "wave send repeat")
00368 
00369    oc = t5_count
00370    time.sleep(5)
00371    c = t5_count - oc
00372    CHECK(5, 4, c, 50, 1, "callback")
00373 
00374    e = pi.wave_tx_stop()
00375    CHECK(5, 5, e, 0, 0, "wave tx stop")
00376 
00377    e = pi.bb_serial_read_open(GPIO, BAUD)
00378    CHECK(5, 6, e, 0, 0, "serial read open")
00379 
00380    pi.wave_clear()
00381    e = pi.wave_add_serial(GPIO, BAUD, TEXT, 5000000)
00382    CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial")
00383 
00384    wid = pi.wave_create()
00385    e = pi.wave_send_once(wid)
00386    if e < 6964:
00387       CHECK(5, 8, e, 6811, 0, "wave send once")
00388    else:
00389       CHECK(5, 8, e, 7116, 0, "wave send once")
00390 
00391    oc = t5_count
00392    time.sleep(3)
00393    c = t5_count - oc
00394    CHECK(5, 9, c, 0, 0, "callback")
00395 
00396    oc = t5_count
00397    while pi.wave_tx_busy():
00398       time.sleep(0.2)
00399    time.sleep(0.2)
00400    c = t5_count - oc
00401    CHECK(5, 10, c, 1702, 0, "wave tx busy, callback")
00402 
00403    c, text = pi.bb_serial_read(GPIO)
00404    CHECK(5, 11, STRCMP(text, TEXT), True, 0, "wave tx busy, serial read");
00405 
00406    e = pi.bb_serial_read_close(GPIO)
00407    CHECK(5, 12, e, 0, 0, "serial read close")
00408 
00409    c = pi.wave_get_micros()
00410    CHECK(5, 13, c, 6158148, 0, "wave get micros")
00411 
00412    CHECK(5, 14, 0, 0, 0, "NOT APPLICABLE")
00413 
00414    c = pi.wave_get_max_micros()
00415    CHECK(5, 15, c, 1800000000, 0, "wave get max micros")
00416 
00417    c = pi.wave_get_pulses()
00418    CHECK(5, 16, c, 3405, 0, "wave get pulses")
00419 
00420    CHECK(5, 17, 0, 0, 0, "NOT APPLICABLE")
00421 
00422    c = pi.wave_get_max_pulses()
00423    CHECK(5, 18, c, 12000, 0, "wave get max pulses")
00424 
00425    c = pi.wave_get_cbs()
00426    if c < 6963:
00427       CHECK(5, 19, c, 6810, 0, "wave get cbs")
00428    else:
00429       CHECK(5, 19, c, 7115, 0, "wave get cbs")
00430 
00431    CHECK(5, 20, 0, 0, 0, "NOT APPLICABLE")
00432 
00433    c = pi.wave_get_max_cbs()
00434    CHECK(5, 21, c, 25016, 0, "wave get max cbs")
00435 
00436    e = pi.wave_clear()
00437    CHECK(5, 22, e, 0, 0, "wave clear")
00438 
00439    e = pi.wave_add_generic(wf)
00440    CHECK(5, 23, e, 4, 0, "pulse, wave add generic")
00441 
00442    w1 = pi.wave_create()
00443    CHECK(5, 24, w1, 0, 0, "wave create")
00444 
00445    e = pi.wave_send_repeat(w1)
00446    if e < 14:
00447       CHECK(5, 25, e, 9, 0, "wave send repeat")
00448    else:
00449       CHECK(5, 25, e, 19, 0, "wave send repeat")
00450 
00451    oc = t5_count
00452    time.sleep(5)
00453    c = t5_count - oc
00454    CHECK(5, 26, c, 50, 1, "callback")
00455 
00456    e = pi.wave_tx_stop()
00457    CHECK(5, 27, e, 0, 0, "wave tx stop")
00458 
00459    e = pi.wave_add_serial(GPIO, BAUD, TEXT, 5000000)
00460    CHECK(5, 28, e, 3405, 0, "wave add serial")
00461 
00462    w2 = pi.wave_create()
00463    CHECK(5, 29, w2, 1, 0, "wave create")
00464 
00465    e = pi.wave_send_once(w2)
00466    if e < 6964:
00467       CHECK(5, 30, e, 6811, 0, "wave send once")
00468    else:
00469       CHECK(5, 30, e, 7116, 0, "wave send once")
00470 
00471    oc = t5_count
00472    time.sleep(3)
00473    c = t5_count - oc
00474    CHECK(5, 31, c, 0, 0, "callback")
00475 
00476    oc = t5_count
00477    while pi.wave_tx_busy():
00478       time.sleep(0.2)
00479    time.sleep(0.2)
00480    c = t5_count - oc
00481    CHECK(5, 32, c, 1702, 0, "wave tx busy, callback")
00482 
00483    e = pi.wave_delete(0)
00484    CHECK(5, 33, e, 0, 0, "wave delete")
00485 
00486    t5cb.cancel()
00487 
00488 t6_count=0
00489 t6_on=0
00490 t6_on_tick=None
00491 
00492 def t6cbf(gpio, level, tick):
00493    global t6_count, t6_on, t6_on_tick
00494    if level == 1:
00495       t6_on_tick = tick
00496       t6_count += 1
00497    else:
00498       if t6_on_tick is not None:
00499          t6_on += pigpio.tickDiff(t6_on_tick, tick)
00500 
00501 def t6():
00502    global t6_count, t6_on
00503 
00504    print("Trigger tests.")
00505 
00506    pi.write(GPIO, pigpio.LOW)
00507 
00508    tp = 0
00509 
00510    t6cb = pi.callback(GPIO, pigpio.EITHER_EDGE, t6cbf)
00511 
00512    for t in range(5):
00513       time.sleep(0.1)
00514       p = 10 + (t*10)
00515       tp += p;
00516       pi.gpio_trigger(GPIO, p, 1)
00517 
00518    time.sleep(0.5)
00519 
00520    CHECK(6, 1, t6_count, 5, 0, "gpio trigger count")
00521 
00522    CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length")
00523 
00524    t6cb.cancel()
00525 
00526 t7_count=0
00527 
00528 def t7cbf(gpio, level, tick):
00529    global t7_count
00530    if level == pigpio.TIMEOUT:
00531       t7_count += 1
00532 
00533 def t7():
00534    global t7_count
00535 
00536    print("Watchdog tests.")
00537 
00538    # type of edge shouldn't matter for watchdogs
00539    t7cb = pi.callback(GPIO, pigpio.FALLING_EDGE, t7cbf)
00540 
00541    pi.set_watchdog(GPIO, 50) # 50 ms, 20 per second
00542    time.sleep(0.5)
00543    oc = t7_count
00544    time.sleep(2)
00545    c = t7_count - oc
00546    CHECK(7, 1, c, 39, 5, "set watchdog on count")
00547 
00548    pi.set_watchdog(GPIO, 0) # 0 switches watchdog off
00549    time.sleep(0.5)
00550    oc = t7_count
00551    time.sleep(2)
00552    c = t7_count - oc
00553    CHECK(7, 2, c, 0, 1, "set watchdog off count")
00554 
00555    t7cb.cancel()
00556 
00557 def t8():
00558    print("Bank read/write tests.")
00559 
00560    pi.write(GPIO, 0)
00561    v = pi.read_bank_1() & (1<<GPIO)
00562    CHECK(8, 1, v, 0, 0, "read bank 1")
00563 
00564    pi.write(GPIO, 1)
00565    v = pi.read_bank_1() & (1<<GPIO)
00566    CHECK(8, 2, v, (1<<GPIO), 0, "read bank 1")
00567 
00568    pi.clear_bank_1(1<<GPIO)
00569    v = pi.read(GPIO)
00570    CHECK(8, 3, v, 0, 0, "clear bank 1")
00571 
00572    pi.set_bank_1(1<<GPIO)
00573    v = pi.read(GPIO)
00574    CHECK(8, 4, v, 1, 0, "set bank 1")
00575 
00576    v = pi.read_bank_2()
00577 
00578    if v:
00579       v = 0
00580    else:
00581       v = 1
00582 
00583    CHECK(8, 5, v, 0, 0, "read bank 2")
00584 
00585    v = pi.clear_bank_2(0)
00586    CHECK(8, 6, v, 0, 0, "clear bank 2")
00587 
00588    pigpio.exceptions = False
00589    v = pi.clear_bank_2(0xffffff)
00590    pigpio.exceptions = True
00591    CHECK(8, 7, v, pigpio.PI_SOME_PERMITTED, 0, "clear bank 2")
00592 
00593    v = pi.set_bank_2(0)
00594    CHECK(8, 8, v, 0, 0, "set bank 2")
00595 
00596    pigpio.exceptions = False
00597    v = pi.set_bank_2(0xffffff)
00598    pigpio.exceptions = True
00599    CHECK(8, 9, v, pigpio.PI_SOME_PERMITTED, 0, "set bank 2")
00600 
00601 def t9waitNotHalted(s):
00602    for check in range(10):
00603       time.sleep(0.1)
00604       e, p = pi.script_status(s)
00605       if e != pigpio.PI_SCRIPT_HALTED:
00606          return
00607 
00608 def t9():
00609    print("Script store/run/status/stop/delete tests.")
00610 
00611    pi.write(GPIO, 0) # need known state
00612 
00613    # 100 loops per second
00614    # p0 number of loops
00615    # p1 GPIO
00616    script="""
00617    ld p9 p0
00618    tag 0
00619    w p1 1
00620    mils 5
00621    w p1 0
00622    mils 5
00623    dcr p9
00624    jp 0"""
00625 
00626    t9cb = pi.callback(GPIO)
00627 
00628    old_exceptions = pigpio.exceptions
00629 
00630    pigpio.exceptions = False
00631 
00632    s = pi.store_script(script)
00633 
00634    # Ensure the script has finished initing.
00635    while True:
00636       e, p = pi.script_status(s)
00637       if e != pigpio.PI_SCRIPT_INITING:
00638          break
00639       time.sleep(0.1)
00640 
00641    oc = t9cb.tally()
00642    pi.run_script(s, [99, GPIO])
00643 
00644    t9waitNotHalted(s)
00645 
00646    while True:
00647       e, p = pi.script_status(s)
00648       if e != pigpio.PI_SCRIPT_RUNNING:
00649          break
00650       time.sleep(0.1)
00651    time.sleep(0.2)
00652    c = t9cb.tally() - oc
00653    CHECK(9, 1, c, 100, 0, "store/run script")
00654 
00655    oc = t9cb.tally()
00656    pi.run_script(s, [200, GPIO])
00657 
00658    t9waitNotHalted(s)
00659 
00660    while True:
00661       e, p = pi.script_status(s)
00662       if e != pigpio.PI_SCRIPT_RUNNING:
00663          break
00664       time.sleep(0.1)
00665    time.sleep(0.2)
00666    c = t9cb.tally() - oc
00667    CHECK(9, 2, c, 201, 0, "run script/script status")
00668 
00669    oc = t9cb.tally()
00670    pi.run_script(s, [2000, GPIO])
00671 
00672    t9waitNotHalted(s)
00673 
00674    while True:
00675       e, p = pi.script_status(s)
00676       if e != pigpio.PI_SCRIPT_RUNNING:
00677          break
00678       if p[9] < 1900:
00679          pi.stop_script(s)
00680       time.sleep(0.1)
00681    time.sleep(0.2)
00682    c = t9cb.tally() - oc
00683    CHECK(9, 3, c, 110, 20, "run/stop script/script status")
00684 
00685    e = pi.delete_script(s)
00686    CHECK(9, 4, e, 0, 0, "delete script")
00687 
00688    t9cb.cancel()
00689 
00690    pigpio.exceptions = old_exceptions
00691 
00692 def ta():
00693    print("Serial link tests.")
00694 
00695    # this test needs RXD and TXD to be connected
00696 
00697    h = pi.serial_open("/dev/ttyAMA0", 57600)
00698    CHECK(10, 1, h>=0, 1, 0, "serial open")
00699 
00700    (b, s) = pi.serial_read(h, 1000) # flush buffer
00701 
00702    b = pi.serial_data_available(h)
00703    CHECK(10, 2, b, 0, 0, "serial data available")
00704 
00705    TEXT = """
00706 To be, or not to be, that is the question-
00707 Whether 'tis Nobler in the mind to suffer
00708 The Slings and Arrows of outrageous Fortune,
00709 Or to take Arms against a Sea of troubles,
00710 """
00711    e = pi.serial_write(h, TEXT)
00712    CHECK(10, 3, e, 0, 0, "serial write")
00713 
00714    e = pi.serial_write_byte(h, 0xAA)
00715    e = pi.serial_write_byte(h, 0x55)
00716    e = pi.serial_write_byte(h, 0x00)
00717    e = pi.serial_write_byte(h, 0xFF)
00718 
00719    CHECK(10, 4, e, 0, 0, "serial write byte")
00720 
00721    time.sleep(0.1) # allow time for transmission
00722 
00723    b = pi.serial_data_available(h)
00724    CHECK(10, 5, b, len(TEXT)+4, 0, "serial data available")
00725 
00726    (b, text) = pi.serial_read(h, len(TEXT))
00727    CHECK(10, 6, b, len(TEXT), 0, "serial read")
00728    CHECK(10, 7, STRCMP(text, TEXT), True, 0, "serial read")
00729 
00730    b = pi.serial_read_byte(h)
00731    CHECK(10, 8, b, 0xAA, 0, "serial read byte")
00732 
00733    b = pi.serial_read_byte(h)
00734    CHECK(10, 9, b, 0x55, 0, "serial read byte")
00735 
00736    b = pi.serial_read_byte(h)
00737    CHECK(10, 10, b, 0x00, 0, "serial read byte")
00738 
00739    b = pi.serial_read_byte(h)
00740    CHECK(10, 11, b, 0xFF, 0, "serial read byte")
00741 
00742    b = pi.serial_data_available(h)
00743    CHECK(10, 12, b, 0, 0, "serial data available")
00744 
00745    e = pi.serial_close(h)
00746    CHECK(10, 13, e, 0, 0, "serial close")
00747 
00748 def tb():
00749    print("SMBus / I2C tests.")
00750 
00751    # this test requires an ADXL345 on I2C bus 1 addr 0x53
00752 
00753    h = pi.i2c_open(1, 0x53)
00754    CHECK(11, 1, h>=0, 1, 0, "i2c open")
00755 
00756    e = pi.i2c_write_device(h, "\x00") # move to known register
00757    CHECK(11, 2, e, 0, 0, "i2c write device")
00758 
00759    (b, d)  = pi.i2c_read_device(h, 1)
00760    CHECK(11, 3, b, 1, 0, "i2c read device")
00761    CHECK(11, 4, ord(d), 0xE5, 0, "i2c read device")
00762 
00763    b = pi.i2c_read_byte(h)
00764    CHECK(11, 5, b, 0xE5, 0, "i2c read byte")
00765 
00766    b = pi.i2c_read_byte_data(h, 0)
00767    CHECK(11, 6, b, 0xE5, 0, "i2c read byte data")
00768 
00769    b = pi.i2c_read_byte_data(h, 48)
00770    CHECK(11, 7, b, 2, 0, "i2c read byte data")
00771 
00772    exp = b"[aB\x08cD\xAAgHj\xFD]"
00773 
00774    e = pi.i2c_write_device(h, b'\x1D'+ exp)
00775    CHECK(11, 8, e, 0, 0, "i2c write device")
00776 
00777    e = pi.i2c_write_device(h, '\x1D')
00778    (b, d)  = pi.i2c_read_device(h, 12)
00779    CHECK(11, 9, b, 12, 0, "i2c read device")
00780    CHECK(11, 10, STRCMP(d, exp), True, 0, "i2c read device")
00781 
00782    e = pi.i2c_write_byte_data(h, 0x1d, 0xAA)
00783    CHECK(11, 11, e, 0, 0, "i2c write byte data")
00784 
00785    b = pi.i2c_read_byte_data(h, 0x1d)
00786    CHECK(11, 12, b, 0xAA, 0, "i2c read byte data")
00787 
00788    e = pi.i2c_write_byte_data(h, 0x1d, 0x55)
00789    CHECK(11, 13, e, 0, 0, "i2c write byte data")
00790 
00791    b = pi.i2c_read_byte_data(h, 0x1d)
00792    CHECK(11, 14, b, 0x55, 0, "i2c read byte data")
00793 
00794    exp  =  "[1234567890#]"
00795 
00796    e = pi.i2c_write_block_data(h, 0x1C, exp)
00797    CHECK(11, 15, e, 0, 0, "i2c write block data")
00798 
00799    e = pi.i2c_write_device(h, '\x1D')
00800    (b, d)  = pi.i2c_read_device(h, 13)
00801    CHECK(11, 16, b, 13, 0, "i2c read device")
00802    CHECK(11, 17, STRCMP(d, exp), True, 0, "i2c read device")
00803 
00804    (b, d)  = pi.i2c_read_i2c_block_data(h, 0x1D, 13)
00805    CHECK(11, 18, b, 13, 0, "i2c read i2c block data")
00806    CHECK(11, 19, STRCMP(d, exp), True, 0, "i2c read i2c block data")
00807 
00808    expl = [0x01, 0x05, 0x06, 0x07, 0x09, 0x1B, 0x99, 0xAA, 0xBB, 0xCC]
00809    exp = "\x01\x05\x06\x07\x09\x1B\x99\xAA\xBB\xCC"
00810 
00811    e = pi.i2c_write_i2c_block_data(h, 0x1D, expl)
00812    CHECK(11, 20, e, 0, 0, "i2c write i2c block data")
00813 
00814    (b, d)  = pi.i2c_read_i2c_block_data(h, 0x1D, 10)
00815    CHECK(11, 21, b, 10, 0, "i2c read i2c block data")
00816    CHECK(11, 22, STRCMP(d, exp), True, 0, "i2c read i2c block data")
00817 
00818    e = pi.i2c_close(h)
00819    CHECK(11, 23, e, 0, 0, "i2c close")
00820 
00821 def tca(b, d):
00822    if b == 3:
00823       c1 = d[1] & 0x0F
00824       c2 = d[2]
00825       time.sleep(1.0)
00826       print((c1*256)+c2)
00827 
00828 def tc():
00829    print("SPI tests.")
00830 
00831    # this test requires a MCP3202 on SPI channel 1
00832 
00833    h = pi.spi_open(1, 50000)
00834    CHECK(12, 1, h>=0, 1, 0, "spi open")
00835 
00836    (b, d) = pi.spi_xfer(h, [1,128,0])
00837    CHECK(12, 2, b, 3, 0, "spi xfer")
00838    tca(b, d)
00839 
00840    (b, d) = pi.spi_xfer(h, "\x01\x80\x00")
00841    CHECK(12, 2, b, 3, 0, "spi xfer")
00842    tca(b, d)
00843 
00844    (b, d) = pi.spi_xfer(h, b"\x01\x80\x00")
00845    CHECK(12, 2, b, 3, 0, "spi xfer")
00846    tca(b, d)
00847 
00848    (b, d) = pi.spi_xfer(h, '\x01\x80\x00')
00849    CHECK(12, 2, b, 3, 0, "spi xfer")
00850    tca(b, d)
00851 
00852    (b, d) = pi.spi_xfer(h, b'\x01\x80\x00')
00853    CHECK(12, 2, b, 3, 0, "spi xfer")
00854    tca(b, d)
00855 
00856    e = pi.spi_close(h)
00857    CHECK(12, 99, e, 0, 0, "spi close")
00858 
00859 def td():
00860 
00861    print("Wavechains & filter tests.")
00862 
00863    tdcb = pi.callback(GPIO)
00864 
00865    pi.set_mode(GPIO, pigpio.OUTPUT)
00866 
00867    pi.write(GPIO, pigpio.LOW)
00868 
00869    e = pi.wave_clear()
00870    CHECK(13, 1, e, 0, 0, "callback, set mode, wave clear")
00871 
00872    wf = []
00873 
00874    wf.append(pigpio.pulse(1<<GPIO, 0,  50))
00875    wf.append(pigpio.pulse(0, 1<<GPIO,  70))
00876    wf.append(pigpio.pulse(1<<GPIO, 0, 130))
00877    wf.append(pigpio.pulse(0, 1<<GPIO, 150))
00878    wf.append(pigpio.pulse(1<<GPIO, 0,  90))
00879    wf.append(pigpio.pulse(0, 1<<GPIO, 110))
00880 
00881    e = pi.wave_add_generic(wf)
00882    CHECK(13, 2, e, 6, 0, "pulse, wave add generic")
00883 
00884    wid = pi.wave_create()
00885 
00886    chain = [
00887       255, 0, wid, 255, 1, 128, 0, 255, 2, 0, 8,
00888       255, 0, wid, 255, 1,   0, 1, 255, 2, 0, 4,
00889       255, 0, wid, 255, 1,   0, 2]
00890 
00891    e = pi.set_glitch_filter(GPIO, 0)
00892    CHECK(13, 3, e, 0, 0, "clear glitch filter")
00893 
00894    e = pi.set_noise_filter(GPIO, 0, 0)
00895    CHECK(13, 4, e, 0, 0, "clear noise filter")
00896 
00897    tdcb.reset_tally()
00898    e = pi.wave_chain(chain)
00899    CHECK(13, 5, e, 0, 0, "wave chain")
00900    while pi.wave_tx_busy():
00901       time.sleep(0.1)
00902    time.sleep(0.3)
00903    tally = tdcb.tally()
00904    CHECK(13, 6, tally, 2688, 2, "wave chain, tally")
00905 
00906    pi.set_glitch_filter(GPIO, 80)
00907    tdcb.reset_tally()
00908    pi.wave_chain(chain)
00909    while pi.wave_tx_busy():
00910       time.sleep(0.1)
00911    time.sleep(0.3)
00912    tally = tdcb.tally()
00913    CHECK(13, 7, tally, 1792, 2, "glitch filter, wave chain, tally")
00914 
00915    pi.set_glitch_filter(GPIO, 120)
00916    tdcb.reset_tally()
00917    pi.wave_chain(chain)
00918    while pi.wave_tx_busy():
00919       time.sleep(0.1)
00920    time.sleep(0.2)
00921    tally = tdcb.tally()
00922    CHECK(13, 8, tally, 896, 2, "glitch filter, wave chain, tally")
00923 
00924    pi.set_glitch_filter(GPIO, 140)
00925    tdcb.reset_tally()
00926    pi.wave_chain(chain)
00927    while pi.wave_tx_busy():
00928       time.sleep(0.1)
00929    time.sleep(0.2)
00930    tally = tdcb.tally()
00931    CHECK(13, 9, tally, 0, 0, "glitch filter, wave chain, tally")
00932 
00933    pi.set_glitch_filter(GPIO, 0)
00934 
00935    pi.wave_chain(chain)
00936    pi.set_noise_filter(GPIO, 1000, 150000)
00937    tdcb.reset_tally()
00938    while pi.wave_tx_busy():
00939       time.sleep(0.1)
00940    time.sleep(0.2)
00941    tally = tdcb.tally()
00942    CHECK(13, 10, tally, 1500, 2, "noise filter, wave chain, tally")
00943 
00944    pi.wave_chain(chain)
00945    pi.set_noise_filter(GPIO, 2000, 150000)
00946    tdcb.reset_tally()
00947    while pi.wave_tx_busy():
00948       time.sleep(0.1)
00949    time.sleep(0.2)
00950    tally = tdcb.tally()
00951    CHECK(13, 11, tally, 750, 2, "noise filter, wave chain, tally")
00952 
00953    pi.wave_chain(chain)
00954    pi.set_noise_filter(GPIO, 3000, 5000)
00955    tdcb.reset_tally()
00956    while pi.wave_tx_busy():
00957       time.sleep(0.1)
00958    time.sleep(0.2)
00959    tally = tdcb.tally()
00960    CHECK(13, 12, tally, 0, 2, "noise filter, wave chain, tally")
00961 
00962    pi.set_noise_filter(GPIO, 0, 0)
00963 
00964    e = pi.wave_delete(wid)
00965    CHECK(13, 13, e, 0, 0, "wave delete")
00966 
00967    tdcb.cancel()
00968 
00969 if len(sys.argv) > 1:
00970    tests = ""
00971    for C in sys.argv[1]:
00972       c = C.lower()
00973       if c not in tests:
00974          tests += c
00975 
00976 else:
00977    tests = "0123456789d"
00978 
00979 pi = pigpio.pi()
00980 
00981 if pi.connected:
00982    print("Connected to pigpio daemon.")
00983 
00984    if '0' in tests: t0()
00985    if '1' in tests: t1()
00986    if '2' in tests: t2()
00987    if '3' in tests: t3()
00988    if '4' in tests: t4()
00989    if '5' in tests: t5()
00990    if '6' in tests: t6()
00991    if '7' in tests: t7()
00992    if '8' in tests: t8()
00993    if '9' in tests: t9()
00994    if 'a' in tests: ta()
00995    if 'b' in tests: tb()
00996    if 'c' in tests: tc()
00997    if 'd' in tests: td()
00998 
00999 pi.stop()
01000 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57