@@ -1727,8 +1727,11 @@ class Button(object):
17271727 EVIOCGKEY = (2 << (14 + 8 + 8 ) | KEY_BUF_LEN << (8 + 8 ) | ord ('E' ) << 8 | 0x18 )
17281728
17291729 def __init__ (self ):
1730- self ._buf = array . array ( 'B' , [ 0 ] * self . KEY_BUF_LEN )
1730+ self .buffer_cache = {}
17311731 self .filehandle_cache = {}
1732+ for b in self ._buttons :
1733+ self ._button_file ( self ._buttons [b ]['name' ] )
1734+ self ._button_buffer ( self ._buttons [b ]['name' ] )
17321735
17331736 def _button_file (self , name ):
17341737 if name not in self .filehandle_cache :
@@ -1738,40 +1741,79 @@ def _button_file(self, name):
17381741 f = self .filehandle_cache [name ]
17391742 return f
17401743
1741- def read_button (self , name , button ):
1742- ret = fcntl .ioctl (self ._button_file (name ), self .EVIOCGKEY , self ._buf )
1743- if (ret < 0 ):
1744- return None
1745- else :
1746- return not bool (self ._buf [int (button / 8 )] & 1 << button % 8 )
1744+ def _button_buffer (self , name ):
1745+ if name not in self .buffer_cache :
1746+ self .buffer_cache [name ] = array .array ( 'B' , [0 ] * self .KEY_BUF_LEN )
1747+ return self .buffer_cache [name ]
1748+
1749+ def read_buttons (self ):
1750+ for b in self .buffer_cache :
1751+ fcntl .ioctl (self .filehandle_cache [b ], self .EVIOCGKEY , self .buffer_cache [b ])
1752+
1753+ def check_buttons (self ):
1754+ pressed = []
1755+ self .read_buttons ()
1756+ for k ,v in self ._buttons .items ():
1757+ buf = self .buffer_cache [v ['name' ]]
1758+ bit = v ['value' ]
1759+ if not bool (buf [int (bit / 8 )] & 1 << bit % 8 ):
1760+ pressed += [k ]
1761+ return pressed
1762+
1763+ @property
1764+ def any (self ):
1765+ return bool (len (self .check_buttons ()) != 0 )
1766+
1767+ @property
1768+ def which (self ):
1769+ return self .check_buttons ()
1770+
1771+ def these (self ,buttons = {}):
1772+ if len (buttons ) == 0 :
1773+ return False
1774+ s = self .check_buttons ()
1775+ if len (s ) == 0 :
1776+ return False
1777+ for b in buttons :
1778+ if b not in s :
1779+ return False
1780+ return True
17471781
17481782#~autogen
17491783 if current_platform () == 'ev3' :
17501784#~autogen button-property platforms.ev3.button>currentClass
1785+ _buttons = {
1786+ 'up' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 103 },
1787+ 'down' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 108 },
1788+ 'left' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 105 },
1789+ 'right' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 106 },
1790+ 'enter' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 28 },
1791+ 'backspace' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 14 },
1792+ }
17511793
17521794 @property
17531795 def up (self ):
1754- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 103 )
1796+ return 'up' in self . check_buttons ( )
17551797
17561798 @property
17571799 def down (self ):
1758- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 108 )
1800+ return 'down' in self . check_buttons ( )
17591801
17601802 @property
17611803 def left (self ):
1762- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 105 )
1804+ return 'left' in self . check_buttons ( )
17631805
17641806 @property
17651807 def right (self ):
1766- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 106 )
1808+ return 'right' in self . check_buttons ( )
17671809
17681810 @property
17691811 def enter (self ):
1770- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 28 )
1812+ return 'enter' in self . check_buttons ( )
17711813
17721814 @property
17731815 def backspace (self ):
1774- return self . read_button ( '/dev/input/by-path/platform-gpio-keys.0-event' , 14 )
1816+ return 'backspace' in self . check_buttons ( )
17751817
17761818
17771819#~autogen
0 commit comments