Skip to content

Instantly share code, notes, and snippets.

@bulletmark
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bulletmark/11127013 to your computer and use it in GitHub Desktop.
Save bulletmark/11127013 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import time, threading
import pifaceio
# Width and number of output pulses in sequence
PULSE_ON = 2 # secs
PULSE_OFF = 1 # secs
PULSE_NUM = 3 # count of ON pulses in sequence
PIN_TRANSITIONS = PULSE_NUM * 2
# Global lock for threads
lock = threading.Lock()
# Fetch instance of and initialize PiFace board
pf = pifaceio.PiFace()
# Initialize all board pin outputs to 0
pin_outputs = 0x0
pf.write(pin_outputs)
# Currently active pins
pin_active = 0x0
def send_output(pin_mask):
'Runs in thread to drive a single output pin through transitions'
global pin_outputs, pin_active
for count in range(PIN_TRANSITIONS):
with lock:
if count & 1:
pin_outputs &= ~pin_mask
delay = PULSE_OFF
else:
pin_outputs |= pin_mask
delay = PULSE_ON
pf.write(pin_outputs)
# Don't delay on final off
if count != PIN_TRANSITIONS - 1:
time.sleep(delay)
with lock:
pin_active &= ~pin_mask
# Forever ..
while True:
with lock:
inputs = pf.read()
# Iterate through all 8 inputs pins and start output thread if input on
for pin in range(8):
pin_mask = 1 << pin
if inputs & pin_mask and not (pin_active & pin_mask):
with lock:
pin_active |= pin_mask
t = threading.Thread(target=send_output, args=[pin_mask])
t.daemon = True
t.start()
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment