def __init__(self, gpio_0, gpio_1, callback, bit_timeout=5): """ Instantiate with the gpio for 0 (green wire), the gpio for 1 (white wire), the callback function, and the bit timeout in milliseconds which indicates the end of a code. The callback is passed the code length in bits and the value. """ self.gpio_0 = gpio_0 self.gpio_1 = gpio_1 self.callback = callback self.bit_timeout = bit_timeout self.in_code = False pigpio.set_mode(gpio_0, pigpio.INPUT) pigpio.set_mode(gpio_1, pigpio.INPUT) pigpio.set_pull_up_down(gpio_0, pigpio.PUD_UP) pigpio.set_pull_up_down(gpio_1, pigpio.PUD_UP) self.cb_0 = pigpio.callback(gpio_0, pigpio.FALLING_EDGE, self._cb) self.cb_1 = pigpio.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)
def start(self): pigpio.set_mode(self.GPIO_TRIGGER, pigpio.OUTPUT) pigpio.set_mode(self.GPIO_ECHO, pigpio.INPUT) pigpio.write(self.GPIO_TRIGGER, 0) self.echo_cb = pigpio.callback( self.GPIO_ECHO, pigpio.EITHER_EDGE, self.on_echo) pigpio.set_watchdog(self.GPIO_ECHO, 100)
def t2(): global t2_count print("PWM dutycycle/range/frequency tests.") pigpio.set_PWM_range(GPIO, 255) pigpio.set_PWM_frequency(GPIO, 0) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency") t2cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t2cbf) pigpio.set_PWM_dutycycle(GPIO, 0) time.sleep(0.5) # allow old notifications to flush oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 2, f, 0, 0, "set PWM dutycycle, callback") pigpio.set_PWM_dutycycle(GPIO, 128) time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 3, f, 40, 5, "set PWM dutycycle, callback") pigpio.set_PWM_frequency(GPIO, 100) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 4, f, 100, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 5, f, 400, 1, "callback") pigpio.set_PWM_frequency(GPIO, 1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 6, f, 1000, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 7, f, 4000, 1, "callback") r = pigpio.get_PWM_range(GPIO) CHECK(2, 8, r, 255, 0, "get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 9, rr, 200, 0, "get PWM real range") pigpio.set_PWM_range(GPIO, 2000) r = pigpio.get_PWM_range(GPIO) CHECK(2, 10, r, 2000, 0, "set/get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 11, rr, 200, 0, "get PWM real range") pigpio.set_PWM_dutycycle(GPIO, 0)
def t2(): global t2_count print("PWM dutycycle/range/frequency tests.") pigpio.set_PWM_range(GPIO, 255) pigpio.set_PWM_frequency(GPIO,0) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency") t2cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t2cbf) pigpio.set_PWM_dutycycle(GPIO, 0) time.sleep(0.5) # allow old notifications to flush oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 2, f, 0, 0, "set PWM dutycycle, callback") pigpio.set_PWM_dutycycle(GPIO, 128) time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 3, f, 40, 5, "set PWM dutycycle, callback") pigpio.set_PWM_frequency(GPIO,100) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 4, f, 100, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 5, f, 400, 1, "callback") pigpio.set_PWM_frequency(GPIO,1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(2, 6, f, 1000, 0, "set/get PWM frequency") time.sleep(1) oc = t2_count time.sleep(2) f = t2_count - oc CHECK(2, 7, f, 4000, 1, "callback") r = pigpio.get_PWM_range(GPIO) CHECK(2, 8, r, 255, 0, "get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 9, rr, 200, 0, "get PWM real range") pigpio.set_PWM_range(GPIO, 2000) r = pigpio.get_PWM_range(GPIO) CHECK(2, 10, r, 2000, 0, "set/get PWM range") rr = pigpio.get_PWM_real_range(GPIO) CHECK(2, 11, rr, 200, 0, "get PWM real range") pigpio.set_PWM_dutycycle(GPIO, 0)
def t9(): print("Script store/run/status/stop/delete tests.") pigpio.write(GPIO, 0) # need known state # 100 loops per second # p0 number of loops # p1 GPIO script=""" ldap 0 ldva 0 label 0 w p1 1 milli 5 w p1 0 milli 5 dcrv 0 ldav 0 ldpa 9 jp 0""" t9cb = pigpio.callback(GPIO) s = pigpio.store_script(script) oc = t9cb.tally() pigpio.run_script(s, [99, GPIO]) time.sleep(2) c = t9cb.tally() - oc CHECK(9, 1, c, 100, 0, "store/run script") oc = t9cb.tally() pigpio.run_script(s, [200, GPIO]) while True: e, p = pigpio.script_status(s) if e != pigpio.PI_SCRIPT_RUNNING: break time.sleep(0.5) c = t9cb.tally() - oc time.sleep(0.1) CHECK(9, 2, c, 201, 0, "run script/script status") oc = t9cb.tally() pigpio.run_script(s, [2000, GPIO]) while True: e, p = pigpio.script_status(s) if e != pigpio.PI_SCRIPT_RUNNING: break if p[9] < 1900: pigpio.stop_script(s) time.sleep(0.1) c = t9cb.tally() - oc time.sleep(0.1) CHECK(9, 3, c, 110, 10, "run/stop script/script status") e = pigpio.delete_script(s) CHECK(9, 4, e, 0, 0, "delete script")
def t9(): print("Script store/run/status/stop/delete tests.") pigpio.write(GPIO, 0) # need known state # 100 loops per second # p0 number of loops # p1 GPIO script = """ lda p0 sta v0 tag 0 w p1 1 mils 5 w p1 0 mils 5 dcr v0 lda v0 sta p9 jp 0""" t9cb = pigpio.callback(GPIO) s = pigpio.store_script(script) oc = t9cb.tally() pigpio.run_script(s, [99, GPIO]) time.sleep(2) c = t9cb.tally() - oc CHECK(9, 1, c, 100, 0, "store/run script") oc = t9cb.tally() pigpio.run_script(s, [200, GPIO]) while True: e, p = pigpio.script_status(s) if e != pigpio.PI_SCRIPT_RUNNING: break time.sleep(0.5) c = t9cb.tally() - oc time.sleep(0.1) CHECK(9, 2, c, 201, 0, "run script/script status") oc = t9cb.tally() pigpio.run_script(s, [2000, GPIO]) while True: e, p = pigpio.script_status(s) if e != pigpio.PI_SCRIPT_RUNNING: break if p[9] < 1900: pigpio.stop_script(s) time.sleep(0.1) c = t9cb.tally() - oc time.sleep(0.1) CHECK(9, 3, c, 110, 10, "run/stop script/script status") e = pigpio.delete_script(s) CHECK(9, 4, e, 0, 0, "delete script")
def __init__(self, gpio, callback, timeout=5): """ Initialises an IR remote hasher on a gpio. A gap of timeout milliseconds indicates the end of the remote key press. """ self.gpio = gpio self.code_timeout = timeout self.callback = callback self.in_code = False pigpio.set_mode(gpio, pigpio.INPUT) self.cb = pigpio.callback(gpio, pigpio.EITHER_EDGE, self._cb)
def t3(): global t3_reset, t3_count, t3_on, t3_off pw=[500.0, 1500.0, 2500.0] dc=[0.2, 0.4, 0.6, 0.8] print("PWM/Servo pulse accuracy tests.") t3cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t3cbf) t = 0 for x in pw: t += 1 pigpio.set_servo_pulsewidth(GPIO, x) time.sleep(1) t3_reset = True time.sleep(4) c = t3_count on = t3_on off = t3_off CHECK(3, t, int((1E3*(on+off))/on), int(2E7/x), 1, "set servo pulsewidth") pigpio.set_servo_pulsewidth(GPIO, 0) pigpio.set_PWM_frequency(GPIO, 1000) f = pigpio.get_PWM_frequency(GPIO) CHECK(3, 4, f, 1000, 0, "set/get PWM frequency") rr = pigpio.set_PWM_range(GPIO, 100) CHECK(3, 5, rr, 200, 0, "set PWM range") t = 5 for x in dc: t += 1 pigpio.set_PWM_dutycycle(GPIO, x*100) time.sleep(1) t3_reset = True time.sleep(2) c = t3_count on = t3_on off = t3_off CHECK(3, t, int((1E3*on)/(on+off)), int(1E3*x), 1, "set PWM dutycycle") pigpio.set_PWM_dutycycle(GPIO, 0)
def t7(): global t7_count print("Watchdog tests.") # type of edge shouldn't matter for watchdogs t7cb = pigpio.callback(GPIO, pigpio.FALLING_EDGE, t7cbf) pigpio.set_watchdog(GPIO, 10) # 10 ms, 100 per second time.sleep(0.5) oc = t7_count time.sleep(2) c = t7_count - oc CHECK(7, 1, c, 200, 1, "set watchdog on count") pigpio.set_watchdog(GPIO, 0) # 0 switches watchdog off time.sleep(0.5) oc = t7_count time.sleep(2) c = t7_count - oc CHECK(7, 2, c, 0, 1, "set watchdog off count")
def __init__(self, trigger, echo): """ The class is instantiated with the gpios connected to the trigger and echo pins. """ self._trig = trigger self._echo = echo self._ping = False self._high = None self._time = None self._trig_mode = pigpio.get_mode(self._trig) self._echo_mode = pigpio.get_mode(self._echo) pigpio.set_mode(self._trig, pigpio.OUTPUT) pigpio.set_mode(self._echo, pigpio.INPUT) self._cb = pigpio.callback(self._echo, pigpio.EITHER_EDGE, self._cbf) self._inited = True
def t6(): global t6_count, t6_on print("Trigger tests.") pigpio.write(GPIO, pigpio.LOW) tp = 0 t6cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t6cbf) for t in range(5): time.sleep(0.1) p = 10 + (t * 10) tp += p pigpio.gpio_trigger(GPIO, p, 1) time.sleep(0.5) CHECK(6, 1, t6_count, 5, 0, "gpio trigger count") CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length")
def t6(): global t6_count, t6_on print("Trigger tests.") pigpio.write(GPIO, pigpio.LOW) tp = 0 t6cb = pigpio.callback(GPIO, pigpio.EITHER_EDGE, t6cbf) for t in range(10): time.sleep(0.1) p = 10 + (t*10) tp += p; pigpio.gpio_trigger(4, p, 1) time.sleep(0.5) CHECK(6, 1, t6_count, 10, 0, "gpio trigger count") CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length")
def __init__(self, gpio): """ Instantiate with the gpio to which the DHT22 output pin is connected. """ self.gpio = gpio self.bad_CS = 0 # checksum self.bad_TO = 0 # time-out self.accumulating = False self.rhum = -999 self.temp = -999 self.tov = None self.tick = 0 pigpio.set_mode(gpio, pigpio.INPUT) pigpio.set_pull_up_down(gpio, pigpio.PUD_OFF) self.cb = pigpio.callback(gpio, pigpio.EITHER_EDGE, self._cb)
def t5(): global t5_count BAUD = 4800 TEXT = """ Now is the winter of our discontent Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. Now are our brows bound with victorious wreaths; Our bruised arms hung up for monuments; Our stern alarums changed to merry meetings, Our dreadful marches to delightful measures. Grim-visaged war hath smooth'd his wrinkled front; And now, instead of mounting barded steeds To fright the souls of fearful adversaries, He capers nimbly in a lady's chamber To the lascivious pleasing of a lute. """ print("Waveforms & serial read/write tests.") t5cb = pigpio.callback(GPIO, pigpio.FALLING_EDGE, t5cbf) pigpio.set_mode(GPIO, pigpio.OUTPUT) e = pigpio.wave_clear() CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear") wf = [] wf.append(pigpio.pulse(1 << GPIO, 0, 10000)) wf.append(pigpio.pulse(0, 1 << GPIO, 30000)) wf.append(pigpio.pulse(1 << GPIO, 0, 60000)) wf.append(pigpio.pulse(0, 1 << GPIO, 100000)) e = pigpio.wave_add_generic(wf) CHECK(5, 2, e, 4, 0, "pulse, wave add generic") e = pigpio.wave_tx_repeat() CHECK(5, 3, e, 9, 0, "wave tx repeat") oc = t5_count time.sleep(5) c = t5_count - oc CHECK(5, 4, c, 50, 1, "callback") e = pigpio.wave_tx_stop() CHECK(5, 5, e, 0, 0, "wave tx stop") e = pigpio.serial_read_open(GPIO, BAUD) CHECK(5, 6, e, 0, 0, "serial read open") pigpio.wave_clear() e = pigpio.wave_add_serial(GPIO, BAUD, 5000000, TEXT) CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial") e = pigpio.wave_tx_start() CHECK(5, 8, e, 6811, 0, "wave tx start") oc = t5_count time.sleep(3) c = t5_count - oc CHECK(5, 9, c, 0, 0, "callback") oc = t5_count while pigpio.wave_tx_busy(): time.sleep(0.1) time.sleep(0.1) c = t5_count - oc CHECK(5, 10, c, 1702, 0, "wave tx busy, callback") c, text = pigpio.serial_read(GPIO) CHECK(5, 11, TEXT == text, True, 0, "wave tx busy, serial read") e = pigpio.serial_read_close(GPIO) CHECK(5, 12, e, 0, 0, "serial read close") c = pigpio.wave_get_micros() CHECK(5, 13, c, 6158704, 0, "wave get micros") CHECK(5, 14, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_micros() CHECK(5, 15, c, 1800000000, 0, "wave get max micros") c = pigpio.wave_get_pulses() CHECK(5, 16, c, 3405, 0, "wave get pulses") CHECK(5, 17, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_pulses() CHECK(5, 18, c, 12000, 0, "wave get max pulses") c = pigpio.wave_get_cbs() CHECK(5, 19, c, 6810, 0, "wave get cbs") CHECK(5, 20, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_cbs() CHECK(5, 21, c, 25016, 0, "wave get max cbs") e = pigpio.wave_clear() CHECK(5, 22, e, 0, 0, "wave clear") e = pigpio.wave_add_generic(wf) CHECK(5, 23, e, 4, 0, "pulse, wave add generic") w1 = pigpio.wave_create() CHECK(5, 24, w1, 0, 0, "wave create") e = pigpio.wave_send_repeat(w1) CHECK(5, 25, e, 9, 0, "wave send repeat") oc = t5_count time.sleep(5) c = t5_count - oc CHECK(5, 26, c, 50, 1, "callback") e = pigpio.wave_tx_stop() CHECK(5, 27, e, 0, 0, "wave tx stop") e = pigpio.wave_add_serial(GPIO, BAUD, 5000000, TEXT) CHECK(5, 28, e, 3405, 0, "wave add serial") w2 = pigpio.wave_create() CHECK(5, 29, w2, 1, 0, "wave create") e = pigpio.wave_send_once(w2) CHECK(5, 30, e, 6811, 0, "wave send once") oc = t5_count time.sleep(3) c = t5_count - oc CHECK(5, 31, c, 0, 0, "callback") oc = t5_count while pigpio.wave_tx_busy(): time.sleep(0.1) time.sleep(0.1) c = t5_count - oc CHECK(5, 32, c, 1702, 0, "wave tx busy, callback") e = pigpio.wave_delete(0) CHECK(5, 33, e, 0, 0, "wave delete")
def start_gpio_monitor(): global gpioCallbackControl gpioCallbackControl = pigpio.callback(GPIO_PIN, pigpio.RISING_EDGE, movement_detected)
if listFull > 0: del imageList[0] print "List Length = " + str(len(imageList)) listFull = updateDisplay(imageList, False) pygame.event.clear(BUTTON_PRESSED) # ######################################################################## # # Main Program # # ######################################################################## #Setup the GPIO stuff pigpio.start() pigpio.set_pull_up_down(24, pigpio.PUD_UP) cb = pigpio.callback(24, pigpio.FALLING_EDGE, cbf) myDirs = readDirs() print myDirs if myDirs: lastDirectory = len(myDirs)-1 #List is zero-referenced, first directory begins with 1 else: lastDirectory = 1 loadFiles(lastDirectory) # This should populate the list of images with something... listFull = updateDisplay(imageList) #Make sure that the faces are displayed onscreen print "Length of myDirs is " + str(len(myDirs)) #create a directory for this run's worth of files currentDir = os.path.join(imageRoot, 'imagesFolder/', str(len(myDirs)+1)) print currentDir if not os.path.isdir(currentDir):
import time tick0 = None tick1 = None def mycallback(gpio, level, tick): global tick0, tick1 if level == 0: tick0 = tick if tick1 is not None: diff = pigpio.tickDiff(tick1, tick) print "high for " + str(diff) + " microseconds" else: tick1 = tick if tick0 is not None: diff = pigpio.tickDiff(tick0, tick) print "low for " + str(diff) + " microseconds" pigpio.start() cb = pigpio.callback(4, pigpio.EITHER_EDGE, mycallback) time.sleep(5) cb.cancel() # cancel callback pigpio.stop()
curses.endwin() pigpio.stop() pigpio.start() stdscr = curses.initscr() curses.noecho() curses.cbreak() atexit.register(cleanup) cb = [] for g in range(GPIOS): cb.append(pigpio.callback(g, pigpio.EITHER_EDGE)) # disable gpio 28 as the PCM clock is swamping the system cb[28].cancel() stdscr.nodelay(1) stdscr.addstr(0, 23, "Status of gpios 0-31", curses.A_REVERSE) while True: for g in range(GPIOS): tally = cb[g].tally() mode = pigpio.get_mode(g)
del imageList[0] print "List Length = " + str(len(imageList)) listFull = updateDisplay(imageList, False) pygame.event.clear(BUTTON_PRESSED) # ######################################################################## # # Main Program # # ######################################################################## #Setup the GPIO stuff pigpio.start() pigpio.set_pull_up_down(24, pigpio.PUD_UP) cb = pigpio.callback(24, pigpio.FALLING_EDGE, cbf) myDirs = readDirs() print myDirs if myDirs: lastDirectory = len( myDirs) - 1 #List is zero-referenced, first directory begins with 1 else: lastDirectory = 1 loadFiles( lastDirectory) # This should populate the list of images with something... listFull = updateDisplay( imageList) #Make sure that the faces are displayed onscreen print "Length of myDirs is " + str(len(myDirs)) #create a directory for this run's worth of files
def t5(): global t5_count BAUD=4800 TEXT=""" Now is the winter of our discontent Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. Now are our brows bound with victorious wreaths; Our bruised arms hung up for monuments; Our stern alarums changed to merry meetings, Our dreadful marches to delightful measures. Grim-visaged war hath smooth'd his wrinkled front; And now, instead of mounting barded steeds To fright the souls of fearful adversaries, He capers nimbly in a lady's chamber To the lascivious pleasing of a lute. """ print("Waveforms & serial read/write tests.") t5cb = pigpio.callback(GPIO, pigpio.FALLING_EDGE, t5cbf) pigpio.set_mode(GPIO, pigpio.OUTPUT) e = pigpio.wave_clear() CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear") wf = [] wf.append(pigpio.pulse(1<<GPIO, 0, 10000)) wf.append(pigpio.pulse(0, 1<<GPIO, 30000)) wf.append(pigpio.pulse(1<<GPIO, 0, 60000)) wf.append(pigpio.pulse(0, 1<<GPIO, 100000)) e = pigpio.wave_add_generic(wf) CHECK(5, 2, e, 4, 0, "pulse, wave add generic") e = pigpio.wave_tx_repeat() CHECK(5, 3, e, 9, 0, "wave tx repeat") oc = t5_count time.sleep(5) c = t5_count - oc CHECK(5, 4, c, 50, 1, "callback") e = pigpio.wave_tx_stop() CHECK(5, 5, e, 0, 0, "wave tx stop") e = pigpio.serial_read_open(GPIO, BAUD) CHECK(5, 6, e, 0, 0, "serial read open") pigpio.wave_clear() e = pigpio.wave_add_serial(GPIO, BAUD, 5000000, TEXT) CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial") e = pigpio.wave_tx_start() CHECK(5, 8, e, 6811, 0, "wave tx start") oc = t5_count time.sleep(3) c = t5_count - oc CHECK(5, 9, c, 0, 0, "callback") oc = t5_count while pigpio.wave_tx_busy(): time.sleep(0.1) time.sleep(0.1) c = t5_count - oc CHECK(5, 10, c, 1702, 0, "wave tx busy, callback") c, text = pigpio.serial_read(GPIO) CHECK(5, 11, TEXT == text, True, 0, "wave tx busy, serial read"); e = pigpio.serial_read_close(GPIO) CHECK(5, 12, e, 0, 0, "serial read close") c = pigpio.wave_get_micros() CHECK(5, 13, c, 6158704, 0, "wave get micros") CHECK(5, 14, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_micros() CHECK(5, 15, c, 1800000000, 0, "wave get max micros") c = pigpio.wave_get_pulses() CHECK(5, 16, c, 3405, 0, "wave get pulses") CHECK(5, 17, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_pulses() CHECK(5, 18, c, 12000, 0, "wave get max pulses") c = pigpio.wave_get_cbs() CHECK(5, 19, c, 6810, 0, "wave get cbs") CHECK(5, 20, 0, 0, 0, "NOT APPLICABLE") c = pigpio.wave_get_max_cbs() CHECK(5, 21, c, 25016, 0, "wave get max cbs")