FT232Hボードにneopixelsを接続する
2020/4/4
FT232H neopixels(spi)
FT232H neopixels(spi)
概要
以下のFT232Hボードにneopixelsを接続する。
FT232H使用USB⇔GPIO+SPI+I2C変換モジュール
Adafruit FT232H Breakout
PCのpython3でCircuitPythonのAPIを使用する
接続
D1(MOSI)をneopixelsのDINに接続する。
demo1
Module Install:
pip3 install adafruit-circuitpython-neopixels_spi
Demo Script:
neopixel_spi_test.py
# FT232H board
import math
from time import sleep
import board
import neopixel_spi as neopixel
NUM_PIXELS = 64
PIXEL_ORDER = neopixel.GRB
COLORS = (0xFF0000, 0x00FF00, 0x0000FF)
DELAY = 0.1
spi = board.SPI()
pixels = neopixel.NeoPixel_SPI(spi,
NUM_PIXELS,
pixel_order=PIXEL_ORDER,
auto_write=False)
def show_pixels(data):
for n in range(NUM_PIXELS):
pixels[n] = data[n]
pixels.show()
def data_generator(led_count):
data = [(0, 0, 0) for i in range(led_count)]
step = 0
while True:
red = int((1 + math.sin(step * 0.1324)) * 127)
green = int((1 + math.sin(step * 0.1654)) * 127)
blue = int((1 + math.sin(step * 0.1)) * 127)
data[step % led_count] = (red, green, blue)
yield data
step += 1
for data in data_generator(NUM_PIXELS):
show_pixels(data)
sleep(0.01)
実行:
$ export BLINKA_FT232H=1
$ python3 neopixel_spi_test.py
demo2
Module Install:
git clone https://github.com/JanBednarik/micropython-ws2812.git
cd micropython-ws2812
cp ws2812.py ws2812_spi0_cpy.py
nano ws2812_spi0_cpy.py
# 次の「Patched Module」に記載されているパッチを反映する。
cp ws2812_spi0_cpy.py ~/ft232h_ws/
Patched Module:
ws2812_spi0_cpy.py
「#m」を含む行が修正行になる。
# -*- coding: utf-8 -*-
import gc
#m import pyb
import board #m
import busio #m
class WS2812:
"""
Driver for WS2812 RGB LEDs. May be used for controlling single LED or chain
of LEDs.
Example of use:
chain = WS2812(spi_bus=1, led_count=4)
data = [
(255, 0, 0), # red
(0, 255, 0), # green
(0, 0, 255), # blue
(85, 85, 85), # white
]
chain.show(data)
Version: 1.0
"""
buf_bytes = (0x88, 0x8e, 0xe8, 0xee)
def __init__(self, spi_bus=1, led_count=1, intensity=1):
"""
Params:
* spi_bus = SPI bus ID (1 or 2)
* led_count = count of LEDs
* intensity = light intensity (float up to 1)
"""
self.led_count = led_count
self.intensity = intensity
# prepare SPI data buffer (4 bytes for each color)
self.buf_length = self.led_count * 3 * 4
self.buf = bytearray(self.buf_length)
# SPI init
#m self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1)
self.spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI) #m
self.spi.try_lock() #m
self.spi.configure(baudrate=3200000, polarity=0, phase=0) #m
# turn LEDs off
self.show([])
def show(self, data):
"""
Show RGB data on LEDs. Expected data = [(R, G, B), ...] where R, G and B
are intensities of colors in range from 0 to 255. One RGB tuple for each
LED. Count of tuples may be less than count of connected LEDs.
"""
self.fill_buf(data)
self.send_buf()
def send_buf(self):
"""
Send buffer over SPI.
"""
#m self.spi.send(self.buf)
self.spi.write(self.buf) #m
#m gc.collect()
def update_buf(self, data, start=0):
"""
Fill a part of the buffer with RGB data.
Order of colors in buffer is changed from RGB to GRB because WS2812 LED
has GRB order of colors. Each color is represented by 4 bytes in buffer
(1 byte for each 2 bits).
Returns the index of the first unfilled LED
Note: If you find this function ugly, it's because speed optimisations
beated purity of code.
"""
buf = self.buf
buf_bytes = self.buf_bytes
intensity = self.intensity
mask = 0x03
index = start * 12
for red, green, blue in data:
red = int(red * intensity)
green = int(green * intensity)
blue = int(blue * intensity)
buf[index] = buf_bytes[green >> 6 & mask]
buf[index+1] = buf_bytes[green >> 4 & mask]
buf[index+2] = buf_bytes[green >> 2 & mask]
buf[index+3] = buf_bytes[green & mask]
buf[index+4] = buf_bytes[red >> 6 & mask]
buf[index+5] = buf_bytes[red >> 4 & mask]
buf[index+6] = buf_bytes[red >> 2 & mask]
buf[index+7] = buf_bytes[red & mask]
buf[index+8] = buf_bytes[blue >> 6 & mask]
buf[index+9] = buf_bytes[blue >> 4 & mask]
buf[index+10] = buf_bytes[blue >> 2 & mask]
buf[index+11] = buf_bytes[blue & mask]
index += 12
return index // 12
def fill_buf(self, data):
"""
Fill buffer with RGB data.
All LEDs after the data are turned off.
"""
end = self.update_buf(data)
# turn off the rest of the LEDs
buf = self.buf
off = self.buf_bytes[0]
for index in range(end * 12, self.buf_length):
buf[index] = off
index += 1
demo1のAdafruit製モジュールよりも表示が速い。たぶん、gc.collect()をコメントアウトしているせいと思われる。PCのpython3で動作させる前提ではメモリが潤沢にあるので gc.collect()は不要と判断した。
注意:APIはAdafruitのものと異なっている。
Demo Script:
ws2812_spi_test.py
from time import sleep
import math
from ws2812_spi0_cpy import WS2812
#ring = WS2812(led_count=64, intensity=0.1)
ring = WS2812(led_count=64, intensity=1)
def data_generator(led_count):
data = [(0, 0, 0) for i in range(led_count)]
step = 0
while True:
red = int((1 + math.sin(step * 0.1324)) * 127)
green = int((1 + math.sin(step * 0.1654)) * 127)
blue = int((1 + math.sin(step * 0.1)) * 127)
data[step % led_count] = (red, green, blue)
yield data
step += 1
for data in data_generator(ring.led_count):
ring.show(data)
sleep(0.01)
実行:
$ export BLINKA_FT232H=1
$ python3 ws2812_spi_test.py
参照情報
CircuitPython Libraries on any Computer with FT232H
FT232H使用USB⇔GPIO+SPI+I2C変換モジュール
Adafruit FT232H Breakout
CircuitPython on Linux and Raspberry Pi
CircuitPython 5.0.x - I2C
以上
| 固定リンク
« FT232Hボードに気圧センサーMPL115A2(i2c)を接続する | トップページ | FT232HボードにElecFreaks-Alphanumeric-Display(i2c)を接続する »
「linux」カテゴリの記事
- platfomioを使ってnaitive(linux/windows)のプログラムをビルドする方法(2021.03.10)
- micro:bit Yotta開発ツール(linux版)(2021.01.09)
- PlatformIOをRaspberryPi4で動かしてみる(実験)(2020.10.20)
- headless RaspberryPiインストール方法(v2)(2020.10.20)
- wio-terminalのファームウェア・アップデートについて(linux版)(2020.10.15)
「MicroPython」カテゴリの記事
- microbit-v2にMicropythonをインストールする(2021.05.06)
- PicoボードのMicroPythonをVS_CodeのextensionのPico-Goでプログラミングする(2021.02.10)
- MicroPython/CircuitPython Performance Test(2021.02.07)
- PicoボードにMicropython/CircuitPythonをインストールする(2021.02.03)
- MicroPython(F767ZI) Network Samples(2021.01.03)
「CircuitPython」カテゴリの記事
- MicroPython/CircuitPython Performance Test(2021.02.07)
- PicoボードにMicropython/CircuitPythonをインストールする(2021.02.03)
- XIAOでMPL3115A2(気圧・高度・気温センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOでMCP9808(温度センサ)を使用する(CircuitPython版)(2020.07.02)
- XIAOにCircuitPythonをインストールする(2020.06.09)
コメント