Skip to content

Commit 1a0e62a

Browse files
committed
Add autogen button class - still needs autogen for platform specific buttons
1 parent a834d09 commit 1a0e62a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

ev3dev.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,70 @@ def Led_all_off():
17011701
Led.all_off = Led_all_off
17021702

17031703

1704+
#~autogen
1705+
#~autogen button-class classes.button>currentClass
1706+
1707+
import fcntl
1708+
import array
1709+
1710+
class Button():
1711+
1712+
"""
1713+
Provides a generic button reading mechanism that can be adapted
1714+
to platform specific implementations. Each platform's specific
1715+
button capabilites are enumerated in the 'platforms' section
1716+
of this specification
1717+
"""
1718+
1719+
KEY_MAX = 0x2FF
1720+
KEY_BUF_LEN = int((KEY_MAX + 7) / 8)
1721+
EVIOCGKEY = (2 << (14 + 8 + 8) | KEY_BUF_LEN << (8 + 8) | ord('E') << 8 | 0x18)
1722+
1723+
def __init__(self):
1724+
self._buf = array.array( 'B', [0] * self.KEY_BUF_LEN )
1725+
self.filehandle_cache = {}
1726+
1727+
def _button_file(self, name):
1728+
if name not in self.filehandle_cache:
1729+
f = open( name, 'r' )
1730+
self.filehandle_cache[name] = f
1731+
else:
1732+
f = self.filehandle_cache[name]
1733+
return f
1734+
1735+
def read_button(self, name, button):
1736+
ret = fcntl.ioctl(self._button_file('/dev/input/by-path/platform-gpio-keys.0-event'), self.EVIOCGKEY, self._buf)
1737+
if (ret < 0):
1738+
return None
1739+
else:
1740+
return not bool(self._buf[int(button / 8)] & 1 << button % 8)
1741+
1742+
@property
1743+
def up(self):
1744+
return self.read_button( 'platform-gpio-keys.0-event', 103 )
1745+
1746+
@property
1747+
def down(self):
1748+
return self.read_button( 'platform-gpio-keys.0-event', 108 )
1749+
1750+
@property
1751+
def left(self):
1752+
return self.read_button( 'platform-gpio-keys.0-event', 105 )
1753+
1754+
@property
1755+
def right(self):
1756+
return self.read_button( 'platform-gpio-keys.0-event', 106 )
1757+
1758+
@property
1759+
def enter(self):
1760+
return self.read_button( 'platform-gpio-keys.0-event', 28 )
1761+
1762+
@property
1763+
def backspace(self):
1764+
return self.read_button( 'platform-gpio-keys.0-event', 14 )
1765+
1766+
1767+
17041768
#~autogen
17051769
#~autogen generic-class classes.powerSupply>currentClass
17061770

0 commit comments

Comments
 (0)