File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """This example shows how to add/remove callbacks to particular switch events.
3+ """
4+
5+ from letmecreate .core import switch
6+ from letmecreate .core .switch import SWITCH_1_PRESSED , SWITCH_2_PRESSED
7+ from letmecreate .core .switch import SWITCH_1_RELEASED , SWITCH_2_RELEASED
8+ from time import sleep
9+
10+
11+ def switch_pressed ():
12+ print ("A switch has been pressed" )
13+
14+
15+ def switch_released ():
16+ print ("A switch has been released" )
17+
18+
19+ # Initialise the switch and attach two callbacks
20+ switch .init ()
21+ callback_id_pressed = switch .add_callback (SWITCH_1_PRESSED | SWITCH_2_PRESSED ,
22+ switch_pressed )
23+ callback_id_released = switch .add_callback (SWITCH_1_RELEASED | SWITCH_2_RELEASED ,
24+ switch_released )
25+
26+ # Sleep main thread during 15 seconds.
27+ # The switch wrapper lives in another thread and will call switch_pressed
28+ # and switch_released
29+ print ("Callbacks are now active for 15 seconds." )
30+ sleep (15 )
31+
32+ # Remove callbacks and release switch
33+ # In this case, it is not necessary to call switch.remove_callback()
34+ # since callbacks would get destroyed in switch.release().
35+ switch .remove_callback (callback_id_pressed )
36+ switch .remove_callback (callback_id_released )
37+ switch .release ()
You can’t perform that action at this time.
0 commit comments