修复mini的蜂鸣器,大气呀及更新lib固件
This commit is contained in:
@@ -1,114 +1,115 @@
|
|||||||
"""
|
"""
|
||||||
_SPL06-001
|
_SPL06-001
|
||||||
|
|
||||||
MicroPython library for the _SPL06-001(Air pressure sensor)
|
MicroPython library for the _SPL06-001(Air pressure sensor)
|
||||||
=======================================================
|
=======================================================
|
||||||
#Preliminary composition 20240108
|
@dahanzimin From the Mixly Team
|
||||||
|
"""
|
||||||
@dahanzimin From the Mixly Team
|
import time
|
||||||
"""
|
from micropython import const
|
||||||
import time
|
|
||||||
from micropython import const
|
_SPL06_ADDRESS = const(0x77)
|
||||||
|
_SPL06_REG_PSR = const(0x00)
|
||||||
_SPL06_ADDRESS = const(0x77)
|
_SPL06_REG_TMP = const(0x03)
|
||||||
_SPL06_REG_PSR = const(0x00)
|
_SPL06_PSR_CFG = const(0x06)
|
||||||
_SPL06_REG_TMP = const(0x03)
|
_SPL06_TMP_CFG = const(0x07)
|
||||||
_SPL06_PSR_CFG = const(0x06)
|
_SPL06_MEAS_CFG = const(0x08)
|
||||||
_SPL06_TMP_CFG = const(0x07)
|
_SPL06_CFG_REG = const(0x09)
|
||||||
_SPL06_MEAS_CFG = const(0x08)
|
_SPL06_REG_RST = const(0x0C)
|
||||||
_SPL06_CFG_REG = const(0x09)
|
_SPL06_REG_ID = const(0x0D)
|
||||||
_SPL06_REG_RST = const(0x0C)
|
_SPL06_REG_COEF = const(0x10)
|
||||||
_SPL06_REG_ID = const(0x0D)
|
|
||||||
_SPL06_REG_COEF = const(0x10)
|
#Parameter selection(sample/sec, times, kT/kP)
|
||||||
|
_SPL06_PSR_TMP_1 = (0<<4, 0, 524288)
|
||||||
#Parameter selection(sample/sec, times, kT/kP)
|
_SPL06_PSR_TMP_2 = (1<<4, 1, 1572864)
|
||||||
_SPL06_PSR_TMP_1 = (0<<4, 0, 524288)
|
_SPL06_PSR_TMP_4 = (2<<4, 2, 3670016)
|
||||||
_SPL06_PSR_TMP_2 = (1<<4, 1, 1572864)
|
_SPL06_PSR_TMP_8 = (3<<4, 3, 7864320)
|
||||||
_SPL06_PSR_TMP_4 = (2<<4, 2, 3670016)
|
_SPL06_PSR_TMP_16 = (4<<4, 4, 253952)
|
||||||
_SPL06_PSR_TMP_8 = (3<<4, 3, 7864320)
|
_SPL06_PSR_TMP_32 = (5<<4, 5, 516096)
|
||||||
_SPL06_PSR_TMP_16 = (4<<4, 4, 253952)
|
_SPL06_PSR_TMP_64 = (6<<4, 6, 1040384)
|
||||||
_SPL06_PSR_TMP_32 = (5<<4, 5, 516096)
|
_SPL06_PSR_TMP_128 = (7<<4, 7, 2088960)
|
||||||
_SPL06_PSR_TMP_64 = (6<<4, 6, 1040384)
|
|
||||||
_SPL06_PSR_TMP_128 = (7<<4, 7, 2088960)
|
class SPL06:
|
||||||
|
def __init__(self, i2c_bus, addr=_SPL06_ADDRESS, rate=_SPL06_PSR_TMP_32):
|
||||||
class SPL06:
|
self._device = i2c_bus
|
||||||
def __init__(self, i2c_bus, addr=_SPL06_ADDRESS, rate=_SPL06_PSR_TMP_32):
|
self._address = addr
|
||||||
self._device = i2c_bus
|
self._rate = rate
|
||||||
self._address = addr
|
self._psr = 0
|
||||||
self._rate = rate
|
self._tmp = 0
|
||||||
self._psr = 0
|
self._alt = 0
|
||||||
self._tmp = 0
|
if self._rreg(_SPL06_REG_ID) != 0x10:
|
||||||
self._alt = 0
|
raise AttributeError("Cannot find a SPL06-001")
|
||||||
if self._rreg(_SPL06_REG_ID) != 0x10:
|
self._init()
|
||||||
raise AttributeError("Cannot find a SPL06-001")
|
|
||||||
self._init()
|
def _wreg(self, reg, val):
|
||||||
|
'''Write memory address'''
|
||||||
def _wreg(self, reg, val):
|
self._device.writeto_mem(self._address,reg,val.to_bytes(1, 'little'))
|
||||||
'''Write memory address'''
|
|
||||||
self._device.writeto_mem(self._address,reg,val.to_bytes(1, 'little'))
|
def _rreg(self, reg, nbytes=1):
|
||||||
|
'''Read memory address'''
|
||||||
def _rreg(self, reg, nbytes=1):
|
return self._device.readfrom_mem(self._address, reg, nbytes)[0] if nbytes<=1 else self._device.readfrom_mem(self._address, reg, nbytes)[0:nbytes]
|
||||||
'''Read memory address'''
|
|
||||||
return self._device.readfrom_mem(self._address, reg, nbytes)[0] if nbytes<=1 else self._device.readfrom_mem(self._address, reg, nbytes)[0:nbytes]
|
def _u2s(self, value, n=8):
|
||||||
|
return value if value < (1 << (n-1)) else value - (1 << n)
|
||||||
def _u2s(self, value, n=8):
|
|
||||||
return value if value < (1 << (n-1)) else value - (1 << n)
|
def _status(self):
|
||||||
|
'''数据转换状态'''
|
||||||
def _status(self):
|
status = self._rreg(_SPL06_MEAS_CFG)
|
||||||
'''数据转换状态'''
|
return status & 0x80, status & 0x40, (status >> 4 & 0x01) & (status >> 5 & 0x01) #COEF_RDY,SENSOR_RDY,TMP_RDY+PRS_RDY
|
||||||
status = self._rreg(_SPL06_MEAS_CFG)
|
|
||||||
return status & 0x80, status & 0x40, (status >> 4 & 0x01) & (status >> 5 & 0x01) #COEF_RDY,SENSOR_RDY,TMP_RDY+PRS_RDY
|
def _init(self):
|
||||||
|
'''软复位'''
|
||||||
def _init(self):
|
self._wreg(_SPL06_REG_RST, 0x89)
|
||||||
'''软复位'''
|
time.sleep_ms(50)
|
||||||
self._wreg(_SPL06_REG_RST, 0x89)
|
'''判断校准数据是否就绪,并读取'''
|
||||||
time.sleep_ms(50)
|
while not self._status()[0]:
|
||||||
'''判断校准数据是否就绪,并读取'''
|
time.sleep_ms(1)
|
||||||
while not self._status()[0]:
|
buf = self._rreg(_SPL06_REG_COEF, 18)
|
||||||
time.sleep_ms(1)
|
self._c0 = self._u2s(buf[0] << 4 | buf[1] >> 4, 12)
|
||||||
buf = self._rreg(_SPL06_REG_COEF, 18)
|
self._c1 = self._u2s((buf[1] & 0x0F) << 8 | buf[2], 12)
|
||||||
self._c0 = self._u2s(buf[0] << 4 | buf[1] >> 4, 12)
|
self._c00 = self._u2s(buf[3] << 12 | buf[4] << 4 | buf[5] >> 4, 20)
|
||||||
self._c1 = self._u2s((buf[1] & 0x0F) << 8 | buf[2], 12)
|
self._c10 = self._u2s((buf[5] & 0x0F) << 16 | buf[6] << 8 | buf[7], 20)
|
||||||
self._c00 = self._u2s(buf[3] << 12 | buf[4] << 4 | buf[5] >> 4, 20)
|
self._c01 = self._u2s(buf[8] << 8 | buf[9], 16)
|
||||||
self._c10 = self._u2s((buf[5] & 0x0F) << 16 | buf[6] << 8 | buf[7], 20)
|
self._c11 = self._u2s(buf[10] << 8 | buf[11], 16)
|
||||||
self._c01 = self._u2s(buf[8] << 8 | buf[9], 16)
|
self._c20 = self._u2s(buf[12] << 8 | buf[13], 16)
|
||||||
self._c11 = self._u2s(buf[10] << 8 | buf[11], 16)
|
self._c21 = self._u2s(buf[14] << 8 | buf[15], 16)
|
||||||
self._c20 = self._u2s(buf[12] << 8 | buf[13], 16)
|
self._c30 = self._u2s(buf[16] << 8 | buf[17], 16)
|
||||||
self._c21 = self._u2s(buf[14] << 8 | buf[15], 16)
|
|
||||||
self._c30 = self._u2s(buf[16] << 8 | buf[17], 16)
|
'''判断传感器是否就绪,并设置'''
|
||||||
|
while not self._status()[1]:
|
||||||
'''判断传感器是否就绪,并设置'''
|
time.sleep_ms(1)
|
||||||
while not self._status()[1]:
|
self._wreg(_SPL06_MEAS_CFG, 0x07) #Continuous pressure and temperature
|
||||||
time.sleep_ms(1)
|
self._wreg(_SPL06_PSR_CFG, self._rate[0] | self._rate[1]) #Configuration of pressure measurement.
|
||||||
self._wreg(_SPL06_MEAS_CFG, 0x07) #Continuous pressure and temperature
|
self._wreg(_SPL06_TMP_CFG, self._rate[0] | self._rate[1] | 0x80) #Configuration of temperature measurement.
|
||||||
self._wreg(_SPL06_PSR_CFG, self._rate[0] | self._rate[1]) #Configuration of pressure measurement.
|
self._rreg(_SPL06_REG_PSR, 6)
|
||||||
self._wreg(_SPL06_TMP_CFG, self._rate[0] | self._rate[1] | 0x80) #Configuration of temperature measurement.
|
|
||||||
self._rreg(_SPL06_REG_PSR, 6)
|
if self._rate[1] > 3:
|
||||||
|
self._wreg(_SPL06_CFG_REG, self._rreg(_SPL06_CFG_REG) | 0x0C) #when the oversampling rate is >8 times.
|
||||||
if self._rate[1] > 3:
|
|
||||||
self._wreg(_SPL06_CFG_REG, self._rreg(_SPL06_CFG_REG) | 0x0C) #when the oversampling rate is >8 times.
|
''''判断数据是否就绪,并读取'''
|
||||||
|
#while not self._status()[2]:
|
||||||
''''判断数据是否就绪,并读取'''
|
#time.sleep_ms(1) #数据就绪需要耗时1s左右
|
||||||
#while not self._status()[2]:
|
|
||||||
#time.sleep_ms(1) #数据就绪需要耗时1s左右
|
@property
|
||||||
|
def getdata(self):
|
||||||
@property
|
'''处理获取数据'''
|
||||||
def getdata(self):
|
if self._status()[2]:
|
||||||
'''处理获取数据'''
|
buf = self._rreg(_SPL06_REG_PSR, 6)
|
||||||
if self._status()[2]:
|
praw = self._u2s(buf[0] << 16 | buf[1] << 8 | buf[2], 24) / self._rate[2]
|
||||||
buf = self._rreg(_SPL06_REG_PSR, 6)
|
traw = self._u2s(buf[3] << 16 | buf[4] << 8 | buf[5], 24) / self._rate[2]
|
||||||
praw = self._u2s(buf[0] << 16 | buf[1] << 8 | buf[2], 24) / self._rate[2]
|
try:
|
||||||
traw = self._u2s(buf[3] << 16 | buf[4] << 8 | buf[5], 24) / self._rate[2]
|
self._psr = self._c00 + praw * (self._c10 + praw *(self._c20 + praw * self._c30)) + traw * self._c01 + traw * praw * (self._c11 + praw * self._c21)
|
||||||
self._psr = self._c00 + praw * (self._c10 + praw *(self._c20 + praw * self._c30)) + traw * self._c01 + traw * praw * (self._c11 + praw * self._c21)
|
except:
|
||||||
self._tmp = self._c0 * 0.5 + self._c1 * traw
|
self._psr = 0
|
||||||
self._alt = (1 - (self._psr / 101325) ** (1/5.255)) * 44330
|
self._tmp = self._c0 * 0.5 + self._c1 * traw
|
||||||
return round(self._psr/100, 2), round(self._tmp, 2), round(self._alt,2)
|
self._alt = (1 - (self._psr / 101325) ** (1/5.255)) * 44330
|
||||||
|
return round(self._psr/100, 2), round(self._tmp, 2), round(self._alt,2)
|
||||||
def pressure(self):
|
|
||||||
return self.getdata[0]
|
def pressure(self):
|
||||||
|
return self.getdata[0]
|
||||||
def temperature(self):
|
|
||||||
return self.getdata[1]
|
def temperature(self):
|
||||||
|
return self.getdata[1]
|
||||||
def altitude(self):
|
|
||||||
return self.getdata[2]
|
def altitude(self):
|
||||||
|
return self.getdata[2]
|
||||||
|
|||||||
Binary file not shown.
@@ -1,121 +1,123 @@
|
|||||||
"""
|
"""
|
||||||
Music buzzer(BOT)
|
Music buzzer(BOT)
|
||||||
|
|
||||||
Micropython library for the Music buzzer(Coprocessor I2C communication)
|
Micropython library for the Music buzzer(Coprocessor I2C communication)
|
||||||
=======================================================
|
=======================================================
|
||||||
@dahanzimin From the Mixly Team
|
@dahanzimin From the Mixly Team
|
||||||
"""
|
"""
|
||||||
from time import sleep_ms
|
from time import sleep_ms
|
||||||
|
|
||||||
normal_tone = {
|
normal_tone = {
|
||||||
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
|
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
|
||||||
'A2': 110, 'B2': 123, 'C2': 65, 'D2': 73, 'E2': 82, 'F2': 87, 'G2': 98,
|
'A2': 110, 'B2': 123, 'C2': 65, 'D2': 73, 'E2': 82, 'F2': 87, 'G2': 98,
|
||||||
'A3': 220, 'B3': 247, 'C3': 131, 'D3': 147, 'E3': 165, 'F3': 175, 'G3': 196,
|
'A3': 220, 'B3': 247, 'C3': 131, 'D3': 147, 'E3': 165, 'F3': 175, 'G3': 196,
|
||||||
'A4': 440, 'B4': 494, 'C4': 262, 'D4': 294, 'E4': 330, 'F4': 349, 'G4': 392,
|
'A4': 440, 'B4': 494, 'C4': 262, 'D4': 294, 'E4': 330, 'F4': 349, 'G4': 392,
|
||||||
'A5': 880, 'B5': 988, 'C5': 523, 'D5': 587, 'E5': 659, 'F5': 698, 'G5': 784,
|
'A5': 880, 'B5': 988, 'C5': 523, 'D5': 587, 'E5': 659, 'F5': 698, 'G5': 784,
|
||||||
'A6': 1760, 'B6': 1976, 'C6': 1047, 'D6': 1175, 'E6': 1319, 'F6': 1397, 'G6': 1568,
|
'A6': 1760, 'B6': 1976, 'C6': 1047, 'D6': 1175, 'E6': 1319, 'F6': 1397, 'G6': 1568,
|
||||||
'A7': 3520, 'B7': 3951, 'C7': 2093, 'D7': 2349, 'E7': 2637, 'F7': 2794, 'G7': 3135,
|
'A7': 3520, 'B7': 3951, 'C7': 2093, 'D7': 2349, 'E7': 2637, 'F7': 2794, 'G7': 3135,
|
||||||
'A8': 7040, 'B8': 7902, 'C8': 4186, 'D8': 4699, 'E8': 5274, 'F8': 5588, 'G8': 6271,
|
'A8': 7040, 'B8': 7902, 'C8': 4186, 'D8': 4699, 'E8': 5274, 'F8': 5588, 'G8': 6271,
|
||||||
'A9': 14080, 'B9': 15804 }
|
'A9': 14080, 'B9': 15804 }
|
||||||
|
|
||||||
Letter = 'ABCDEFG#R'
|
Letter = 'ABCDEFG#R'
|
||||||
|
|
||||||
class MIDI():
|
class MIDI():
|
||||||
def __init__(self, bus, volume=100):
|
def __init__(self, bus, volume=100):
|
||||||
self.reset()
|
self.reset()
|
||||||
self._bus = bus
|
self._bus = bus
|
||||||
self._volume = volume // 5
|
self._volume = volume // 5
|
||||||
|
|
||||||
def set_volume(self, volume):
|
def set_volume(self, volume):
|
||||||
self._volume = max(min(volume, 100), 0) // 5
|
self._volume = max(min(volume, 100), 0) // 5
|
||||||
|
|
||||||
def set_tempo(self, ticks=4, bpm=120):
|
def set_tempo(self, ticks=4, bpm=120):
|
||||||
self.ticks = ticks
|
self.ticks = ticks
|
||||||
self.bpm = bpm
|
self.bpm = bpm
|
||||||
self.beat = 60000 / self.bpm / self.ticks
|
self.beat = 60000 / self.bpm / self.ticks
|
||||||
|
|
||||||
def set_octave(self, octave=4):
|
def set_octave(self, octave=4):
|
||||||
self.octave = octave
|
self.octave = octave
|
||||||
|
|
||||||
def set_duration(self, duration=4):
|
def set_duration(self, duration=4):
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
|
|
||||||
def get_tempo(self):
|
def get_tempo(self):
|
||||||
return (self.ticks, self.bpm)
|
return (self.ticks, self.bpm)
|
||||||
|
|
||||||
def get_octave(self):
|
def get_octave(self):
|
||||||
return self.octave
|
return self.octave
|
||||||
|
|
||||||
def get_duration(self):
|
def get_duration(self):
|
||||||
return self.duration
|
return self.duration
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.set_duration()
|
self.set_duration()
|
||||||
self.set_octave()
|
self.set_octave()
|
||||||
self.set_tempo()
|
self.set_tempo()
|
||||||
|
|
||||||
def parse(self, tone, dict):
|
def parse(self, tone, dict):
|
||||||
time = self.beat * self.duration
|
time = self.beat * self.duration
|
||||||
pos = tone.find(':')
|
pos = tone.find(':')
|
||||||
if pos != -1:
|
if pos != -1:
|
||||||
time = self.beat * int(tone[(pos + 1):])
|
time = self.beat * int(tone[(pos + 1):])
|
||||||
tone = tone[:pos]
|
tone = tone[:pos]
|
||||||
freq, tone_size = 1, len(tone)
|
freq, tone_size = 1, len(tone)
|
||||||
if 'R' in tone:
|
if 'R' in tone:
|
||||||
freq = 400000
|
freq = 400000
|
||||||
elif tone_size == 1:
|
elif tone_size == 1:
|
||||||
freq = dict[tone[0] + str(self.octave)]
|
freq = dict[tone[0] + str(self.octave)]
|
||||||
elif tone_size == 2:
|
elif tone_size == 2:
|
||||||
freq = dict[tone]
|
freq = dict[tone]
|
||||||
self.set_octave(tone[1:])
|
self.set_octave(tone[1:])
|
||||||
return int(freq), int(time)
|
return int(freq), int(time)
|
||||||
|
|
||||||
def midi(self, tone):
|
def midi(self, tone):
|
||||||
pos = tone.find('#')
|
pos = tone.find('#')
|
||||||
if pos != -1:
|
if pos != -1:
|
||||||
return self.parse(tone.replace('#', ''), normal_tone)
|
return self.parse(tone.replace('#', ''), normal_tone)
|
||||||
pos = tone.find('B')
|
pos = tone.find('B')
|
||||||
if pos != -1 and pos != 0:
|
if pos != -1 and pos != 0:
|
||||||
return self.parse(tone.replace('B', ''), normal_tone)
|
return self.parse(tone.replace('B', ''), normal_tone)
|
||||||
return self.parse(tone, normal_tone)
|
return self.parse(tone, normal_tone)
|
||||||
|
|
||||||
def set_default(self, tone):
|
def set_default(self, tone):
|
||||||
pos = tone.find(':')
|
pos = tone.find(':')
|
||||||
if pos != -1:
|
if pos != -1:
|
||||||
self.set_duration(int(tone[(pos + 1):]))
|
self.set_duration(int(tone[(pos + 1):]))
|
||||||
tone = tone[:pos]
|
tone = tone[:pos]
|
||||||
|
|
||||||
def play(self, tune, duration=None):
|
def play(self, tune, duration=None):
|
||||||
if duration is None:
|
if duration is None:
|
||||||
self.set_default(tune[0])
|
self.set_default(tune[0])
|
||||||
else:
|
else:
|
||||||
self.set_duration(duration)
|
self.set_duration(duration)
|
||||||
for tone in tune:
|
for tone in tune:
|
||||||
tone = tone.upper()
|
tone = tone.upper()
|
||||||
if tone[0] not in Letter:
|
if tone[0] not in Letter:
|
||||||
continue
|
continue
|
||||||
midi = self.midi(tone)
|
midi = self.midi(tone)
|
||||||
self._bus.buzzer(self._volume, midi[0])
|
self._bus.buzzer(self._volume, midi[0])
|
||||||
sleep_ms(midi[1])
|
sleep_ms(midi[1])
|
||||||
self._bus.buzzer(0)
|
self._bus.buzzer(0)
|
||||||
sleep_ms(1)
|
sleep_ms(1)
|
||||||
sleep_ms(10)
|
sleep_ms(10)
|
||||||
|
|
||||||
def pitch(self, freq):
|
def pitch(self, freq):
|
||||||
self._bus.buzzer(self._volume, int(freq))
|
self._bus.buzzer(self._volume, int(freq))
|
||||||
|
|
||||||
def pitch_time(self, freq, delay):
|
def pitch_time(self, freq, delay):
|
||||||
self._bus.buzzer(self._volume, int(freq))
|
self._bus.buzzer(self._volume, int(freq))
|
||||||
sleep_ms(delay)
|
sleep_ms(delay)
|
||||||
self._bus.buzzer(0)
|
self._bus.buzzer(0)
|
||||||
|
sleep_ms(10)
|
||||||
def stop(self):
|
|
||||||
self._bus.buzzer(0)
|
def stop(self):
|
||||||
|
self._bus.buzzer(0)
|
||||||
BA_DING=('b5:1','e6:3')
|
sleep_ms(10)
|
||||||
JUMP_UP=('c5:1','d','e','f','g')
|
|
||||||
JUMP_DOWN=('g5:1','f','e','d','c')
|
BA_DING=('b5:1','e6:3')
|
||||||
POWER_UP=('g4:1','c5','e4','g5:2','e5:1','g5:3')
|
JUMP_UP=('c5:1','d','e','f','g')
|
||||||
POWER_DOWN=('g5:1','d#','c','g4:2','b:1','c5:3')
|
JUMP_DOWN=('g5:1','f','e','d','c')
|
||||||
DADADADUM=('r4:2','g','g','g','eb:8','r:2','f','f','f','d:8')
|
POWER_UP=('g4:1','c5','e4','g5:2','e5:1','g5:3')
|
||||||
BIRTHDAY=('c4:4','c:1','d:4','c:4','f','e:8','c:3','c:1','d:4','c:4','g','f:8','c:3','c:1','c5:4','a4','f','e','d','a#:3','a#:1','a:4','f','g','f:8')
|
POWER_DOWN=('g5:1','d#','c','g4:2','b:1','c5:3')
|
||||||
|
DADADADUM=('r4:2','g','g','g','eb:8','r:2','f','f','f','d:8')
|
||||||
|
BIRTHDAY=('c4:4','c:1','d:4','c:4','f','e:8','c:3','c:1','d:4','c:4','g','f:8','c:3','c:1','c5:4','a4','f','e','d','a#:3','a#:1','a:4','f','g','f:8')
|
||||||
|
|||||||
Reference in New Issue
Block a user