Commit Graph

46 Commits

Author SHA1 Message Date
Dave Jones
1ec2b763f5 Fix #359, close #396 2016-08-14 12:42:25 +01:00
Ben Nuttall
a20e03527c Merge pull request #348 from lurch/freeze_composite_device_internals
Make CompositeDevice._named a frozendict (and add frozendict to compat.py)
2016-06-09 21:29:41 +01:00
Andrew Scheller
68f5f74668 Minor CompositeDevice tweaks
Always make `__repr__` print subdevices in the same order as `value`
No need to check for missing keys if _order wasn't explicitly supplied
2016-06-06 02:38:41 +01:00
Ben Nuttall
8b81057f8b Fix typo in pigoiod import 2016-06-05 13:00:24 +01:00
Andrew Scheller
5455034a12 Make CompositeDevice._named a frozendict (and add frozendict to compat.py)
This prevents it being modified post-construction (just like the way
CompositeDevice._all and CompositeDevice._order are already 'frozen' by being
tuples)
2016-05-31 16:52:14 +01:00
Andrew Scheller
da9b0bb644 Always make PWMOutputDevice operate on floats.
And better 'encapsulation' of the active_high properties.
2016-05-11 16:23:39 +01:00
Dave Jones
79a0fd2a27 Fix #278
Allow the creation of remote pins easily with PiGPIOPin. Also changes
DefaultPin to a pin_factory callable which accepts the input pin number.
2016-04-16 00:30:22 +01:00
Dave Jones
e2b8171bbb Merge pull request #274 from waveform80/doc-reorg
Doc reorganization
2016-04-10 16:58:27 +01:00
Dave Jones
ecc8df8041 Doc reorganization 2016-04-10 16:47:16 +01:00
Andrew Scheller
8e09a077fd Check that the sub-devices of CompositeDevice are actually Device instances 2016-04-10 15:02:34 +01:00
Dave Jones
8c4096b19c Some minor doc fixes for 1.2 2016-04-09 10:30:03 +01:00
Dave Jones
c2a9392ea5 Make tests work reliably on the Pi
While the tests work well on a PC or Travis, the Pi (where I ought to be
running them!) has some issues with the timing tests. Need to relax the
tolerance of the "assert_states_and_times" method to 0.05 seconds
otherwise it periodically fails even on something reasonably quick like
a Pi 2 (less failures on a Pi 3 but still occasionally).

Also reduced default fps to 25; if the default timing occasionally fails
on a Pi 2 it's evidently too fast for a Pi 1 and shouldn't be the
default; 25 also doesn't look any different to me on a pulsing LED.

There's also a bunch of miscellaneous fixes in here; last minute typos
and chart re-gens for the 1.2 release.
2016-04-08 23:11:14 +01:00
Dave Jones
f746ecb7cc Merge pull request #259 from lurch/patch-2
Ensure CompositeDevice._order has deterministic behaviour
2016-04-07 20:49:34 +01:00
Andrew Scheller
72ca075668 fix various bugs found by the 'prospector' static-analysis tool 2016-04-07 17:58:51 +01:00
Andrew Scheller
53dc1750c1 Ensure CompositeDevice._order has deterministic behaviour
... by sorting the kwargs, if no _order was specifically supplied.
2016-04-07 17:50:00 +01:00
Dave Jones
3acf8a0d3a More work on the tests... 2016-04-05 13:22:40 +01:00
Dave Jones
69dd8a439a The source/values toolkit
Me and my big mouth. No sooner do I declare the base classes "relatively
stable" than I go and mess around with it all again. Anyway, this is the
long promised set of utilities to make source/values more interesting.
It includes a few interesting little utility functions, a whole bunch of
examples and introduces the notion of "pseudo" devices with no (obvious)
hardware representation like a time-of-day device.

This necessitated making the event system a little more generic (it's
not exclusive the GPIO devices after all; no reason we can't use it on
composite devices in future) and by this point the mixins have gotten
large enough to justify their own module.

The pseudo-devices are a bit spartan and basic at the moment but I'm
sure there'll be plenty of future ideas...
2016-04-04 23:59:05 +01:00
Dave Jones
8fc4b58ec2 Fix #182
Fixes LEDBarGraph (broken by snowpi updates) and adds PWM support and
relevant recipes
2016-04-01 20:05:10 +01:00
Dave Jones
d6af02933a Fix #130, fix #131, fix #189
This PR implements SnowPi, adds the ability for LEDBoard's to own other
LEDBoards as well as LEDs, and enhances blink so that manually
controlling a LED automatically stops it from blinking (no matter
whether it's blinking itself or a LEDBoard is blinking it).

It also fixes up RGBLED and Motor which I managed to break with the last
PR ...
2016-04-01 17:32:46 +01:00
Dave Jones
759a6a58e6 Fix #140, fix #69, fix #185
This PR adds a software SPI implementation. Firstly this removes the
absolute necessity for spidev (#140), which also means when it's not
present things still work (effectively fixes #185), and also enables any
four pins to be used for SPI devices (which don't require the hardware
implementation).

The software implementation is simplistic but still supports clock
polarity and phase, select-high, and variable bits per word. However it
doesn't allow precise speeds to be implemented because it just wibbles
the clock as fast as it can (which being pure Python isn't actually that
fast).

Finally, because this PR involves creating a framework for "shared"
devices (like SPI devices with multiple channels), it made sense to bung
Energenie (#69) in as wells as this is a really simple shared device.
2016-04-01 12:57:17 +01:00
Dave Jones
351a323832 Fix #181
Added source_delay property to SourceMixin which means it'll now appear
everywhere. Default is 0.01 which is just enough to drop CPU usage while
remaining responsive enough for the majority of purposes.
2016-03-19 16:55:04 +00:00
Andrew Scheller
d970af73fd Catch AttributeError inside GPIODevice._read
Fixes #202
2016-02-18 18:53:52 +00:00
Dave Jones
0978b4c459 Merge pull request #192 from waveform80/pigpiod
Fix #180 - Add support for pigpio
2016-02-12 20:15:25 +00:00
Dave Jones
aebe42875d Fix #180 - Add support for pigpio 2016-02-12 20:14:10 +00:00
Dave Jones
83fb6ae8b4 Fix #114 - ultrasonic sensors
Implements support for the HC-SR04 ultrasonic sensor as an input device
class named DistanceSensor
2016-02-12 19:27:24 +00:00
Dave Jones
1f2140a9f5 Work on #87
Implement a test suite, including Travis-CI integration
2016-02-11 21:11:22 +00:00
Dave Jones
8e0c6e243b Refactor low level implementation
This commit is a fairly major piece of work that abstracts all pin
operations (function, state, edge detection, PWM, etc.) into a base
"Pin" class which is then used by input/output/composite devices to
perform all required configuration.

The idea is to pave the way for I2C based IO extenders which can present
additional GPIO ports with similar capabilities to the Pi's "native"
GPIO ports. As a bonus it also abstracts away the reliance on the
RPi.GPIO library to allow alternative pin implementations (e.g. using
RPIO to take advantage of DMA based PWM), or even pure Python
implementations.
2016-02-08 14:54:18 +00:00
Dave Jones
0bc62aee73 Generic docs need reST
Conversion of all docs to reST so that the generic docs can link easily
with the rest of the docs.
2016-01-30 23:40:12 +00:00
Dave Jones
59ba7154c5 Move exceptions to their own sub-module
This removes the circular dependency introduced in PR#137. This also
fixes up an issue in the base meta-class which meant it wasn't working
in Python 3 (only Python 2), and adds a bit to the meta-class to allow
docstrings to be inherited (taken from the rest-docs branch).
2016-01-07 11:01:49 +00:00
Alex Chan
6538d5de7b Add missing InputDeviceError import to devices.py 2016-01-06 14:59:10 +00:00
Dave Jones
c929b9a53b Fix #107
Add forward_device and backward_device to Motor, left_motor and
right_motor to Robot, and ensure all CompositeDevice descendents have a
proper close() method and closed property. Also, add a few more
_check_open calls around the place to make sure GPIODeviceClosed is
properly raised in response to read and writing values.
2015-11-15 18:19:15 +00:00
Dave Jones
fa0a1b3cdd Fix #76, fix #79
This finishes off implementing values and source for all (current)
classes in gpiozero. I'm afraid things get rather complex in this
commit. For starters, we've now got quite a few "aggregate" classes
which necessarily don't descend from GPIODevice. To implement values and
source on these I could either repeat a helluva lot of code or ... turn
to mixin classes. Yeah, it's multiple inheritance time, baby!

Unfortunately multiple inheritance doesn't work with __slots__ but we
really ought to keep functionality that they provide us (raise
AttributeError when an unknown attribute is set). So I've implemented
this with ... erm ... metaclasses. Sorry!
2015-10-22 21:44:42 +01:00
Dave Jones
f6294a5bf4 Fix #76
Makes `value` an alias of `is_active` in the base `GPIODevice`, and
`values` an infinite iterable of this value. Then adds `source` property
to `OutputDevice` allowing simple linkage of devices via iterators.
2015-10-19 11:01:09 +01:00
Dave Jones
6583223299 Fix #74
Add more property aliases and do them properly (no more lambdas and
string lookups) which means we can remove `_alias`. This commit also
defines `__slots__` for all classes which should prevent assignation of
invalid attributes with an AttributeError (more friendly than silently
doing the wrong thing). Finally, it cleans up all the property defs to
use Ben's preferred decorator style.
2015-10-17 22:04:52 +01:00
Ben Nuttall
82319ba672 Cleanup 2015-10-05 12:08:55 +01:00
Dave Jones
7429c03117 Docs clean up part 1
Big push on getting the docs cleaned up before 1.0. Proper wrapping of
everything so it's decently viewable from the command line (or as
decently viewable as markdown can be - the tables will never look great
from the command line).

Only one code change in this PR: rename bouncetime to bounce_time
(everything else is PEP-8, so this probably should be too) and change
its units to seconds from milliseconds (again, all other durations in
the library are in seconds, so it feels inconsistent that this one
isn't; for the sake of those who won't read the docs - which is most
people - I figure consistency helps with guessing!).
2015-10-03 16:24:12 +01:00
Dave Jones
034b88b478 Add close method and context manager
Permit devices to be explicitly closed and used as context managers.
Also deal with cleanup properly at script end and ensure objects don't
step on the global cleanup function.
2015-09-29 19:10:01 +01:00
Dave Jones
980e4e7144 Fix #32 2015-09-24 16:45:55 +01:00
Ben Nuttall
cfed671972 Add docstrings, close #26 2015-09-24 11:39:50 +01:00
Dave Jones
6b7a3c8851 Fix #25
Add a nice __repr__ to the GPIODevice base class.

This isn't much but generally I think `__repr__` implementations should
be deliberately simple: firstly, they're frequently used for debugging
so if they're at all complex you risk making a debugging tool buggy
(very annoying!). Secondly, if you pour too much info into them you risk
making the debugging output cluttered, so I tend to prefer keeping it to
straight-forward simple to retrieve/calculate info without excessive
detail (if the user wants more, they can always query it directly).

There is one refinement here: in SmoothedInputDevice, `__repr__` is
tweaked to ensure that when partial is False (the default), and the
queue isn't filled, `__repr__` doesn't block (because it should *never*
block).
2015-09-23 14:07:17 +01:00
Ben Nuttall
65d363b7ed Tidy up 2015-09-22 17:52:19 +01:00
Dave Jones
0c208efff8 Merge branch 'master' into when-active-when-inactive
Conflicts:
	gpiozero/devices.py
	gpiozero/input_devices.py
2015-09-22 12:55:56 +01:00
Dave Jones
b1913e5e39 Add events to all input devices
Fairly major tidy up of the hierarchy as well. There's now a trivial
base class: InputDevice which simply permits reading of state.
WaitableInputDevice descends from this and introduces waitable events
and callbacks, and provides a hook for calling them but needs further
machinery to activate that hook.

DigitalInputDevice (crap name?) descends from WaitableInputDevice and
uses the standard RPi.GPIO callback mechanisms to handle events. This is
intended for use with trivial on/off devices with predictably small
bounce times.

Next is SmoothedInputDevice (crap name?) which also descends from
WaitableInputDevice. This includes a background threaded queue which
constantly monitors the state of the device and provides a running mean
of its state. This is compared to a threshold for determining active /
inactive state. This is intended for use with on/off devices that
"jitter" a lot and for which a running average is therefore appropriate
or for devices which provide an effectively analog readout (like
charging capacitor timings).

MonitorSensor and LightSensor now descend from SmoothedInputDevice, and
Button descends from DigitalInputDevice. All "concrete" classes provide
event aliases appropriate to their function (e.g. when_dark,
when_pressed, etc.)
2015-09-22 12:48:09 +01:00
Ben Nuttall
ae1c31e313 pep8 cleanup 2015-09-22 09:36:25 +01:00
Dave Jones
0d7c7e28fc Lots of fixes
* Move `is_active` to `GPIODevice`; it's equally applicable to inputs
  and outputs so there's no point having it just in inputs
* Flip the pull-up status for `MotionSensor` (it was backwards leading
  to reversed readings from the sensor)
* Add a `threshold` to `MotionSensor` (optional), and `value` (similar
  to `LightSensor`)
* Also expose `pull_up` as a simple bool property
* Rejig `LightSensor` so it also derives from `InputDevice` (it inherits
  enough to make it worthwhile) and so that its API is similar to
  `MotionSensor` (a `value` property with a `*_detected` property, and a
  background threaded queue which constantly monitors values)
2015-09-20 19:41:19 +01:00
Dave Jones
e2ddad6fea Shut down GPIO threads nicely
The motion sensor queue doesn't shut down properly at script end at the
moment and prevents the interpreter shutting down. This is because it's
a non-daemon thread so `__del__` never gets run and so on.

This is a bit of a major PR - I can split it up if you want. Firstly it
makes a common base class called `GPIODevice` for both `InputDevice` and
`OutputDevice`. This just takes care of the read-only pin stuff. Next it
makes a `GPIOThread` class that ensures its a daemon thread, and which
also ensures proper cleanup on shutdown.

Finally, it fixes `MotionSensor` to use the new `GPIOThread` class
(tested this time! Works nicely) and adds the `blink` method to the
`LED` class (which also works nicely this time).
2015-09-18 12:45:30 +01:00