From cfec76ee40c4d026552bf638070c277a0ff52ae5 Mon Sep 17 00:00:00 2001 From: liubiren Date: Thu, 13 Nov 2025 20:32:25 +0800 Subject: [PATCH] =?UTF-8?q?20251113=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 164 +++++++++++++++++++++++++++++++++++++++++++++ WS2812/main.py | 97 +++++++++++++++++++++++++++ 发送摩斯码/main.py | 134 ++++++++++++++++++++++++++++++++++++ 3 files changed, 395 insertions(+) create mode 100644 .gitignore create mode 100644 WS2812/main.py create mode 100644 发送摩斯码/main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a38dbc0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,164 @@ +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + diff --git a/WS2812/main.py b/WS2812/main.py new file mode 100644 index 0000000..a3e3c7b --- /dev/null +++ b/WS2812/main.py @@ -0,0 +1,97 @@ +import utime + + +class WS2812B: + """RP2350的WS2812B驱动""" + + def __init__(self, pin=16): + """ + 初始化WS2812B驱动 + :param pin: WS2812B的DIN引脚编号(RP2350ZERO默认为16) + """ + + import rp2 + from machine import Pin + + # 基于RP2350的可编程输入输出(PIO)生成输入数据信号 + @rp2.asm_pio( + sideset_init=rp2.PIO.OUT_LOW, # 初始化侧置引脚为低电平 + out_shiftdir=rp2.PIO.SHIFT_LEFT, # 输出移位方向为左移(高位先出) + autopull=True, # 自动从缓存区拉取数据 + pull_thresh=24, # 拉取数据阈值 + ) + def ws2812b(): + # 时序参数 + T1 = 3 # 比特0或比特1由低电平上升为高电平前的低电平周期数 + T2 = 2 # 比特0高电平周期数,或比特1前置高电平周期数 + T3 = 5 # 比特0由高电平下降为低电平后的低电平周期数,或比特1后置高电平周期数 + + wrap_target() + label("bit_loop") + # 从输出移位寄存器(OSR)移出一个比特到X寄存器,同时将侧置引脚置为低电平,延迟T1-1周期数 + out(x, 1).side(0)[T1 - 1] + # 判断X寄存器中为比特0,同时将侧置引脚置为高电平,延迟T2-1周期数。若为比特0则跳转至bit_0,否则跳转至bit_loop,同时将侧置引脚置为高电平,延迟T3-1周期数 + jmp(not_x, "bit_0").side(1)[T2 - 1] + jmp("bit_loop").side(1)[T3 - 1] + label("bit_0") + # 无操作,同时将侧置引脚置为低电平,延迟T3-1周期数 + nop().side(0)[T3 - 1] + wrap() + + try: + # 初始化状态机 + self.state_mechine = rp2.StateMachine( + 0, ws2812b, freq=8_000_000, sideset_base=Pin(pin) + ) + # 启动状态机 + self.state_mechine.active(1) + + except Exception as exception: + raise + + def send(self, rgb): + """ + 发送RGB颜色数据 + :param rgb: RGB颜色数据(元组) + """ + r, g, b = rgb + # 组合RGB颜色数据 + rgb = (r << 24) | (g << 16) | (b << 8) + + self.state_mechine.put(rgb) + + def iridesce(self): + """炫彩""" + # 色相 + H = 0 + # 饱和度 + S = 255 + # 亮度 + V = 16 + + while True: + H = H % 360 + # 色域 + region = H // 60 + # 色域相对位置 + remainder = (H - region * 60) * 256 // 60 + + # 最小颜色分量值 + P = (V * (255 - S)) >> 8 + # 中间颜色分量值 + Q = (V * (255 - ((S * remainder) >> 8))) >> 8 + # 最大颜色分量值 + T = (V * (255 - ((S * (256 - remainder)) >> 8))) >> 8 + + state_mechine.send( + { + 0: (V, T, P), + 1: (Q, V, P), + 2: (P, V, T), + 3: (P, Q, V), + 4: (T, P, V), + }.get(region, (V, P, Q)) + ) + utime.sleep_ms(20) + + H = H + 1 diff --git a/发送摩斯码/main.py b/发送摩斯码/main.py new file mode 100644 index 0000000..3ae9374 --- /dev/null +++ b/发送摩斯码/main.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- + +from machine import Pin, PWM, Timer + +import utime + + +class MorseCodeTransmitter: + """摩斯码发送器""" + + # 摩斯码本 + MORSECODEBOOK = { + "A": ".-", + "B": "-...", + "C": "-.-.", + "D": "-..", + "E": ".", + "F": "..-.", + "G": "--.", + "H": "....", + "I": "..", + "J": ".---", + "K": "-.-", + "L": ".-..", + "M": "--", + "N": "-.", + "O": "---", + "P": ".--.", + "Q": "--.-", + "R": ".-.", + "S": "...", + "T": "-", + "U": "..-", + "V": "...-", + "W": ".--", + "X": "-..-", + "Y": "-.--", + "Z": "--..", + "0": "-----", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + } + + # 合法字符集 + CHARACTERSALLOWED = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789") + + def __init__(self): + + # 单位时距(单位为毫秒) + self.unit_time_interval = 120 + + # 初始化LED + self.LED = Pin("LED", Pin.OUT, value=0) + + def emit(self, characters: str) -> bool: + """发送摩斯码""" + + """ + :parma characters: 字符串,数据类型为字符 + + """ + + # 检查字符串 + if not all( + character.upper() in self.CHARACTERSALLOWED for character in characters + ): + + print("allowed: a-z, 0-9, space") + + return False + + # 转为大写 + characters = characters.upper() + + print(f"emitting {characters} ...", end="") + + for character in characters: + + # 单词间隔 + if character == " ": + + utime.sleep_ms(self.unit_time_interval * 7) + + continue + + for code in self.MORSECODEBOOK.get(character): + + # 点亮LED + self.LED.value(1) + + if code == ".": + + utime.sleep_ms(self.unit_time_interval * 1) + + else: + + utime.sleep_ms(self.unit_time_interval * 3) + + # 熄灭LED + self.LED.value(0) + + # 符号间隔 + utime.sleep_ms(self.unit_time_interval * 1) + + # 字母间隔 + utime.sleep_ms(self.unit_time_interval * 3) + + print("done") + + return True + + def __enter__(self): + """支持上下文管理""" + + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + """退出""" + + self.LED.off() + + +if __name__ == "__main__": + + with MorseCodeTransmitter() as transmitter: + + result = transmitter.emit(characters="hello world")