how to use the two Calliope buttons to develop interactive programmes.
REACT TO PRESSING A BUTTON
The Calliope has two programmable buttons. The blue one is labelled A, the red one B .
The function button_a.is_pressed() returns True if the left button is pressed (analogue button_b_is pressed()).
Your programme waits in an infinite loop until you press button A. Then the LEDs arranged in a cross are switched on for as long as you keep the button pressed.
Program:
from calliope_mini import *
while True:
if button_a.is_pressed():
display.show(Image.NO)
else:
display.clear()
sleep(10)
The seemingly superfluous sleep(10) is important so that you don't waste unnecessary computer resources if the programme doesn't have to do anything other than check whether the button is pressed. In technical terms, the state of the button is also said to be ‘polled’ in the infinite loop.
REACTING TO THE CLICK OF A BUTTON
Just as you know it from mouse clicks, you want to trigger an action with a button click (not by holding it down). To do this, you use the button_a.was_pressed() function. This returns True if you have pressed the button briefly at any time.
In your example, clicking on button A displays the image SQUARE and clicking on button B displays the image NO. In contrast to the first example, you do not have to keep the buttons pressed.
Program:
from calliope_mini import *
def blink(x, y):
display.set_pixel(x, y, 9)
sleep(500)
display.set_pixel(x, y, 0)
sleep(500)
while True:
if button_a.is_pressed():
display.show(Image.SQUARE)
sleep(1000)
display.clear()
blink(2, 2)
sleep(10)
Just as you know it from mouse clicks, you can also interrupt a running programme with a button click and execute another action. In an endless while loop, the centre LED flashes with a period of 200 ms. Click on button A to interrupt the flashing and display a square for 1000 ms. The programme then resumes flashing.
You also use the button_a.was_pressed() function here. The click is regarded as an event that is registered by the system even if your programme is doing something else.
Program:
from calliope_mini import *
def blink(x, y):
display.set_pixel(x, y, 9)
sleep(500)
display.set_pixel(x, y, 0)
sleep(500)
while True:
if button_a.was_pressed():
display.show(Image.SQUARE)
sleep(1000)
display.clear()
blink(2, 2)
sleep(10)
You can develop interactive programmes that react to a button being held down or to a button click. With the is_pressed() function, the button must be pressed for True to be returned; with the was_pressed() function, True is returned if the button has been clicked at any time since the programme was started or since the last button click.
TO SOLVE BY YOURSELF
1.
Programme the buttons as follows:
If you press button A, an arrow pointing to the left (ARROW_W) appears, if you press the right button, an arrow pointing to the right (ARROW_E) appears.
2.
Programme a click counter: Each time you click on button A, the number n, which is initially 0, is increased by one and written out in the terminal window for checking purposes. When button B is clicked, the total number should appear as scrolling text on the display.
Note The scroll() function can only display text. With str(n) you can convert a number into a text.
3.
You simulate throwing a dice with the Calliope. Each time you click on button A, the image of a random side of the dice is shown on the display.
Note:
The easiest way is to create the images as images, e.g. the four with
img = Image('00000:09090:00000:09090:00000:')