esp32用microPython点亮WS2812B彩灯


 ██████╗ ███████╗██████╗ ██╗   ██╗███████╗
██╔═══██╗██╔════╝██╔══██╗╚██╗ ██╔╝██╔════╝
██║   ██║███████╗██████╔╝ ╚████╔╝ █████╗  
██║▄▄ ██║╚════██║██╔══██╗  ╚██╔╝  ██╔══╝  
╚██████╔╝███████║██████╔╝   ██║   ███████╗
 ╚══▀▀═╝ ╚══════╝╚═════╝    ╚═╝   ╚══════╝
                                          

保命声明:笔者代码能力有限,若行文中有错漏之处欢迎大家指出。

microPython简介

[https://micropython.org]

MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments.

The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on the bare metal, giving you a low-level Python operating system that can be used to control all kinds of electronic projects.

MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.

MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.
MicroPython is a full Python compiler and runtime that runs on the bare-metal. You get an interactive prompt (the REPL) to execute commands immediately, along with the ability to run and import scripts from the built-in filesystem. The REPL has history, tab completion, auto-indent and paste mode for a great user experience.

MicroPython strives to be as compatible as possible with normal Python (known as CPython) so that if you know Python you already know MicroPython. On the other hand, the more you learn about MicroPython the better you become at Python.

In addition to implementing a selection of core Python libraries, MicroPython includes modules such as "machine" for accessing low-level hardware.

[https://www.cnblogs.com/juwan/p/11445120.html]
大多数时候,Python 的发展以 CPython 为主,以下列出一些与 CPython 的差异化信息。

  • MicroPython 和 CPython 在 Python3 语法上保持高度的一致性,常用的标准语法命令都已经支持。
  • MicroPython 虽然只实现了 CPython 的标准库和容器库的一些部分,常见容器库有同类功能,但不同名的模块,但大多算法类的 Python 逻辑代码是可以拿来即用的。
  • MicroPython 兼容实现的 CPython 的异常机制、没有实现元类(metaclass)机制,独立的 GC 机制。
  • 在许多不同的硬件微芯片(最低在 nRF51)的移植上, MicroPython 代码接口缺乏一致性,呈现碎片化。
  • MicroPython 编译(mpy-corss)后得到的是 mpy ,而不是 CPython 的 pyc 文件。
  • MicroPython 在移植 CPython 代码时,经常缺少各种方法,所以要习惯寻找同类接口,而它们的使用方法除了看文档外就只能看源码。

WS2812B简介

主要特点
● IC控制电路与LED点光源共用一个电源。
● 每个通道工作电流12mA.
● 控制电路与RGB芯片集成在一个2020封装的元器件中,构成一个完整的外控像素点。
● 内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。 
● 内置上电复位和掉电复位电路。
● 每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示。
● 端口扫描频率2KHz/s。
● 串行级联接口,能通过一根信号线完成数据的接收与解码。
● 当刷新速率30帧/秒时,级联数不小于1024点。
● 数据发送速度可达800Kbps。
● 光的颜色高度一致,性价比高。
● 具有电源反接不会损坏。
● 外围不需要包含电容在内的所有任何电子元器件。

引脚:

  • DOUT : 数据输出(用于级联)
  • GND
  • DIN : 数据输入
  • VDD : 3.7~5.3V

esp32-c3配置microPython运行环境

  1. 烧写microPython固件:

[https://micropython.org/download/esp32c3-usb/]]

ls /dev/cu*
esptool.py --chip esp32c3 --port /dev/cu.usbmodem143101 erase_flash
esptool.py --chip auto --port /dev/cu.usbmodem143101 -b 921600 --before=default_reset --after=hard_reset write_flash --flash_mode=keep --flash_freq 80m --flash_size 4MB 0x0000 files/esp32c3-usb-20220618-v1.19.1.bin
  1. 安装支持microPython的uPyCraft IDE

[https://www.cnblogs.com/fw-qql/p/14431734.html]
[http://docs.dfrobot.com.cn/upycraft/2.1下载和安装upyCraft.html]

实现

代码

test.py

#-*- encoding:utf-8
import neopixel #ws2812库
import time #时间库
#级联数量
_n=9
#WS2812_DIN
ws2812_din=7

#初始化
p = machine.Pin(ws2812_din)
n = neopixel.NeoPixel(p, _n)

'''
@功能:点亮
参数:颜色
'''
def Light_it(color_r,color_g,color_b):
    # Draw a red gradient.
    # (R,G,B)
    for i in range(_n):
        n[i] = (color_r, color_g, color_b) 
    #n[0].fill(0,255,0)
    # Update the strip.
    n.write()

'''
@功能:清空颜色
'''
def Light_clear():
    for i in range(0,_n):
        n[i]=(0,0,0)
    n.write()
'''
主函数
'''
def main():
    #红色
    Light_clear()
    Light_it(100,0,0)
    #延时(秒)
    time.sleep(2)
    #绿色
    Light_clear()
    Light_it(0,100,0)
    #延时
    time.sleep(2)
    #蓝色
    Light_clear()
    Light_it(0,0,100)
    #延时
    time.sleep(2)

if(__name__=='__main__'):
    while 1:
        main()

neopixel.py

# NeoPixel driver for MicroPython
# MIT license; Copyright (c) 2016 Damien P. George, 2021 Jim Mussared

from machine import bitstream


class NeoPixel:
    # G R B W
    ORDER = (1, 0, 2, 3)

    def __init__(self, pin, n, bpp=3, timing=1):
        self.pin = pin
        self.n = n
        self.bpp = bpp
        self.buf = bytearray(n * bpp)
        self.pin.init(pin.OUT)
        # Timing arg can either be 1 for 800kHz or 0 for 400kHz,
        # or a user-specified timing ns tuple (high_0, low_0, high_1, low_1).
        self.timing = (
            ((400, 850, 800, 450) if timing else (800, 1700, 1600, 900))
            if isinstance(timing, int)
            else timing
        )

    def __len__(self):
        return self.n

    def __setitem__(self, i, v):
        offset = i * self.bpp
        for i in range(self.bpp):
            self.buf[offset + self.ORDER[i]] = v[i]

    def __getitem__(self, i):
        offset = i * self.bpp
        return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp))

    def fill(self, v):
        b = self.buf
        l = len(self.buf)
        bpp = self.bpp
        for i in range(bpp):
            c = v[i]
            j = self.ORDER[i]
            while j < l:
                b[j] = c
                j += bpp

    def write(self):
        # BITSTREAM_TYPE_HIGH_LOW = 0
        bitstream(self.pin, 0, self.timing, self.buf)

上传python代码

效果


posted @ 2023-02-13 20:13  qsBye  阅读(767)  评论(0编辑  收藏  举报