Posts: 3
Joined: Thu Aug 15, 2013 3:44 pm

Python Gtk and PiFace listener - help needed.

Mon Sep 16, 2013 6:46 pm

Hi, I need some help with my first python project.
The project -
Target: to use a R-Pi to control a camera atop a 100 foot mast.
Current state: head control finished, camera control (gphoto2) in progress.
Current task: read output of anemometer and convert it to windspeed.

I found the following on piface.github

Code: Select all

>>> import pifacedigitalio
>>> pifacedigitalio.init()
>>> pfd = pifacedigitalio.PiFaceDigital()
>>> def toggle_led0(event):
...     pfd.leds[0].toggle()
>>> listener = pifacedigitalio.InputEventListener()
>>> listener.register(0, pifacedigitalio.IODIR_ON, toggle_led0)
>>> listener.activate()
and decided this would do for counting output pulses.

I found it worked perfectly through Python shell but when I tried to incorporate it into a python script everything went pear shaped. I must point out that this is the first time I have tried to write a python script so any suggestions you can make should take this into consideration.

Code: Select all

from gi.repository import Gtk

import pifacedigitalio

pfd = pifacedigitalio.PiFaceDigital()
listener = pifacedigitalio.InputEventListener()

icount = 1

class MyWindow(Gtk.Window):

  def toggle_led0(self, event):
    global icount
    icount += 1

  def start_listen(self, widget):
    listener.register(0, pifacedigitalio.IODIR_ON, self.toggle_led0)
    print "listener activated"

  def stop_listen(self, widget):
    print "listener deactivated"

  def __init__(self): # this is the class constructor where we build widgets, etc
    Gtk.Window.__init__(self, title="Wind Speed Indicator") = Gtk.Box(spacing=6)

    self.button1 = Gtk.Button(label="Start Listener")
    self.button1.connect("clicked", self.start_listen), True, True, 0)

    self.label = Gtk.Label(label="Hello World", halign=Gtk.Align.CENTER), True, True, 2)

    self.button2 = Gtk.Button(label="Stop Listener")
    self.button2.connect("clicked", self.stop_listen), False, False, 5)

    self.button3 = Gtk.Button(label="Show icount")
    self.button3.connect("clicked", self.on_button3_clicked), True, True, 0)

  def on_button1_clicked(self, widget):
    self.label.set_text("Starting Listener")

  def on_button2_clicked(self, widget):
    self.label.set_text("Stopping Listener")

  def on_button3_clicked(self, widget):
    icntprt = "Count is " + str(icount)

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
What happens -
The window, I click 'Start Listener', the shell window shows 'listener activated'. I press the input button a few times and then click 'Show icount'.
'Count is 1' appears in the window. This is the default value. I expected it to have increased by the number of button pushes.
I click 'Stop Listener' and then click 'Show icount'. Now the number has been incremented.
I think I must have the counter and/or listener functions in the wrong place but I have tried various things which usually produced errors. This script does not raise any errors but doesn't work as I want.
I'd be very grateful for any help you can give me.

Return to “Python”