Comments (6)
I noticed in https://github.com/pingo-io/pingo-py/blob/master/pingo/parts/test/test_switch.py
class FakeDigitalPin(object):
def __init__(self):
self.mode = 'IN'
self.state = 'LOW'
Maybe this class shouldn't be there (in a test) but should be merged with DigitalPin
https://github.com/pingo-io/pingo-py/blob/master/pingo/board.py
from pingo-py.
When but
is initialized with a pin
(a DigitalPin
) from a GhostBoard
, press
and release
method should change internal state of this pin
but when but
is initialized with a pin
(a DigitalPin
) from a "real" board it should raise an exception.
from pingo-py.
Here is a possible implementation
import pingo
import time
class PushButton(object):
"""A single Push button"""
def __init__(self, pin, pressed_state=pingo.LOW):
"""Set pressed_state to pingo.LOW when button is pressed
:param pin: A instance of DigitalPin
:param pressed_state: use pingo.LOW for pull-up, pingo.HIGH for pull-down
"""
pin.mode = pingo.IN
self.pin = pin
self._pressed_state = pressed_state
d = {
pingo.LOW: pingo.HIGH,
pingo.HIGH: pingo.LOW,
}
self._released_state = d[pressed_state]
def _test_pin_instance(self):
if not isinstance(self.pin.board, pingo.ghost.ghost.GhostBoard):
raise(NotImplementedError('pin from a GhostBoard is required'))
elif not isinstance(self.pin, pingo.board.DigitalPin):
raise(NotImplementedError('DigitalPin is required'))
def press(self):
self._test_pin_instance()
board = self.pin.board
pin = self.pin
board._set_pin_state(pin, self._pressed_state)
def release(self):
self._test_pin_instance()
board = self.pin.board
pin = self.pin
board._set_pin_state(pin, self._released_state)
@property
def pressed(self):
return True if self.pin.state == self._pressed_state else False
@property
def released(self):
return True if self.pin.state == self._released_state else False
from pingo-py.
It may be worth to have a look at https://github.com/RPi-Distro/python-gpiozero/blob/master/gpiozero/input_devices.py#L387
from pingo-py.
Hallo @scls19fr ,
I guess I don't get it. Do you need a 'mock-up' Button-object for testing?
Let me list the features you proposed:
- methods
press(self)
andrelease(self)
, propertiespressed(self)
andreleased(self)
- This class will be enable to switch on and off a
DigitalPin
So it would behave like a virtual Button, which can set a state via software.
There is something that's not clear:
On the example you posted, pin.mode
is set to pingo.IN
. In fact, you should not (or perhaps, can not) change a state when pingo.IN
is set (when working with a real hardware).
Given you are using GhostBoard
, I believe what you are looking for is a way to simulate a "input device". Am I right?
The Question: What is the main purpose of this feature?
(testing, using GhostBoard or Something else?)
About FakeDigitalPin, it is used for testing. It is not exposed on the API.
from pingo-py.
The goal of such a virtual part is to run unit tests for a app with a virtual push button.
I use it in https://github.com/RaspberryPi-Samples/py-my-key see https://github.com/RaspberryPi-Samples/py-my-key/blob/master/tests/test_py_my_key.py
you can see
my_app.hw.black_btn.press()
...
my_app.hw.black_btn.release()
and in https://github.com/RaspberryPi-Samples/py-my-key/blob/master/py_my_key/cli_rpi_access.py#L97
you can also see
if self.hw.black_btn.pressed and self.hw.red_btn.released:
...
from pingo-py.
Related Issues (20)
- Python 3 support HOT 2
- board = pingo.detect.get_board() or board = pingo.detect.MyBoard() ? HOT 1
- time to sleep should be configurable
- Switch / led parts : stop blinking led should fallback to previous led state (before blinking)
- Using a signal / slot pattern for parts ?
- Pingo and RPi-Distro/python-gpiozero HOT 4
- Failed to detect HOT 2
- Is there any preview for getting Pingo in Python 3.5 HOT 3
- Support WRTnode
- raise ArgumentOutOfRange() missing apropriated value
- The GPIOs of Galileo2 only have location, they not have gpio_id HOT 1
- PWM on broken on Arduino HOT 1
- test_jumpwire fails on my Raspberry Pi 2 model B HOT 3
- New to pingo, can't get it working ! HOT 2
- Create a Debian package and submit to mentors.debian.net HOT 2
- Version on Pypi differs to tag version in github HOT 1
- Automagically create mocks (fakes) when the app is running outside a board HOT 5
- Switch / Push button : short pressed / long pressed events
- Pingo, MQTT and IoT
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pingo-py.