@@ -2296,44 +2296,86 @@ class RemoteControl(object):
2296
2296
11 : ['blue_up' , 'blue_down' ]
2297
2297
}
2298
2298
2299
+ on_red_up = None
2300
+ on_red_down = None
2301
+ on_blue_up = None
2302
+ on_blue_down = None
2303
+ on_beacon = None
2304
+ on_change = None
2305
+
2299
2306
def __init__ (self , sensor = None , channel = 1 ):
2300
2307
if sensor is None :
2301
2308
self ._sensor = InfraredSensor ()
2302
2309
else :
2303
2310
self ._sensor = sensor
2304
2311
2305
2312
self ._channel = max (1 , min (4 , channel )) - 1
2313
+ self ._state = set ([])
2306
2314
2307
2315
if self ._sensor .connected :
2308
2316
self ._sensor .mode = 'IR-REMOTE'
2309
2317
2310
2318
@property
2311
2319
def buttons_pressed (self ):
2320
+ """
2321
+ Returns list of currently pressed buttons.
2322
+ """
2312
2323
return RemoteControl ._BUTTON_VALUES .get (self ._sensor .value (self ._channel ), [])
2313
2324
2314
2325
@property
2315
2326
def any (self ):
2327
+ """
2328
+ Checks if any button is pressed.
2329
+ """
2316
2330
return bool (self .buttons_pressed )
2317
2331
2318
2332
def check_buttons (self , buttons = []):
2319
2333
return set (self .buttons_pressed ) == set (buttons )
2320
2334
2321
2335
@property
2322
2336
def red_up (self ):
2337
+ """
2338
+ Checks if `red_up` button is pressed
2339
+ """
2323
2340
return 'red_up' in self .buttons_pressed
2324
2341
2325
2342
@property
2326
2343
def red_down (self ):
2344
+ """
2345
+ Checks if `red_down` button is pressed
2346
+ """
2327
2347
return 'red_down' in self .buttons_pressed
2328
2348
2329
2349
@property
2330
2350
def blue_up (self ):
2351
+ """
2352
+ Checks if `blue_up` button is pressed
2353
+ """
2331
2354
return 'blue_up' in self .buttons_pressed
2332
2355
2333
2356
@property
2334
2357
def blue_down (self ):
2358
+ """
2359
+ Checks if `blue_down` button is pressed
2360
+ """
2335
2361
return 'blue_down' in self .buttons_pressed
2336
2362
2337
2363
@property
2338
2364
def beacon (self ):
2365
+ """
2366
+ Checks if `beacon` button is pressed
2367
+ """
2339
2368
return 'beacon' in self .buttons_pressed
2369
+
2370
+ def process (self ):
2371
+ new_state = set (self .buttons_pressed )
2372
+ old_state = self ._state
2373
+ self ._state = new_state
2374
+
2375
+ state_diff = new_state .symmetric_difference (old_state )
2376
+ for button in state_diff :
2377
+ handler = getattr (self , 'on_' + button )
2378
+ if handler is not None : handler (button in new_state )
2379
+
2380
+ if self .on_change is not None and state_diff :
2381
+ self .on_change ([(button , button in new_state ) for button in state_diff ])
0 commit comments