初始化提交

This commit is contained in:
王立帮
2024-07-19 10:16:00 +08:00
parent 4c7b571f20
commit 4a2d56dcc4
7084 changed files with 741212 additions and 63 deletions

View File

@@ -0,0 +1,172 @@
"""
MixGo-Onboard resources
Micropython library for the MixGo-Onboard resources
=======================================================
#Instantiate mixgo onboard resources 20220622
#Repair brightness adjustment range 0-100% 20220623
dahanzimin From the Mixly Team
"""
import time,gc
from machine import Pin,SoftI2C,ADC,PWM,RTC,TouchPad
'''i2c-onboard'''
onboard_i2c=SoftI2C(scl = Pin(22), sda = Pin(21), freq = 400000)
'''RTC'''
rtc_clock=RTC()
'''MPU9250'''
try :
import mpu9250
onboard_mpu = mpu9250.MPU9250(onboard_i2c)
onboard_compass = mpu9250.Compass(onboard_mpu)
except Exception as e:
print("Warning: Failed to communicate with MPU9250 or",e)
'''Matrix16x8'''
try :
import matrix16x8
onboard_matrix = matrix16x8.Matrix(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with Matrix16x8 or",e)
'''2-RGB'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(2), 2)
'''1Buzzer-Music'''
from music import MIDI
onboard_music =MIDI(27)
'''2-Button'''
class Button:
def __init__(self, pin):
self._pin = Pin(pin, Pin.IN)
self._flag = True
def get_presses(self, delay = 1):
last_time,presses = time.time(), 0
while time.time() < last_time + delay:
time.sleep(0.05)
if self.was_pressed():
presses += 1
return presses
def is_pressed(self):
return self._pin.value() == False
def was_pressed(self):
if self._pin.value() != self._flag:
time.sleep(0.01)
self._flag = self._pin.value()
if self._flag:
return False
else:
return True
def irq(self, handler, trigger):
self._pin.irq(handler = handler, trigger = trigger)
button_a = Button(17)
button_b = Button(16)
'''2-TouchPad'''
class Touch_Pad:
def __init__(self, pin,value=220):
self._pin = TouchPad(Pin(pin))
self.value = value
def is_touched(self):
return self._pin.read() < self.value
def raw_value(self):
return self._pin.read()
touch1 = Touch_Pad(32)
touch2 = Touch_Pad(33)
'''4-ADCSensor'''
class ADCSensor:
__species = {}
__first_init = True
def __new__(cls, pin, *args, **kwargs):
if pin not in cls.__species.keys():
cls.__first_init = True
cls.__species[pin]=object.__new__(cls)
return cls.__species[pin]
def __init__(self, pin):
if self.__first_init:
self.__first_init = False
self._adc=ADC(Pin(pin))
self._adc.atten(ADC.ATTN_11DB)
self._switch = Pin(15, Pin.OUT)
def read(self):
return self._adc.read_u16()
def switch(self,val):
self._switch.value(val)
def infrared_left():
ADCSensor(34).switch(1)
time.sleep(0.02)
adc=ADCSensor(34).read()
ADCSensor(34).switch(0)
return adc
def infrared_right():
ADCSensor(36).switch(1)
time.sleep(0.02)
adc=ADCSensor(36).read()
ADCSensor(36).switch(0)
return adc
def get_brightness():
return ADCSensor(39).read()
def get_soundlevel():
value_d= []
for _ in range(5):
values = []
for _ in range(5):
val = ADCSensor(35).read()
values.append(val)
value_d.append(max(values) - min(values))
return max(value_d)
'''2-LED''' #Repair brightness adjustment range 0-100%
class LED:
def __init__(self, pin):
self._pin =PWM(Pin(pin),freq=5000,duty_u16=65535)
self.setbrightness(0)
def setbrightness(self,val):
if not 0 <= val <= 100:
raise ValueError("Brightness must be in the range: 0-100%")
self._brightness=val
self._pin.duty_u16(65535-val*65535//100)
def getbrightness(self):
return self._brightness
def setonoff(self,val):
if(val == -1):
self.setbrightness(100) if self._brightness<50 else self.setbrightness(0)
elif(val == 1):
self.setbrightness(100)
elif(val == 0):
self.setbrightness(0)
def getonoff(self):
return True if self._brightness>0 else False
led1 = LED(0)
led2 = LED(5)
'''Reclaim memory'''
gc.collect()

View File

@@ -0,0 +1,51 @@
"""
MixGo PE-Onboard resources
Micropython library for the MixGo PE-Onboard resources
=======================================================
#Preliminary composition 20230126
dahanzimin From the Mixly Team
"""
import time,gc
from machine import Pin,RTC
'''RTC'''
rtc_clock=RTC()
'''Matrix8x5'''
try :
import matrix8x5
onboard_matrix = matrix8x5.Matrix(0,brightness=0.5)
except Exception as e:
print("Warning: Failed to communicate with Matrix8x5 or",e)
'''4-TouchPad'''
class Touch_Pad:
__species = {}
__first_init = True
def __new__(cls, pin, *args, **kwargs):
if pin not in cls.__species.keys():
cls.__first_init = True
cls.__species[pin]=object.__new__(cls)
return cls.__species[pin]
def __init__(self, pin):
if self.__first_init:
self.__first_init = False
from machine import TouchPad
self._pin = TouchPad(Pin(pin))
def is_touched(self,value):
return self._pin.read() < value
def raw_value(self):
return self._pin.read()
#Touch with function call
def touched(pin,value=350):
return Touch_Pad(pin).is_touched(value) if value else Touch_Pad(pin).raw_value()
'''Reclaim memory'''
gc.collect()

View File

@@ -0,0 +1,154 @@
"""
mPython-Onboard resources
Micropython library for the mPython-Onboard resources
=======================================================
#Preliminary composition 20220716
dahanzimin From the Mixly Team
"""
import time,gc
from machine import Pin,SoftI2C,ADC,PWM,RTC,TouchPad
'''i2c-onboard'''
onboard_i2c=SoftI2C(scl = Pin(22), sda = Pin(23), freq = 400000)
'''RTC'''
rtc_clock=RTC()
'''OLED128X64'''
try :
import oled128x64
onboard_oled = oled128x64.OLED(onboard_i2c,font_address=0x700000)
except Exception as e:
print("Warning: Failed to communicate with OLED128x64 or",e)
'''Magnetic'''
try :
import mmc5603
magnetic = mmc5603.MMC5603(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with MMC5603 or",e)
'''Motion''' #Including temperature、accelerometer、gyroscope
try :
import qmi8658
motion = qmi8658.QMI8658(onboard_i2c)
except Exception as e:
print("Warning: Failed to communicate with QMI8658 or",e)
'''2-RGB'''
from ws2812 import NeoPixel
onboard_rgb = NeoPixel(Pin(17), 3)
'''1-Buzzer'''
from music import MIDI
onboard_music =MIDI(16, invert=1)
'''Port mapping'''
class PIN:
P0=33
P1=32
P2=35
P3=34
P4=39
P5=0
P6=16
P7=17
P8=26
P9=25
P10=36
P11=2
P13=18
P14=19
P15=21
P16=5
P19=22
P20=23
P23=27
P24=14
P25=12
P26=13
P27=15
P28=4
'''2-Button'''
class Button:
def __init__(self, pin):
self._pin = Pin(pin, Pin.IN)
self._flag = True
def get_presses(self, delay = 1):
last_time,presses = time.time(), 0
while time.time() < last_time + delay:
time.sleep(0.05)
if self.was_pressed():
presses += 1
return presses
def is_pressed(self):
return self._pin.value() == False
def was_pressed(self):
if self._pin.value() != self._flag:
time.sleep(0.01)
self._flag = self._pin.value()
if self._flag:
return False
else:
return True
def irq(self, handler, trigger):
self._pin.irq(handler = handler, trigger = trigger)
button_a = Button(0)
button_b = Button(2)
'''2-TouchPad'''
class Touch_Pad:
def __init__(self, pin,value=220):
self._pin = TouchPad(Pin(pin))
self.value = value
def is_touched(self):
return self._pin.read() < self.value
def raw_value(self):
return self._pin.read()
touch_p = Touch_Pad(27)
touch_y = Touch_Pad(14)
touch_t = Touch_Pad(12)
touch_h = Touch_Pad(13)
touch_o = Touch_Pad(15)
touch_n = Touch_Pad(4)
'''2-ADCSensor'''
class ADCSensor:
def __init__(self, pin):
self.adc=ADC(Pin(pin))
self.adc.atten(ADC.ATTN_11DB)
def brightness(self):
return self.adc.read_u16()
def soundlevel(self):
value_d= []
for _ in range(5):
values = []
for _ in range(10):
val = self.adc.read_u16()
values.append(val)
value_d.append(max(values) - min(values))
return max(value_d)
onboard_sound = ADCSensor(36)
onboard_light = ADCSensor(39)
'''Microphone''' #Later, it is used for recording and acquisition
microphone=ADC(Pin(38))
microphone.atten(ADC.ATTN_11DB)
'''Reclaim memory'''
gc.collect()