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)
@@ -21,7 +23,7 @@ class Camera(Camera):
time.sleep_ms(50) time.sleep_ms(50)
self.set_vflip(not vflip) self.set_vflip(not vflip)
time.sleep_ms(50) time.sleep_ms(50)
SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) #恢复I2C SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) # 恢复I2C
def deinit(self): def deinit(self):
super().deinit() super().deinit()

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
@@ -19,7 +20,7 @@ class CI1302(CI130X):
try: try:
self._device.writeto(self._address, reg) self._device.writeto(self._address, reg)
except: except:
self._func(1, 700) #Power on self._func(1, 700) # Power on
self._device.writeto(self._address, reg) self._device.writeto(self._address, reg)
def _rreg(self, reg, nbytes=1): def _rreg(self, reg, nbytes=1):
@@ -27,5 +28,5 @@ class CI1302(CI130X):
try: try:
return self._device.readfrom_mem(self._address, reg, nbytes) return self._device.readfrom_mem(self._address, reg, nbytes)
except: except:
self._func(1, 700) #Power on self._func(1, 700) # Power on
return self._device.readfrom_mem(self._address, reg, nbytes) return self._device.readfrom_mem(self._address, reg, nbytes)

View File

@@ -28,62 +28,67 @@ 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
self._writeReg(0x00, 0x03)# IC Rst stop self._writeReg(0x00, 0x03) # IC Rst stop
self._writeReg(0x01, 0x7F)# IC clk on # M ORG 7F self._writeReg(0x01, 0x7F) # IC clk on # M ORG 7F
self._writeReg(0x0f, (self._readReg(0x0F) & 0x7f) | (ms_mode << 7))# CODEC IN I2S SLAVE MODE self._writeReg(0x0f, (self._readReg(0x0F) & 0x7f) | (ms_mode << 7)) # CODEC IN I2S SLAVE MODE
self._writeReg(0x6F, 0xA0)# pll set:mode enable self._writeReg(0x6F, 0xA0) # pll set:mode enable
self._writeReg(0x72, 0x41)# pll set:mode set self._writeReg(0x72, 0x41) # pll set:mode set
self._writeReg(0x09, 0x01)# pll set:reset on ,set start self._writeReg(0x09, 0x01) # pll set:reset on ,set start
self._writeReg(0x0C, 0x22)# pll set:k self._writeReg(0x0C, 0x22) # pll set:k
self._writeReg(0x0D, 0x2E)# pll set:k self._writeReg(0x0D, 0x2E) # pll set:k
self._writeReg(0x0E, 0xC6)# pll set:k self._writeReg(0x0E, 0xC6) # pll set:k
self._writeReg(0x0A, 0x3A)# pll set: self._writeReg(0x0A, 0x3A) # pll set:
self._writeReg(0x0B, 0x07)# pll set:n self._writeReg(0x0B, 0x07) # pll set:n
self._writeReg(0x09, 0x41)# pll set:reset off ,set stop self._writeReg(0x09, 0x41) # pll set:reset off ,set stop
self.i2sConfigClock() self.i2sConfigClock()
self._writeReg(0x24, 0x08)# adc set self._writeReg(0x24, 0x08) # adc set
self._writeReg(0x36, 0x00)# dac set self._writeReg(0x36, 0x00) # dac set
self._writeReg(0x12, 0x30)# timming set self._writeReg(0x12, 0x30) # timming set
self._writeReg(0x13, 0x20)# timming set self._writeReg(0x13, 0x20) # timming set
self.configI2SFormat(_ES_MODULE_ADC, fmt) self.configI2SFormat(_ES_MODULE_ADC, fmt)
self.configI2SFormat(_ES_MODULE_DAC, fmt) self.configI2SFormat(_ES_MODULE_DAC, fmt)
self._writeReg(0x21, 0x50)# adc set: SEL LIN1 CH+PGAGAIN=0DB self._writeReg(0x21, 0x50) # adc set: SEL LIN1 CH+PGAGAIN=0DB
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(0x00, 0x80)# IC START self._writeReg(0x08, 0x21)
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
self._writeReg(0x38, 0x00)# DACVOLUME on self._writeReg(0x38, 0x00) # DACVOLUME on
self._writeReg(0x14, 0x8A)# IC START self._writeReg(0x14, 0x8A) # IC START
self._writeReg(0x15, 0x40)# IC START self._writeReg(0x15, 0x40) # IC START
self._writeReg(0x1A, 0xA0)# monoout set self._writeReg(0x1A, 0xA0) # monoout set
self._writeReg(0x1B, 0x19)# monoout set self._writeReg(0x1B, 0x19) # monoout set
self._writeReg(0x1C, 0x90)# spk set self._writeReg(0x1C, 0x90) # spk set
self._writeReg(0x1D, 0x01)# spk set self._writeReg(0x1D, 0x01) # spk set
self._writeReg(0x1F, 0x00)# spk set self._writeReg(0x1F, 0x00) # spk set
self._writeReg(0x1E, 0x20)# spk on self._writeReg(0x1E, 0x20) # spk on
self._writeReg(0x28, 0x70)# alc set 0x70 self._writeReg(0x28, 0x70) # alc set 0x70
#self._writeReg(0x26, 0x4E)# alc set # self._writeReg(0x26, 0x4E)# alc set
#self._writeReg(0x27, 0x10)# alc set # self._writeReg(0x27, 0x10)# alc set
#self._writeReg(0x29, 0x00)# alc set # self._writeReg(0x29, 0x00)# alc set
#self._writeReg(0x2B, 0x00)# alc set # self._writeReg(0x2B, 0x00)# alc set
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(0x71, 0x05)# for automute setting self._writeReg(0x6D, 0x60)
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
''' '''
reg = self._readReg(0x1a) # disable lout reg = self._readReg(0x1a) # disable lout
reg |= 0x08 reg |= 0x08
@@ -104,20 +109,23 @@ 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)
self._writeReg(0x1a, self._readReg(0x1a)& 0xdf) self._writeReg(0x1a, self._readReg(0x1a) & 0xdf)
self._writeReg(0x1D, 0x12)# mute speaker self._writeReg(0x1D, 0x12) # mute speaker
self._writeReg(0x1E, 0x20)# disable class d self._writeReg(0x1E, 0x20) # disable class d
self._writeReg(0x15, self._readReg(0x15)| 0x20) self._writeReg(0x15, self._readReg(0x15) | 0x20)
self._writeReg(0x10, self._readReg(0x10)| 0xc0) self._writeReg(0x10, self._readReg(0x10) | 0xc0)
self._writeReg(0x21, self._readReg(0x21)| 0xc0) self._writeReg(0x21, self._readReg(0x21) | 0xc0)
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)
@@ -125,13 +133,13 @@ class ES8374:
if (mode == _ES_MODULE_ADC or mode == _ES_MODULE_ADC_DAC): if (mode == _ES_MODULE_ADC or mode == _ES_MODULE_ADC_DAC):
reg = self._readReg(0x10) reg = self._readReg(0x10)
reg &= 0xfc reg &= 0xfc
self._writeReg(0x10, (reg|fmt_i2s)) self._writeReg(0x10, (reg | fmt_i2s))
self.setBitsPerSample(mode, 3) self.setBitsPerSample(mode, 3)
if (mode == _ES_MODULE_DAC or mode == _ES_MODULE_ADC_DAC): if (mode == _ES_MODULE_DAC or mode == _ES_MODULE_ADC_DAC):
reg = self._readReg(0x11) reg = self._readReg(0x11)
reg &= 0xfc reg &= 0xfc
self._writeReg(0x11, (reg|fmt_i2s)) self._writeReg(0x11, (reg | fmt_i2s))
self.setBitsPerSample(mode, 3) self.setBitsPerSample(mode, 3)
# set Bits Per Sample # set Bits Per Sample
@@ -140,12 +148,12 @@ class ES8374:
if (mode == _ES_MODULE_ADC or mode == _ES_MODULE_ADC_DAC): if (mode == _ES_MODULE_ADC or mode == _ES_MODULE_ADC_DAC):
reg = self._readReg(0x10) reg = self._readReg(0x10)
reg &= 0xe3 reg &= 0xe3
self._writeReg(0x10, (reg| (bits << 2))) self._writeReg(0x10, (reg | (bits << 2)))
if (mode == _ES_MODULE_DAC or mode == _ES_MODULE_ADC_DAC): if (mode == _ES_MODULE_DAC or mode == _ES_MODULE_ADC_DAC):
reg = self._readReg(0x11) reg = self._readReg(0x11)
reg &= 0xe3 reg &= 0xe3
self._writeReg(0x11, (reg| (bits << 2))) self._writeReg(0x11, (reg | (bits << 2)))
def configDACOutput(self, output): def configDACOutput(self, output):
self._writeReg(0x1d, 0x02) self._writeReg(0x1d, 0x02)
@@ -159,7 +167,7 @@ class ES8374:
def configADCInput(self, input): def configADCInput(self, input):
reg = self._readReg(0x21) reg = self._readReg(0x21)
reg = (reg & 0xcf) | 0x24 reg = (reg & 0xcf) | 0x24
self._writeReg( 0x21, reg) self._writeReg(0x21, reg)
def mic_volume(self, volume=None): def mic_volume(self, volume=None):
if volume is None: if volume is None:
@@ -177,11 +185,11 @@ class ES8374:
if enable is None: if enable is None:
return True if self._readReg(0x36) & 0x40 else False return True if self._readReg(0x36) & 0x40 else False
else: else:
self._writeReg(0x36, (self._readReg(0x36)& 0xdf) | (enable << 5)) self._writeReg(0x36, (self._readReg(0x36) & 0xdf) | (enable << 5))
def mic_gain(self, gain): def mic_gain(self, gain):
gain_n = max(min(gain, 15), 0) gain_n = max(min(gain, 15), 0)
self._writeReg(0x22, (gain_n | (gain_n<<4))) # MIC PGA -3.5db ~ 24db self._writeReg(0x22, (gain_n | (gain_n << 4))) # MIC PGA -3.5db ~ 24db
def pga_enable(self, enable): def pga_enable(self, enable):
self._writeReg(0x21, (self._readReg(0x21) & 0xfb) | (enable << 2)) # MIC PGA 0db or 15db self._writeReg(0x21, (self._readReg(0x21) & 0xfb) | (enable << 2)) # MIC PGA 0db or 15db

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,69 +35,71 @@ 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:
import mxc6655xa import mxc6655xa
onboard_acc = mxc6655xa.MXC6655XA(onboard_i2c, front=True) onboard_acc = mxc6655xa.MXC6655XA(onboard_i2c, front=True)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with MXC6655XA (ACC) or",e) print("Warning: Failed to communicate with MXC6655XA (ACC) or", e)
'''ALS_PS-Sensor *2''' '''ALS_PS-Sensor *2'''
try : try:
import ltr553als import ltr553als
onboard_als_l = ltr553als.LTR_553ALS(onboard_i2c) onboard_als_l = ltr553als.LTR_553ALS(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or",e) print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or", e)
try : try:
import ltr553als import ltr553als
onboard_als_r = ltr553als.LTR_553ALS(onboard_i2c_soft) onboard_als_r = ltr553als.LTR_553ALS(onboard_i2c_soft)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or",e) print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or", e)
'''BPS-Sensor''' '''BPS-Sensor'''
if 0x76 in onboard_i2c_scan: if 0x76 in onboard_i2c_scan:
try : try:
import hp203x import hp203x
onboard_bps = hp203x.HP203X(onboard_i2c_soft) onboard_bps = hp203x.HP203X(onboard_i2c_soft)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with HP203X (BPS) or",e) print("Warning: Failed to communicate with HP203X (BPS) or", e)
'''THS-Sensor''' '''THS-Sensor'''
if 0x38 in onboard_i2c_scan: if 0x38 in onboard_i2c_scan:
try : try:
import ahtx0 import ahtx0
onboard_ths = ahtx0.AHTx0(onboard_i2c) onboard_ths = ahtx0.AHTx0(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with AHTx0 (THS) or",e) print("Warning: Failed to communicate with AHTx0 (THS) or", e)
if 0x70 in onboard_i2c_scan: if 0x70 in onboard_i2c_scan:
try : try:
import shtc3 import shtc3
onboard_ths = shtc3.SHTC3(onboard_i2c) onboard_ths = shtc3.SHTC3(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with GXHTC3 (THS) or",e) print("Warning: Failed to communicate with GXHTC3 (THS) or", e)
'''RFID-Sensor''' '''RFID-Sensor'''
try : try:
import rc522 import rc522
onboard_rfid = rc522.RC522(onboard_i2c) onboard_rfid = rc522.RC522(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with RC522 (RFID) or",e) print("Warning: Failed to communicate with RC522 (RFID) or", e)
'''MGS-Sensor''' '''MGS-Sensor'''
try : try:
import mmc5603 import mmc5603
onboard_mgs = mmc5603.MMC5603(onboard_i2c) onboard_mgs = mmc5603.MMC5603(onboard_i2c)
except Exception as e: 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
@@ -108,8 +114,8 @@ class KEYSensor:
time.sleep_us(2) time.sleep_us(2)
return (self.range-200) < min(values) < (self.range+200) return (self.range-200) < min(values) < (self.range+200)
def get_presses(self, delay = 1): def get_presses(self, delay=1):
last_time,presses = time.time(), 0 last_time, presses = time.time(), 0
while time.time() < last_time + delay: while time.time() < last_time + delay:
time.sleep_ms(50) time.sleep_ms(50)
if self.was_pressed(): if self.was_pressed():
@@ -120,17 +126,20 @@ class KEYSensor:
return self._value() return self._value()
def was_pressed(self): def was_pressed(self):
if(self._value() != self.flag): if (self._value() != self.flag):
self.flag = self._value() self.flag = self._value()
if self.flag : if self.flag:
return True return True
else: else:
return False return False
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,21 +149,25 @@ 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)
A2key = KEYSensor(17,2300) A2key = KEYSensor(17, 2300)
A3key = KEYSensor(17,1650) 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
cls.__species[pin]=object.__new__(cls) cls.__species[pin] = object.__new__(cls)
return cls.__species[pin] return cls.__species[pin]
def __init__(self, pin, default=30000): def __init__(self, pin, default=30000):
@@ -166,25 +179,31 @@ class Touch_Pad:
if self.raw >= default * 1.5: if self.raw >= default * 1.5:
self.raw = default self.raw = default
def touch(self,value=None ): def touch(self, value=None):
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
self._pwm = 0 self._pwm = 0
self._index_pwm = [0,0] self._index_pwm = [0, 0]
Timer(timer_id, freq=2500, mode=Timer.PERIODIC, callback=self.tim_callback) Timer(timer_id, freq=2500, mode=Timer.PERIODIC, callback=self.tim_callback)
def _cutonoff(self,val): def _cutonoff(self, val):
if val == 0: if val == 0:
Pin(self._pin, Pin.IN) Pin(self._pin, Pin.IN)
elif val == 1: elif val == 1:
@@ -192,7 +211,7 @@ class LED_T:
elif val == -1: elif val == -1:
Pin(self._pin, Pin.OUT).value(0) Pin(self._pin, Pin.OUT).value(0)
def tim_callback(self,tim): def tim_callback(self, tim):
if self._pwm <= 25: if self._pwm <= 25:
if self._pwm * 4 < self._index_pwm[0]: if self._pwm * 4 < self._index_pwm[0]:
self._cutonoff(1) self._cutonoff(1)
@@ -205,29 +224,32 @@ class LED_T:
self._cutonoff(0) self._cutonoff(0)
self._pwm = self._pwm + 1 if self._pwm <= 51 else 0 self._pwm = self._pwm + 1 if self._pwm <= 51 else 0
def setbrightness(self,index,val): def setbrightness(self, index, val):
if not 0 <= val <= 100: if not 0 <= val <= 100:
raise ValueError("Brightness must be in the range: 0~100%") raise ValueError("Brightness must be in the range: 0~100%")
self._index_pwm[index-1] = val self._index_pwm[index-1] = val
def getbrightness(self,index): def getbrightness(self, index):
return self._index_pwm[index-1] return self._index_pwm[index-1]
def setonoff(self,index,val): def setonoff(self, index, val):
if(val == -1): if (val == -1):
if self._index_pwm[index-1] < 50: if self._index_pwm[index-1] < 50:
self._index_pwm[index-1] = 100 self._index_pwm[index-1] = 100
else: else:
self._index_pwm[index-1] = 0 self._index_pwm[index-1] = 0
elif(val == 1): elif (val == 1):
self._index_pwm[index-1] = 100 self._index_pwm[index-1] = 100
elif(val == 0): elif (val == 0):
self._index_pwm[index-1] = 0 self._index_pwm[index-1] = 0
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,35 +276,37 @@ 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
self.xc = x self.xc = x
self.yc = y self.yc = y
self.r = radius self.r = radius
self.color= color self.color = color
self.hour = 0 self.hour = 0
self.min = 0 self.min = 0
self.sec = 0 self.sec = 0
def set_time(self, h, m, s): #设定时间 def set_time(self, h, m, s): # 设定时间
self.hour = h self.hour = h
self.min = m self.min = m
self.sec = s self.sec = s
def set_rtctime(self): #设定时间 def set_rtctime(self): # 设定时间
t = rtc_clock.datetime() t = rtc_clock.datetime()
self.hour = t[4] self.hour = t[4]
self.min = t[5] self.min = t[5]
self.sec = t[6] self.sec = t[6]
def drawDial(self,color): #画钟表刻度 def drawDial(self, color): # 画钟表刻度
r_tic1 = self.r - 1 r_tic1 = self.r - 1
r_tic2 = self.r - 2 r_tic2 = self.r - 2
self.display.ellipse(self.xc, self.yc, self.r,self.r, self.color) self.display.ellipse(self.xc, self.yc, self.r, self.r, self.color)
self.display.ellipse(self.xc, self.yc, 2, 2, self.color,True) self.display.ellipse(self.xc, self.yc, 2, 2, self.color, True)
for h in range(12): for h in range(12):
at = math.pi * 2.0 * h / 12.0 at = math.pi * 2.0 * h / 12.0
@@ -291,28 +316,28 @@ class Clock:
y2 = round(self.yc - r_tic2 * math.cos(at)) y2 = round(self.yc - r_tic2 * math.cos(at))
self.display.line(x1, y1, x2, y2, color) self.display.line(x1, y1, x2, y2, color)
def drawHour(self,color): #画时针 def drawHour(self, color): # 画时针
r_hour = int(self.r / 10.0 * 5) r_hour = int(self.r / 10.0 * 5)
ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0 ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0
xh = int(self.xc + r_hour * math.sin(ah)) xh = int(self.xc + r_hour * math.sin(ah))
yh = int(self.yc - r_hour * math.cos(ah)) yh = int(self.yc - r_hour * math.cos(ah))
self.display.line(self.xc, self.yc, xh, yh, color) self.display.line(self.xc, self.yc, xh, yh, color)
def drawMin(self,color): #画分针 def drawMin(self, color): # 画分针
r_min = int(self.r / 10.0 * 7) r_min = int(self.r / 10.0 * 7)
am = math.pi * 2.0 * self.min / 60.0 am = math.pi * 2.0 * self.min / 60.0
xm = round(self.xc + r_min * math.sin(am)) xm = round(self.xc + r_min * math.sin(am))
ym = round(self.yc - r_min * math.cos(am)) ym = round(self.yc - r_min * math.cos(am))
self.display.line(self.xc, self.yc, xm, ym, color) self.display.line(self.xc, self.yc, xm, ym, color)
def drawSec(self,color): #画秒针 def drawSec(self, color): # 画秒针
r_sec = int(self.r / 10.0 * 9) r_sec = int(self.r / 10.0 * 9)
asec = math.pi * 2.0 * self.sec / 60.0 asec = math.pi * 2.0 * self.sec / 60.0
xs = round(self.xc + r_sec * math.sin(asec)) xs = round(self.xc + r_sec * math.sin(asec))
ys = round(self.yc - r_sec * math.cos(asec)) ys = round(self.yc - r_sec * math.cos(asec))
self.display.line(self.xc, self.yc, xs, ys, color) self.display.line(self.xc, self.yc, xs, ys, color)
def draw_clock(self): #画完整钟表 def draw_clock(self): # 画完整钟表
self.drawDial(self.color) self.drawDial(self.color)
self.drawHour(self.color) self.drawHour(self.color)
self.drawMin(self.color) self.drawMin(self.color)
@@ -320,10 +345,11 @@ class Clock:
self.display.show() self.display.show()
self.clear(0) self.clear(0)
def clear(self,color=0): #清除 def clear(self, color=0): # 清除
self.drawHour(color) self.drawHour(color)
self.drawMin(color) self.drawMin(color)
self.drawSec(color) self.drawSec(color)
'''Reclaim memory''' '''Reclaim memory'''
gc.collect() gc.collect()

View File

@@ -20,22 +20,25 @@ sample_rate = 22050
ob_code = es8374.ES8374(onboard_i2c) ob_code = es8374.ES8374(onboard_i2c)
time.sleep(0.2) time.sleep(0.2)
#ps 特殊改双全工i2s支持 # ps 特殊改双全工i2s支持
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)
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 = []
ob_audio.readinto(buf) ob_audio.readinto(buf)
for i in range(len(buf)//2): for i in range(len(buf)//2):
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)
@@ -78,12 +87,14 @@ def play_audio_url(url):
if header[8:12] != b'WAVE': if header[8:12] != b'WAVE':
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,14 +6,19 @@ 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()
'''I2C-onboard''' '''I2C-onboard'''
#inboard_i2c = I2C(0) # inboard_i2c = I2C(0)
inboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) inboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000)
onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(38), freq=400000) onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(38), freq=400000)
@@ -21,65 +26,65 @@ onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(38), freq=400000)
onboard_spi = SPI(1, baudrate=80000000, polarity=1, phase=1) onboard_spi = SPI(1, baudrate=80000000, polarity=1, phase=1)
'''BOT035-Sensor''' '''BOT035-Sensor'''
try : try:
import sant_bot import sant_bot
onboard_bot = sant_bot.BOT035(inboard_i2c) onboard_bot = sant_bot.BOT035(inboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e) print("Warning: Failed to communicate with BOT035 (Coprocessor) or", e)
'''TFT/240*240''' '''TFT/240*240'''
onboard_tft = st7789_cf.ST7789(onboard_spi, 240, 240, dc_pin=45 ,reset=onboard_bot.tft_reset, backlight=onboard_bot.tft_brightness, font_address=0xF00000) onboard_tft = st7789_cf.ST7789(onboard_spi, 240, 240, dc_pin=45, reset=onboard_bot.tft_reset, backlight=onboard_bot.tft_brightness, font_address=0xF00000)
'''ACC-Sensor''' '''ACC-Sensor'''
try : try:
import sc7a20 import sc7a20
onboard_acc = sc7a20.SC7A20(inboard_i2c) onboard_acc = sc7a20.SC7A20(inboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with SC7A20H (ACC) or",e) print("Warning: Failed to communicate with SC7A20H (ACC) or", e)
'''ALS_PS-Sensor *2''' '''ALS_PS-Sensor *2'''
try : try:
import ltr553als import ltr553als
onboard_als_l = ltr553als.LTR_553ALS(onboard_i2c) onboard_als_l = ltr553als.LTR_553ALS(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with TR_553ALS-L (ALS&PS) or",e) print("Warning: Failed to communicate with TR_553ALS-L (ALS&PS) or", e)
try : try:
#import ltr553als # import ltr553als
onboard_als_r = ltr553als.LTR_553ALS(inboard_i2c) onboard_als_r = ltr553als.LTR_553ALS(inboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with TR_553ALS-R (ALS&PS) or",e) print("Warning: Failed to communicate with TR_553ALS-R (ALS&PS) or", e)
'''THS-Sensor''' '''THS-Sensor'''
try : try:
import shtc3 import shtc3
onboard_ths = shtc3.SHTC3(inboard_i2c) onboard_ths = shtc3.SHTC3(inboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with GXHTC3 (THS) or",e) print("Warning: Failed to communicate with GXHTC3 (THS) or", e)
'''MGS-Sensor''' '''MGS-Sensor'''
try : try:
import mmc5603 import mmc5603
onboard_mgs = mmc5603.MMC5603(inboard_i2c) onboard_mgs = mmc5603.MMC5603(inboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or",e) print("Warning: Failed to communicate with MMC5603 (MGS) or", e)
'''ASR-Sensor''' '''ASR-Sensor'''
try : try:
from ci1302x import CI1302 from ci1302x import CI1302
onboard_asr = CI1302(inboard_i2c, onboard_bot.asr_en) onboard_asr = CI1302(inboard_i2c, onboard_bot.asr_en)
except Exception as e: 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
@@ -95,8 +100,8 @@ class KEYSensor:
time.sleep_us(5) time.sleep_us(5)
return (self.range-200) < min(values) < (self.range+200) return (self.range-200) < min(values) < (self.range+200)
def get_presses(self, delay = 1): def get_presses(self, delay=1):
last_time,presses = time.time(), 0 last_time, presses = time.time(), 0
while time.time() < last_time + delay: while time.time() < last_time + delay:
time.sleep_ms(50) time.sleep_ms(50)
if self.was_pressed(): if self.was_pressed():
@@ -107,17 +112,20 @@ class KEYSensor:
return self._value() return self._value()
def was_pressed(self): def was_pressed(self):
if(self._value() != self.flag): if (self._value() != self.flag):
self.flag = self._value() self.flag = self._value()
if self.flag : if self.flag:
return True return True
else: else:
return False return False
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,44 +168,49 @@ 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
self.xc = x self.xc = x
self.yc = y self.yc = y
self.r = radius self.r = radius
self.color= color self.color = color
self.hour = 0 self.hour = 0
self.min = 0 self.min = 0
self.sec = 0 self.sec = 0
def set_time(self, h, m, s): #设定时间 def set_time(self, h, m, s): # 设定时间
self.hour = h self.hour = h
self.min = m self.min = m
self.sec = s self.sec = s
def set_rtctime(self): #设定时间 def set_rtctime(self): # 设定时间
t = rtc_clock.datetime() t = rtc_clock.datetime()
self.hour = t[4] self.hour = t[4]
self.min = t[5] self.min = t[5]
self.sec = t[6] self.sec = t[6]
def drawDial(self,color): #画钟表刻度 def drawDial(self, color): # 画钟表刻度
r_tic1 = self.r - 1 r_tic1 = self.r - 1
r_tic2 = self.r - 2 r_tic2 = self.r - 2
self.display.ellipse(self.xc, self.yc, self.r, self.r, color) self.display.ellipse(self.xc, self.yc, self.r, self.r, color)
self.display.ellipse(self.xc, self.yc, 2, 2, color,True) self.display.ellipse(self.xc, self.yc, 2, 2, color, True)
for h in range(12): for h in range(12):
at = math.pi * 2.0 * h / 12.0 at = math.pi * 2.0 * h / 12.0
@@ -203,28 +220,28 @@ class Clock:
y2 = round(self.yc - r_tic2 * math.cos(at)) y2 = round(self.yc - r_tic2 * math.cos(at))
self.display.line(x1, y1, x2, y2, color) self.display.line(x1, y1, x2, y2, color)
def drawHour(self,color): #画时针 def drawHour(self, color): # 画时针
r_hour = int(self.r / 10.0 * 5) r_hour = int(self.r / 10.0 * 5)
ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0 ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0
xh = int(self.xc + r_hour * math.sin(ah)) xh = int(self.xc + r_hour * math.sin(ah))
yh = int(self.yc - r_hour * math.cos(ah)) yh = int(self.yc - r_hour * math.cos(ah))
self.display.line(self.xc, self.yc, xh, yh, color) self.display.line(self.xc, self.yc, xh, yh, color)
def drawMin(self,color): #画分针 def drawMin(self, color): # 画分针
r_min = int(self.r / 10.0 * 7) r_min = int(self.r / 10.0 * 7)
am = math.pi * 2.0 * self.min / 60.0 am = math.pi * 2.0 * self.min / 60.0
xm = round(self.xc + r_min * math.sin(am)) xm = round(self.xc + r_min * math.sin(am))
ym = round(self.yc - r_min * math.cos(am)) ym = round(self.yc - r_min * math.cos(am))
self.display.line(self.xc, self.yc, xm, ym, color) self.display.line(self.xc, self.yc, xm, ym, color)
def drawSec(self,color): #画秒针 def drawSec(self, color): # 画秒针
r_sec = int(self.r / 10.0 * 9) r_sec = int(self.r / 10.0 * 9)
asec = math.pi * 2.0 * self.sec / 60.0 asec = math.pi * 2.0 * self.sec / 60.0
xs = round(self.xc + r_sec * math.sin(asec)) xs = round(self.xc + r_sec * math.sin(asec))
ys = round(self.yc - r_sec * math.cos(asec)) ys = round(self.yc - r_sec * math.cos(asec))
self.display.line(self.xc, self.yc, xs, ys, color) self.display.line(self.xc, self.yc, xs, ys, color)
def draw_clock(self, bg_color=0): #画完整钟表 def draw_clock(self, bg_color=0): # 画完整钟表
self.drawDial(self.color) self.drawDial(self.color)
self.drawHour(self.color) self.drawHour(self.color)
self.drawMin(self.color) self.drawMin(self.color)
@@ -234,8 +251,9 @@ class Clock:
self.drawMin(bg_color) self.drawMin(bg_color)
self.drawSec(bg_color) self.drawSec(bg_color)
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,62 +6,67 @@ 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()
'''I2C-onboard''' '''I2C-onboard'''
#onboard_i2c = I2C(0) # onboard_i2c = I2C(0)
onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000)
'''SPI-onboard''' '''SPI-onboard'''
onboard_spi = SPI(1, baudrate=80000000, polarity=0, phase=0) onboard_spi = SPI(1, baudrate=80000000, polarity=0, phase=0)
'''BOT035-Sensor''' '''BOT035-Sensor'''
try : try:
import soar_bot import soar_bot
onboard_bot = soar_bot.BOT035(onboard_i2c) onboard_bot = soar_bot.BOT035(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e) print("Warning: Failed to communicate with BOT035 (Coprocessor) or", e)
'''BPS-Sensor''' '''BPS-Sensor'''
try : try:
import spl06_001 import spl06_001
onboard_bps = spl06_001.SPL06(onboard_i2c) onboard_bps = spl06_001.SPL06(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with SPL06-001 (BPS) or",e) print("Warning: Failed to communicate with SPL06-001 (BPS) or", e)
'''IMU-Sensor''' '''IMU-Sensor'''
try : try:
import qmi8658 import qmi8658
onboard_imu = qmi8658.QMI8658(onboard_i2c) onboard_imu = qmi8658.QMI8658(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with QMI8658 (IMU) or",e) print("Warning: Failed to communicate with QMI8658 (IMU) or", e)
'''ALS_PS-Sensor''' '''ALS_PS-Sensor'''
try : try:
import ltr553als import ltr553als
onboard_als = ltr553als.LTR_553ALS(onboard_i2c) onboard_als = ltr553als.LTR_553ALS(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or",e) print("Warning: Failed to communicate with TR_553ALS (ALS&PS) or", e)
'''MGS-Sensor''' '''MGS-Sensor'''
try : try:
import mmc5603 import mmc5603
onboard_mgs = mmc5603.MMC5603(onboard_i2c) onboard_mgs = mmc5603.MMC5603(onboard_i2c)
except Exception as e: except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or",e) print("Warning: Failed to communicate with MMC5603 (MGS) or", e)
'''TFT/240*240''' '''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) 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
@@ -76,8 +81,8 @@ class KEYSensor:
time.sleep_us(2) time.sleep_us(2)
return (self.range-200) < min(values) < (self.range+200) return (self.range-200) < min(values) < (self.range+200)
def get_presses(self, delay = 1): def get_presses(self, delay=1):
last_time,presses = time.time(), 0 last_time, presses = time.time(), 0
while time.time() < last_time + delay: while time.time() < last_time + delay:
time.sleep_ms(50) time.sleep_ms(50)
if self.was_pressed(): if self.was_pressed():
@@ -88,17 +93,20 @@ class KEYSensor:
return self._value() return self._value()
def was_pressed(self): def was_pressed(self):
if(self._value() != self.flag): if (self._value() != self.flag):
self.flag = self._value() self.flag = self._value()
if self.flag : if self.flag:
return True return True
else: else:
return False return False
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,14 +116,17 @@ 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)
A2key = KEYSensor(17,1600) A2key = KEYSensor(17, 1600)
A3key = KEYSensor(17,800) 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,35 +153,37 @@ 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
self.xc = x self.xc = x
self.yc = y self.yc = y
self.r = radius self.r = radius
self.color= color self.color = color
self.hour = 0 self.hour = 0
self.min = 0 self.min = 0
self.sec = 0 self.sec = 0
def set_time(self, h, m, s): #设定时间 def set_time(self, h, m, s): # 设定时间
self.hour = h self.hour = h
self.min = m self.min = m
self.sec = s self.sec = s
def set_rtctime(self): #设定时间 def set_rtctime(self): # 设定时间
t = rtc_clock.datetime() t = rtc_clock.datetime()
self.hour = t[4] self.hour = t[4]
self.min = t[5] self.min = t[5]
self.sec = t[6] self.sec = t[6]
def drawDial(self,color): #画钟表刻度 def drawDial(self, color): # 画钟表刻度
r_tic1 = self.r - 1 r_tic1 = self.r - 1
r_tic2 = self.r - 2 r_tic2 = self.r - 2
self.display.ellipse(self.xc, self.yc, self.r, self.r, color) self.display.ellipse(self.xc, self.yc, self.r, self.r, color)
self.display.ellipse(self.xc, self.yc, 2, 2, color,True) self.display.ellipse(self.xc, self.yc, 2, 2, color, True)
for h in range(12): for h in range(12):
at = math.pi * 2.0 * h / 12.0 at = math.pi * 2.0 * h / 12.0
@@ -179,28 +193,28 @@ class Clock:
y2 = round(self.yc - r_tic2 * math.cos(at)) y2 = round(self.yc - r_tic2 * math.cos(at))
self.display.line(x1, y1, x2, y2, color) self.display.line(x1, y1, x2, y2, color)
def drawHour(self,color): #画时针 def drawHour(self, color): # 画时针
r_hour = int(self.r / 10.0 * 5) r_hour = int(self.r / 10.0 * 5)
ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0 ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0
xh = int(self.xc + r_hour * math.sin(ah)) xh = int(self.xc + r_hour * math.sin(ah))
yh = int(self.yc - r_hour * math.cos(ah)) yh = int(self.yc - r_hour * math.cos(ah))
self.display.line(self.xc, self.yc, xh, yh, color) self.display.line(self.xc, self.yc, xh, yh, color)
def drawMin(self,color): #画分针 def drawMin(self, color): # 画分针
r_min = int(self.r / 10.0 * 7) r_min = int(self.r / 10.0 * 7)
am = math.pi * 2.0 * self.min / 60.0 am = math.pi * 2.0 * self.min / 60.0
xm = round(self.xc + r_min * math.sin(am)) xm = round(self.xc + r_min * math.sin(am))
ym = round(self.yc - r_min * math.cos(am)) ym = round(self.yc - r_min * math.cos(am))
self.display.line(self.xc, self.yc, xm, ym, color) self.display.line(self.xc, self.yc, xm, ym, color)
def drawSec(self,color): #画秒针 def drawSec(self, color): # 画秒针
r_sec = int(self.r / 10.0 * 9) r_sec = int(self.r / 10.0 * 9)
asec = math.pi * 2.0 * self.sec / 60.0 asec = math.pi * 2.0 * self.sec / 60.0
xs = round(self.xc + r_sec * math.sin(asec)) xs = round(self.xc + r_sec * math.sin(asec))
ys = round(self.yc - r_sec * math.cos(asec)) ys = round(self.yc - r_sec * math.cos(asec))
self.display.line(self.xc, self.yc, xs, ys, color) self.display.line(self.xc, self.yc, xs, ys, color)
def draw_clock(self, bg_color=0): #画完整钟表 def draw_clock(self, bg_color=0): # 画完整钟表
self.drawDial(self.color) self.drawDial(self.color)
self.drawHour(self.color) self.drawHour(self.color)
self.drawMin(self.color) self.drawMin(self.color)
@@ -210,9 +224,10 @@ class Clock:
self.drawMin(bg_color) self.drawMin(bg_color)
self.drawSec(bg_color) self.drawSec(bg_color)
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

@@ -17,22 +17,25 @@ sample_rate = 22050
ob_code = es8374.ES8374(onboard_i2c) ob_code = es8374.ES8374(onboard_i2c)
time.sleep(0.2) time.sleep(0.2)
#ps 特殊改双全工i2s支持 # 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) 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) 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 = []
ob_audio.readinto(buf) ob_audio.readinto(buf)
for i in range(len(buf)//2): for i in range(len(buf)//2):
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)
@@ -75,12 +84,14 @@ def play_audio_url(url):
if header[8:12] != b'WAVE': if header[8:12] != b'WAVE':
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

@@ -24,10 +24,11 @@ normal_tone = {
'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, i2s_bus, rate=22050): def __init__(self, i2s_bus, rate=22050):
self.reset() self.reset()
@@ -51,7 +52,7 @@ class MIDI():
_wbytes = 0 _wbytes = 0
while _wbytes < len(_data): while _wbytes < len(_data):
send_size = min(512, len(_data) - _wbytes) send_size = min(512, len(_data) - _wbytes)
self.i2s_bus.write(_data[_wbytes : _wbytes + send_size]) self.i2s_bus.write(_data[_wbytes: _wbytes + send_size])
_wbytes += send_size _wbytes += send_size
def set_tempo(self, ticks=4, bpm=120): def set_tempo(self, ticks=4, bpm=120):
@@ -134,24 +135,36 @@ class MIDI():
def stop(self): def stop(self):
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']
PUNCHLINE=['c4:3','g3:1','f#','g','g#:3','g','r','b','c4'] FUNK = ['c2:2', 'c', 'd#', 'c:1', 'f:2', 'c:1', 'f:2', 'f#',
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'] 'g', 'c', 'c', 'g', 'c:1', 'f#:2', 'c:1', 'f#:2', 'f', 'd#']
BADDY=['c3:3','r','d:2','d#','r','c','r','f#: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',
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'] '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']
BA_DING=['b5:1','e6:3'] BIRTHDAY = ['c4:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
WAWAWAWAA=['e3:3','r:1','d#:3','r:1','d:4','r:1','c#:8'] 'f:8', 'c:3', 'c:1', 'c5:4', 'a4', 'f', 'e', 'd', 'a#:3', 'a#:1', 'a:4', 'f', 'g', 'f:8']
JUMP_UP=['c5:1','d','e','f','g'] WEDDING = ['c4:4', 'f:3', 'f:1', 'f:8', 'c:4', 'g:3', 'e:1', 'f:8',
JUMP_DOWN=['g5:1','f','e','d','c'] 'c:4', 'f:3', 'a:1', 'c5:4', 'a4:3', 'f:1', 'f:4', 'e:3', 'f:1', 'g:8']
POWER_UP=['g4:1','c5','e4','g5:2','e5:1','g5:3'] FUNERAL = ['c3:4', 'c:3', 'c:1', 'c:4', 'd#:3',
POWER_DOWN=['g5:1','d#','c','g4:2','b:1','c5: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']
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']
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']
WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8']
JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g']
JUMP_DOWN = ['g5:1', 'f', 'e', 'd', 'c']
POWER_UP = ['g4:1', 'c5', 'e4', 'g5:2', 'e5:1', 'g5:3']
POWER_DOWN = ['g5:1', 'd#', 'c', 'g4:2', 'b:1', 'c5:3']

View File

@@ -19,9 +19,9 @@ _NOVA_G1_PWM = const(0x04)
class NOVA_G1: class NOVA_G1:
def __init__(self, i2c_bus, addr=_NOVA_G1_ADDRESS): def __init__(self, i2c_bus, addr=_NOVA_G1_ADDRESS):
self._i2c=i2c_bus self._i2c = i2c_bus
self._addr = addr self._addr = addr
if self._rreg(_NOVA_G1_ID)!= 0x25: if self._rreg(_NOVA_G1_ID) != 0x25:
raise AttributeError("Cannot find a NOVA_G1") raise AttributeError("Cannot find a NOVA_G1")
self.reset() self.reset()
@@ -36,18 +36,18 @@ class NOVA_G1:
'''Read memory address''' '''Read memory address'''
try: try:
self._i2c.writeto(self._addr, reg.to_bytes(1, 'little')) self._i2c.writeto(self._addr, reg.to_bytes(1, 'little'))
return self._i2c.readfrom(self._addr, nbytes)[0] if nbytes<=1 else self._i2c.readfrom(self._addr, nbytes)[0:nbytes] return self._i2c.readfrom(self._addr, nbytes)[0] if nbytes <= 1 else self._i2c.readfrom(self._addr, nbytes)[0:nbytes]
except: except:
return 0 return 0
def reset(self): def reset(self):
"""Reset all registers to default state""" """Reset all registers to default state"""
for reg in range(_NOVA_G1_PWM,_NOVA_G1_PWM + 6): for reg in range(_NOVA_G1_PWM, _NOVA_G1_PWM + 6):
self._wreg(reg,0x00) self._wreg(reg, 0x00)
def varistor(self, ratio=100/1023): def varistor(self, ratio=100/1023):
'''Read battery power''' '''Read battery power'''
_adc= self._rreg(_NOVA_G1_ADC) << 2 | self._rreg(_NOVA_G1_ADC+1) >> 6 _adc = self._rreg(_NOVA_G1_ADC) << 2 | self._rreg(_NOVA_G1_ADC+1) >> 6
return round(_adc * ratio) return round(_adc * ratio)
def pwm(self, index, duty=None): def pwm(self, index, duty=None):
@@ -62,27 +62,27 @@ class NOVA_G1:
if not 0 <= index <= 1: if not 0 <= index <= 1:
raise ValueError("Motor port must be a number in the range: 0~1") raise ValueError("Motor port must be a number in the range: 0~1")
speed = min(100, max(speed, -100)) speed = min(100, max(speed, -100))
if action=="N": if action == "N":
self.pwm(index * 2, 0) self.pwm(index * 2, 0)
self.pwm(index * 2 + 1, 0) self.pwm(index * 2 + 1, 0)
elif action=="P": elif action == "P":
self.pwm(index * 2, 255) self.pwm(index * 2, 255)
self.pwm(index * 2 + 1, 255) self.pwm(index * 2 + 1, 255)
elif action=="CW": elif action == "CW":
if speed >= 0: if speed >= 0:
self.pwm(index * 2, 0) self.pwm(index * 2, 0)
self.pwm(index * 2 + 1, speed * 255 // 100) self.pwm(index * 2 + 1, speed * 255 // 100)
else: else:
self.pwm(index * 2, 0) self.pwm(index * 2, 0)
self.pwm(index * 2 + 1, - speed * 255 // 100) self.pwm(index * 2 + 1, - speed * 255 // 100)
elif action=="CCW": elif action == "CCW":
if speed >= 0: if speed >= 0:
self.pwm(index * 2, speed * 255 // 100) self.pwm(index * 2, speed * 255 // 100)
self.pwm(index * 2 + 1, 0) self.pwm(index * 2 + 1, 0)
else: else:
self.pwm(index * 2, - speed * 255 // 100) self.pwm(index * 2, - speed * 255 // 100)
self.pwm(index * 2 + 1, 0) self.pwm(index * 2 + 1, 0)
elif action=="NC": elif action == "NC":
return round(self.pwm(index * 2) * 100 / 255), round(self.pwm(index * 2 + 1) * 100 / 255) return round(self.pwm(index * 2) * 100 / 255), round(self.pwm(index * 2 + 1) * 100 / 255)
else: else:
raise ValueError('Invalid input, valid are "N","P","CW","CCW"') raise ValueError('Invalid input, valid are "N","P","CW","CCW"')
@@ -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,14 +40,16 @@ 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()
self.dc.off() self.dc.off()
self.spi.write(bytearray([cmd])) self.spi.write(bytearray([cmd]))
@@ -60,7 +63,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),
@@ -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
@@ -49,8 +50,9 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
self._buffer[:] = _decoder.decode(_jpeg) self._buffer[:] = _decoder.decode(_jpeg)
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
@@ -36,9 +37,9 @@ class NeoPixel:
def write(self): def write(self):
self.func(self.rgb_buf) self.func(self.rgb_buf)
def color_chase(self,R, G, B, wait): def color_chase(self, R, G, B, wait):
for i in range(self.rgbs): for i in range(self.rgbs):
self.__setitem__(i,(R, G, B)) self.__setitem__(i, (R, G, B))
self.write() self.write()
sleep(wait/1000) sleep(wait/1000)
@@ -46,14 +47,14 @@ class NeoPixel:
for j in range(255): for j in range(255):
for i in range(self.rgbs): for i in range(self.rgbs):
rc_index = (i * 256 // self.rgbs) + j rc_index = (i * 256 // self.rgbs) + j
self.__setitem__(i,self.wheel(rc_index & 255)) self.__setitem__(i, self.wheel(rc_index & 255))
self.write() self.write()
sleep(wait / 1000 / 256) sleep(wait / 1000 / 256)
if clear: if clear:
self.fill((0, 0, 0)) self.fill((0, 0, 0))
self.write() self.write()
def wheel(self,pos): def wheel(self, pos):
if pos < 0 or pos > 255: if pos < 0 or pos > 255:
return (0, 0, 0) return (0, 0, 0)
elif pos < 85: elif pos < 85: