Modifications

Sauter à la navigation Sauter à la recherche
5 277 octets ajoutés ,  26 août 2015 à 13:36
Ligne 157 : Ligne 157 :  
ap.set_pixel(0, 2, [0, 0, 255])
 
ap.set_pixel(0, 2, [0, 0, 255])
 
ap.set_pixel(7, 4, [255, 0, 0])</nowiki>
 
ap.set_pixel(7, 4, [255, 0, 0])</nowiki>
 +
 +
Can you guess what the following code creates?
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
sense.set_pixel(2, 2, [0, 0, 255])
 +
sense.set_pixel(4, 2, [0, 0, 255])
 +
sense.set_pixel(3, 4, [100, 0, 0])
 +
sense.set_pixel(1, 5, [255, 0, 0])
 +
sense.set_pixel(2, 6, [255, 0, 0])
 +
sense.set_pixel(3, 6, [255, 0, 0])
 +
sense.set_pixel(4, 6, [255, 0, 0])
 +
sense.set_pixel(5, 5, [255, 0, 0])</nowiki>
 +
 +
'''2.''' Click "File -- Save As", give your program a name e.g. {{fname|simple_image.py}}, then press '''F5''' to run.
 +
 +
 +
'''3.''' Setting pixels individually can work brilliantly, but it gets rather complex when you want to set more pixels. There is another method which can set all the pixels in one go called {{fname|sense.set_pixels}}. Its use is quite straightforward; we just give a list of colour values for each pixel in the matrix.
 +
 +
We could enter something like...
 +
 +
<nowiki>sense.set_pixels([[255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0],......])</nowiki>
 +
 +
...but this would take ages and be really complex.
 +
 +
Instead, you can use some variables to define your colour palette (in this example we're using the 7 colours of the rainbow):
 +
 +
<nowiki>r = [255, 0, 0]
 +
o = [255, 127, 0]
 +
y = [255, 255, 0]
 +
g = [0, 255, 0]
 +
b = [0, 0, 255]
 +
i = [75, 0, 130]
 +
v = [159, 0, 255]
 +
e = [0, 0, 0]  # e stands for empty/black</nowiki>
 +
 +
We can then describe our matrix by creating a 2D list of colour names:
 +
 +
<nowiki>image = [
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,r,r,e,e,e,
 +
e,r,r,o,o,r,r,e,
 +
r,o,o,y,y,o,o,r,
 +
o,y,y,g,g,y,y,o,
 +
y,g,g,b,b,g,g,y,
 +
b,b,b,i,i,b,b,b,
 +
b,i,i,v,v,i,i,b
 +
]</nowiki>
 +
 +
We then give the image list to the {{fname|sense.set_pixels}} method and draw the image. The finished program would look like this:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
r = [255,0,0]
 +
o = [255,127,0]
 +
y = [255,255,0]
 +
g = [0,255,0]
 +
b = [0,0,255]
 +
i = [75,0,130]
 +
v = [159,0,255]
 +
e = [0,0,0]
 +
 +
image = [
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,r,r,e,e,e,
 +
e,r,r,o,o,r,r,e,
 +
r,o,o,y,y,o,o,r,
 +
o,y,y,g,g,y,y,o,
 +
y,g,g,b,b,g,g,y,
 +
b,b,b,i,i,b,b,b,
 +
b,i,i,v,v,i,i,b
 +
]
 +
 +
sense.set_pixels(image)</nowiki>
 +
 +
'''4.''' Click "File -- Save As", give your program a name e.g. {{fname|rainbow.py}}, then press '''F5''' to run.
 +
 +
You should have a beautiful rainbow displayed on your LED matrix.
 +
 +
== Idées ==
 +
* Now you can create images on your LED matrix in two different ways, try creating your own images or sprites.
 +
* Can you alternate between images to create an animation? Check out this Geek Gurl Diaries video for some inspiration.
 +
 +
== Modifier l'orientation ==
 +
[[Fichier:hat-sense-banner-03.jpg]]
 +
 +
So far, all our text and images have appeared the same way up, assuming that the HDMI port is at the bottom. However, this may not always be the case (especially in space) so you may want to change the orientation of the matrix. To do this, you can use the {{fname|sense.set_rotation()}} method and inside the brackets enter one of four angles (0, 90, 180, 270).
 +
 +
To rotate your screen by 180 degrees you'd use this line:
 +
 +
<nowiki>sense.set_rotation(180)</nowiki>
 +
 +
'''1.''' When used in the rainbow program it would look like this:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
r = [255, 0, 0]
 +
o = [255, 127, 0]
 +
y = [255, 255, 0]
 +
g = [0, 255, 0]
 +
b = [0, 0, 255]
 +
i = [75, 0, 130]
 +
v = [159, 0, 255]
 +
e = [0, 0, 0]
 +
 +
image = [
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,r,r,e,e,e,
 +
e,r,r,o,o,r,r,e,
 +
r,o,o,y,y,o,o,r,
 +
o,y,y,g,g,y,y,o,
 +
y,g,g,b,b,g,g,y,
 +
b,b,b,i,i,b,b,b,
 +
b,i,i,v,v,i,i,b
 +
]
 +
 +
sense.set_pixels(image)
 +
sense.set_rotation(180)</nowiki>
 +
 +
'''2.''' Click "File -- Save As", give your program a name e.g. {{fname|rainbow_flip.py}}, then press '''F5''' to run.
 +
 +
'''3.''' You could also create spinning text using a for loop:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
import time
 +
 +
sense = SenseHat()
 +
 +
sense.show_letter("J")
 +
 +
angles = [0, 90, 180, 270, 0, 90, 180, 270]
 +
for r in angles:
 +
    sense.set_rotation(r)
 +
    time.sleep(0.5)</nowiki>
 +
 +
This program displays the letter "J" and then sets the rotation to each value in the angles list with a 0.5 second pause.
 +
 +
'''4.''' Click "File -- Save As", give your program a name e.g. {{fname|spinning_j.py}}, then press '''F5''' to run.
 +
 +
'''5.''' You can also flip the image on the screen, either horizontally or vertically, using these lines:
 +
 +
<nowiki>sense.flip_h()</nowiki>
 +
 +
ou
 +
 +
<nowiki>sense.flip_v()</nowiki>
 +
 +
With this example you could create a simple animation by flipping the image repeatedly:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
import time
 +
 +
sense = SenseHat()
 +
 +
w = [150, 150, 150]
 +
b = [0, 0, 255]
 +
e = [0, 0, 0]
 +
 +
image = [
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,e,e,e,e,e,
 +
w,w,w,e,e,w,w,w,
 +
w,w,b,e,e,w,w,b,
 +
w,w,w,e,e,w,w,w,
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,e,e,e,e,e,
 +
e,e,e,e,e,e,e,e
 +
]
 +
 +
sense.set_pixels(image)
 +
 +
while True:
 +
    time.sleep(1)
 +
    sense.flip_h()</nowiki>
 +
 +
'''6.''' Click "File -- Save As", give your program a name e.g. {{fname|eyes.py}}, then press "F5" to run.
 +
 +
=== Idées ===
 +
 +
* Create a spinning image, using one of the drawing techniques shown already, and then use the sense.set_rotation method to make it spin.
 +
* Using what you've done so far, you should be able to make an electronic dice like the one shown here:
 +
 +
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Debuter-20.jpg]]<br />[https://www.youtube.com/watch?v=4jT7GyyudP4 Cliquer ici pour voir la vidéo YouTube correspondant]
    
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
 
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
29 917

modifications

Menu de navigation