2

I'm looking to build an automation application using the Rasbery Pi + Piface board, using Python.

I've got as far as being able to switch the LEDs on and off from a command line Python script, however every time the script is called it re-sets the board when the init command is given. So for example if I call it once to turn LED1 on, when I call it again to turn LED2 on it turns LED1 off when the init is issued, but you have to issue the init before you can control things. Is there any way to call init without causing a Piface reset? I want to be able to turn specific things on and off without affecting other things.

Here's the test script - I'm sure there are far better ways to do this but it does as a starting point. This is the first time I've ever looked at Python, I normally work in Perl. It's called as for example: ./test2 on2

#!/usr/bin/env python

import time
import piface.pfio
import sys # import command line arguments

piface.pfio.init()

led1 = piface.pfio.LED(1)
led2 = piface.pfio.LED(2)
led3 = piface.pfio.LED(3)
led4 = piface.pfio.LED(4)
led5 = piface.pfio.LED(5)

if str(sys.argv[1]) == "on1":
  led1.turn_on()
elif str(sys.argv[1]) == "on2":
  led2.turn_on()
elif str(sys.argv[1]) == "on3":
  led3.turn_on()
elif str(sys.argv[1]) == "on4":
  led4.turn_on()
elif str(sys.argv[1]) == "on5":
  led5.turn_on()
elif str(sys.argv[1]) == "off1":
  led1.turn_off()
elif str(sys.argv[1]) == "off2":
  led2.turn_off()
elif str(sys.argv[1]) == "off3":
  led3.turn_off()
elif str(sys.argv[1]) == "off4":
  led4.turn_off()
elif str(sys.argv[1]) == "off5":
  led5.turn_off()
else:
  print "No valid input specified\n"

print 'Number of arguments:', len(sys.argv), 'arguments.'
4
  • As far as I can see you can run piface.pfio.init(False) to not initialize the ports. Though you'd need a script to run it without the parameter on boot.
    – Gerben
    Commented Jul 3, 2013 at 14:36
  • @Gerben Thanks, didn't think of passing options in the call, will check the possibilities. I eventually worked round it - built a deamon process in Python that monitors the status of the inputs, and watches for triggers from external processes. The daemon initialises the board on startup but then runs continuously in the background, reporting status changes and reacting to triggers. Probably not the most elegant solution in the world but it works. Not bad given brand new system and language.
    – Pyromancer
    Commented Jul 4, 2013 at 14:35
  • Sound quite elegant actually. Glad you got it to work.
    – Gerben
    Commented Jul 4, 2013 at 17:46
  • Looks like theyve kinda abandoned python2, most of the stuff in now python3 Commented Apr 18, 2014 at 10:58

1 Answer 1

2

By the way, there are more up-to-date Python libraries for PiFace: https://github.com/piface/pifacedigitalio/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.