microPython中的esp32,从neopixel到bitstream,再到bytearray。

在写ws2812矩阵的代码时候,发现有些操作很是浪费系统时间,比如一个数组代表所有灯的颜色,如果只改变了个别灯的颜色,在写入的时候,还是要把整个数组的值赋给整条灯带。
赋值的方式是,遍历数组,对每个灯珠调用set方法。那如果能找到更底层的方法,直接去修改系统里对应灯带的数组,代码效率就会提高很多。

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)

在github里面找到了neopixel.py的源文件,它调用的是bitstream,但是接下来在找bitstream的用法时候,没有找到什么例程,只能慢慢摸索了。
关于bitstream,官网的文档里只有这么几行描述:
machine.bitstream(pin, encoding, timing, data, /)

Transmits data by bit-banging the specified pin. The encoding argument specifies how the bits are encoded, and timing is an encoding-specific timing specification.

The supported encodings are:

        0 for “high low” pulse duration modulation. This will transmit 0 and 1 bits as timed pulses, starting with the most significant bit. The timing must be a four-tuple of nanoseconds in the format (high_time_0, low_time_0, high_time_1, low_time_1). For example, (400, 850, 800, 450) is the timing specification for WS2812 RGB LEDs at 800kHz.

The accuracy of the timing varies between ports. On Cortex M0 at 48MHz, it is at best +/- 120ns, however on faster MCUs (ESP8266, ESP32, STM32, Pyboard), it will be closer to +/-30ns.

Note

For controlling WS2812 / NeoPixel strips, see the neopixel module for a higher-level API.

简单理解,就是规定了编码方式,时序,byte数据,接下来就是研究每个参数的影响了。

import time
import machine
from machine import bitstream
buf=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
for i in range(100):
    for j in (0,12):
        buf[j]=90
        bitstream(machine.Pin(4), 0, (400, 850, 800, 450),buf)
        time.sleep_ms(20)
        buf[j]=00
      

简易的实现灯带颜色,颜色用bytearray定义,每个字节代表一个颜色,如果灯带是rgb意味着每个灯珠的颜色占用3个字节。

接下来就是,要自己写一个灯带的类,在每次刷新颜色值后,不清除原始数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值