@@ -1761,12 +1761,48 @@ def Led_all_off():
1761
1761
1762
1762
1763
1763
#~autogen
1764
- #~autogen button-class classes.button>currentClass
1765
1764
1766
- import fcntl
1767
- import array
1765
+ class ButtonBase (object ):
1766
+ """
1767
+ Abstract button interface.
1768
+ """
1769
+
1770
+ on_change = None
1771
+
1772
+ _state = set ([])
1773
+
1774
+ @property
1775
+ def any (self ):
1776
+ """
1777
+ Checks if any button is pressed.
1778
+ """
1779
+ return bool (self .buttons_pressed )
1768
1780
1769
- class Button (object ):
1781
+ def check_buttons (self ,buttons = []):
1782
+ """
1783
+ Check if currently pressed buttons exactly match the given list.
1784
+ """
1785
+ return set (self .buttons_pressed ) == set (buttons )
1786
+
1787
+ def process (self ):
1788
+ """
1789
+ Check for currenly pressed buttons. If the new state differs from the
1790
+ old state, call the appropriate button event handlers.
1791
+ """
1792
+ new_state = set (self .buttons_pressed )
1793
+ old_state = self ._state
1794
+ self ._state = new_state
1795
+
1796
+ state_diff = new_state .symmetric_difference (old_state )
1797
+ for button in state_diff :
1798
+ handler = getattr (self , 'on_' + button )
1799
+ if handler is not None : handler (button in new_state )
1800
+
1801
+ if self .on_change is not None and state_diff :
1802
+ self .on_change ([(button , button in new_state ) for button in state_diff ])
1803
+
1804
+ #~autogen button-class classes.button>currentClass
1805
+ class Button (ButtonBase ):
1770
1806
1771
1807
"""
1772
1808
Provides a generic button reading mechanism that can be adapted
@@ -1805,31 +1841,30 @@ def _button_buffer(self, name):
1805
1841
self .buffer_cache [name ] = array .array ( 'B' , [0 ] * self .KEY_BUF_LEN )
1806
1842
return self .buffer_cache [name ]
1807
1843
1808
- def read_buttons (self ):
1844
+ @property
1845
+ def buttons_pressed (self ):
1809
1846
for b in self .buffer_cache :
1810
1847
fcntl .ioctl (self .filehandle_cache [b ], self .EVIOCGKEY , self .buffer_cache [b ])
1811
1848
1812
- @property
1813
- def buttons_pressed (self ):
1814
1849
pressed = []
1815
- self .read_buttons ()
1816
1850
for k ,v in self ._buttons .items ():
1817
1851
buf = self .buffer_cache [v ['name' ]]
1818
1852
bit = v ['value' ]
1819
1853
if not bool (buf [int (bit / 8 )] & 1 << bit % 8 ):
1820
1854
pressed += [k ]
1821
1855
return pressed
1822
1856
1823
- @property
1824
- def any (self ):
1825
- return bool (self .buttons_pressed )
1826
-
1827
- def check_buttons (self ,buttons = []):
1828
- return set (self .buttons_pressed ) == set (buttons )
1829
-
1830
1857
if current_platform () == 'ev3' :
1831
1858
#~autogen button-property platforms.ev3.button>currentClass
1832
- _buttons = {
1859
+
1860
+ on_up = None
1861
+ on_down = None
1862
+ on_left = None
1863
+ on_right = None
1864
+ on_enter = None
1865
+ on_backspace = None
1866
+
1867
+ _buttons = {
1833
1868
'up' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 103 },
1834
1869
'down' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 108 },
1835
1870
'left' : { 'name' : '/dev/input/by-path/platform-gpio-keys.0-event' , 'value' : 105 },
@@ -1865,108 +1900,95 @@ def backspace(self):
1865
1900
1866
1901
#~autogen
1867
1902
1868
- class RemoteControl (object ):
1903
+ #~autogen remote-control classes.infraredSensor.remoteControl>currentClass
1904
+ class RemoteControl (ButtonBase ):
1869
1905
"""
1870
- EV3 Remote Control
1906
+ EV3 Remote Controller
1871
1907
"""
1872
1908
1873
1909
_BUTTON_VALUES = {
1874
- 1 : ['red_up' ],
1875
- 2 : ['red_down' ],
1876
- 3 : ['blue_up' ],
1877
- 4 : ['blue_down' ],
1878
- 5 : ['red_up' , 'blue_up' ],
1879
- 6 : ['red_up' , 'blue_down' ],
1880
- 7 : ['red_down' , 'blue_up' ],
1881
- 8 : ['red_down' , 'blue_down' ],
1882
- 9 : ['beacon' ],
1883
- 10 : ['red_up' , 'red_down' ],
1884
- 11 : ['blue_up' , 'blue_down' ]
1910
+ 0 : [],
1911
+ 1 : ['red_up' ],
1912
+ 2 : ['red_down' ],
1913
+ 3 : ['blue_up' ],
1914
+ 4 : ['blue_down' ],
1915
+ 5 : ['red_up' , 'blue_up' ],
1916
+ 6 : ['red_up' , 'blue_down' ],
1917
+ 7 : ['red_down' , 'blue_up' ],
1918
+ 8 : ['red_down' , 'blue_down' ],
1919
+ 9 : ['beacon' ],
1920
+ 10 : ['red_up' , 'red_down' ],
1921
+ 11 : ['blue_up' , 'blue_down' ]
1885
1922
}
1886
1923
1887
- on_red_up = None
1888
- on_red_down = None
1889
- on_blue_up = None
1924
+ on_red_up = None
1925
+ on_red_down = None
1926
+ on_blue_up = None
1890
1927
on_blue_down = None
1891
- on_beacon = None
1892
- on_change = None
1928
+ on_beacon = None
1893
1929
1894
- def __init__ (self , sensor = None , channel = 1 ):
1895
- if sensor is None :
1896
- self ._sensor = InfraredSensor ()
1897
- else :
1898
- self ._sensor = sensor
1899
-
1900
- self ._channel = max (1 , min (4 , channel )) - 1
1901
- self ._state = set ([])
1902
-
1903
- if self ._sensor .connected :
1904
- self ._sensor .mode = 'IR-REMOTE'
1905
-
1906
- @property
1907
- def buttons_pressed (self ):
1908
- """
1909
- Returns list of currently pressed buttons.
1910
- """
1911
- return RemoteControl ._BUTTON_VALUES .get (self ._sensor .value (self ._channel ), [])
1912
-
1913
- @property
1914
- def any (self ):
1915
- """
1916
- Checks if any button is pressed.
1917
- """
1918
- return bool (self .buttons_pressed )
1919
-
1920
- def check_buttons (self , buttons = []):
1921
- return set (self .buttons_pressed ) == set (buttons )
1922
1930
1923
1931
@property
1924
1932
def red_up (self ):
1925
1933
"""
1926
- Checks if `red_up` button is pressed
1934
+ Checks if `red_up` button is pressed.
1927
1935
"""
1928
1936
return 'red_up' in self .buttons_pressed
1929
1937
1938
+
1930
1939
@property
1931
1940
def red_down (self ):
1932
1941
"""
1933
- Checks if `red_down` button is pressed
1942
+ Checks if `red_down` button is pressed.
1934
1943
"""
1935
1944
return 'red_down' in self .buttons_pressed
1936
1945
1946
+
1937
1947
@property
1938
1948
def blue_up (self ):
1939
1949
"""
1940
- Checks if `blue_up` button is pressed
1950
+ Checks if `blue_up` button is pressed.
1941
1951
"""
1942
1952
return 'blue_up' in self .buttons_pressed
1943
1953
1954
+
1944
1955
@property
1945
1956
def blue_down (self ):
1946
1957
"""
1947
- Checks if `blue_down` button is pressed
1958
+ Checks if `blue_down` button is pressed.
1948
1959
"""
1949
1960
return 'blue_down' in self .buttons_pressed
1950
1961
1962
+
1951
1963
@property
1952
1964
def beacon (self ):
1953
1965
"""
1954
- Checks if `beacon` button is pressed
1966
+ Checks if `beacon` button is pressed.
1955
1967
"""
1956
1968
return 'beacon' in self .buttons_pressed
1957
1969
1958
- def process (self ):
1959
- new_state = set (self .buttons_pressed )
1960
- old_state = self ._state
1961
- self ._state = new_state
1962
1970
1963
- state_diff = new_state .symmetric_difference (old_state )
1964
- for button in state_diff :
1965
- handler = getattr (self , 'on_' + button )
1966
- if handler is not None : handler (button in new_state )
1967
1971
1968
- if self .on_change is not None and state_diff :
1969
- self .on_change ([(button , button in new_state ) for button in state_diff ])
1972
+ #~autogen
1973
+
1974
+ def __init__ (self , sensor = None , channel = 1 ):
1975
+ if sensor is None :
1976
+ self ._sensor = InfraredSensor ()
1977
+ else :
1978
+ self ._sensor = sensor
1979
+
1980
+ self ._channel = max (1 , min (4 , channel )) - 1
1981
+ self ._state = set ([])
1982
+
1983
+ if self ._sensor .connected :
1984
+ self ._sensor .mode = 'IR-REMOTE'
1985
+
1986
+ @property
1987
+ def buttons_pressed (self ):
1988
+ """
1989
+ Returns list of currently pressed buttons.
1990
+ """
1991
+ return RemoteControl ._BUTTON_VALUES .get (self ._sensor .value (self ._channel ), [])
1970
1992
1971
1993
#~autogen generic-class classes.powerSupply>currentClass
1972
1994
0 commit comments