RASP-SENSE-HAT-ASTRO-PI-Débuter

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Astro Pi is an add-on board for the Raspberry Pi, which adds the ability to sense all kinds of things and output information using a built-in 8x8 LED matrix. You can find out more about Astro Pi by following the Astro Pi Guide, which will show you how to connect and test your Astro Pi board. It also has some helpful explanations and examples of what the different inputs and outputs can do.

Once you are set up and have run your first program using the guide, you can begin to experiment further using this worksheet. In order to write your programs you will need to boot your Raspberry Pi to the desktop and start IDLE3, like you did in the guide.

sudo idle3 &

Afficher du Texte

When following the guide you will have written a sample program which scrolls text across the LED matrix. The program contains two crucial lines, which import the Astro Pi software and create an ap object which represents the Astro Pi board.

from astro_pi import AstroPi

ap = AstroPi()

The third line is the one that makes the Astro Pi do something:

ap.show_message("Hello my name is Tim Peake")

You have probably already discovered that you can easily change the message to your own text, but there are more things that we can do.

1. We can expand the ap.show_message command to include some extra parameters which will change the behaviour of the message.

Parameter Effect
scroll_speed The scroll_speed parameter affects how quickly the text moves on the screen. The default value is 0.1. The bigger the number, the slower the speed
text_colour The text_colour parameter alters the colour of the text and is specified using 3 values for Red, Green, Blue. Each value can be between 0 - 255, so [255,0,255] would be Red + Blue = Purple
back_colour The back_colour parameter alters the colour of the background and works in the same way as the text_colour

So this program would display the text Astro Pi is awesome!! more slowly, with the text in yellow [255,255,0] and the background in blue [0,0,255]:

from astro_pi import AstroPi

ap = AstroPi()

ap.show_message("Astro Pi is awesome!!", scroll_speed=0.05, text_colour=[255,255,0], back_colour=[0,0,255])

You could also make the message repeat using a while loop like this:

from astro_pi import AstroPi

ap = AstroPi()

while True:
    ap.show_message("Astro Pi is awesome!!", scroll_speed=0.05, text_colour=[255,255,0], back_colour=[0,0,255])

2. Click File -- Save As, give your program a name e.g. loop_text.py, then press F5 to run.

3. The LED matrix can also display a single character, rather than an entire message, using the ap.show_letter function which also has some optional parameters.

Parameter Effect
scroll_speed The scroll_speed parameter affects how quickly the text moves on the screen. The default value is 0.1. The bigger the number, the slower the speed
text_colour The text_colour parameter alters the colour of the text and is specified as 3 values for Red, Green, Blue. Each value can be between 0 - 255, so [255,0,255] would be Red + Blue = Purple
back_colour The back_colour parameter alters the colour of the background and is specified as 3 values for Red, Green, Blue. Each value can be between 0 - 255, so [255,0,255] would be Red + Blue = Purple

So this program would display a single Red "J":

from astro_pi import AstroPi

ap = AstroPi()

ap.show_letter("J",text_colour=[255, 0, 0])

And this program would add the sleep function to display letters separated by a brief pause:

from astro_pi import AstroPi
import time

ap = AstroPi()

ap.show_letter("O",text_colour=[255, 0, 0])
time.sleep(1)
ap.show_letter("M",text_colour=[0, 0, 255])
time.sleep(1)
ap.show_letter("G",text_colour=[0, 255, 0])
time.sleep(1)
ap.show_letter("!",text_colour=[0, 0, 0], back_colour=[255, 255, 255])
time.sleep(1)
ap.clear()

Click File -- Save As, give your program a name eg omg.py, then press F5 to run.

For added interest you could use a random number generator to choose a number between 0 and 255 for the colours:

from astro_pi import AstroPi
import time
import random

ap = AstroPi()

r = random.randint(0,255)
ap.show_letter("O",text_colour=[r, 0, 0])
time.sleep(1)

r = random.randint(0,255)
ap.show_letter("M",text_colour=[0, 0, r])
time.sleep(1)

r = random.randint(0,255)
ap.show_letter("G",text_colour=[0, r, 0])
time.sleep(1)

ap.show_letter("!", text_colour=[0, 0, 0], back_colour=[255, 255, 255])
time.sleep(1)
ap.clear()

4. Click File -- Save As, give your program a name eg random_omg.py, then press F5 to run.

In both these programs the ap.clear() method has been used at the end to clear the matrix.

Ideas

  • Could you use the ideas used so far to tell a joke via the LED screen?
  • All the examples so far could be made shorter, while still achieving the same thing. Can you find ways to make these shorter and more efficient?
  • How would you choose a totally random colour, rather than just a random shade of a colour?
  • If your Astro Pi is connected to the internet you could use a Twitter library to make it display incoming tweets!

Afficher des images

The LED matrix can display more than just text! We can control each LED individually to create an image. We can accomplish this in a couple of ways.

1. The first approach is to set pixels (LEDs) individually; we can do this using the ap.set_pixel() method. First, we need to be clear about how we describe each pixel.

The Astro Pi board uses a coordinate system like the one shown below; crucially the numbering begins at 0, not 1. Also, the origin is in the top left rather than the bottom left as you may be used to.

RASP-SENSE-HAT-ASTRO-PI-Debuter-20.png

  • the blue pixel is at coordinates (0, 2)
  • the red pixel is at coordinates (7, 4)



Source: Getting Started with Astro PI et Astro-Pi Guide proposé par Raspberry Pi Learning Resource (www.raspberrypi.org)

Licence Creative Commons - CC-BY-SA
The learning resource is provided for free by the Raspberry Pi Foundation under a Creative Commons licence.
Find more at raspberrypi.org/resources and github.com/raspberrypilearning.

Traduction réalisée par Meurisse. D pour shop.MCHobby.be - Licence CC-BY-SA.
Crédit de traduction: Toute référence, mention ou extrait de cette traduction doit également être explicitement accompagné du crédit de traduction suivant : «  Traduction par MCHobby (shop.MCHobby.be) » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.