更新飞翔的库

This commit is contained in:
dahanzimin
2025-09-16 18:03:23 +08:00
parent 6fa10e5451
commit 4adf341226
5 changed files with 461 additions and 26 deletions

View File

@@ -3,12 +3,10 @@ QMI8658
Micropython library for the QMI8658(Accelerometer+Gyroscope)
=======================================================
#Preliminary composition 20220716
dahanzimin From the Mixly Team
@dahanzimin From the Mixly Team
"""
import time
from math import atan, sqrt, degrees
from micropython import const
QMI8658_REG_DEVICE_ID = const(0x00)
@@ -54,7 +52,7 @@ class QMI8658:
raise AttributeError("Cannot find a QMI8658")
self._wreg(QMI8658_REG_Ctrl9,0xA2) #做selftest提高精度
time.sleep(1)
time.sleep(0.1)
self._wreg(QMI8658_REG_Ctrl1,0x60)
self._wreg(QMI8658_REG_Ctrl7,0x03) #启动
self._wreg(QMI8658_REG_Ctrl2,(AccRange<< 4)|Acc_Gyr_Odr) #ACC-500HZ/8G
@@ -78,8 +76,9 @@ class QMI8658:
return self._rreg(QMI8658_REG_StatusInt)
def u2s(self,n):
return n if n < (1 << 15) else n - (1 << 16)
return n if n < (1 << 15) else n - (1 << 16)
@property
def getdata(self):
while self.status() == 0x81:
time.sleep(0.001)
@@ -92,12 +91,21 @@ class QMI8658:
gyr_y=float(self.u2s(_buffer[11]<<8|_buffer[10]))/self.gyr_lsb_div
gyr_z=float(self.u2s(_buffer[13]<<8|_buffer[12]))/self.gyr_lsb_div
return (acc_x,acc_y,acc_z),(gyr_x,gyr_y,gyr_z),round(tmp,2)
def accelerometer(self):
return self.getdata()[0]
def acceleration(self):
return self.getdata[0]
def strength(self):
return sqrt(self.getdata[0][0]**2+self.getdata[0][1]**2+self.getdata[0][2]**2)
def gyroscope(self):
return self.getdata()[1]
return self.getdata[1]
def temperature(self):
return self.getdata()[2]
return self.getdata[2]
def eulerangles(self,upright=False):
x,y,z = self.acceleration()
pitch = degrees(atan(z / sqrt(x ** 2 + y ** 2))) if upright else degrees(atan(y / sqrt(x ** 2 + z ** 2)))
roll = degrees(atan(x / sqrt(y ** 2 + z ** 2)))
return round(pitch,2),round(roll,2)

View File

@@ -0,0 +1,218 @@
"""
mixgo_soar onboard resources
Micropython library for the mixgo_soar onboard resources
=======================================================
@dahanzimin From the Mixly Team
"""
from machine import *
import time, gc, math, st7789_bf
'''RTC'''
rtc_clock = RTC()
'''I2C-onboard'''
#onboard_i2c = I2C(0)
onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000)
'''SPI-onboard'''
onboard_spi = SPI(1, baudrate=80000000, polarity=0, phase=0)
'''BOT035-Sensor'''
try :
import soar_bot
onboard_bot = soar_bot.BOT035(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e)
'''BPS-Sensor'''
try :
import spl06_001
onboard_bps = spl06_001.SPL06(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with SPL06-001 (BPS) or",e)
'''IMU-Sensor'''
try :
import qmi8658
onboard_imu = qmi8658.QMI8658(onboard_i2c, 0x6A)
except Exception as e:
print("Warning: Failed to communicate with QMI8658 (IMU) or",e)
'''ALS_PS-Sensor'''
try :
import ltr553als
onboard_als = ltr553als.LTR_553ALS(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or",e)
'''MGS-Sensor'''
try :
import mmc5603
onboard_mgs = mmc5603.MMC5603(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or",e)
'''TFT/240*240'''
onboard_tft = st7789_bf.ST7789(onboard_spi, 240, 240, dc_pin=46, cs_pin=45, bl_pin=onboard_bot.tft_brightness, brightness=0, font_address=0x700000)
'''2RGB_WS2812'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(40), 4)
'''5KEY_Sensor'''
class KEYSensor:
def __init__(self, pin, range):
self.pin = pin
self.adc = ADC(Pin(pin), atten=ADC.ATTN_0DB)
self.range = range
self.flag = True
def _value(self):
values = []
for _ in range(50):
values.append(self.adc.read())
time.sleep_us(2)
return (self.range-200) < min(values) < (self.range+200)
def get_presses(self, delay = 1):
last_time,presses = time.time(), 0
while time.time() < last_time + delay:
time.sleep_ms(50)
if self.was_pressed():
presses += 1
return presses
def is_pressed(self):
return self._value()
def was_pressed(self):
if(self._value() != self.flag):
self.flag = self._value()
if self.flag :
return True
else:
return False
def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler = handler, trigger = trigger)
'''1KEY_Button'''
class Button(KEYSensor):
def __init__(self, pin):
self.pin = pin
self.key = Pin(pin, Pin.IN)
self.flag = True
def _value(self):
return not self.key.value()
B1key = Button(0)
B2key = KEYSensor(17,0)
A1key = KEYSensor(17,2300)
A2key = KEYSensor(17,1600)
A3key = KEYSensor(17,800)
A4key = KEYSensor(17,2900)
'''2LED-Independent'''
class LED:
def __init__(self, pins=[]):
self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins]
self._brightness = [0 for _ in range(len(self._pins))]
def setbrightness(self, index, val):
if not 0 <= val <= 100:
raise ValueError("Brightness must be in the range: 0-100%")
self._brightness[index - 1] = val
self._pins[index - 1].duty_u16(val * 65535 // 100)
def getbrightness(self, index):
return self._brightness[index - 1]
def setonoff(self, index, val):
if val == -1:
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0)
elif val == 1:
self.setbrightness(index, 100)
elif val == 0:
self.setbrightness(index, 0)
def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False
onboard_led = LED([38, 39])
class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): #定义时钟中心点和半径
self.display = oled
self.xc = x
self.yc = y
self.r = radius
self.color= color
self.hour = 0
self.min = 0
self.sec = 0
def set_time(self, h, m, s): #设定时间
self.hour = h
self.min = m
self.sec = s
def set_rtctime(self): #设定时间
t = rtc_clock.datetime()
self.hour = t[4]
self.min = t[5]
self.sec = t[6]
def drawDial(self,color): #画钟表刻度
r_tic1 = self.r - 1
r_tic2 = self.r - 2
self.display.ellipse(self.xc, self.yc, self.r, self.r, color)
self.display.ellipse(self.xc, self.yc, 2, 2, color,True)
for h in range(12):
at = math.pi * 2.0 * h / 12.0
x1 = round(self.xc + r_tic1 * math.sin(at))
x2 = round(self.xc + r_tic2 * math.sin(at))
y1 = round(self.yc - r_tic1 * math.cos(at))
y2 = round(self.yc - r_tic2 * math.cos(at))
self.display.line(x1, y1, x2, y2, color)
def drawHour(self,color): #画时针
r_hour = int(self.r / 10.0 * 5)
ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0
xh = int(self.xc + r_hour * math.sin(ah))
yh = int(self.yc - r_hour * math.cos(ah))
self.display.line(self.xc, self.yc, xh, yh, color)
def drawMin(self,color): #画分针
r_min = int(self.r / 10.0 * 7)
am = math.pi * 2.0 * self.min / 60.0
xm = round(self.xc + r_min * math.sin(am))
ym = round(self.yc - r_min * math.cos(am))
self.display.line(self.xc, self.yc, xm, ym, color)
def drawSec(self,color): #画秒针
r_sec = int(self.r / 10.0 * 9)
asec = math.pi * 2.0 * self.sec / 60.0
xs = round(self.xc + r_sec * math.sin(asec))
ys = round(self.yc - r_sec * math.cos(asec))
self.display.line(self.xc, self.yc, xs, ys, color)
def draw_clock(self, bg_color=0): #画完整钟表
self.drawDial(self.color)
self.drawHour(self.color)
self.drawMin(self.color)
self.drawSec(self.color)
self.display.show()
self.drawHour(bg_color)
self.drawMin(bg_color)
self.drawSec(bg_color)
def clear(self, color=0): #清除
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
'''Reclaim memory'''
gc.collect()
onboard_tft.set_brightness(0.6)

View File

@@ -0,0 +1,86 @@
"""
Soar Voice Onboard resources
Micropython library for the Soar Onboard resources
=======================================================
@dahanzimin From the Mixly Team
"""
import ustruct
import time
import music_spk
import es8374
from machine import Pin, I2S
from mixgo_soar import onboard_i2c
sample_rate = 22050
ob_code = es8374.ES8374(onboard_i2c)
time.sleep(0.2)
#ps 特殊改双全工i2s支持
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
spk_midi = music_spk.MIDI(ob_audio, sample_rate)
def u2s(n):
return n if n < (1 << 15) else n - (1 << 16)
def sound_level():
buf = bytearray(100)
values = []
ob_audio.readinto(buf)
for i in range(len(buf)//2):
values.append(u2s(buf[i * 2] | buf[i * 2 + 1]<<8))
return max(values) - min(values)
def play_audio(path):
file = open(path, 'rb')
header = file.read(44)
if header[8:12] != b'WAVE':
raise Error('not a WAVE file')
_rate = ustruct.unpack('<I', header[24:28])[0]
print("sample_rate", _rate)
file.seek(44)
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000)
while True:
block = file.read(1024)
if not block:
break
ob_audio.write(block)
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
file.close()
def record_audio(path, seconds=5):
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate*4, ibuf=20000)
file_size = sample_rate * 16 * 1 * seconds // 8
wav_header = bytearray(44)
wav_header[0:4] = b'RIFF'
ustruct.pack_into('<I', wav_header, 4, file_size + 36)
wav_header[8:40] = b'WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00"V\x00\x00D\xac\x00\x00\x02\x00\x10\x00data'
ustruct.pack_into('<I', wav_header, 40, file_size)
buf = bytearray(512)
file = open(path, 'wb')
file.write(wav_header)
for _ in range(file_size // 512):
ob_audio.readinto(buf)
file.write(buf)
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
file.close()
def play_audio_url(url):
import urequests
response = urequests.get(url, stream=True)
header = response.raw.read(44)
if header[8:12] != b'WAVE':
raise Error('not a WAVE file')
_rate = ustruct.unpack('<I', header[24:28])[0]
#print("sample_rate", _rate)
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000)
while True:
block = response.raw.read(1024)
if not block:
break
ob_audio.write(block)
ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(33), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
response.close()

View File

@@ -0,0 +1,106 @@
"""
SOAR_WCH
Micropython library for the SOAR_WCH
=======================================================
@dahanzimin From the Mixly Team
"""
import time
from micropython import const
_BOT035_ADDRESS = const(0x13)
_BOT5_TOUCH = const(0x01)
_BOT035_ADC = const(0x05)
_BOT035_OPA = const(0x09)
_BOT035_PWM = const(0x0B)
_BOT035_CMD = const(0x11)
_BOT035_STA = const(0x12)
_BOT035_KB = const(0x13)
_BOT035_MS = const(0x17)
_BOT035_STR = const(0x1B)
class BOT035:
def __init__(self, i2c_bus):
self._i2c = i2c_bus
self._touchs = [self.touch(0), self.touch(1)]
self.reset()
def _wreg(self, reg, val, digit=1):
'''Write memory address'''
self._i2c.writeto_mem(_BOT035_ADDRESS, reg, val.to_bytes(digit, 'little'))
def _rreg(self, reg, nbytes=1):
'''Read memory address'''
self._i2c.writeto(_BOT035_ADDRESS, reg.to_bytes(1, 'little'))
return int.from_bytes(self._i2c.readfrom(_BOT035_ADDRESS, nbytes), 'little')
def reset(self):
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_PWM, b' Ndddd\xc0')
def touch(self, index, value=None):
index = max(min(index, 1), 0)
touch = 4095 - self._rreg(_BOT5_TOUCH + index * 2, 2)
return touch > value if value else touch
def touched(self, index, value=600):
return self.touch(index, value)
def touch_slide(self):
values = []
for i in range(20):
values.append((self.touch(1) - self._touchs[1]) - (self.touch(0) - self._touchs[0]))
return round(sorted(values)[10] / 10)
def brightness(self, index):
index = max(min(index, 1), 0)
return 4095 - self._rreg(_BOT035_ADC + index * 2, 2)
def amp_pga(self, k=1, b=155, pga=3, dp=0):
"""0:4x,1:8x,2:16x,3:32x"""
self._wreg(_BOT035_CMD, (self._rreg(_BOT035_CMD) & 0x3F) | (pga & 0x03) << 6)
return round((self._rreg(_BOT035_OPA, 2) * k - b) / (2 ** (pga + 2)), dp)
def usben(self, index=1, duty=None, freq=None):
index = max(min(index, 4), 1)
if duty is not None:
self._wreg(_BOT035_PWM + index + 1, int(max(min(duty, 100), 0)))
if freq is not None:
self._wreg(_BOT035_PWM, max(min(freq, 65535), 10), 2)
if freq is None and duty is None:
return self._rreg(_BOT035_PWM + index + 1), self._rreg(_BOT035_PWM ,2)
def tft_brightness(self, brightness=None):
if brightness is None:
return round((self._rreg(_BOT035_CMD) & 0x3F) * 1.587)
else:
brightness = round(brightness* 0.63)
self._wreg(_BOT035_CMD, (self._rreg(_BOT035_CMD) & 0xC0) | max(min(brightness, 63), 0))
def hid_keyboard(self, special=0, general=0, release=True):
self._buf = bytearray(4)
self._buf[0] = special
if type(general) in (tuple, list):
for i in range(len(general)):
if i > 2: break
self._buf[i + 1] = general[i]
else:
self._buf[1] = general
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_KB, self._buf)
if release:
time.sleep_ms(10)
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_KB, bytes(4))
def hid_keyboard_str(self, string, delay=0):
for char in str(string):
self._wreg(_BOT035_STR, ord(char) & 0xFF)
time.sleep_ms(20 + delay)
def hid_keyboard_state(self):
state = self._rreg(_BOT035_STA)
return bool(state & 0x10), bool(state & 0x20), bool(state & 0x40)
def hid_mouse(self, keys=0, move=(0, 0), wheel=0, release=True):
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_MS, bytes([keys & 0x0F, move[0] & 0xFF, move[1] & 0xFF, wheel & 0xFF]))
if release:
time.sleep_ms(10)
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_MS, bytes(4))

View File

@@ -30,9 +30,7 @@ _CMD_COLMOD = const(0x3A)
_CMD_MADCTL = const(0x36)
class ST7789(uframebuf.FrameBuffer_Uincode):
def __init__(self, spi, width, height, dc_pin=None, cs_pin=None, bl_pin=None, font_address=0x700000):
if height != 240 or width not in [320, 240, 135]:
raise ValueError("Unsupported display. 320x240, 240x240 and 135x240 are supported.")
def __init__(self, spi, width, height, dc_pin=None, cs_pin=None, bl_pin=None, brightness=0.6, font_address=0x700000):
self.spi = spi
self.dc = Pin(dc_pin, Pin.OUT, value=1)
self.cs = Pin(cs_pin, Pin.OUT, value=1)
@@ -41,9 +39,12 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
self.font(font_address)
self._init()
self.show()
time.sleep_ms(100)
self._brightness = 0.6
self.bl_led = PWM(Pin(bl_pin), duty_u16=int(self._brightness * 60000)) if bl_pin else None
if brightness > 0: time.sleep_ms(100)
self._brightness = brightness
if callable(bl_pin):
self.bl_led = bl_pin
else:
self.bl_led = PWM(Pin(bl_pin), duty_u16=int(self._brightness * 60000)) if bl_pin else None
def _write(self, cmd, dat = None):
self.cs.off()
@@ -59,13 +60,26 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def _init(self):
"""Display initialization configuration"""
for cmd, data, delay in [
(_CMD_SWRESET, None, 150),
(_CMD_SLPOUT, None, None),
(_CMD_COLMOD, b'\x55', 50),
(_CMD_MADCTL, b'\x60', 50),
(_CMD_INVOFF, None, 10),
(_CMD_NORON, None, 10),
(_CMD_DISPON, None, 200),
#(_CMD_SWRESET, None, 20000),
(_CMD_SLPOUT, None, 120000),
(_CMD_MADCTL, b'\x00', 50),
(_CMD_COLMOD, b'\x05', 50),
(0xB2, b'\x0c\x0c\x00\x33\x33', 10),
(0xB7, b'\x35', 10),
(0xBB, b'\x19', 10),
(0xC0, b'\x2C', 10),
(0xC2, b'\x01', 10),
(0xC3, b'\x12', 10),
(0xC4, b'\x20', 10),
(0xC6, b'\x0F', 10),
(0xD0, b'\xA4\xA1', 10),
(0xE0, b'\xD0\x04\x0D\x11\x13\x2B\x3F\x54\x4C\x18\x0D\x0B\x1F\x23', 10),
(0xE1, b'\xD0\x04\x0C\x11\x13\x2C\x3F\x44\x51\x2F\x1F\x1F\x20\x23', 10),
(0x21, None, 10),
(0x29, None, 10),
# (_CMD_INVOFF, None, 10),
# (_CMD_NORON, None, 10),
# (_CMD_DISPON, None, 200),
]:
self._write(cmd, data)
if delay:
@@ -91,7 +105,10 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
if not 0.0 <= brightness <= 1.0:
raise ValueError("Brightness must be a decimal number in the range: 0.0~1.0")
self._brightness = brightness
self.bl_led.duty_u16(int(brightness*60000))
if callable(self.bl_led):
self.bl_led(brightness * 100)
elif isinstance(self.bl_led, PWM):
self.bl_led.duty_u16(int(brightness*60000))
def color(self, red, green=None, blue=None):
""" Convert red, green and blue values (0-255) into a 16-bit 565 encoding."""