更新espnow报错信息显示,自强蜂鸣器PA使能控制

This commit is contained in:
dahanzimin
2025-03-13 14:20:40 +08:00
parent b8e0acc4be
commit 6688ccdb42
4 changed files with 33 additions and 10 deletions

View File

@@ -14,6 +14,7 @@ _CI_ID_GET = const(0x02)
_CI_ID_SET = const(0x03)
_CI_ID_NUM = const(0x06)
_CI_ID_CLE = const(0x07)
_CI_ID_PACTRL = const(0x09)
_CI_ID_END = const(0x5A)
class CI130X:
@@ -24,7 +25,11 @@ class CI130X:
try:
self._rreg(_CI_ID_GET, 3)
except:
raise AttributeError("Cannot find a CI130X")
try: #C130X 启动慢,加延时判断
time.sleep_ms(500)
self._rreg(_CI_ID_GET, 3)
except:
raise AttributeError("Cannot find a CI130X")
def _wreg(self, reg):
'''Write memory address'''
@@ -83,3 +88,7 @@ class CI130X:
if end is not None:
self.play_id(end)
time.sleep_ms(delay)
def pa_ctrl(self, value=True, delay=10):
self._wreg(bytes([_CI_ID_PACTRL, int(value), 0, _CI_ID_END]))
if value: time.sleep_ms(delay)

View File

@@ -28,12 +28,13 @@ normal_tone = {
Letter = 'ABCDEFG#R'
class MIDI():
def __init__(self,pin,volume=100,invert=0):
def __init__(self, pin, volume=100, invert=0, pa_ctrl=None):
self.reset()
self._invert=invert
self._pin = pin
self._volume = volume
self._pwm = None
self._pa_ctrl = pa_ctrl
def set_volume(self,volume):
if not 0 <= volume <= 100:
@@ -97,6 +98,7 @@ class MIDI():
tone = tone[:pos]
def play(self, tune, duration=None):
if self._pa_ctrl: self._pa_ctrl(1)
self._pwm = PWM(Pin(self._pin), duty=1023 if self._invert else 0)
if duration is None:
self.set_default(tune[0])
@@ -112,25 +114,29 @@ class MIDI():
sleep_ms(midi[1])
self._pwm.freq(400000)
sleep_ms(1)
if self._pa_ctrl: self._pa_ctrl(0)
self._pwm.deinit()
sleep_ms(10)
def pitch(self, freq):
if self._pa_ctrl: self._pa_ctrl(1)
self._pwm = PWM(Pin(self._pin))
self._pwm.duty(1023-self._volume) if self._invert else self._pwm.duty(self._volume)
self._pwm.freq(int(freq))
def pitch_time(self, freq, delay):
if self._pa_ctrl: self._pa_ctrl(1)
self._pwm = PWM(Pin(self._pin))
self._pwm.duty(1023-self._volume) if self._invert else self._pwm.duty(self._volume)
self._pwm.freq(int(freq))
sleep_ms(delay)
if self._pa_ctrl: self._pa_ctrl(0)
self._pwm.deinit()
sleep_ms(10)
def stop(self):
if self._pwm:
self._pwm.deinit()
if self._pa_ctrl: self._pa_ctrl(0)
if self._pwm: self._pwm.deinit()
sleep_ms(10)
DADADADUM=['r4:2','g','g','g','eb:8','r:2','f','f','f','d:8']

View File

@@ -21,6 +21,7 @@ class ESPNow(espnow.ESPNow):
self._channel = channel
self._txpower = txpower
self._on_handle = {}
self._once_irq = True
self._nic = network.WLAN(network.STA_IF) #if version else network.WLAN(network.AP_IF)
self._nic.active(True)
self._nic.config(channel=self._channel, txpower=self._txpower)
@@ -46,7 +47,10 @@ class ESPNow(espnow.ESPNow):
self._nic.active(True)
elif err.args[1] == 'ESP_ERR_ESPNOW_NOT_FOUND':
super().add_peer(_peer, channel=self._channel)
return super().send(_peer, str(msg))
try:
return super().send(_peer, str(msg))
except:
raise OSError("ESPNOW channel ({}) conflicts with WiFi channel ({})".format(self._channel, self.channel))
elif err.args[1] == 'ESP_ERR_ESPNOW_NO_MEM':
raise OSError("internal ESP-NOW buffers are full")
elif err.args[1] == 'ESP_ERR_ESPNOW_ARG':
@@ -65,6 +69,8 @@ class ESPNow(espnow.ESPNow):
def set_channel(self, channel=None, txpower=None):
self._channel = self._channel if channel is None else channel
self._nic.config(channel=self._channel, txpower=self._txpower if txpower is None else txpower)
if self._channel != self.channel:
print("Warning: The set channel ({}) does not match the actual channel ({})".format(self._channel, self.channel))
def _cb_handle0(self, event_code, data):
'''Callback processing conversion'''
@@ -112,12 +118,14 @@ class ESPNow(espnow.ESPNow):
self._on_handle(hexlify(host).decode(), msg.decode())
def recv_cb(self, *args):
'''Receive callback'''
if isinstance(args[0], str):
self._on_handle[args[0]] = args[1]
'''Receive callback (single dictionary or lists)'''
if len(args) >= 2:
self._on_handle.update({args[0]: args[1]})
else:
self._on_handle = args[0]
if args[0]:
if self._once_irq:
self._once_irq = False
if version == 0:
self.irq(self._cb_handle0)
else:

View File

@@ -85,7 +85,7 @@ onboard_rgb = NeoPixel(Pin(21), 4)
'''1Buzzer-Music'''
from music import MIDI
onboard_music = MIDI(16)
onboard_music = MIDI(16, pa_ctrl=onboard_asr.pa_ctrl)
'''5KEY_Sensor'''
class KEYSensor: