chore(board): micropython_esp32s3调整部分py模块的缩进格式

调整部分模块的缩进格式,防止构建句法树时报错
This commit is contained in:
王立帮
2025-10-24 22:35:16 +08:00
parent 714f157551
commit 17ad6d31a0
14 changed files with 1167 additions and 1053 deletions

View File

@@ -7,12 +7,14 @@ MicroPython library for the Camera(Inherit C module)
""" """
import time import time
import base64, jpeg import base64
import jpeg
from _camera import * from _camera import *
from jpeg import Encoder from jpeg import Encoder
from machine import SoftI2C, Pin from machine import SoftI2C, Pin
from mixgo_sant import onboard_bot from mixgo_sant import onboard_bot
class Camera(Camera): class Camera(Camera):
def __init__(self, frame_size=FrameSize.R240X240, pixel_format=PixelFormat.RGB565, hmirror=False, vflip=False, **kwargs): def __init__(self, frame_size=FrameSize.R240X240, pixel_format=PixelFormat.RGB565, hmirror=False, vflip=False, **kwargs):
onboard_bot.cam_en(1, 150) onboard_bot.cam_en(1, 150)

View File

@@ -7,6 +7,7 @@ MicroPython library for the CI130Xx (ASR-I2C)
""" """
from ci130x import CI130X from ci130x import CI130X
class CI1302(CI130X): class CI1302(CI130X):
def __init__(self, i2c_bus, func, addr=0x64): def __init__(self, i2c_bus, func, addr=0x64):
self._device = i2c_bus self._device = i2c_bus

View File

@@ -28,7 +28,8 @@ class ES8374:
return self.i2c_bus.readfrom_mem(self.i2c_addr, regAddr, 1)[0] return self.i2c_bus.readfrom_mem(self.i2c_addr, regAddr, 1)[0]
def _writeReg(self, regAddr, data): def _writeReg(self, regAddr, data):
self.i2c_bus.writeto_mem(self.i2c_addr, regAddr, data.to_bytes(1, 'little')) self.i2c_bus.writeto_mem(
self.i2c_addr, regAddr, data.to_bytes(1, 'little'))
def init_reg(self, ms_mode, fmt, out_channel, in_channel): def init_reg(self, ms_mode, fmt, out_channel, in_channel):
self._writeReg(0x00, 0x3F) # IC Rst start self._writeReg(0x00, 0x3F) # IC Rst start
@@ -56,7 +57,8 @@ class ES8374:
self._writeReg(0x22, 0xFF) # adc set: PGA GAIN=0DB self._writeReg(0x22, 0xFF) # adc set: PGA GAIN=0DB
self._writeReg(0x21, 0x14) # adc set: SEL LIN1 CH+PGAGAIN=18DB self._writeReg(0x21, 0x14) # adc set: SEL LIN1 CH+PGAGAIN=18DB
self._writeReg(0x22, 0x55) # pga = +15db self._writeReg(0x22, 0x55) # pga = +15db
self._writeReg(0x08, 0x21)# set class d divider = 33, to avoid the high frequency tone on laudspeaker # set class d divider = 33, to avoid the high frequency tone on laudspeaker
self._writeReg(0x08, 0x21)
self._writeReg(0x00, 0x80) # IC START self._writeReg(0x00, 0x80) # IC START
time.sleep(0.05) time.sleep(0.05)
self._writeReg(0x25, 0x00) # ADCVOLUME on self._writeReg(0x25, 0x00) # ADCVOLUME on
@@ -77,11 +79,14 @@ class ES8374:
self._writeReg(0x25, 0x00) # ADCVOLUME on self._writeReg(0x25, 0x00) # ADCVOLUME on
self._writeReg(0x38, 0x00) # DACVOLUME on self._writeReg(0x38, 0x00) # DACVOLUME on
self._writeReg(0x37, 0x30) # dac set self._writeReg(0x37, 0x30) # dac set
self._writeReg(0x6D, 0x60)# SEL:GPIO1=DMIC CLK OUT+SEL:GPIO2=PLL CLK OUT # SEL:GPIO1=DMIC CLK OUT+SEL:GPIO2=PLL CLK OUT
self._writeReg(0x6D, 0x60)
self._writeReg(0x71, 0x05) # for automute setting self._writeReg(0x71, 0x05) # for automute setting
self._writeReg(0x73, 0x70) self._writeReg(0x73, 0x70)
self.configDACOutput(out_channel)# 0x3c Enable DAC and Enable Lout/Rout/1/2 # 0x3c Enable DAC and Enable Lout/Rout/1/2
self.configADCInput(in_channel)# 0x00 LINSEL & RINSEL, LIN1/RIN1 as ADC Input DSSEL,use one DS Reg11 DSR, LINPUT1-RINPUT1 self.configDACOutput(out_channel)
# 0x00 LINSEL & RINSEL, LIN1/RIN1 as ADC Input DSSEL,use one DS Reg11 DSR, LINPUT1-RINPUT1
self.configADCInput(in_channel)
self.voice_volume(95) self.voice_volume(95)
self._writeReg(0x37, 0x00) # dac set self._writeReg(0x37, 0x00) # dac set
''' '''
@@ -104,6 +109,7 @@ class ES8374:
self._writeReg(0x1E, 0xa0) # disable class d self._writeReg(0x1E, 0xa0) # disable class d
self.voice_mute(0) self.voice_mute(0)
''' '''
def stop(self): def stop(self):
self.voice_mute(1) self.voice_mute(1)
self._writeReg(0x1a, self._readReg(0x1a) | 0x08) self._writeReg(0x1a, self._readReg(0x1a) | 0x08)
@@ -116,8 +122,10 @@ class ES8374:
def i2sConfigClock(self): def i2sConfigClock(self):
self._writeReg(0x0f, (self._readReg(0x0F) & 0xe0) | _MCLK_DIV) self._writeReg(0x0f, (self._readReg(0x0F) & 0xe0) | _MCLK_DIV)
self._writeReg(0x06, _LCLK_DIV >> 8)# ADCFsMode,singel SPEED,RATIO=256 # ADCFsMode,singel SPEED,RATIO=256
self._writeReg(0x07, _LCLK_DIV & 0xFF)# ADCFsMode,singel SPEED,RATIO=256 self._writeReg(0x06, _LCLK_DIV >> 8)
# ADCFsMode,singel SPEED,RATIO=256
self._writeReg(0x07, _LCLK_DIV & 0xFF)
def configI2SFormat(self, mode, fmt): def configI2SFormat(self, mode, fmt):
fmt_tmp = ((fmt & 0xf0) >> 4) fmt_tmp = ((fmt & 0xf0) >> 4)

View File

@@ -10,8 +10,12 @@ Micropython library for the mixgo_zero Zi Onboard resources
dahanzimin From the Mixly Team dahanzimin From the Mixly Team
""" """
from ws2812 import NeoPixel
from machine import * from machine import *
import time, gc, st7735, math import time
import gc
import st7735
import math
'''RTC''' '''RTC'''
rtc_clock = RTC() rtc_clock = RTC()
@@ -31,7 +35,8 @@ except:
onboard_spi = SPI(1, baudrate=50000000, polarity=0, phase=0) onboard_spi = SPI(1, baudrate=50000000, polarity=0, phase=0)
'''TFT/128*160''' '''TFT/128*160'''
onboard_tft = st7735.ST7735(onboard_spi, 160, 128, dc_pin=18, cs_pin=45, bl_pin=14, font_address=0x700000) onboard_tft = st7735.ST7735(
onboard_spi, 160, 128, dc_pin=18, cs_pin=45, bl_pin=14, font_address=0x700000)
'''ACC-Sensor''' '''ACC-Sensor'''
try: try:
@@ -90,10 +95,11 @@ except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or", e) print("Warning: Failed to communicate with MMC5603 (MGS) or", e)
'''2RGB_WS2812''' '''2RGB_WS2812'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(38), 4) onboard_rgb = NeoPixel(Pin(38), 4)
'''5KEY_Sensor''' '''5KEY_Sensor'''
class KEYSensor: class KEYSensor:
def __init__(self, pin, range): def __init__(self, pin, range):
self.pin = pin self.pin = pin
@@ -130,7 +136,10 @@ class KEYSensor:
def irq(self, handler, trigger): def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger) Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger)
'''1KEY_Button''' '''1KEY_Button'''
class Button(KEYSensor): class Button(KEYSensor):
def __init__(self, pin): def __init__(self, pin):
self.pin = pin self.pin = pin
@@ -140,6 +149,7 @@ class Button(KEYSensor):
def _value(self): def _value(self):
return not self.key.value() return not self.key.value()
B1key = Button(0) B1key = Button(0)
B2key = KEYSensor(17, 0) B2key = KEYSensor(17, 0)
A1key = KEYSensor(17, 2900) A1key = KEYSensor(17, 2900)
@@ -148,9 +158,12 @@ A3key = KEYSensor(17,1650)
A4key = KEYSensor(17, 850) A4key = KEYSensor(17, 850)
'''2-TouchPad''' '''2-TouchPad'''
class Touch_Pad: class Touch_Pad:
__species = {} __species = {}
__first_init = True __first_init = True
def __new__(cls, pin, *args, **kwargs): def __new__(cls, pin, *args, **kwargs):
if pin not in cls.__species.keys(): if pin not in cls.__species.keys():
cls.__first_init = True cls.__first_init = True
@@ -170,13 +183,19 @@ class Touch_Pad:
return self._pin.read() > value if value else self._pin.read() return self._pin.read() > value if value else self._pin.read()
# Touch with function call # Touch with function call
def touched(pin, value=60000): def touched(pin, value=60000):
return Touch_Pad(pin).touch(value) return Touch_Pad(pin).touch(value)
def touch_slide(pina, pinb): def touch_slide(pina, pinb):
return ((Touch_Pad(pina).touch() - Touch_Pad(pina).raw) - (Touch_Pad(pinb).touch() - Touch_Pad(pinb).raw)) // 10 return ((Touch_Pad(pina).touch() - Touch_Pad(pina).raw) - (Touch_Pad(pinb).touch() - Touch_Pad(pinb).raw)) // 10
'''2LED-Tristate''' '''2LED-Tristate'''
class LED_T: class LED_T:
def __init__(self, pin, timer_id=3): def __init__(self, pin, timer_id=3):
self._pin = pin self._pin = pin
@@ -227,7 +246,10 @@ class LED_T:
def getonoff(self, index): def getonoff(self, index):
return True if self._index_pwm[index-1] > 0 else False return True if self._index_pwm[index-1] > 0 else False
'''2LED-Independent''' '''2LED-Independent'''
class LED_I: class LED_I:
def __init__(self, pins=[]): def __init__(self, pins=[]):
self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins] self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins]
@@ -244,7 +266,8 @@ class LED_I:
def setonoff(self, index, val): def setonoff(self, index, val):
if val == -1: if val == -1:
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0) self.setbrightness(index, 100) if self.getbrightness(
index) < 50 else self.setbrightness(index, 0)
elif val == 1: elif val == 1:
self.setbrightness(index, 100) self.setbrightness(index, 100)
elif val == 0: elif val == 0:
@@ -253,8 +276,10 @@ class LED_I:
def getonoff(self, index): def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False return True if self.getbrightness(index) > 50 else False
onboard_led = LED_I(pins=[42, 13]) if version else LED_T(42, timer_id=3) onboard_led = LED_I(pins=[42, 13]) if version else LED_T(42, timer_id=3)
class Clock: class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径 def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
self.display = oled self.display = oled
@@ -325,5 +350,6 @@ class Clock:
self.drawMin(color) self.drawMin(color)
self.drawSec(color) self.drawSec(color)
'''Reclaim memory''' '''Reclaim memory'''
gc.collect() gc.collect()

View File

@@ -25,9 +25,11 @@ ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(3
spk_midi = music_spk.MIDI(ob_audio, sample_rate) spk_midi = music_spk.MIDI(ob_audio, sample_rate)
def u2s(n): def u2s(n):
return n if n < (1 << 15) else n - (1 << 16) return n if n < (1 << 15) else n - (1 << 16)
def sound_level(): def sound_level():
buf = bytearray(100) buf = bytearray(100)
values = [] values = []
@@ -36,6 +38,7 @@ def sound_level():
values.append(u2s(buf[i * 2] | buf[i * 2 + 1] << 8)) values.append(u2s(buf[i * 2] | buf[i * 2 + 1] << 8))
return max(values) - min(values) return max(values) - min(values)
def play_audio(path): def play_audio(path):
file = open(path, 'rb') file = open(path, 'rb')
header = file.read(44) header = file.read(44)
@@ -44,17 +47,21 @@ def play_audio(path):
_rate = ustruct.unpack('<I', header[24:28])[0] _rate = ustruct.unpack('<I', header[24:28])[0]
print("sample_rate", _rate) print("sample_rate", _rate)
file.seek(44) file.seek(44)
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000)
while True: while True:
block = file.read(1024) block = file.read(1024)
if not block: if not block:
break break
ob_audio.write(block) ob_audio.write(block)
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
file.close() file.close()
def record_audio(path, seconds=5): def record_audio(path, seconds=5):
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate*4, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate*4, ibuf=20000)
file_size = sample_rate * 16 * 1 * seconds // 8 file_size = sample_rate * 16 * 1 * seconds // 8
wav_header = bytearray(44) wav_header = bytearray(44)
wav_header[0:4] = b'RIFF' wav_header[0:4] = b'RIFF'
@@ -68,9 +75,11 @@ def record_audio(path, seconds=5):
for _ in range(file_size // 512): for _ in range(file_size // 512):
ob_audio.readinto(buf) ob_audio.readinto(buf)
file.write(buf) file.write(buf)
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
file.close() file.close()
def play_audio_url(url): def play_audio_url(url):
import urequests import urequests
response = urequests.get(url, stream=True) response = urequests.get(url, stream=True)
@@ -79,11 +88,13 @@ def play_audio_url(url):
raise Error('not a WAVE file') raise Error('not a WAVE file')
_rate = ustruct.unpack('<I', header[24:28])[0] _rate = ustruct.unpack('<I', header[24:28])[0]
# print("sample_rate", _rate) # print("sample_rate", _rate)
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=_rate, ibuf=20000)
while True: while True:
block = response.raw.read(1024) block = response.raw.read(1024)
if not block: if not block:
break break
ob_audio.write(block) ob_audio.write(block)
ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000) ob_audio = I2S(0, sck=Pin(34), ws=Pin(47), dout=Pin(48), din=Pin(33), mck=Pin(
35), mode=I2S.RTX, bits=16, format=I2S.MONO, rate=sample_rate, ibuf=20000)
response.close() response.close()

View File

@@ -6,8 +6,13 @@ Micropython library for the mixgo_sant Onboard resources
@dahanzimin From the Mixly Team @dahanzimin From the Mixly Team
""" """
from music import MIDI
from ws2812x import NeoPixel
from machine import * from machine import *
import time, gc, st7789_cf, math import time
import gc
import st7789_cf
import math
'''RTC''' '''RTC'''
rtc_clock = RTC() rtc_clock = RTC()
@@ -72,14 +77,14 @@ except Exception as e:
print("Warning: Failed to communicate with CI130X (ASR) or", e) print("Warning: Failed to communicate with CI130X (ASR) or", e)
'''2RGB_WS2812''' '''2RGB_WS2812'''
from ws2812x import NeoPixel
onboard_rgb = NeoPixel(onboard_bot.rgb_sync, 4) onboard_rgb = NeoPixel(onboard_bot.rgb_sync, 4)
'''1Buzzer-Music''' '''1Buzzer-Music'''
from music import MIDI
onboard_music = MIDI(46, pa_ctrl=onboard_bot.spk_en) onboard_music = MIDI(46, pa_ctrl=onboard_bot.spk_en)
'''5KEY_Sensor''' '''5KEY_Sensor'''
class KEYSensor: class KEYSensor:
def __init__(self, pin, range): def __init__(self, pin, range):
self.pin = pin self.pin = pin
@@ -117,7 +122,10 @@ class KEYSensor:
def irq(self, handler, trigger): def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger) Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger)
'''1KEY_Button''' '''1KEY_Button'''
class Button(KEYSensor): class Button(KEYSensor):
def __init__(self, pin): def __init__(self, pin):
self.pin = pin self.pin = pin
@@ -127,6 +135,7 @@ class Button(KEYSensor):
def _value(self): def _value(self):
return not self.key.value() return not self.key.value()
B1key = Button(0) B1key = Button(0)
B2key = KEYSensor(17, 0) B2key = KEYSensor(17, 0)
A1key = KEYSensor(17, 1600) A1key = KEYSensor(17, 1600)
@@ -135,6 +144,8 @@ A3key = KEYSensor(17, 550)
A4key = KEYSensor(17, 2100) A4key = KEYSensor(17, 2100)
'''2-LED''' '''2-LED'''
class LED: class LED:
def __init__(self, func): def __init__(self, func):
self._func = func self._func = func
@@ -147,7 +158,8 @@ class LED:
def setonoff(self, index, val): def setonoff(self, index, val):
if val == -1: if val == -1:
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0) self.setbrightness(index, 100) if self.getbrightness(
index) < 50 else self.setbrightness(index, 0)
elif val == 1: elif val == 1:
self.setbrightness(index, 100) self.setbrightness(index, 100)
elif val == 0: elif val == 0:
@@ -156,17 +168,22 @@ class LED:
def getonoff(self, index): def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False return True if self.getbrightness(index) > 50 else False
onboard_led = LED(onboard_bot.led_pwm) onboard_led = LED(onboard_bot.led_pwm)
class Voice_Energy: class Voice_Energy:
def read(self, samples=10): def read(self, samples=10):
values = [] values = []
for _ in range(samples): for _ in range(samples):
values.append(int.from_bytes(onboard_asr._rreg(0x08, 3)[:2], 'little')) #在语音识别里获取 values.append(int.from_bytes(onboard_asr._rreg(
0x08, 3)[:2], 'little')) # 在语音识别里获取
return sorted(values)[samples // 2] return sorted(values)[samples // 2]
onboard_sound = Voice_Energy() onboard_sound = Voice_Energy()
class Clock: class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径 def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
self.display = oled self.display = oled
@@ -237,5 +254,6 @@ class Clock:
def clear(self, color=0): # 清除 def clear(self, color=0): # 清除
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True) self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
'''Reclaim memory''' '''Reclaim memory'''
gc.collect() gc.collect()

View File

@@ -6,8 +6,12 @@ Micropython library for the mixgo_soar onboard resources
@dahanzimin From the Mixly Team @dahanzimin From the Mixly Team
""" """
from ws2812 import NeoPixel
from machine import * from machine import *
import time, gc, math, st7789_bf import time
import gc
import math
import st7789_bf
'''RTC''' '''RTC'''
rtc_clock = RTC() rtc_clock = RTC()
@@ -58,10 +62,11 @@ except Exception as e:
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) 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''' '''2RGB_WS2812'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(40), 4) onboard_rgb = NeoPixel(Pin(40), 4)
'''5KEY_Sensor''' '''5KEY_Sensor'''
class KEYSensor: class KEYSensor:
def __init__(self, pin, range): def __init__(self, pin, range):
self.pin = pin self.pin = pin
@@ -98,7 +103,10 @@ class KEYSensor:
def irq(self, handler, trigger): def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger) Pin(self.pin, Pin.IN).irq(handler=handler, trigger=trigger)
'''1KEY_Button''' '''1KEY_Button'''
class Button(KEYSensor): class Button(KEYSensor):
def __init__(self, pin): def __init__(self, pin):
self.pin = pin self.pin = pin
@@ -108,6 +116,7 @@ class Button(KEYSensor):
def _value(self): def _value(self):
return not self.key.value() return not self.key.value()
B1key = Button(0) B1key = Button(0)
B2key = KEYSensor(17, 0) B2key = KEYSensor(17, 0)
A1key = KEYSensor(17, 2300) A1key = KEYSensor(17, 2300)
@@ -116,6 +125,8 @@ A3key = KEYSensor(17,800)
A4key = KEYSensor(17, 2900) A4key = KEYSensor(17, 2900)
'''2LED-Independent''' '''2LED-Independent'''
class LED: class LED:
def __init__(self, pins=[]): def __init__(self, pins=[]):
self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins] self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins]
@@ -132,7 +143,8 @@ class LED:
def setonoff(self, index, val): def setonoff(self, index, val):
if val == -1: if val == -1:
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0) self.setbrightness(index, 100) if self.getbrightness(
index) < 50 else self.setbrightness(index, 0)
elif val == 1: elif val == 1:
self.setbrightness(index, 100) self.setbrightness(index, 100)
elif val == 0: elif val == 0:
@@ -141,8 +153,10 @@ class LED:
def getonoff(self, index): def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False return True if self.getbrightness(index) > 50 else False
onboard_led = LED([38, 39]) onboard_led = LED([38, 39])
class Clock: class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径 def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
self.display = oled self.display = oled
@@ -213,6 +227,7 @@ class Clock:
def clear(self, color=0): # 清除 def clear(self, color=0): # 清除
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True) self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
'''Reclaim memory''' '''Reclaim memory'''
gc.collect() gc.collect()
onboard_tft.set_brightness(0.6) onboard_tft.set_brightness(0.6)

View File

@@ -22,9 +22,11 @@ ob_audio = I2S(0, sck=Pin(37), din=Pin(36) , ws=Pin(35), dout=Pin(34), mck=Pin(3
spk_midi = music_spk.MIDI(ob_audio, sample_rate) spk_midi = music_spk.MIDI(ob_audio, sample_rate)
def u2s(n): def u2s(n):
return n if n < (1 << 15) else n - (1 << 16) return n if n < (1 << 15) else n - (1 << 16)
def sound_level(): def sound_level():
buf = bytearray(100) buf = bytearray(100)
values = [] values = []
@@ -33,6 +35,7 @@ def sound_level():
values.append(u2s(buf[i * 2] | buf[i * 2 + 1] << 8)) values.append(u2s(buf[i * 2] | buf[i * 2 + 1] << 8))
return max(values) - min(values) return max(values) - min(values)
def play_audio(path): def play_audio(path):
file = open(path, 'rb') file = open(path, 'rb')
header = file.read(44) header = file.read(44)
@@ -41,17 +44,21 @@ def play_audio(path):
_rate = ustruct.unpack('<I', header[24:28])[0] _rate = ustruct.unpack('<I', header[24:28])[0]
print("sample_rate", _rate) print("sample_rate", _rate)
file.seek(44) 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) 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: while True:
block = file.read(1024) block = file.read(1024)
if not block: if not block:
break break
ob_audio.write(block) 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) 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() file.close()
def record_audio(path, seconds=5): 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) 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 file_size = sample_rate * 16 * 1 * seconds // 8
wav_header = bytearray(44) wav_header = bytearray(44)
wav_header[0:4] = b'RIFF' wav_header[0:4] = b'RIFF'
@@ -65,9 +72,11 @@ def record_audio(path, seconds=5):
for _ in range(file_size // 512): for _ in range(file_size // 512):
ob_audio.readinto(buf) ob_audio.readinto(buf)
file.write(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) 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() file.close()
def play_audio_url(url): def play_audio_url(url):
import urequests import urequests
response = urequests.get(url, stream=True) response = urequests.get(url, stream=True)
@@ -76,11 +85,13 @@ def play_audio_url(url):
raise Error('not a WAVE file') raise Error('not a WAVE file')
_rate = ustruct.unpack('<I', header[24:28])[0] _rate = ustruct.unpack('<I', header[24:28])[0]
# print("sample_rate", _rate) # 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) 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: while True:
block = response.raw.read(1024) block = response.raw.read(1024)
if not block: if not block:
break break
ob_audio.write(block) 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) 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() response.close()

View File

@@ -28,6 +28,7 @@ normal_tone = {
Letter = 'ABCDEFG#R' Letter = 'ABCDEFG#R'
class MIDI(): class MIDI():
def __init__(self, i2s_bus, rate=22050): def __init__(self, i2s_bus, rate=22050):
self.reset() self.reset()
@@ -135,20 +136,32 @@ class MIDI():
pass pass
DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8'] DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
ENTERTAINER=['d4:1','d#','e','c5:2','e4:1','c5:2','e4:1','c5:3','c:1','d','d#','e','c','d','e:2','b4:1','d5:2','c:4'] ENTERTAINER = ['d4:1', 'd#', 'e', 'c5:2', 'e4:1', 'c5:2', 'e4:1',
PRELUDE=['c4:1','e','g','c5','e','g4','c5','e','c4','e','g','c5','e','g4','c5','e','c4','d','g','d5','f','g4','d5','f','c4','d','g','d5','f','g4','d5','f','b3','d4','g','d5','f','g4','d5','f','b3','d4','g','d5','f','g4','d5','f','c4','e','g','c5','e','g4','c5','e','c4','e','g','c5','e','g4','c5','e'] 'c5:3', 'c:1', 'd', 'd#', 'e', 'c', 'd', 'e:2', 'b4:1', 'd5:2', 'c:4']
ODE=['e4','e','f','g','g','f','e','d','c','c','d','e','e:6','d:2','d:8','e:4','e','f','g','g','f','e','d','c','c','d','e','d:6','c:2','c:8'] PRELUDE = ['c4:1', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'd', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'd', 'g', 'd5', 'f', 'g4', 'd5',
NYAN=['f#5:1','g#','c#:1','d#:2','b4:1','d5:1','c#','b4:2','b','c#5','d','d:1','c#','b4:1','c#5:1','d#','f#','g#','d#','f#','c#','d','b4','c#5','b4','d#5:2','f#','g#:1','d#','f#','c#','d#','b4','d5','d#','d','c#','b4','c#5','d:2','b4:1','c#5','d#','f#','c#','d','c#','b4','c#5:2','b4','c#5','b4','f#:1','g#','b:2','f#:1','g#','b','c#5','d#','b4','e5','d#','e','f#','b4:2','b','f#:1','g#','b','f#','e5','d#','c#','b4','f#','d#','e','f#','b:2','f#:1','g#','b:2','f#:1','g#','b','b','c#5','d#','b4','f#','g#','f#','b:2','b:1','a#','b','f#','g#','b','e5','d#','e','f#','b4:2','c#5'] 'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e']
RINGTONE=['c4:1','d','e:2','g','d:1','e','f:2','a','e:1','f','g:2','b','c5:4'] ODE = ['e4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'e:6', 'd:2', 'd:8',
FUNK=['c2:2','c','d#','c:1','f:2','c:1','f:2','f#','g','c','c','g','c:1','f#:2','c:1','f#:2','f','d#'] 'e:4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'd:6', 'c:2', 'c:8']
BLUES=['c2:2','e','g','a','a#','a','g','e','c2:2','e','g','a','a#','a','g','e','f','a','c3','d','d#','d','c','a2','c2:2','e','g','a','a#','a','g','e','g','b','d3','f','f2','a','c3','d#','c2:2','e','g','e','g','f','e','d'] NYAN = ['f#5:1', 'g#', 'c#:1', 'd#:2', 'b4:1', 'd5:1', 'c#', 'b4:2', 'b', 'c#5', 'd', 'd:1', 'c#', 'b4:1', 'c#5:1', 'd#', 'f#', 'g#', 'd#', 'f#', 'c#', 'd', 'b4', 'c#5', 'b4', 'd#5:2', 'f#', 'g#:1', 'd#', 'f#', 'c#', 'd#', 'b4', 'd5', 'd#', 'd', 'c#', 'b4', 'c#5', 'd:2', 'b4:1', 'c#5', 'd#', 'f#', 'c#', 'd', 'c#', 'b4', 'c#5:2', 'b4', 'c#5',
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'] 'b4', 'f#:1', 'g#', 'b:2', 'f#:1', 'g#', 'b', 'c#5', 'd#', 'b4', 'e5', 'd#', 'e', 'f#', 'b4:2', 'b', 'f#:1', 'g#', 'b', 'f#', 'e5', 'd#', 'c#', 'b4', 'f#', 'd#', 'e', 'f#', 'b:2', 'f#:1', 'g#', 'b:2', 'f#:1', 'g#', 'b', 'b', 'c#5', 'd#', 'b4', 'f#', 'g#', 'f#', 'b:2', 'b:1', 'a#', 'b', 'f#', 'g#', 'b', 'e5', 'd#', 'e', 'f#', 'b4:2', 'c#5']
WEDDING=['c4:4','f:3','f:1','f:8','c:4','g:3','e:1','f:8','c:4','f:3','a:1','c5:4','a4:3','f:1','f:4','e:3','f:1','g:8'] RINGTONE = ['c4:1', 'd', 'e:2', 'g', 'd:1', 'e',
FUNERAL=['c3:4','c:3','c:1','c:4','d#:3','d:1','d:3','c:1','c:3','b2:1','c3:4'] 'f:2', 'a', 'e:1', 'f', 'g:2', 'b', 'c5:4']
FUNK = ['c2:2', 'c', 'd#', 'c:1', 'f:2', 'c:1', 'f:2', 'f#',
'g', 'c', 'c', 'g', 'c:1', 'f#:2', 'c:1', 'f#:2', 'f', 'd#']
BLUES = ['c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'f', 'a', 'c3', 'd', 'd#', 'd', 'c',
'a2', 'c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'g', 'b', 'd3', 'f', 'f2', 'a', 'c3', 'd#', 'c2:2', 'e', 'g', 'e', 'g', 'f', 'e', 'd']
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']
WEDDING = ['c4:4', 'f:3', 'f:1', 'f:8', 'c:4', 'g:3', 'e:1', 'f:8',
'c:4', 'f:3', 'a:1', 'c5:4', 'a4:3', 'f:1', 'f:4', 'e:3', 'f:1', 'g:8']
FUNERAL = ['c3:4', 'c:3', 'c:1', 'c:4', 'd#:3',
'd:1', 'd:3', 'c:1', 'c:3', 'b2:1', 'c3:4']
PUNCHLINE = ['c4:3', 'g3:1', 'f#', 'g', 'g#:3', 'g', 'r', 'b', 'c4'] PUNCHLINE = ['c4:3', 'g3:1', 'f#', 'g', 'g#:3', 'g', 'r', 'b', 'c4']
PYTHON=['d5:1','b4','r','b','b','a#','b','g5','r','d','d','r','b4','c5','r','c','c','r','d','e:5','c:1','a4','r','a','a','g#','a','f#5','r','e','e','r','c','b4','r','b','b','r','c5','d:5','d:1','b4','r','b','b','a#','b','b5','r','g','g','r','d','c#','r','a','a','r','a','a:5','g:1','f#:2','a:1','a','g#','a','e:2','a:1','a','g#','a','d','r','c#','d','r','c#','d:2','r:3'] PYTHON = ['d5:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'g5', 'r', 'd', 'd', 'r', 'b4', 'c5', 'r', 'c', 'c', 'r', 'd', 'e:5', 'c:1', 'a4', 'r', 'a', 'a', 'g#', 'a', 'f#5', 'r', 'e', 'e', 'r', 'c', 'b4', 'r', 'b', 'b', 'r', 'c5', 'd:5',
'd:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'b5', 'r', 'g', 'g', 'r', 'd', 'c#', 'r', 'a', 'a', 'r', 'a', 'a:5', 'g:1', 'f#:2', 'a:1', 'a', 'g#', 'a', 'e:2', 'a:1', 'a', 'g#', 'a', 'd', 'r', 'c#', 'd', 'r', 'c#', 'd:2', 'r:3']
BADDY = ['c3:3', 'r', 'd:2', 'd#', 'r', 'c', 'r', 'f#:8'] BADDY = ['c3:3', 'r', 'd:2', 'd#', 'r', 'c', 'r', 'f#:8']
CHASE=['a4:1','b','c5','b4','a:2','r','a:1','b','c5','b4','a:2','r','a:2','e5','d#','e','f','e','d#','e','b4:1','c5','d','c','b4:2','r','b:1','c5','d','c','b4:2','r','b:2','e5','d#','e','f','e','d#','e'] CHASE = ['a4:1', 'b', 'c5', 'b4', 'a:2', 'r', 'a:1', 'b', 'c5', 'b4', 'a:2', 'r', 'a:2', 'e5', 'd#', 'e', 'f', 'e', 'd#',
'e', 'b4:1', 'c5', 'd', 'c', 'b4:2', 'r', 'b:1', 'c5', 'd', 'c', 'b4:2', 'r', 'b:2', 'e5', 'd#', 'e', 'f', 'e', 'd#', 'e']
BA_DING = ['b5:1', 'e6:3'] BA_DING = ['b5:1', 'e6:3']
WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8'] WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8']
JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g'] JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g']

View File

@@ -107,5 +107,6 @@ class NOVA_G1:
else: else:
self._wreg(_NOVA_G1_IO, (self._rreg(_NOVA_G1_IO)) & 0xFE) self._wreg(_NOVA_G1_IO, (self._rreg(_NOVA_G1_IO)) & 0xFE)
# Constructor # Constructor
ext_g1 = NOVA_G1(onboard_i2c) ext_g1 = NOVA_G1(onboard_i2c)

View File

@@ -7,7 +7,8 @@ MicroPython library for the ST7789(TFT-SPI)
@dahanzimin From the Mixly Team @dahanzimin From the Mixly Team
""" """
import time, uframebuf import time
import uframebuf
from machine import Pin, PWM from machine import Pin, PWM
from micropython import const from micropython import const
@@ -39,12 +40,14 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
self.font(font_address) self.font(font_address)
self._init() self._init()
self.show() self.show()
if brightness > 0: time.sleep_ms(100) if brightness > 0:
time.sleep_ms(100)
self._brightness = brightness self._brightness = brightness
if callable(bl_pin): if callable(bl_pin):
self.bl_led = bl_pin self.bl_led = bl_pin
else: else:
self.bl_led = PWM(Pin(bl_pin), duty_u16=int(self._brightness * 60000)) if bl_pin else None self.bl_led = PWM(Pin(bl_pin), duty_u16=int(
self._brightness * 60000)) if bl_pin else None
def _write(self, cmd, dat=None): def _write(self, cmd, dat=None):
self.cs.off() self.cs.off()
@@ -103,7 +106,8 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def set_brightness(self, brightness): def set_brightness(self, brightness):
if not 0.0 <= brightness <= 1.0: if not 0.0 <= brightness <= 1.0:
raise ValueError("Brightness must be a decimal number in the range: 0.0~1.0") raise ValueError(
"Brightness must be a decimal number in the range: 0.0~1.0")
self._brightness = brightness self._brightness = brightness
if callable(self.bl_led): if callable(self.bl_led):
self.bl_led(brightness * 100) self.bl_led(brightness * 100)

View File

@@ -5,7 +5,8 @@ MicroPython library for the ST7789(TFT-SPI)
======================================================= =======================================================
@dahanzimin From the Mixly Team @dahanzimin From the Mixly Team
""" """
import time, uframebuf import time
import uframebuf
from machine import Pin from machine import Pin
from jpeg import Decoder from jpeg import Decoder
from micropython import const from micropython import const
@@ -50,7 +51,8 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
del _decoder del _decoder
else: else:
self._buffer[:] = data # 后期做图像大小处理 self._buffer[:] = data # 后期做图像大小处理
if sync: self.show() if sync:
self.show()
return self._buffer return self._buffer
def _write(self, cmd, dat=None): def _write(self, cmd, dat=None):
@@ -63,7 +65,7 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def _init(self): def _init(self):
"""Display initialization configuration""" """Display initialization configuration"""
for cmd, data, delay in [ for cmd, data, delay in [
##(_CMD_SWRESET, None, 20000), # (_CMD_SWRESET, None, 20000),
(_CMD_SLPOUT, None, 120000), (_CMD_SLPOUT, None, 120000),
(_CMD_MADCTL, b'\x00', 50), (_CMD_MADCTL, b'\x00', 50),
(_CMD_COLMOD, b'\x05', 50), (_CMD_COLMOD, b'\x05', 50),
@@ -90,7 +92,8 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def set_brightness(self, brightness): def set_brightness(self, brightness):
if not 0.0 <= brightness <= 1.0: if not 0.0 <= brightness <= 1.0:
raise ValueError("Brightness must be a decimal number in the range: 0.0~1.0") raise ValueError(
"Brightness must be a decimal number in the range: 0.0~1.0")
self._backlight(int(brightness * 100)) self._backlight(int(brightness * 100))
def color(self, red, green=None, blue=None): def color(self, red, green=None, blue=None):

View File

@@ -7,6 +7,7 @@ Micropython library for the WS2812 NeoPixel-RGB(method inheritance)
""" """
from time import sleep from time import sleep
class NeoPixel: class NeoPixel:
def __init__(self, func, n, bpp=3, ORDER=(0, 1, 2, 3)): def __init__(self, func, n, bpp=3, ORDER=(0, 1, 2, 3)):
self.func = func self.func = func