Skip to content

Button Module

This module help you to manage integrated button.


Examples:

>>> from pybox.button import SIMPLEBUTTON
>>> but = SIMPLEBUTTON()
>>> but.value
0
>>> # press and hold the button
>>> but.value
1
The module consists basically in two classes
  • SIMPLEBUTTON: to get the value of the button
  • BUTTON: to bind custom functions to button behavior

Classes

SIMPLEBUTTON

SimpleButton class.


Examples:

>>> but = SIMPLEBUTTON()
>>> but.value
0                           # if button is not pressed
Source code in pybox/button.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class SIMPLEBUTTON:
    """*SimpleButton class.*

    ---

    Examples:
        >>> but = SIMPLEBUTTON()
        >>> but.value
        0                           # if button is not pressed
    """

    def __init__(self):
        self.__btn = DigitalInOut(board.GP6)
        self.__btn.direction = Direction.INPUT
        self.__btn.pull = Pull.UP

    def deinit(self):
        """Turn off the button.
        """
        self.__btn.deinit()

    @property
    def value(self) -> int:
        """Raw button value.

        It contains the value of the button based on its status: 1 if pressed, 0 otherwise.

        Examples:
            >>> but.value
            0
        """
        return int(not self.__btn.value)

Attributes

value: int property

Raw button value.

It contains the value of the button based on its status: 1 if pressed, 0 otherwise.

Examples:

>>> but.value
0

Functions

deinit()

Turn off the button.

Source code in pybox/button.py
42
43
44
45
def deinit(self):
    """Turn off the button.
    """
    self.__btn.deinit()

BUTTON

Button class.


Examples:

>>> but = BUTTON()
>>> def press_callback():
...     print("PRESSED")
>>> def release_callback():
...     print("RELEASED")
>>> but.press_handler(press_callback)
>>> but.release_handler(release_callback)
>>> while True:
...     but.update()
Source code in pybox/button.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class BUTTON:
    """*Button class.*

    ---

    Examples:
        >>> but = BUTTON()
        >>> def press_callback():
        ...     print("PRESSED")
        >>> def release_callback():
        ...     print("RELEASED")
        >>> but.press_handler(press_callback)
        >>> but.release_handler(release_callback)
        >>> while True:
        ...     but.update()

    """

    def __init__(self):
        self.__keys = keypad.Keys((board.GP6,), value_when_pressed=False)
        self.__press_function = None
        self.__release_function = None
        self.__press_time = None
        self.__press_timestamp = None

    def update(self) -> None:
        """Update button state on main loop.

        Examples:
            >>> but = BUTTON()
            >>> while True:
            ...     but.update()
        """

        event = self.__keys.events.get()

        if event:
            if event.pressed:
                self.__press_timestamp = time.monotonic()
                if self.__press_function:
                    self.__press_function()
            elif event.released:
                self.__press_time = time.monotonic() - self.__press_timestamp
                if self.__release_function:
                    self.__release_function()

    def press_handler(self, callback: callable = None) -> None:
        """Bind a function to call when button is pressed.

        Args:
            callback: a function to be executed when button is pressed. 

        Examples:
            >>> def press_callback():
            ...     print("PRESSED")
            >>> push = BUTTON()
            >>> push.press_handler(press_callback)
            >>> while True:
            ...     # print PRESSED on press button
            ...     push.update()


        Todo:
            - Add possibility to add arguments to callback
        """

        self.__press_function = callback

    def release_handler(self, callback: callable = None) -> None:
        """Bind a function to call when button is released.

        Args:
            callback (callable): a function to be executed when button is released.

        Examples:
            >>> def release_callback():
            ...     print("RELEASED")
            >>> push = BUTTON()
            >>> push.release_handler(release_callback)
            >>> while True:
            ...     # print RELEASED on release button
            ...     push.update() 

        Todo:
            - Add possibility to add arguments to callback
        """

        self.__release_function = callback

    def deinit(self):
        """Turn off the button.
        """
        self.__keys.deinit()

    @property
    def press_time(self) -> float:
        """Time of pressure.
        """
        return self.__press_time

Attributes

press_time: float property

Time of pressure.

Functions

update() -> None

Update button state on main loop.

Examples:

>>> but = BUTTON()
>>> while True:
...     but.update()
Source code in pybox/button.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def update(self) -> None:
    """Update button state on main loop.

    Examples:
        >>> but = BUTTON()
        >>> while True:
        ...     but.update()
    """

    event = self.__keys.events.get()

    if event:
        if event.pressed:
            self.__press_timestamp = time.monotonic()
            if self.__press_function:
                self.__press_function()
        elif event.released:
            self.__press_time = time.monotonic() - self.__press_timestamp
            if self.__release_function:
                self.__release_function()
press_handler(callback: callable = None) -> None

Bind a function to call when button is pressed.

Parameters:

  • callback (callable, default: None ) –

    a function to be executed when button is pressed.

Examples:

>>> def press_callback():
...     print("PRESSED")
>>> push = BUTTON()
>>> push.press_handler(press_callback)
>>> while True:
...     # print PRESSED on press button
...     push.update()
Todo
  • Add possibility to add arguments to callback
Source code in pybox/button.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def press_handler(self, callback: callable = None) -> None:
    """Bind a function to call when button is pressed.

    Args:
        callback: a function to be executed when button is pressed. 

    Examples:
        >>> def press_callback():
        ...     print("PRESSED")
        >>> push = BUTTON()
        >>> push.press_handler(press_callback)
        >>> while True:
        ...     # print PRESSED on press button
        ...     push.update()


    Todo:
        - Add possibility to add arguments to callback
    """

    self.__press_function = callback
release_handler(callback: callable = None) -> None

Bind a function to call when button is released.

Parameters:

  • callback (callable, default: None ) –

    a function to be executed when button is released.

Examples:

>>> def release_callback():
...     print("RELEASED")
>>> push = BUTTON()
>>> push.release_handler(release_callback)
>>> while True:
...     # print RELEASED on release button
...     push.update() 
Todo
  • Add possibility to add arguments to callback
Source code in pybox/button.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def release_handler(self, callback: callable = None) -> None:
    """Bind a function to call when button is released.

    Args:
        callback (callable): a function to be executed when button is released.

    Examples:
        >>> def release_callback():
        ...     print("RELEASED")
        >>> push = BUTTON()
        >>> push.release_handler(release_callback)
        >>> while True:
        ...     # print RELEASED on release button
        ...     push.update() 

    Todo:
        - Add possibility to add arguments to callback
    """

    self.__release_function = callback
deinit()

Turn off the button.

Source code in pybox/button.py
149
150
151
152
def deinit(self):
    """Turn off the button.
    """
    self.__keys.deinit()