Skip to content

Commit 8c680eb

Browse files
committed
Merge pull request #69 from ddemidov/button-doc
Improve documentation for Button and RemoteControl classes
2 parents dced8aa + 47a9192 commit 8c680eb

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Sensors
101101

102102
.. autoclass:: RemoteControl
103103
:members:
104+
:inherited-members:
104105

105106
Other
106107
^^^^^
@@ -113,6 +114,7 @@ Other
113114

114115
.. autoclass:: Button
115116
:members:
117+
:inherited-members:
116118

117119
.. autoclass:: Sound
118120
:members:

ev3dev/core.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,14 @@ class ButtonBase(object):
16941694
Abstract button interface.
16951695
"""
16961696

1697-
on_change = None
1697+
@staticmethod
1698+
def on_change(changed_buttons):
1699+
"""
1700+
This handler is called by `process()` whenever state of any button has
1701+
changed since last `process()` call. `changed_buttons` is a list of
1702+
tuples of changed button names and their states.
1703+
"""
1704+
pass
16981705

16991706
_state = set([])
17001707

@@ -1763,6 +1770,9 @@ def _button_buffer(self, name):
17631770

17641771
@property
17651772
def buttons_pressed(self):
1773+
"""
1774+
Returns list of names of pressed buttons.
1775+
"""
17661776
for b in self._buffer_cache:
17671777
fcntl.ioctl(self._button_file(b), self.EVIOCGKEY, self._buffer_cache[b])
17681778

ev3dev/ev3.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,67 @@ def all_off():
109109

110110
# ~autogen
111111

112-
class Button(ButtonEVIO):
112+
class Button(object):
113113
"""
114114
EV3 Buttons
115115
"""
116116

117117
# ~autogen button-property platforms.ev3.button>currentClass
118118

119-
on_up = None
120-
on_down = None
121-
on_left = None
122-
on_right = None
123-
on_enter = None
124-
on_backspace = None
119+
@staticmethod
120+
def on_up(state):
121+
"""
122+
This handler is called by `process()` whenever state of 'up' button
123+
has changed since last `process()` call. `state` parameter is the new
124+
state of the button.
125+
"""
126+
pass
127+
128+
@staticmethod
129+
def on_down(state):
130+
"""
131+
This handler is called by `process()` whenever state of 'down' button
132+
has changed since last `process()` call. `state` parameter is the new
133+
state of the button.
134+
"""
135+
pass
136+
137+
@staticmethod
138+
def on_left(state):
139+
"""
140+
This handler is called by `process()` whenever state of 'left' button
141+
has changed since last `process()` call. `state` parameter is the new
142+
state of the button.
143+
"""
144+
pass
145+
146+
@staticmethod
147+
def on_right(state):
148+
"""
149+
This handler is called by `process()` whenever state of 'right' button
150+
has changed since last `process()` call. `state` parameter is the new
151+
state of the button.
152+
"""
153+
pass
154+
155+
@staticmethod
156+
def on_enter(state):
157+
"""
158+
This handler is called by `process()` whenever state of 'enter' button
159+
has changed since last `process()` call. `state` parameter is the new
160+
state of the button.
161+
"""
162+
pass
163+
164+
@staticmethod
165+
def on_backspace(state):
166+
"""
167+
This handler is called by `process()` whenever state of 'backspace' button
168+
has changed since last `process()` call. `state` parameter is the new
169+
state of the button.
170+
"""
171+
pass
172+
125173

126174
_buttons = {
127175
'up': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 103},
@@ -134,26 +182,44 @@ class Button(ButtonEVIO):
134182

135183
@property
136184
def up(self):
185+
"""
186+
Check if 'up' button is pressed.
187+
"""
137188
return 'up' in self.buttons_pressed
138189

139190
@property
140191
def down(self):
192+
"""
193+
Check if 'down' button is pressed.
194+
"""
141195
return 'down' in self.buttons_pressed
142196

143197
@property
144198
def left(self):
199+
"""
200+
Check if 'left' button is pressed.
201+
"""
145202
return 'left' in self.buttons_pressed
146203

147204
@property
148205
def right(self):
206+
"""
207+
Check if 'right' button is pressed.
208+
"""
149209
return 'right' in self.buttons_pressed
150210

151211
@property
152212
def enter(self):
213+
"""
214+
Check if 'enter' button is pressed.
215+
"""
153216
return 'enter' in self.buttons_pressed
154217

155218
@property
156219
def backspace(self):
220+
"""
221+
Check if 'backspace' button is pressed.
222+
"""
157223
return 'backspace' in self.buttons_pressed
158224

159225

templates/button-property.liquid

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
{% for instance in currentClass.instances %}
2-
on_{{ instance.name }} = None{%
3-
endfor %}
2+
@staticmethod
3+
def on_{{ instance.name }}(state):
4+
"""
5+
This handler is called by `process()` whenever state of '{{ instance.name }}' button
6+
has changed since last `process()` call. `state` parameter is the new
7+
state of the button.
8+
"""
9+
pass
10+
{% endfor %}
411

512
_buttons = {
613
{% for instance in currentClass.instances %} '{{ instance.name }}': {'name': '{{ currentClass.systemPath }}/{{ instance.systemName }}', 'value': {{ instance.systemValue }}},
714
{% endfor %} }
815
{% for instance in currentClass.instances %}
916
@property
1017
def {{ instance.name }}(self):
18+
"""
19+
Check if '{{ instance.name }}' button is pressed.
20+
"""
1121
return '{{ instance.name }}' in self.buttons_pressed
1222
{% endfor %}

0 commit comments

Comments
 (0)