264 lines
7.3 KiB
Python
264 lines
7.3 KiB
Python
"""
|
|
mixgo_sowl Onboard resources(v1.3)
|
|
|
|
Micropython library for the mixgo_sant Onboard resources
|
|
=======================================================
|
|
@dahanzimin From the Mixly Team
|
|
"""
|
|
import gc
|
|
import time
|
|
import math
|
|
from machine import *
|
|
from music import MIDI
|
|
from ws2812x import NeoPixel
|
|
|
|
'''RTC'''
|
|
rtc_clock = RTC()
|
|
|
|
'''I2C-onboard'''
|
|
# onboard_i2c = I2C(0)
|
|
onboard_i2c = SoftI2C(scl=Pin(27), sda=Pin(26), freq=400000)
|
|
|
|
'''SPI-onboard'''
|
|
onboard_spi = SPI(1, baudrate=80000000, polarity=0, phase=0)
|
|
|
|
'''BOT035-Sensor'''
|
|
try:
|
|
import sowl_bot
|
|
onboard_bot = sowl_bot.BOT035(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with BOT035 (Coprocessor) or", e)
|
|
|
|
'''TFT/128*128'''
|
|
import st7735_cf
|
|
onboard_tft = st7735_cf.ST7735(onboard_spi, 128, 128, dc_pin=23, reset=onboard_bot.tft_reset, backlight=onboard_bot.tft_brightness, font_address=0x700000)
|
|
|
|
'''BPS-Sensor'''
|
|
try:
|
|
import spl06_001
|
|
onboard_bps = spl06_001.SPL06(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with SPL06-001 (BPS) or", e)
|
|
|
|
'''ACC-Sensor'''
|
|
try:
|
|
import sc7a20
|
|
onboard_acc = sc7a20.SC7A20(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with SC7A20H (ACC) 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 (ALS&PS) or", e)
|
|
|
|
'''THS-Sensor'''
|
|
try:
|
|
import shtc3
|
|
onboard_ths = shtc3.SHTC3(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with GXHTC3 (THS) or", e)
|
|
|
|
'''MGS-Sensor'''
|
|
try:
|
|
import mmc5603
|
|
onboard_mgs = mmc5603.MMC5603(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with MMC5603 (MGS) or", e)
|
|
|
|
'''RFID_Sensor'''
|
|
try :
|
|
import rc522
|
|
onboard_rfid = rc522.RC522(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with SI522 (RFID) or",e)
|
|
|
|
'''ASR-Sensor'''
|
|
try:
|
|
from ci130x import CI130X
|
|
onboard_asr = CI130X(onboard_i2c)
|
|
except Exception as e:
|
|
print("Warning: Failed to communicate with CI1316XP (ASR) or", e)
|
|
|
|
'''2RGB_WS2812'''
|
|
onboard_rgb = NeoPixel(onboard_bot.rgb_sync, 2)
|
|
|
|
'''1Buzzer-Music'''
|
|
onboard_music = MIDI(0, pa_ctrl=onboard_asr.pa_ctrl)
|
|
|
|
'''5KEY_Sensor'''
|
|
class KEYSensor:
|
|
def __init__(self, pin, range):
|
|
self.pin = pin
|
|
self.adc = ADC(Pin(pin))
|
|
self.adc.atten(ADC.ATTN_0DB)
|
|
self.range = range
|
|
self.flag = True
|
|
|
|
def _value(self):
|
|
values = []
|
|
for _ in range(25):
|
|
values.append(self.adc.read())
|
|
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(28)
|
|
B2key = KEYSensor(5, 0)
|
|
A1key = KEYSensor(5, 800)
|
|
A2key = KEYSensor(5, 2050)
|
|
A3key = KEYSensor(5, 2600)
|
|
A4key = KEYSensor(5, 1450)
|
|
|
|
'''2-LED'''
|
|
class LED:
|
|
def __init__(self, func):
|
|
self._func = func
|
|
|
|
def setbrightness(self, index, val):
|
|
self._func(index, val)
|
|
|
|
def getbrightness(self, index):
|
|
return self._func(index)
|
|
|
|
def setonoff(self, index, val):
|
|
if val == -1:
|
|
self.setbrightness(index, 100) if self.getbrightness(index) < 50 else self.setbrightness(index, 0)
|
|
elif val == 1:
|
|
self.setbrightness(index, 100)
|
|
elif val == 0:
|
|
self.setbrightness(index, 0)
|
|
|
|
def getonoff(self, index):
|
|
return True if self.getbrightness(index) > 50 else False
|
|
|
|
onboard_led = LED(onboard_bot.led_pwm)
|
|
|
|
'''MIC_Sensor'''
|
|
class MICSensor:
|
|
def __init__(self,pin):
|
|
self.adc=ADC(Pin(pin), 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(6)
|
|
|
|
class Clock:
|
|
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
|
|
self.display = oled
|
|
self.xc = x
|
|
self.yc = y
|
|
self.r = radius
|
|
self.color = color
|
|
self.hour = 0
|
|
self.min = 0
|
|
self.sec = 0
|
|
|
|
def set_time(self, h, m, s): # 设定时间
|
|
self.hour = h
|
|
self.min = m
|
|
self.sec = s
|
|
|
|
def set_rtctime(self): # 设定时间
|
|
t = rtc_clock.datetime()
|
|
self.hour = t[4]
|
|
self.min = t[5]
|
|
self.sec = t[6]
|
|
|
|
def drawDial(self, color): # 画钟表刻度
|
|
r_tic1 = self.r - 1
|
|
r_tic2 = self.r - 2
|
|
self.display.ellipse(self.xc, self.yc, self.r, self.r, color)
|
|
self.display.ellipse(self.xc, self.yc, 2, 2, color, True)
|
|
|
|
for h in range(12):
|
|
at = math.pi * 2.0 * h / 12.0
|
|
x1 = round(self.xc + r_tic1 * math.sin(at))
|
|
x2 = round(self.xc + r_tic2 * math.sin(at))
|
|
y1 = round(self.yc - r_tic1 * math.cos(at))
|
|
y2 = round(self.yc - r_tic2 * math.cos(at))
|
|
self.display.line(x1, y1, x2, y2, color)
|
|
|
|
def drawHour(self, color): # 画时针
|
|
r_hour = int(self.r / 10.0 * 5)
|
|
ah = math.pi * 2.0 * ((self.hour % 12) + self.min / 60.0) / 12.0
|
|
xh = int(self.xc + r_hour * math.sin(ah))
|
|
yh = int(self.yc - r_hour * math.cos(ah))
|
|
self.display.line(self.xc, self.yc, xh, yh, color)
|
|
|
|
def drawMin(self, color): # 画分针
|
|
r_min = int(self.r / 10.0 * 7)
|
|
am = math.pi * 2.0 * self.min / 60.0
|
|
xm = round(self.xc + r_min * math.sin(am))
|
|
ym = round(self.yc - r_min * math.cos(am))
|
|
self.display.line(self.xc, self.yc, xm, ym, color)
|
|
|
|
def drawSec(self, color): # 画秒针
|
|
r_sec = int(self.r / 10.0 * 9)
|
|
asec = math.pi * 2.0 * self.sec / 60.0
|
|
xs = round(self.xc + r_sec * math.sin(asec))
|
|
ys = round(self.yc - r_sec * math.cos(asec))
|
|
self.display.line(self.xc, self.yc, xs, ys, color)
|
|
|
|
def draw_clock(self, bg_color=0): # 画完整钟表
|
|
self.drawDial(self.color)
|
|
self.drawHour(self.color)
|
|
self.drawMin(self.color)
|
|
self.drawSec(self.color)
|
|
self.display.show()
|
|
self.drawHour(bg_color)
|
|
self.drawMin(bg_color)
|
|
self.drawSec(bg_color)
|
|
|
|
def clear(self, color=0): # 清除
|
|
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
|
|
|
|
'''Reclaim memory'''
|
|
gc.collect()
|