build(boards): xpython板卡执行 npm run build:prod

This commit is contained in:
王立帮
2025-12-14 21:18:41 +08:00
parent 26bae5da89
commit 0ff5878d5f
416 changed files with 12404 additions and 48341 deletions

View File

@@ -5,43 +5,113 @@ MicroPython library for the Camera(Inherit C module)
=======================================================
@dahanzimin From the Mixly Team
"""
import time
import base64
import jpeg
import time, gc
import urequests
from _camera import *
from jpeg import Encoder
from base64 import b64encode
from machine import SoftI2C, Pin
from mixgo_sant import onboard_bot
from jpeg import Encoder, Decoder
class IMG:
def __init__(self, image, width, height):
self.image = image
self.width = width
self.height = height
self.format = "RGB565"
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, skip_frame=3, hmirror=False, vflip=False, **kwargs):
from mixgo_sant import onboard_bot
onboard_bot.cam_reset(1, 0)
onboard_bot.cam_en(1, 150)
super().__init__(frame_size=frame_size, pixel_format=pixel_format, **kwargs)
self.set_hmirror(not hmirror)
time.sleep_ms(50)
time.sleep_ms(150)
self.set_vflip(not vflip)
time.sleep_ms(50)
SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) # 恢复I2C
for _ in range(skip_frame):
super().capture()
def deinit(self):
super().deinit()
gc.collect()
onboard_bot.cam_reset(0, 0)
onboard_bot.cam_en(0, 100)
def snapshot(self, path=None, formats=0, quality=90, rotation=0):
if formats == 0 and path is None:
def snapshot(self, path=None, quality=90, rotation=0):
if path is None:
return self.capture()
else:
_encoder = Encoder(pixel_format="RGB565_BE", quality=quality, rotation=rotation, width=self.get_pixel_width(), height=self.get_pixel_height())
_jpeg = _encoder.encode(self.capture())
del _encoder
if path is None:
if formats == 1:
return _jpeg
else:
return b'data:image/jpg;base64,' + base64.b64encode(_jpeg)
Image.save(self.capture(), path, quality=quality, rotation=rotation)
def capture(self):
return IMG(super().capture(), self.get_pixel_width(), self.get_pixel_height())
class Image:
def save(self, img, path="mixly.jpg", **kwargs):
'''quality(1-100), rotation (0, 90, 180, 270)'''
_encoder = Encoder(pixel_format="RGB565_BE", width=img.width, height=img.height, **kwargs)
_jpeg = _encoder.encode(img.image)
del _encoder
gc.collect()
if isinstance(path, str):
with open(path, 'wb') as f:
f.write(_jpeg)
else:
return _jpeg
def open(self, path="mixly.jpg", scale_width=None, scale_height=None, tft_width=240, tft_height=240, **kwargs):
'''rotation (0, 90, 180, 270), clipper_width, clipper_height'''
with open(path, "rb") as f:
_jpeg = f.read()
return self._jpg_decoder(_jpeg, scale_width, scale_height, tft_width, tft_height, **kwargs)
def convert(self, img, formats=0, **kwargs):
if formats == 0:
return self.save(img, None, **kwargs)
elif formats == 1:
return b'data:image/jpg;base64,' + b64encode(self.save(img, None, **kwargs))
def download(self, url, path=None, scale_width=None, scale_height=None, tft_width=240, tft_height=240, block=1024, **kwargs):
'''rotation (0, 90, 180, 270), clipper_width, clipper_height'''
response = urequests.get(url, stream=True)
if path is None:
_image = self._jpg_decoder(response.raw.read(), scale_width, scale_height, tft_width, tft_height, **kwargs)
response.close()
return _image
else:
with open(path, 'wb') as f:
while True:
_data = response.raw.read(block)
if not _data:
break
else:
f.write(_data)
response.close()
def _jpg_decoder(self, jpg, scale_width, scale_height, tft_width, tft_height, **kwargs):
'''Automatically zoom based on the screen'''
if scale_width is None or scale_height is None:
_width = tft_width
_height = tft_height
for i in range(min(len(jpg), 1024)):
if jpg[i] == 0xFF and (jpg[i + 1] & 0xF0) == 0xC0:
if jpg[i + 1] not in [0xC4, 0xC8, 0xCC]:
_width = jpg[i + 7] << 8 | jpg[i + 8]
_height = jpg[i + 5] << 8| jpg[i + 6]
break
if _width > tft_width or _height > tft_height:
_scale = max(_width / tft_width, _height / tft_height) * 8
_decoder = Decoder(pixel_format="RGB565_BE", scale_width=round(_width / _scale) * 8, scale_height=round(_height / _scale) * 8, **kwargs)
else:
with open(path, 'wb') as f:
f.write(_jpeg)
return True
_decoder = Decoder(pixel_format="RGB565_BE", **kwargs)
else:
_decoder = Decoder(pixel_format="RGB565_BE", scale_width=scale_width // 8 * 8, scale_height=scale_height // 8 * 8, **kwargs)
_info = _decoder.get_img_info(jpg)
_image = IMG(_decoder.decode(jpg), _info[0], _info[1])
del _decoder, jpg
gc.collect()
return _image
#图像处理
Image = Image()

View File

@@ -9,9 +9,54 @@ from espdl import *
def analyze(results, keys=None, num=0):
if keys is None:
return results is not None
return True if results else False
if results:
if keys == "len":
return len(results)
else:
return results[num][keys]
#简单处理模型运行结果
_onboard_tft = None
def simple_run(molde, camera, keys="len", keyx=None, num=0, color=0xF800, size=2, sync=True):
global _onboard_tft
if _onboard_tft is None:
from mixgo_sant import onboard_tft
_onboard_tft = onboard_tft
if sync: _onboard_tft.fill(0, sync=False)
_img = camera.capture()
_onboard_tft.display(_img, sync=False)
_result = molde.run(_img.image)
_data = None
if _result:
_x_of = (camera.get_pixel_width() - _onboard_tft.width) // 2
_y_of = (camera.get_pixel_height() - _onboard_tft.height) // 2
for r in _result:
x = 0
y = 0
if r['box']:
_onboard_tft.rect(r['box'][0] - _x_of, r['box'][1] - _y_of, r['box'][2], r['box'][3], color, sync=False)
x = r['box'][0]
y = r['box'][1] + r['box'][3]
if "person" in r:
_onboard_tft.shows(r['person']['name'], x=x - _x_of, y=y - _y_of, size=size, center=0, color=color, sync=False)
else:
if r['box']:
_onboard_tft.shows(r['data'], x=x - _x_of, y=y - _y_of, size=size, center=0, color=color, sync=False)
else:
_onboard_tft.shows(r['data'], y=0, size=size, center=True, color=color, sync=False)
break
if keys == "len":
_data = len(_result)
else:
_data = _result[num][keys] if keyx is None else _result[num][keys][keyx]
if sync: _onboard_tft.show()
return _data

View File

@@ -2,15 +2,16 @@
"camera": {
"__require__": [
"time",
"base64",
"jpeg",
"gc",
"urequests",
"_camera",
"jpeg",
"base64",
"machine",
"jpeg",
"mixgo_sant"
],
"__file__": true,
"__size__": 1617,
"__size__": 4782,
"__name__": "camera.py"
},
"ci1302x": {
@@ -32,10 +33,11 @@
},
"esp_dl": {
"__require__": [
"espdl"
"espdl",
"mixgo_sant"
],
"__file__": true,
"__size__": 417,
"__size__": 2060,
"__name__": "esp_dl.py"
},
"mixgo_nova": {
@@ -77,13 +79,13 @@
},
"mixgo_sant": {
"__require__": [
"gc",
"time",
"math",
"machine",
"music",
"ws2812x",
"machine",
"time",
"gc",
"st7789_cf",
"math",
"sant_bot",
"sc7a20",
"ltr553als",
@@ -92,7 +94,7 @@
"ci1302x"
],
"__file__": true,
"__size__": 7403,
"__size__": 7375,
"__name__": "mixgo_sant.py"
},
"mixgo_soar": {
@@ -110,7 +112,7 @@
"mmc5603"
],
"__file__": true,
"__size__": 6805,
"__size__": 6752,
"__name__": "mixgo_soar.py"
},
"mixgo_soar_voice": {
@@ -177,6 +179,18 @@
"__size__": 731,
"__name__": "sant_gx.py"
},
"sant_tts": {
"__require__": [
"gc",
"esp_tts",
"machine",
"pwm_audio",
"mixgo_sant"
],
"__file__": true,
"__size__": 747,
"__name__": "sant_tts.py"
},
"soar_bot": {
"__require__": [
"time",
@@ -190,11 +204,10 @@
"__require__": [
"time",
"uframebuf",
"machine",
"micropython"
"machine"
],
"__file__": true,
"__size__": 4455,
"__size__": 3711,
"__name__": "st7789_bf.py"
},
"st7789_cf": {
@@ -202,11 +215,10 @@
"time",
"uframebuf",
"machine",
"jpeg",
"micropython"
"camera"
],
"__file__": true,
"__size__": 3953,
"__size__": 3595,
"__name__": "st7789_cf.py"
},
"ws2812x": {

View File

@@ -1,18 +1,16 @@
"""
mixgo_sant Onboard resources(v1.9)
mixgo_sant Onboard resources(v2.0)
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
from machine import *
import time
import gc
import st7789_cf
import math
'''RTC'''
rtc_clock = RTC()
@@ -33,6 +31,7 @@ except Exception as e:
print("Warning: Failed to communicate with BOT035 (Coprocessor) or", e)
'''TFT/240*240'''
import st7789_cf
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'''
@@ -83,8 +82,6 @@ onboard_rgb = NeoPixel(onboard_bot.rgb_sync, 4)
onboard_music = MIDI(46, pa_ctrl=onboard_bot.spk_en)
'''5KEY_Sensor'''
class KEYSensor:
def __init__(self, pin, range):
self.pin = pin
@@ -122,10 +119,7 @@ class KEYSensor:
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
@@ -135,7 +129,6 @@ class Button(KEYSensor):
def _value(self):
return not self.key.value()
B1key = Button(0)
B2key = KEYSensor(17, 0)
A1key = KEYSensor(17, 1600)
@@ -144,8 +137,6 @@ A3key = KEYSensor(17, 550)
A4key = KEYSensor(17, 2100)
'''2-LED'''
class LED:
def __init__(self, func):
self._func = func
@@ -168,10 +159,8 @@ class LED:
def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False
onboard_led = LED(onboard_bot.led_pwm)
class Voice_Energy:
def read(self, samples=10):
values = []
@@ -180,10 +169,8 @@ class Voice_Energy:
0x08, 3)[:2], 'little')) # 在语音识别里获取
return sorted(values)[samples // 2]
onboard_sound = Voice_Energy()
class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
self.display = oled
@@ -254,6 +241,5 @@ class Clock:
def clear(self, color=0): # 清除
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
'''Reclaim memory'''
gc.collect()

View File

@@ -59,14 +59,12 @@ except Exception as e:
print("Warning: Failed to communicate with MMC5603 (MGS) or", e)
'''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.6, font_address=0x700000)
'''2RGB_WS2812'''
onboard_rgb = NeoPixel(Pin(40), 4)
'''5KEY_Sensor'''
class KEYSensor:
def __init__(self, pin, range):
self.pin = pin
@@ -103,10 +101,7 @@ class KEYSensor:
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
@@ -116,7 +111,6 @@ class Button(KEYSensor):
def _value(self):
return not self.key.value()
B1key = Button(0)
B2key = KEYSensor(17, 0)
A1key = KEYSensor(17, 2300)
@@ -125,8 +119,6 @@ A3key = KEYSensor(17, 800)
A4key = KEYSensor(17, 2900)
'''2LED-Independent'''
class LED:
def __init__(self, pins=[]):
self._pins = [PWM(Pin(pin), duty_u16=0) for pin in pins]
@@ -153,10 +145,8 @@ class LED:
def getonoff(self, index):
return True if self.getbrightness(index) > 50 else False
onboard_led = LED([38, 39])
class Clock:
def __init__(self, x, y, radius, color, oled=onboard_tft): # 定义时钟中心点和半径
self.display = oled
@@ -227,7 +217,5 @@ class Clock:
def clear(self, color=0): # 清除
self.display.ellipse(self.xc, self.yc, self.r, self.r, color, True)
'''Reclaim memory'''
gc.collect()
onboard_tft.set_brightness(0.6)

View File

@@ -0,0 +1,31 @@
"""
SANT-TTS
MicroPython library for the SANT-TTS(暂行)
=======================================================
@dahanzimin From the Mixly Team
"""
import gc
from esp_tts import TTS
from machine import Pin
from pwm_audio import PWMAudio
from mixgo_sant import onboard_bot
audio = PWMAudio(Pin(46))
tts = TTS()
def play(text, speed=3):
try:
onboard_bot.spk_en(1, 100)
if tts.parse_chinese(text):
audio.start()
while True:
data = tts.stream_play(speed)
if not data:
break
else:
audio.write(data)
finally:
onboard_bot.spk_en(0)
audio.stop()
gc.collect()

View File

@@ -3,32 +3,11 @@ ST7789/FrameBuffer
MicroPython library for the ST7789(TFT-SPI)
=======================================================
#Preliminary composition 20240110
@dahanzimin From the Mixly Team
"""
import time
import uframebuf
from machine import Pin, PWM
from micropython import const
_CMD_SWRESET = const(0x01)
_CMD_SLPIN = const(0x10)
_CMD_SLPOUT = const(0x11)
_CMD_PTLON = const(0x12)
_CMD_NORON = const(0x13)
_CMD_INVOFF = const(0x20)
_CMD_INVON = const(0x21)
_CMD_DISPOFF = const(0x28)
_CMD_DISPON = const(0x29)
_CMD_CASET = const(0x2A)
_CMD_RASET = const(0x2B)
_CMD_RAMWR = const(0x2C)
_CMD_RAMRD = const(0x2E)
_CMD_PTLAR = const(0x30)
_CMD_VSCRDEF = const(0x33)
_CMD_COLMOD = const(0x3A)
_CMD_MADCTL = const(0x36)
class ST7789(uframebuf.FrameBuffer_Uincode):
def __init__(self, spi, width, height, dc_pin=None, cs_pin=None, bl_pin=None, brightness=0.6, font_address=0x700000):
@@ -39,15 +18,10 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
super().__init__(self._buffer, width, height, uframebuf.RGB565)
self.font(font_address)
self._init()
self.show()
if brightness > 0:
time.sleep_ms(100)
# self.show()
self.bl_led = bl_pin if callable(bl_pin) else PWM(Pin(bl_pin))
self._oneclight = True
self._brightness = brightness
if callable(bl_pin):
self.bl_led = bl_pin
else:
self.bl_led = PWM(Pin(bl_pin), duty_u16=int(
self._brightness * 60000)) if bl_pin else None
def _write(self, cmd, dat=None):
self.cs.off()
@@ -63,26 +37,24 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def _init(self):
"""Display initialization configuration"""
for cmd, data, delay in [
# (_CMD_SWRESET, None, 20000),
(_CMD_SLPOUT, None, 120000),
(_CMD_MADCTL, b'\x00', 50),
(_CMD_COLMOD, b'\x05', 50),
(0xB2, b'\x0c\x0c\x00\x33\x33', 10),
(0xB7, b'\x35', 10),
(0xBB, b'\x19', 10),
(0x11, None, 120000),
(0x36, b'\x00', 10),
(0x3A, b'\x05', 10),
(0xB2, b'\x1F\x1F\x00\x33\x33', 10),
(0xB7, b'\x00', 10),
(0xBB, b'\x3F', 10),
(0xC0, b'\x2C', 10),
(0xC2, b'\x01', 10),
(0xC3, b'\x12', 10),
(0xC3, b'\x0F', 10),
(0xC4, b'\x20', 10),
(0xC6, b'\x0F', 10),
(0xC6, b'\x13', 10),
(0xD0, b'\xA4\xA1', 10),
(0xE0, b'\xD0\x04\x0D\x11\x13\x2B\x3F\x54\x4C\x18\x0D\x0B\x1F\x23', 10),
(0xE1, b'\xD0\x04\x0C\x11\x13\x2C\x3F\x44\x51\x2F\x1F\x1F\x20\x23', 10),
(0xD6, b'\xA1', 10),
(0xE0, b'\xF0\x06\x0D\x0B\x0A\x07\x2E\x43\x45\x38\x14\x13\x25\x29', 10),
(0xE1, b'\xF0\x07\x0A\x08\x07\x23\x2E\x33\x44\x3A\x16\x17\x26\x2C', 10),
(0xE4, b'\x1D\x00\x00', 10),
(0x21, None, 10),
(0x29, None, 10),
# (_CMD_INVOFF, None, 10),
# (_CMD_NORON, None, 10),
# (_CMD_DISPON, None, 200),
]:
self._write(cmd, data)
if delay:
@@ -112,7 +84,7 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
if callable(self.bl_led):
self.bl_led(brightness * 100)
elif isinstance(self.bl_led, PWM):
self.bl_led.duty_u16(int(brightness*60000))
self.bl_led.duty_u16(int(brightness * 60000))
def color(self, red, green=None, blue=None):
""" Convert red, green and blue values (0-255) into a 16-bit 565 encoding."""
@@ -123,6 +95,9 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def show(self):
"""Refresh the display and show the changes."""
self._write(_CMD_CASET, b'\x00\x00\x01\x3f')
self._write(_CMD_RASET, b'\x00\x00\x00\xef')
self._write(_CMD_RAMWR, self._buffer)
if self._oneclight:
self.set_brightness(self._brightness)
self._oneclight = False
self._write(0x2A, b'\x00\x00\x00\xef')
self._write(0x2B, b'\x00\x00\x00\xef')
self._write(0x2C, self._buffer)

View File

@@ -8,26 +8,7 @@ MicroPython library for the ST7789(TFT-SPI)
import time
import uframebuf
from machine import Pin
from jpeg import Decoder
from micropython import const
_CMD_SWRESET = const(0x01)
_CMD_SLPIN = const(0x10)
_CMD_SLPOUT = const(0x11)
_CMD_PTLON = const(0x12)
_CMD_NORON = const(0x13)
_CMD_INVOFF = const(0x20)
_CMD_INVON = const(0x21)
_CMD_DISPOFF = const(0x28)
_CMD_DISPON = const(0x29)
_CMD_CASET = const(0x2A)
_CMD_RASET = const(0x2B)
_CMD_RAMWR = const(0x2C)
_CMD_RAMRD = const(0x2E)
_CMD_PTLAR = const(0x30)
_CMD_VSCRDEF = const(0x33)
_CMD_COLMOD = const(0x3A)
_CMD_MADCTL = const(0x36)
from camera import Image, IMG
class ST7789(uframebuf.FrameBuffer_Uincode):
def __init__(self, spi, width, height, dc_pin=None, backlight=None, reset=None, font_address=0x700000):
@@ -35,25 +16,25 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
self.dc = Pin(dc_pin, Pin.OUT, value=1)
self._buffer = bytearray(width * height * 2)
super().__init__(self._buffer, width, height, uframebuf.RGB565)
reset(1, 100)
if reset: reset(1, 100)
self.font(font_address)
self._init()
# self.show()
self._oneclight = True
self._backlight = backlight
def display(self, data=None, rotation=0, sync=True):
def display(self, data=None, x=None, y=None, scale_width=None, scale_height=None, rotation=0, sync=True):
if type(data) is str:
with open(data, "rb") as f:
_jpeg = f.read()
_decoder = Decoder(pixel_format="RGB565_BE", rotation=rotation)
self._buffer[:] = _decoder.decode(_jpeg)
del _decoder
data = Image.open(data, scale_width, scale_height, self.width, self.height, rotation=rotation)
if sync: self.fill(0x0, sync=False)
self.blit_rgb565(data.image, data.width, data.height, x, y)
if sync: self.show()
def screenshot(self, x=0, y=0, w=None, h=None):
if (w is None and h is None):
return IMG(memoryview(self._buffer), self.width, self.height)
else:
self._buffer[:] = data # 后期做图像大小处理
if sync:
self.show()
return self._buffer
return IMG(memoryview(self.crop_rgb565(x,y,w,h)), w, h)
def _write(self, cmd, dat=None):
self.dc.off()
@@ -65,23 +46,24 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
def _init(self):
"""Display initialization configuration"""
for cmd, data, delay in [
# (_CMD_SWRESET, None, 20000),
(_CMD_SLPOUT, None, 120000),
(_CMD_MADCTL, b'\x00', 50),
(_CMD_COLMOD, b'\x05', 50),
(0xB2, b'\x0c\x0c\x00\x33\x33', 10),
(0xB7, b'\x35', 10),
(0xBB, b'\x19', 10),
(0x11, None, 120000),
(0x36, b'\x00', 50),
(0x3A, b'\x05', 50),
(0xB2, b'\x1F\x1F\x00\x33\x33', 10),
(0xB7, b'\x00', 10),
(0xBB, b'\x36', 10),
(0xC0, b'\x2C', 10),
(0xC2, b'\x01', 10),
(0xC3, b'\x12', 10),
(0xC3, b'\x13', 10),
(0xC4, b'\x20', 10),
(0xC6, b'\x0F', 10),
(0xC6, b'\x13', 10),
(0xD0, b'\xA4\xA1', 10),
(0xE0, b'\xD0\x04\x0D\x11\x13\x2B\x3F\x54\x4C\x18\x0D\x0B\x1F\x23', 10),
(0xE1, b'\xD0\x04\x0C\x11\x13\x2C\x3F\x44\x51\x2F\x1F\x1F\x20\x23', 10),
(_CMD_INVON, None, 10),
(_CMD_DISPON, None, 10),
(0xD6, b'\xA1', 10),
(0xE0, b'\xF0\x08\x0E\x09\x08\x04\x2F\x33\x45\x36\x13\x12\x2A\x2D', 10),
(0xE1, b'\xF0\x0E\x12\x0C\x0A\x15\x2E\x32\x44\x39\x17\x18\x2B\x2F', 10),
(0xE4, b'\x1D\x00\x00', 10),
(0x21, None, 10),
(0x29, None, 10),
]:
self._write(cmd, data)
if delay:
@@ -108,6 +90,6 @@ class ST7789(uframebuf.FrameBuffer_Uincode):
if self._oneclight:
self.set_brightness(0.6)
self._oneclight = False
self._write(_CMD_CASET, b'\x00\x00\x00\xef')
self._write(_CMD_RASET, b'\x00\x00\x00\xef')
self._write(_CMD_RAMWR, self._buffer)
self._write(0x2A, b'\x00\x00\x00\xef')
self._write(0x2B, b'\x00\x00\x00\xef')
self._write(0x2C, self._buffer)