Files
mixly3-server/mixly/boards/default/micropython_esp32c3/build/lib/mixgo_me.py

173 lines
4.5 KiB
Python

"""
MixGo ME Onboard resources
MicroPython library for the MixGo ME Onboard resources
=======================================================
@dahanzimin From the Mixly Team
"""
import time, gc
from machine import *
'''i2c-onboard'''
onboard_i2c=SoftI2C(scl = Pin(7), sda = Pin(6), freq = 400000)
'''RTC'''
rtc_clock=RTC()
'''ACC-Sensor'''
try :
import mxc6655xa
onboard_acc = mxc6655xa.MXC6655XA(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with MXC6655XA or",e)
'''ALS_PS-Sensor'''
try :
import ltr553als
onboard_als = ltr553als.LTR_553ALS(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with LTR_553ALS or",e)
'''Matrix8x5'''
try :
import matrix8x5
onboard_matrix = matrix8x5.Matrix(8)
except Exception as e:
print("Warning: Failed to communicate with Matrix8x5 or",e)
'''Magnetic'''
try :
import mmc5603
onboard_mgs = mmc5603.MMC5603(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with MMC5603 or",e)
'''2RGB_WS2812'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(9), 2, ORDER=(0, 1, 2, 3), multiplex=1)
'''1Buzzer-Music'''
from music import MIDI
onboard_music = MIDI(10)
'''MIC_Sensor'''
class MICSensor:
def __init__(self):
self.adc=ADC(Pin(4), atten=ADC.ATTN_11DB)
def read(self):
maxloudness = 0
for i in range(5):
loudness = self.sample()
if loudness > maxloudness:
maxloudness = loudness
return maxloudness
def sample(self):
values = []
for i in range(50):
val = self.adc.read_u16()
values.append(val)
return max(values) - min(values)
onboard_sound = MICSensor()
'''5KEY_Sensor'''
class KEYSensor:
def __init__(self, pin, range):
self.pin = pin
self.adc = ADC(Pin(pin), atten=ADC.ATTN_11DB)
self.range = range
self.flag = True
def _value(self):
values = []
for _ in range(25):
try:
values.append(self.adc.read())
except: #IDF>5.2.2存在ADC2问题
pass
time.sleep_us(5)
return (self.range-200) < min(values) < (self.range+200)
def get_presses(self, delay = 1):
last_time,presses = time.time(), 0
while time.time() < last_time + delay:
time.sleep_ms(50)
if self.was_pressed():
presses += 1
return presses
def is_pressed(self):
return self._value()
def was_pressed(self):
if(self._value() != self.flag):
self.flag = self._value()
if self.flag:
return True
else:
return False
def irq(self, handler, trigger):
Pin(self.pin, Pin.IN).irq(handler = handler, trigger = trigger)
'''1KEY_Button'''
class Button(KEYSensor):
def __init__(self, pin):
self.pin = pin
self.key = Pin(pin, Pin.IN)
self.flag = True
def _value(self):
return not self.key.value()
B1key = Button(9)
B2key = KEYSensor(5,20)
A1key = KEYSensor(5,800)
A2key = KEYSensor(5,1600)
A3key = KEYSensor(5,2500)
A4key = KEYSensor(5,3500)
'''2LED-Multiplex RGB'''
class LED:
def __init__(self, rgb, num=2, color=3):
self._rgb = rgb
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,
value if self._color[self._col[index-1]][1] else 0,
value if self._color[self._col[index-1]][2] else 0)
self._rgb.write()
def getbrightness(self, index):
color = self._rgb[index - 1]
return color[0] | color[1] | color[2]
def setonoff(self, index, value):
if value == -1:
if self.getbrightness(index) < 50:
self.setbrightness(index, 100)
else:
self.setbrightness(index, 0)
elif value == 1:
self.setbrightness(index, 100)
elif value == 0:
self.setbrightness(index, 0)
def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False
def setcolor(self, index, color):
self._col[index-1] = color
def getcolor(self, index):
return self._col[index-1]
onboard_led=LED(onboard_rgb)
'''Reclaim memory'''
gc.collect()