更新mini的lib固件,修改3传感器的初始化方式

This commit is contained in:
dahanzimin
2024-07-19 11:35:49 +08:00
parent 25c73da3d5
commit 5cb9d3d4ab
3 changed files with 197 additions and 176 deletions

View File

@@ -0,0 +1,23 @@
"""
MINI G2 -MixGo MINI EXT G2
MicroPython library for the MINI G2 (Expansion board for MixGo MINI)
=======================================================
@dahanzimin From the Mixly Team
"""
import gc
from machine import Pin, SoftI2C
'''i2c-extboard'''
ext_i2c = SoftI2C(scl=Pin(7), sda=Pin(8), freq=400000)
'''RFID_Sensor'''
try :
import rc522
onboard_rfid = rc522.RC522(ext_i2c)
except Exception as e:
print("Warning: Failed to communicate with SI522A (RFID) or",e)
'''Reclaim memory'''
gc.collect()

View File

@@ -1,176 +1,174 @@
""" """
mixgo_mini onboard resources mixgo_mini onboard resources
Micropython library for the mixgo_mini onboard resources Micropython library for the mixgo_mini onboard resources
======================================================= =======================================================
@dahanzimin From the Mixly Team
#Preliminary composition 20240618 """
@dahanzimin From the Mixly Team import time, gc
""" from esp32 import mcu_temperature
import time, gc from machine import Pin, ADC, RTC, SoftI2C
from esp32 import mcu_temperature
from machine import Pin, ADC, RTC, SoftI2C '''Reclaim memory'''
gc.collect()
'''Reclaim memory'''
gc.collect() '''RTC'''
rtc_clock = RTC()
'''RTC'''
rtc_clock = RTC() '''I2C-onboard'''
onboard_i2c = SoftI2C(scl=Pin(10), sda=Pin(18), freq=400000)
'''I2C-onboard''' #onboard_i2c_scan = onboard_i2c.scan()
onboard_i2c = SoftI2C(scl=Pin(10), sda=Pin(18), freq=400000)
onboard_i2c_scan = onboard_i2c.scan() '''ACC-Sensor'''
try :
'''ACC-Sensor''' import sc7a20
try : onboard_acc = sc7a20.SC7A20(onboard_i2c)
import sc7a20 except Exception as e:
onboard_acc = sc7a20.SC7A20(onboard_i2c) print("Warning: Failed to communicate with SC7A20H (ACC) or",e)
except Exception as e:
print("Warning: Failed to communicate with SC7A20H (ACC) or",e) '''ALS_PS-Sensor'''
try :
'''ALS_PS-Sensor''' import ap3216c
try : onboard_als = ap3216c.AP3216C(onboard_i2c)
import ap3216c except Exception as e:
onboard_als = ap3216c.AP3216C(onboard_i2c) print("Warning: Failed to communicate with AP3216C (ALS&PS) or",e)
except Exception as e:
print("Warning: Failed to communicate with AP3216C (ALS&PS) or",e) '''BOT035-Sensor'''
try :
'''BOT035-Sensor''' import mini_bot
try : onboard_bot = mini_bot.BOT035(onboard_i2c)
import mini_bot onboard_matrix = onboard_bot
onboard_bot = mini_bot.BOT035(onboard_i2c) except Exception as e:
onboard_matrix = onboard_bot print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e)
except Exception as e:
print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e) '''BPS-Sensor'''
# if 0x77 in onboard_i2c_scan:
'''BPS-Sensor''' # try :
if 0x77 in onboard_i2c_scan: # import spl06_001
try : # onboard_bps = spl06_001.SPL06(onboard_i2c)
import spl06_001 # except Exception as e:
onboard_bps = spl06_001.SPL06(onboard_i2c) # print("Warning: Failed to communicate with SPL06-001 (BPS) or",e)
except Exception as e:
print("Warning: Failed to communicate with SPL06-001 (BPS) or",e) '''THS-Sensor'''
# if 0x70 in onboard_i2c_scan:
'''THS-Sensor''' # try :
if 0x70 in onboard_i2c_scan: # import shtc3
try : # onboard_ths = shtc3.SHTC3(onboard_i2c)
import shtc3 # except Exception as e:
onboard_ths = shtc3.SHTC3(onboard_i2c) # print("Warning: Failed to communicate with GXHTC3 (THS) or",e)
except Exception as e:
print("Warning: Failed to communicate with GXHTC3 (THS) or",e) '''MGS-Sensor'''
# if 0x30 in onboard_i2c_scan:
'''MGS-Sensor''' # try :
if 0x30 in onboard_i2c_scan: # import mmc5603
try : # onboard_mgs = mmc5603.MMC5603(onboard_i2c)
import mmc5603 # except Exception as e:
onboard_mgs = mmc5603.MMC5603(onboard_i2c) # print("Warning: Failed to communicate with MMC5603 (MGS) or",e)
except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or",e) '''MCU_temperature'''
def onboard_temperature():
'''MCU_temperature''' return mcu_temperature()
def onboard_temperature():
return mcu_temperature() '''2RGB_WS2812'''
from ws2812x import NeoPixel
'''2RGB_WS2812''' onboard_rgb = NeoPixel(Pin(9), 2)
from ws2812x import NeoPixel
onboard_rgb = NeoPixel(Pin(9), 2) '''1Buzzer-Music'''
from musicx import MIDI
'''1Buzzer-Music''' onboard_music = MIDI(onboard_bot)
from musicx import MIDI
onboard_music = MIDI(onboard_bot) '''5KEY_Sensor'''
class KEYSensor:
'''5KEY_Sensor''' def __init__(self, pin, range):
class KEYSensor: self.pin = pin
def __init__(self, pin, range): self.adc = ADC(Pin(pin), atten=ADC.ATTN_0DB)
self.pin = pin self.range = range
self.adc = ADC(Pin(pin), atten=ADC.ATTN_0DB) self.flag = True
self.range = range
self.flag = True def _value(self):
values = []
def _value(self): for _ in range(50):
values = [] values.append(self.adc.read())
for _ in range(50): time.sleep_us(2)
values.append(self.adc.read()) return (self.range-200) < min(values) < (self.range+200)
time.sleep_us(2)
return (self.range-200) < min(values) < (self.range+200) def get_presses(self, delay = 1):
last_time,presses = time.time(), 0
def get_presses(self, delay = 1): while time.time() < last_time + delay:
last_time,presses = time.time(), 0 time.sleep_ms(50)
while time.time() < last_time + delay: if self.was_pressed():
time.sleep_ms(50) presses += 1
if self.was_pressed(): return presses
presses += 1
return presses def is_pressed(self):
return self._value()
def is_pressed(self):
return self._value() def was_pressed(self):
if(self._value() != self.flag):
def was_pressed(self): self.flag = self._value()
if(self._value() != self.flag): if self.flag :
self.flag = self._value() return True
if self.flag : else:
return True return False
else:
return False def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler = handler, trigger = trigger)
def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler = handler, trigger = trigger) '''1KEY_Button'''
class Button(KEYSensor):
'''1KEY_Button''' def __init__(self, pin):
class Button(KEYSensor): self.pin = pin
def __init__(self, pin): self.key = Pin(pin, Pin.IN)
self.pin = pin self.flag = True
self.key = Pin(pin, Pin.IN)
self.flag = True def _value(self):
return not self.key.value()
def _value(self):
return not self.key.value() B1key = Button(9)
B2key = KEYSensor(0,0)
B1key = Button(9) A1key = KEYSensor(0,2100)
B2key = KEYSensor(0,0) A2key = KEYSensor(0,1500)
A1key = KEYSensor(0,2100) A3key = KEYSensor(0,800)
A2key = KEYSensor(0,1500) A4key = KEYSensor(0,2700)
A3key = KEYSensor(0,800)
A4key = KEYSensor(0,2700) '''2LED-Multiplex RGB'''
class LED:
'''2LED-Multiplex RGB''' def __init__(self, rgb, num=2, color=3):
class LED: self._rgb = rgb
def __init__(self, rgb, num=2, color=3): self._col = [color] * num
self._rgb = rgb self._color = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1))
self._col = [color] * num
self._color = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1)) def setbrightness(self, index, value):
self._rgb[index - 1] = (value if self._color[self._col[index-1]][0] else 0,
def setbrightness(self, index, value): value if self._color[self._col[index-1]][1] else 0,
self._rgb[index - 1] = (value if self._color[self._col[index-1]][0] else 0, value if self._color[self._col[index-1]][2] else 0)
value if self._color[self._col[index-1]][1] else 0, self._rgb.write()
value if self._color[self._col[index-1]][2] else 0)
self._rgb.write() def getbrightness(self, index):
color = self._rgb[index - 1]
def getbrightness(self, index): return color[0] | color[1] | color[2]
color = self._rgb[index - 1]
return color[0] | color[1] | color[2] def setonoff(self, index, value):
if value == -1:
def setonoff(self, index, value): if self.getbrightness(index) < 50:
if value == -1: self.setbrightness(index, 100)
if self.getbrightness(index) < 50: else:
self.setbrightness(index, 100) self.setbrightness(index, 0)
else: elif value == 1:
self.setbrightness(index, 0) self.setbrightness(index, 100)
elif value == 1: elif value == 0:
self.setbrightness(index, 100) self.setbrightness(index, 0)
elif value == 0:
self.setbrightness(index, 0) def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False
def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False def setcolor(self, index, color):
self._col[index-1] = color
def setcolor(self, index, color):
self._col[index-1] = color def getcolor(self, index):
return self._col[index-1]
def getcolor(self, index):
return self._col[index-1] onboard_led = LED(onboard_rgb)
onboard_led = LED(onboard_rgb) '''Reclaim memory'''
gc.collect()
'''Reclaim memory'''
gc.collect()