极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12847|回复: 0

【Micropython】发光二极管制作炫彩跑马灯

[复制链接]
发表于 2018-1-6 09:50:10 | 显示全部楼层 |阅读模式
   先甩锅 做完后才发现最后一个灯坏了,就坏了一个灯也不好意思去找淘宝店家,大家视频凑合着看把。不过并不影响实验效果。因为这个发光二极管白天不是很明显 晚上炫彩效果就能出来了。本次实验用的是8个灯珠,大家可以尝试更多用更多灯珠的,或者买灯带,那样呈现的效果会更酷、更炫!视频文章最后面。
实验器材(来自某宝,可惜没有实体店不然省很多麻烦)
TPYBoard v102 1块
ws2812b RGB-Ring-8 1个
micro USB数据线 1条
杜邦线 若干
WS2812B是一个集控制电路与发光电路于一体的智能外控LED光源。 其外型与一个5050LED灯珠相同, 每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路, 还包含有高精度的内部振荡器和可编程定电流控制部分, 有效保证了像素点光的颜色高度一致。
        数据协议采用单线归零码的通讯方式, 像素点在上电复位以后, DIN端接受从控制器传输过来的数据, 首先送过来的24bit数据被第一个像素点提取后, 送到像素点内部的数据锁存器, 剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点, 每经过一个像素点的传输, 信号减少24bit。像素点采用自动整形转发技术, 使得该像素点的级联个数不受信号传送的限制, 仅仅受限信号传输速度要求。
实物图

上图是8个灯珠的。
WS2812B的引脚说明:
管脚名       功能描述
5V             供电引脚,输入5V
GND          电源接地
DOUT(DO) 数据信号输出
DIN(DI)   数据信号输入
硬件连接
将TPYBoard v102与WS2812B的接线示意图,如下:
TPYBoard v102       WS2812B
VIN                       5V
GND                      GND
X7(MISO SPI1)       DO
X8(MOSI SPI1)       DI
程序源码如下:
  1. import pyb
  2. import math
  3. from ws2812 import WS2812
  4. ring = WS2812(spi_bus=1, led_count=8, intensity=0.1)
  5. def data_generator(led_count):
  6.     data = [(0, 0, 0) for i in range(led_count)]
  7.     step = 0
  8.     while True:
  9.         red = int((1 + math.sin(step * 0.1324)) * 127)
  10.         green = int((1 + math.sin(step * 0.1654)) * 127)
  11.         blue = int((1 + math.sin(step * 0.1)) * 127)
  12.         data[step % led_count] = (red, green, blue)
  13.         yield data
  14.         step += 1
  15. for data in data_generator(ring.led_count):
  16.     ring.show(data)
  17.     pyb.delay(100)
复制代码

里面还需要引入一个ws2812.py 文件。内容如下:
  1. import gc
  2. import pyb
  3. class WS2812:
  4.     """
  5.     Driver for WS2812 RGB LEDs. May be used for controlling single LED or chain
  6.     of LEDs.
  7.     Example of use:
  8.         chain = WS2812(spi_bus=1, led_count=4)
  9.         data = [
  10.             (255, 0, 0),    # red
  11.             (0, 255, 0),    # green
  12.             (0, 0, 255),    # blue
  13.             (85, 85, 85),   # white
  14.         chain.show(data)
  15.     Version: 1.0
  16.     """
  17.     buf_bytes = (0x11, 0x13, 0x31, 0x33)
  18.     def __init__(self, spi_bus=1, led_count=1, intensity=1):
  19.         """
  20.         Params:
  21.         * spi_bus = SPI bus ID (1 or 2)
  22.         * led_count = count of LEDs
  23.         * intensity = light intensity (float up to 1)
  24.         """
  25.         self.led_count = led_count
  26.         self.intensity = intensity
  27.         # prepare SPI data buffer (4 bytes for each color)
  28.         self.buf_length = self.led_count * 3 * 4
  29.         self.buf = bytearray(self.buf_length)
  30.         # SPI init
  31.         self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1)
  32.         # turn LEDs off
  33.         self.show([])
  34.     def show(self, data):
  35.         """
  36.         Show RGB data on LEDs. Expected data = [(R, G, B), ...] where R, G and B
  37.         are intensities of colors in range from 0 to 255. One RGB tuple for each
  38.         LED. Count of tuples may be less than count of connected LEDs.
  39.         """
  40.         self.fill_buf(data)
  41.         self.send_buf()
  42.     def send_buf(self):
  43.         """
  44.         Send buffer over SPI.
  45.         """
  46.         self.spi.send(self.buf)
  47.         gc.collect()
  48.     def update_buf(self, data, start=0):
  49.         """
  50.         Fill a part of the buffer with RGB data.
  51.         Order of colors in buffer is changed from RGB to GRB because WS2812 LED
  52.         has GRB order of colors. Each color is represented by 4 bytes in buffer
  53.         (1 byte for each 2 bits).
  54.         Returns the index of the first unfilled LED
  55.         Note: If you find this function ugly, it's because speed optimisations
  56.         beated purity of code.
  57.         """
  58.         buf = self.buf
  59.         buf_bytes = self.buf_bytes
  60.         intensity = self.intensity
  61.         mask = 0x03
  62.         index = start * 12
  63.         for red, green, blue in data:
  64.             red = int(red * intensity)
  65.             green = int(green * intensity)
  66.             blue = int(blue * intensity)
  67.             buf[index] = buf_bytes[green >> 6 & mask]
  68.             buf[index+1] = buf_bytes[green >> 4 & mask]
  69.             buf[index+2] = buf_bytes[green >> 2 & mask]
  70.             buf[index+3] = buf_bytes[green & mask]
  71.             buf[index+4] = buf_bytes[red >> 6 & mask]
  72.             buf[index+5] = buf_bytes[red >> 4 & mask]
  73.             buf[index+6] = buf_bytes[red >> 2 & mask]
  74.             buf[index+7] = buf_bytes[red & mask]
  75.             buf[index+8] = buf_bytes[blue >> 6 & mask]
  76.             buf[index+9] = buf_bytes[blue >> 4 & mask]
  77.             buf[index+10] = buf_bytes[blue >> 2 & mask]
  78.             buf[index+11] = buf_bytes[blue & mask]
  79.             index += 12
  80.         return index // 12
  81.     def fill_buf(self, data):
  82.         """
  83.         Fill buffer with RGB data.
  84.         All LEDs after the data are turned off.
  85.         """
  86.         end = self.update_buf(data)
  87.         # turn off the rest of the LEDs
  88.         buf = self.buf
  89.         off = self.buf_bytes[0]
  90.         for index in range(end * 12, self.buf_length):
  91.             buf[index] = off
  92.             index += 1
复制代码

本次参考的github上的一个项目。项目地址:
https://github.com/JanBednarik/micropython-ws2812
给大家看一下效果
https://v.qq.com/x/page/d05297wxo1b.html

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 19:15 , Processed in 0.046406 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表