We are using a callback handler to respond to button presses, defined in the pressed() function:
def pressed():
led.toggle() # (3)
state = 'on' if led.value == 1 else 'off' # (4)
print("Button pressed: LED is " + state) # (5)
On line (3), our LED is turned on and off each time pressed() is invoked using the toggle() method of led. On line (4), we query the value property of led to determine whether the LED is on (value == 1) or off (value == 0) and store it in the state variable, which we print to the Terminal on line (5).
You can also control the LED with the led.on(), led.off(), and led.blink() methods. You can also directly set the LED on/off state by setting led.value, for example, led.value = 1 will turn the LED on.
Let's continue and see how to create and configure a Button class instance and register the pressed() function so it is called when...