Modifications

Sauter à la navigation Sauter à la recherche
2 390 octets ajoutés ,  28 septembre 2015 à 07:51
Ligne 133 : Ligne 133 :     
[[Fichier:RASP-SENSE-HAT-ASTRO-PI-Mouvement-20.jpg]]
 
[[Fichier:RASP-SENSE-HAT-ASTRO-PI-Mouvement-20.jpg]]
 +
 +
For any of those numbers you can convert them into their X Y coordinate using the code below.
 +
 +
<nowiki>y = number // 8
 +
x = number % 8</nowiki>
 +
 +
For the {{fname|y}} value you floor divide {{fname|//}} the number by 8. This is integer division and ignores the remainder. Then for the {{fname|x}} value you do the modulus {{fname|%}} of 8 which gives you '''only''' the remainder.
 +
 +
For example (using the number 60 which is on the bottom row):
 +
 +
<nowiki>60 // 8 = 7
 +
60 % 8 = 4</nowiki>
 +
 +
'''3.''' Try this code:
 +
 +
<nowiki>number = 60
 +
 +
y = number // 8
 +
x = number % 8
 +
 +
sense.set_pixel(x, y, 255, 255, 255)</nowiki>
 +
 +
'''4.''' The clever trick is to make a list containing the LED numbers for the path you want it to move back and forth through. So say you want to make an LED chase around the edge you would read the numbers accross the top of the table, down the right hand side, backwards along to bottom and up the left side. So it would be:
 +
 +
<nowiki>edge = [0, 1, 2, 3, 4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8]</nowiki>
 +
 +
We can then find the length of the list using the {{fname|len}} function:
 +
 +
<nowiki>length = len(edge)</nowiki>
 +
 +
So the length is 28. If we divide 28 by 360 we have a ratio between, say, the yaw measurement and the positions in our list (how far around the edge we are). We can then get the sequential pixel number out of the list at the calculated position, work out its coordinate and then switch the LED on! Like this:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
sense.clear()
 +
 +
edge = [0, 1, 2, 3, 4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8]
 +
length = len(edge)
 +
ratio = length / 360.0
 +
 +
while True:
 +
    o = sense.get_orientation()
 +
    pitch = o["pitch"]
 +
    roll = o["roll"]
 +
    yaw = o["yaw"]
 +
 +
    yaw_list_position = int(yaw * ratio)
 +
 +
    yaw_pixel_number = edge[yaw_list_position]
 +
 +
    y = yaw_pixel_number // 8
 +
    x = yaw_pixel_number % 8
 +
 +
    sense.set_pixel(x, y, 255, 255, 255)</nowiki>
 +
 +
'''5.''' What you'll notice is that the above code only turns LEDs on, you'll need to figure out how to turn them off yourself. Try having a variable for the previous {{fname|x}} and {{fname|y}} from the last time around the loop and if this is different from the new {{fname|x}} and {{fname|y}} you use {{fname|set_pixel}} to set the LED to black/éteinte.
    
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
 
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
29 922

modifications

Menu de navigation