GithubHelp home page GithubHelp logo

Comments (6)

scls19fr avatar scls19fr commented on August 17, 2024

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.

scls19fr avatar scls19fr commented on August 17, 2024

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.

scls19fr avatar scls19fr commented on August 17, 2024

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.

scls19fr avatar scls19fr commented on August 17, 2024

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.

Vido avatar Vido commented on August 17, 2024

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) and release(self), properties pressed(self) and released(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.

scls19fr avatar scls19fr commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.