ESP32 的通用 micropython 工具 upy-esp32-lib

MicroPython相关代码、库、软件、工具
回复
头像
shaoziyang
帖子: 2497
注册时间: 2019年 10月 21日 13:48

ESP32 的通用 micropython 工具 upy-esp32-lib

#1

帖子 shaoziyang » 2022年 1月 12日 10:47

ESP32 的通用 micropython 工具

FIFO queue in RTC memory

代码: 全选

import machine
import uctypes

from rtc_mem import RTCMemory
from mem_fifo import MemFifo

struct_def = {
    "time":             0 | uctypes.UINT32,
    "temperature":      4 | uctypes.INT16,       # 1/10 Celsius
    "pressure":         8 | uctypes.UINT16       # 1/10 hPa
}
samples = (
    [ 100, 20, 10320],
    [ 200, 21, 10321],
    [ 400,  5,  9000], # Storm!
    [ 500,  5, 10000], # Storm!
    )

# create memory map in RTC slow memory
rtc_mem = RTCMemory([
    'fifo',     50,             # space for ~3 entries + fifo header
    'something_else', 256
    ])

# create FIFO in the "fifo" area defined above
q = MemFifo(rtc_mem.fifo, struct_def)

def create_struct():
    arr = bytearray(uctypes.sizeof(struct_def))
    data = uctypes.struct(uctypes.addressof(arr), struct_def)
    return data

def overrun():
    for sample in samples:
        data = create_struct()
        data.time = sample[0]
        data.temperature = sample[1]
        data.pressure = sample[2]
        print("enqueue t={}".format(data.time))
        q.put_nowait(data)

def sleeptest():
    for sample in samples:
        data = create_struct()
        data.time = sample[0]
        data.temperature = sample[1]
        data.pressure = sample[2]
        print("enqueue t={}".format(data.time))
        q.put_nowait(data)
        machine.deepsleep(500)

def read_one():
    data = q.get_nowait()
    return data

def read():
    data = q.dequeue()
    while data is not None:
        print("data from queue: {} {} {}".format(data.time, data.temperature, data.pressure))
        data = q.get_nowait()
隐藏内容
你必须登入/注册才可观看隐藏内容
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章