Skip to content

Commit bd97db7

Browse files
committed
Make value return int
Value should always be numeric in nature; is_active is the "pure" bool version. This commit also deals with sorting "value" attributes correctly in the docs.
1 parent 2918d1c commit bd97db7

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

docs/api_input.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ Button
5656
------
5757

5858
.. autoclass:: Button(pin, \*, pull_up=True, active_state=None, bounce_time=None, hold_time=1, hold_repeat=False, pin_factory=None)
59-
:members: wait_for_press, wait_for_release, pin, is_pressed, is_held, hold_time, held_time, hold_repeat, pull_up, when_pressed, when_released, when_held
59+
:members: wait_for_press, wait_for_release, pin, is_pressed, is_held, hold_time, held_time, hold_repeat, pull_up, when_pressed, when_released, when_held, value
6060

6161

6262
LineSensor (TRCT5000)
6363
---------------------
6464

6565
.. autoclass:: LineSensor(pin, \*, queue_len=5, sample_rate=100, threshold=0.5, partial=False, pin_factory=None)
66-
:members: wait_for_line, wait_for_no_line, pin, line_detected, when_line, when_no_line
66+
:members: wait_for_line, wait_for_no_line, pin, line_detected, when_line, when_no_line, value
6767

6868

6969
MotionSensor (D-SUN PIR)
7070
------------------------
7171

7272
.. autoclass:: MotionSensor(pin, \*, queue_len=1, sample_rate=10, threshold=0.5, partial=False, pin_factory=None)
73-
:members: wait_for_motion, wait_for_no_motion, pin, motion_detected, when_motion, when_no_motion
73+
:members: wait_for_motion, wait_for_no_motion, pin, motion_detected, when_motion, when_no_motion, value
7474

7575

7676
LightSensor (LDR)

gpiozero/devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def __init__(self, pin=None, **kwargs):
524524
self._inactive_state = False
525525

526526
def _state_to_value(self, state):
527-
return bool(state == self._active_state)
527+
return int(state == self._active_state)
528528

529529
def _read(self):
530530
try:

gpiozero/input_devices.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def __init__(
191191
raise
192192

193193
def _pin_changed(self, ticks, state):
194-
self._fire_events(ticks, self._state_to_value(state))
194+
# XXX This is a bit of a hack; _fire_events takes *is_active* rather
195+
# than *value*. Here we're assuming no-one's overridden the default
196+
# implementation of *is_active*.
197+
self._fire_events(ticks, bool(self._state_to_value(state)))
195198

196199

197200
class SmoothedInputDevice(EventsMixin, InputDevice):
@@ -420,10 +423,6 @@ class Button(HoldMixin, DigitalInputDevice):
420423
:param pin_factory:
421424
See :doc:`api_pins` for more information (this is an advanced feature
422425
which most users can ignore).
423-
424-
.. attribute:: value
425-
426-
Returns 1 if the button is currently pressed, and 0 if it is not.
427426
"""
428427
def __init__(
429428
self, pin=None, pull_up=True, active_state=None, bounce_time=None,
@@ -434,6 +433,13 @@ def __init__(
434433
self.hold_time = hold_time
435434
self.hold_repeat = hold_repeat
436435

436+
@property
437+
def value(self):
438+
"""
439+
Returns 1 if the button is currently pressed, and 0 if it is not.
440+
"""
441+
return super(Button, self).value
442+
437443
Button.is_pressed = Button.is_active
438444
Button.pressed_time = Button.active_time
439445
Button.when_pressed = Button.when_activated
@@ -504,12 +510,6 @@ class LineSensor(SmoothedInputDevice):
504510
which most users can ignore).
505511
506512
.. _CamJam #3 EduKit: http://camjam.me/?page_id=1035
507-
508-
.. attribute:: value
509-
510-
Returns a value representing the average of the queued values. This
511-
is nearer 0 for black under the sensor, and nearer 1 for white under
512-
the sensor.
513513
"""
514514
def __init__(
515515
self, pin=None, pull_up=False, active_state=None, queue_len=5,
@@ -525,6 +525,15 @@ def __init__(
525525
self.close()
526526
raise
527527

528+
@property
529+
def value(self):
530+
"""
531+
Returns a value representing the average of the queued values. This
532+
is nearer 0 for black under the sensor, and nearer 1 for white under
533+
the sensor.
534+
"""
535+
return super(LineSensor, self).value
536+
528537
@property
529538
def line_detected(self):
530539
return not self.is_active
@@ -594,13 +603,6 @@ class MotionSensor(SmoothedInputDevice):
594603
:param pin_factory:
595604
See :doc:`api_pins` for more information (this is an advanced feature
596605
which most users can ignore).
597-
598-
.. attribute:: value
599-
600-
With the default *queue_len* of 1, this is effectively boolean where 0
601-
means no motion detected and 1 means motion detected. If you specify
602-
a *queue_len* greater than 1, this will be an averaged value where
603-
values closer to 1 imply motion detection.
604606
"""
605607
def __init__(
606608
self, pin=None, pull_up=False, active_state=None, queue_len=1,
@@ -615,6 +617,16 @@ def __init__(
615617
self.close()
616618
raise
617619

620+
@property
621+
def value(self):
622+
"""
623+
With the default *queue_len* of 1, this is effectively boolean where 0
624+
means no motion detected and 1 means motion detected. If you specify
625+
a *queue_len* greater than 1, this will be an averaged value where
626+
values closer to 1 imply motion detection.
627+
"""
628+
return super(MotionSensor, self).value
629+
618630
MotionSensor.motion_detected = MotionSensor.is_active
619631
MotionSensor.when_motion = MotionSensor.when_activated
620632
MotionSensor.when_no_motion = MotionSensor.when_deactivated
@@ -675,10 +687,6 @@ class LightSensor(SmoothedInputDevice):
675687
which most users can ignore).
676688
677689
.. _CamJam #2 EduKit: http://camjam.me/?page_id=623
678-
679-
.. attribute:: value
680-
681-
Returns a value between 0 (dark) and 1 (light).
682690
"""
683691
def __init__(
684692
self, pin=None, queue_len=5, charge_time_limit=0.01,
@@ -724,6 +732,13 @@ def _read(self):
724732
self.pin_factory.ticks_diff(self._charge_time, start) /
725733
self.charge_time_limit)
726734

735+
@property
736+
def value(self):
737+
"""
738+
Returns a value between 0 (dark) and 1 (light).
739+
"""
740+
return super(LightSensor, self).value
741+
727742
LightSensor.light_detected = LightSensor.is_active
728743
LightSensor.when_light = LightSensor.when_activated
729744
LightSensor.when_dark = LightSensor.when_deactivated
@@ -827,13 +842,6 @@ class DistanceSensor(SmoothedInputDevice):
827842
which most users can ignore).
828843
829844
.. _CamJam #3 EduKit: http://camjam.me/?page_id=1035
830-
831-
.. attribute:: value
832-
833-
Returns a value between 0, indicating the reflector is either touching
834-
the sensor or is sufficiently near that the sensor can't tell the
835-
difference, and 1, indicating the reflector is at or beyond the
836-
specified *max_distance*.
837845
"""
838846
ECHO_LOCK = Lock()
839847

@@ -916,6 +924,16 @@ def distance(self):
916924
"""
917925
return self.value * self._max_distance
918926

927+
@property
928+
def value(self):
929+
"""
930+
Returns a value between 0, indicating the reflector is either touching
931+
the sensor or is sufficiently near that the sensor can't tell the
932+
difference, and 1, indicating the reflector is at or beyond the
933+
specified *max_distance*.
934+
"""
935+
return super(DistanceSensor, self).value
936+
919937
@property
920938
def trigger(self):
921939
"""

gpiozero/output_devices.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ def toggle(self):
134134
@property
135135
def value(self):
136136
"""
137-
Returns :data:`True` if the device is currently active and
138-
:data:`False` otherwise. Setting this property changes the state of the
139-
device.
137+
Returns 1 if the device is currently active and 0 otherwise. Setting
138+
this property changes the state of the device.
140139
"""
141140
return super(OutputDevice, self).value
142141

@@ -214,7 +213,7 @@ def __init__(
214213

215214
@property
216215
def value(self):
217-
return self._read()
216+
return super(DigitalOutputDevice, self).value
218217

219218
@value.setter
220219
def value(self, value):
@@ -451,7 +450,7 @@ def value(self):
451450
The duty cycle of the PWM device. 0.0 is off, 1.0 is fully on. Values
452451
in between may be specified for varying levels of power in the device.
453452
"""
454-
return self._read()
453+
return super(PWMOutputDevice, self).value
455454

456455
@value.setter
457456
def value(self, value):

0 commit comments

Comments
 (0)