def scenario_2(): """ Declare system with the inheritance based API """ class MyEvent(Event): def on_start(self, context, event): print(context, '=>', event) print('STARTED') def on_cycle_begin(self, context, event): print('LOOP STARTED') def on_cycle_end(self, context, event): print('LOOP FINISHED') context.stop() def on_stop(self, context, event): print('FINISHED') context.exit() def on_exit(self, context, event): print('EXITING') context = Context() event = MyEvent(context) # Make event active event.activate() # Start looping context.start()
def scenario_1(): """ Declare events with the decorator based API """ # Create containers context = Context() event = Event(context) @event def on_start(context, event): print(context, '=>', event) print('STARTED') @event def on_cycle_begin(context, event): print('LOOP STARTED') @event def on_cycle_end(context, event): print('LOOP FINISHED') context.stop() @event def on_stop(context, event): print('FINISHED') context.exit() @event def on_exit(context, event): print('EXITING') # Make event active event.activate() # Start looping context.start()
## INFO ## ## INFO ## # Import kb modules from kb.RPi2 import Context, Event from kb.RPi2.sensors import LED, FourKeys # Create containers context = Context() event1 = Event(context) event2 = Event(context) # Create exit signals @event1 def on_exit(*args, **kwargs): print('EXITING (from event1)') @event2 def on_exit(*args, **kwargs): print('EXITING (from event2)') # Use variables to store pins in use led_pin = 1 four_keys_pins = {'pin_row' : 2, 'pin_col1' : 3, 'pin_col2' : 4, 'pin_col3' : 5, 'pin_col4' : 6}
## INFO ## ## INFO ## # Import kb modules from kb.RPi2 import Context, Event from kb.RPi2.sensors import LED, FourKeys # Create containers context = Context() event1 = Event(context) event2 = Event(context) # Create exit signals @event1 def on_exit(*args, **kwargs): print('EXITING (from event1)') @event2 def on_exit(*args, **kwargs): print('EXITING (from event2)') # Use variables to store pins in use led_pin = 1 four_keys_pins = { 'pin_row': 2, 'pin_col1': 3, 'pin_col2': 4, 'pin_col3': 5,