Skip to content

Instantly share code, notes, and snippets.

@bulletmark
Created October 30, 2014 23:13
Show Gist options
  • Save bulletmark/5e9843fec479b74b293c to your computer and use it in GitHub Desktop.
Save bulletmark/5e9843fec479b74b293c to your computer and use it in GitHub Desktop.
Simple piface program to toggle outputs and keep state over restart
#!/usr/bin/env python
import os, time, pickle, pifaceio
filename = os.path.join(os.path.expanduser('~'), '.' +
os.path.basename(__file__) + '.init')
pf = pifaceio.PiFace()
# Read stored last states
try:
with open(filename, 'rb') as f:
last_in, last_out = pickle.load(f)
except:
last_in, last_out = 0, 0
pf.write(last_out)
print 'Wrote initial outputs 0x%02x' % last_out
# Read inputs and write changed outputs
while True:
now_in = pf.read()
new_in = now_in & (now_in ^ last_in)
last_in = now_in
new_out = (last_out & ~new_in) | ((last_out ^ new_in) & new_in)
if new_out != last_out:
last_out = new_out
pf.write(new_out)
print 'Wrote outputs 0x%02x' % new_out
with open(filename, 'wb') as f:
pickle.dump((last_in, last_out), f)
time.sleep(.1)
@alexiszecharies
Copy link

where do i have to put this code in order for it to start with startup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment