Modifications

Sauter à la navigation Sauter à la recherche
7 893 octets ajoutés ,  18 mars 2017 à 16:19
Ligne 95 : Ligne 95 :     
{{ADFImage|RASP-FT232H-MPSSE-Usage-SPI-00.jpg}}
 
{{ADFImage|RASP-FT232H-MPSSE-Usage-SPI-00.jpg}}
 +
 +
Now create a file neopixels.py and fill it with the following code:
 +
 +
<syntaxhighlight lang="python">
 +
import time
 +
 +
import Adafruit_GPIO as GPIO
 +
import Adafruit_GPIO.FT232H as FT232H
 +
 +
 +
class NeoPixel_FT232H(object):
 +
def __init__(self, n):
 +
# Create an FT232H object.
 +
self.ft232h = FT232H.FT232H()
 +
# Create a SPI interface for the FT232H object.  Set the SPI bus to 6mhz.
 +
self.spi    = FT232H.SPI(self.ft232h, max_speed_hz=6000000)
 +
# Create a pixel data buffer and lookup table.
 +
self.buffer = bytearray(n*24)
 +
self.lookup = self.build_byte_lookup()
 +
 +
def build_byte_lookup(self):
 +
# Create a lookup table to map all byte values to 8 byte values which
 +
# represent the 6mhz SPI data to generate the NeoPixel signal for the
 +
# specified byte.
 +
lookup = {}
 +
for i in range(256):
 +
value = bytearray()
 +
for j in range(7, -1, -1):
 +
if ((i >> j) & 1) == 0:
 +
value.append(0b11100000)
 +
else:
 +
value.append(0b11111000)
 +
lookup[i] = value
 +
return lookup
 +
 +
def set_pixel_color(self, n, r, g, b):
 +
# Set the pixel RGB color for the pixel at position n.
 +
# Assumes GRB NeoPixel color ordering, but it's easy to change below.
 +
index = n*24
 +
self.buffer[index  :index+8 ] = self.lookup[int(g)]
 +
self.buffer[index+8 :index+16] = self.lookup[int(r)]
 +
self.buffer[index+16:index+24] = self.lookup[int(b)]
 +
 +
def show(self):
 +
# Send the pixel buffer out the SPI data output pin (D1) as a NeoPixel
 +
# signal.
 +
self.spi.write(self.buffer)
 +
 +
 +
# Run this code when the script is called at the command line:
 +
if __name__ == '__main__':
 +
# Define the number of pixels in the NeoPixel strip.
 +
# Only up to ~340 pixels can be written using the FT232H.
 +
pixel_count = 16
 +
# Create a NeoPixel_FT232H object.
 +
pixels = NeoPixel_FT232H(pixel_count)
 +
# Animate each pixel turning red.
 +
# Loop through each pixel.
 +
for i in range(pixel_count):
 +
# Set the pixel color to pure red.
 +
pixels.set_pixel_color(i, 255, 0, 0)
 +
# Show the pixel buffer by sending it to the LEDs.
 +
pixels.show()
 +
# Delay for a short period of time.
 +
time.sleep(0.25)
 +
# Animate each pixel turning pure green.
 +
for i in range(pixel_count):
 +
pixels.set_pixel_color(i, 0, 255, 0)
 +
pixels.show()
 +
time.sleep(0.25)
 +
# Animate each pixel turning pure blue.
 +
for i in range(pixel_count):
 +
pixels.set_pixel_color(i, 0, 0, 255)
 +
pixels.show()
 +
time.sleep(0.25)
 +
# Animate a pattern of colors marching around the pixels.
 +
# Create a pattern of colors to display.
 +
colors = [ (255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255),
 +
(0, 0, 255), (255, 0, 255) ]
 +
offset = 0
 +
print 'Press Ctrl-C to quit.'
 +
while True:
 +
# Loop through all the pixels and set their color based on the pattern.
 +
for i in range(pixel_count):
 +
color = colors[(i+offset)%len(colors)]
 +
pixels.set_pixel_color(i, color[0], color[1], color[2])
 +
pixels.show()
 +
# Increase the offset to make the colors change position.
 +
offset += 1
 +
time.sleep(0.25)
 +
</syntaxhighlight>
 +
 +
Save the file and navigate to the folder with it in a terminal, then execute the following in Windows to run the program:
 +
 +
<syntaxhighlight lang="bash">
 +
python neopixels.py
 +
</syntaxhighlight>
 +
 +
Or on Mac OSX or Linux  execute the following to run the program as root:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo python neopixels.py
 +
</syntaxhighlight>
 +
 +
 +
 +
You should see the NeoPixels light up and animate with different colors.  Note that you might need to change the pixel_count variable in the main part of the program to match the number of pixels in your NeoPixel strip, circle, matrix, etc.
 +
 +
This code does a couple things at a high level.  It first defines a class called NeoPixel_FT232H.  This class contains some methods and state which control generating the NeoPixel signal with an FT232H board.  The second part of the code uses the NeoPixel_FT232H class to animate the NeoPixels.
 +
 +
You actually don't need to fully understand the NeoPixel_FT232H class code to use it.  This code performs the 'oversampling' by using a lookup table to expand each byte of color data into 8 bytes of SPI data that approximates the NeoPixel control signal.  The only important thing to know about the NeoPixel_FT232H class is that it exposes a '''set_pixel_color()''' function which allows you to set the red, green, and blue color value of a pixel.
 +
 +
Instead let's walk through a bit of the second half of the code that uses the NeoPixel_FT232H class:
 +
 +
<syntaxhighlight lang="python">
 +
# Run this code when the script is called at the command line:
 +
if __name__ == '__main__':
 +
# Define the number of pixels in the NeoPixel strip.
 +
# Only up to ~340 pixels can be written using the FT232H.
 +
pixel_count = 16
 +
# Create a NeoPixel_FT232H object.
 +
pixels = NeoPixel_FT232H(pixel_count)
 +
</syntaxhighlight>
 +
 +
This portion of code has an if statement that checks if the program is being run from the command line before executing.  This is just a standard Python idiom for defining the main entry point of a program.
 +
 +
Inside the if block you can see the number of pixels is defined and set in the pixel_count variable.  Then the NeoPixel_FT232H object is created by telling it that number of pixels as its only parameter.
 +
 +
 +
<syntaxhighlight lang="python">
 +
# Animate each pixel turning red.
 +
# Loop through each pixel.
 +
for i in range(pixel_count):
 +
# Set the pixel color to pure red.
 +
pixels.set_pixel_color(i, 255, 0, 0)
 +
# Show the pixel buffer by sending it to the LEDs.
 +
pixels.show()
 +
# Delay for a short period of time.
 +
time.sleep(0.25)
 +
# Animate each pixel turning pure green.
 +
for i in range(pixel_count):
 +
pixels.set_pixel_color(i, 0, 255, 0)
 +
pixels.show()
 +
time.sleep(0.25)
 +
# Animate each pixel turning pure blue.
 +
for i in range(pixel_count):
 +
pixels.set_pixel_color(i, 0, 0, 255)
 +
pixels.show()
 +
time.sleep(0.25)
 +
</syntaxhighlight>
 +
 +
The next section performs a few simple animations that turn each pixel on with primary colors.  You can see a loop is used to go through each pixel and the '''set_pixel_color()''' function is called to the pixel color.  This function takes 4 parameters, the first is the number of the pixel (start at 0), and the last 3 parameters are the red, green, and blue color components.  Each component should be a value from 0 to 255, where 0 is no color and 255 is maximum color intensity.
 +
 +
After changing the pixel color, the '''show()''' function is called to send the colors to the LEDs.  You must call '''show()''' in order to make the NeoPixels light up with the colors you've set previously!
 +
 +
Finally notice the '''time.sleep()''' function is used to delay for a short period of time (a quarter of a second in this case).  This sleep function is very useful for animating color changes that should go somewhat slowly.
 +
 +
<syntaxhighlight lang="python">
 +
# Animate a pattern of colors marching around the pixels.
 +
# Create a pattern of colors to display.
 +
colors = [ (255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255),
 +
(0, 0, 255), (255, 0, 255) ]
 +
offset = 0
 +
print 'Press Ctrl-C to quit.'
 +
while True:
 +
# Loop through all the pixels and set their color based on the pattern.
 +
for i in range(pixel_count):
 +
color = colors[(i+offset)%len(colors)]
 +
pixels.set_pixel_color(i, color[0], color[1], color[2])
 +
pixels.show()
 +
# Increase the offset to make the colors change position.
 +
offset += 1
 +
time.sleep(0.25)
 +
</syntaxhighlight>
 +
 +
Finally the code enters an infinite loop where it animates a rainbow of colors marching across the pixels.  This code uses the same '''set_pixel_color()''' function, but has a little extra logic to pick a color from a list and increase the offset of chosen colors every loop iteration.  Also notice the '''show()''' function is again called after updating pixel colors in order to make the LEDs light up with the desired colors.
 +
 +
That's all there is to controlling NeoPixels with SPI from the FT232H breakout!  Feel free to use the code above in your own NeoPixel projects!
    
{{RASP-FT232H-TRAILER}}
 
{{RASP-FT232H-TRAILER}}
29 917

modifications

Menu de navigation