Anthony S
Posts: 40
Joined: Mon Apr 29, 2019 3:58 pm
Location: Bedfordshire, UK

'active_state' parameter in Gpiozero (Solved)

Tue May 21, 2019 5:55 pm

Hi to Everyone,

I've been playing around with the different classes available in Gpiozero and I've created over thirty little programs to test the inputs and outputs. All worked fine with no problems at all. However, I get an unexpected error when using the 'active_state' parameter in the InputDevice class.
According to the documentation at: https://gpiozero.readthedocs.io/en/stab ... input.html the class is defined thus:

class: gpiozero.InputDevice(pin, *, pull_up=False, active_state=None, pin_factory=None)

The relevant parameters are:

pull_up (bool or None) – If True, the pin will be pulled high with an internal resistor. If False (the default), the pin will be pulled low. If None, the pin will be floating. As gpiozero cannot automatically guess the active state when not pulling the pin, the active_state parameter must be passed.

active_state (bool or None) – If True, when the hardware pin state is HIGH, the software pin is HIGH. If False, the input polarity is reversed: when the hardware pin state is HIGH, the software pin state is LOW. Use this parameter to set the active state of the underlying pin when configuring it as not pulled (when pull_up is None). When pull_up is True or False, the active state is automatically set to the proper value.


Clearly, if pull_up is set to None (for example, when interfacing to digital devices which don't require a pull up or down) it is necessary to set active_state to True or False in order to define the active state. To test this, I created the following simple program:

Code: Select all

from gpiozero import InputDevice

input = InputDevice(pin=4, pull_up=None, active_state=True)

input.wait_for_active()
print ("input is active")
When I run the program I get the following error:

TypeError: __init__() got an unexpected keyword argument 'active_state'

Incidentally, I get exactly the same error if I set 'active_state' to False. Can you guys give me a sanity check? Am I doing something stupid?

Cheers,
Tony.
Last edited by Anthony S on Thu May 23, 2019 7:44 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 35361
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: 'active_state' parameter in Gpiozero

Wed May 22, 2019 11:14 am

Have you got the latest version of GPIOzero on the latest version of Raspbian?

Try sudo apt update; sudo apt install --reinstall python{,3}-gpiozero
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Anthony S
Posts: 40
Joined: Mon Apr 29, 2019 3:58 pm
Location: Bedfordshire, UK

Re: 'active_state' parameter in Gpiozero

Wed May 22, 2019 12:17 pm

Hi Dougie,
Many thanks for your suggestions.

I can confirm that both python-gpiozero and python3-gpiozero are the latest versions (1.5.0). I have also recently run sudo update and sudo upgrade so I'm fairly confident everything else is up to scratch.

Sadly, the problem with 'InputDevice' persists. However....the plot thickens.......

If I replace 'InputDevice' with 'DigitalInputDevice' as shown:

Code: Select all

from gpiozero import DigitalInputDevice

input = DigitalInputDevice(pin=4, pull_up=None, active_state=True)

input.wait_for_active()
print ("input is active")
....the program works correctly for both active_state=True and active_state=False.

According to the documentation, the base class 'DigitalInputDevice' is simply an extension of 'InputDevice'. So if 'DigitalInputDevice' works flawlessly, why do I get the error with 'InputDevice' ?!

Kinda weird!

Thanks again,
Tony.

User avatar
B.Goode
Posts: 7868
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 'active_state' parameter in Gpiozero

Wed May 22, 2019 1:00 pm

Perhaps consider raising this as an Issue directly with the developer/maintainer of the library, here: https://github.com/RPi-Distro/python-gpiozero/issues ?

You might have discovered an unintended 'feature' that they would appreciate knowing about. And if not they can explain...

Anthony S
Posts: 40
Joined: Mon Apr 29, 2019 3:58 pm
Location: Bedfordshire, UK

Re: 'active_state' parameter in Gpiozero

Wed May 22, 2019 2:19 pm

Hi B.Goode,

I agree entirely. Perhaps this forum is not quite the right place to pursue this problem, so I have raised an issue at Github.

I'll let you know if I get any response.

Thanks,
Tony.

User avatar
DougieLawson
Posts: 35361
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: 'active_state' parameter in Gpiozero

Wed May 22, 2019 5:58 pm

Your sample program fails differently on my system.

Code: Select all

[email protected]:/tmp $ cat GZ.py
from gpiozero import InputDevice

input = InputDevice(pin=4, pull_up=None, active_state=True)

input.wait_for_active()
print ("input is active")
[email protected]:/tmp $ python3 ./GZ.py
Traceback (most recent call last):
  File "./GZ.py", line 5, in <module>
    input.wait_for_active()
AttributeError: 'InputDevice' object has no attribute 'wait_for_active'
[email protected]:/tmp $ python ./GZ.py
Traceback (most recent call last):
  File "./GZ.py", line 5, in <module>
    input.wait_for_active()
AttributeError: 'InputDevice' object has no attribute 'wait_for_active'
[email protected]:/tmp $
Have you got a "rogue" copy of gpiozero hiding somewhere within the python search path?
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Anthony S
Posts: 40
Joined: Mon Apr 29, 2019 3:58 pm
Location: Bedfordshire, UK

Re: 'active_state' parameter in Gpiozero

Thu May 23, 2019 3:58 pm

I've just received a response from Andrew Scheller via Github regarding the apparent anomalies with 'InputDevice'.

The problem is down to the fact that I was using .wait_for_active() with the base InputDevice class, whereas (as Andrew has kindly clarified) that is only available with the DigitalInputDevice class (or descendants). When I use is_active or value with InputDevice everything works fine.

Many thanks to Andrew clearing this up for me.
Many thanks, also, to @DougieLawson and @B.Goode for your kind suggestions.

Cheers,
Tony.

Return to “Python”