Ligne 32 : |
Ligne 32 : |
| | back_colour ||The back_colour parameter alters the colour of the background and works in the same way as the text_colour | | | 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 {{fname|Astro Pi is awesome!!}} more slowly, with the text in yellow '''[255,255,0]''' and the background in blue '''[0,0,255]''': |
| + | |
| + | <nowiki>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])</nowiki> |
| + | |
| + | You could also make the message repeat using a while loop like this: |
| + | |
| + | <nowiki>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])</nowiki> |
| + | |
| + | '''2.''' Click File -- Save As, give your program a name e.g. {{fname|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 {{fname|ap.show_letter}} function which also has some optional '''parameters'''. |
| + | |
| + | {| class="wikitable" |
| + | | align="center" style="background:#f0f0f0;"|'''Parameter''' |
| + | | align="center" style="background:#f0f0f0;"|'''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": |
| + | |
| + | <nowiki>from astro_pi import AstroPi |
| + | |
| + | ap = AstroPi() |
| + | |
| + | ap.show_letter("J",text_colour=[255, 0, 0])</nowiki> |
| + | |
| + | And this program would add the '''sleep function''' to display letters separated by a brief pause: |
| + | |
| + | <nowiki>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()</nowiki> |
| + | |
| + | Click '''File -- Save As''', give your program a name eg {{fname|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: |
| + | |
| + | <nowiki>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()</nowiki> |
| + | |
| + | Click '''File -- Save As''', give your program a name eg {{fname|random_omg.py}}, then press '''F5''' to run. |
| + | |
| + | In both these programs the {{fname|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 {{fname|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. |
| + | |
| + | [[Fichier:RASP-SENSE-HAT-ASTRO-PI-Debuter-20.png]] |
| + | |
| + | * the blue pixel is at coordinates (0, 2) |
| + | * the red pixel is at coordinates (7, 4) |
| + | |
| | | |
| {{RASP-SENSE-HAT-ASTRO-PI-TRAILER}} | | {{RASP-SENSE-HAT-ASTRO-PI-TRAILER}} |