Unihiker K10 - MicroPython
Overview
CLI toolkit for Unihiker K10 board MicroPython programming. Core principle: Follow reference docs exactly—no improvisation.
When to Use
- - Uploading MicroPython code to K10
- Flashing MicroPython firmware
- Looking up K10 MicroPython APIs (screen, sensors, RGB, audio, AI)
- Port detection or connectivity issues
Commands
| Command | Description |
|---|
| INLINECODE0 | Upload MicroPython |
| INLINECODE1 |
Flash MicroPython firmware |
|
k10-micropython ports | List serial ports |
|
k10-micropython doctor | Environment diagnostic |
Coding
Basic Template
CODEBLOCK0
Important:
- - Auto-execution: Only
main.py runs automatically on boot. Other filenames (e.g., test.py) must be imported or run via REPL - Best practice: Name your entry file as
main.py for auto-start - Reference:
references/micropython-api.md
Common Issues
| Issue | Solution |
|---|
| MicroPython code doesn't run | Only main.py runs automatically. Rename your file or use REPL to run it |
| Flash failed |
Make sure BOOT button is held when connecting USB to enter download mode |
|
mpremote: could not enter raw repl | K10 is running Arduino, flash MicroPython firmware first |
| Port not found |
k10-micropython ports or hold BOOT while connecting |
|
AI + WiFi conflict | Use only one in V0.9.2 |
|
Windows PowerShell执行策略限制 | 运行
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser |
开发经验教训
2025-03-22 实践总结
与Arduino模式的区别:
- 1. 固件互斥: K10不能同时运行Arduino和MicroPython固件,需要刷写对应固件
- 入口文件: MicroPython只有
main.py会自动运行,其他文件需要手动import - 工具链不同: Arduino使用arduino-cli,MicroPython使用mpremote
刷写MicroPython固件步骤:
- 1. 按住BOOT按钮
- 按RST按钮重置
- 释放BOOT按钮
- 运行刷写命令
- 等待完成(30-60秒)
- 按RST重启
注意事项:
- - V0.9.2固件中AI功能和WiFi不能同时使用,会导致内存溢出
- 首次刷写后建议先上传简单的main.py测试
Files
CODEBLOCK1
Manual usage without CLI:
CODEBLOCK2
MicroPython Code Execution
- - Automatic execution: Files named
main.py run automatically after upload and reset - Manual execution: Other filenames require REPL interaction:
CODEBLOCK3
File naming best practice:
CODEBLOCK4
Flashing MicroPython Firmware
Method 1: Manual (Recommended)
- 1. Hold BOOT button on K10
- Press RST button on K10
- Release BOOT button
- Run:
k10-micropython flash-mp or INLINECODE14 - Wait for flash to complete (30-60 seconds)
- Press RST button on K10 to restart
- Upload Python code with INLINECODE15
Method 2: Interactive
- 1. Run: INLINECODE16
- Follow on-screen prompts
- Hold BOOT button, connect USB, release BOOT
Quick Development Workflow
CODEBLOCK5
Key Features
Screen:
- - Screen initialization and direction control
- Text drawing with custom font size and color
- Shape drawing: lines, circles, rectangles, points
- Image display from TF card
- QR code generation and display
Sensors:
- - Buttons A/B (callback and status check)
- Accelerometer (X, Y, Z axes)
- Temperature & humidity (AHT20)
- Light sensor (ALS)
- Microphone (recording to TF card)
RGB LED Control:
- - Individual LED control (0, 1, 2)
- All LEDs control (-1)
- Brightness control (0-9)
Audio:
- - Buzzer control (playTone)
- Microphone recording to TF card
AI Features (V0.9.2):
- - Face Detection: Detect faces, show length, width, center coordinates
- Face Recognition: Enroll faces, recognize faces, display ID
- Cat Recognition: Detect and classify cats (with TF card images)
- Movement Detection: Motion detection with customizable threshold
- QR Code Scanning: Scan QR codes and display content
- Speech Recognition: Wake-up command and voice commands
Note on AI:
- - AI functionality is resource-intensive in V0.9.2 firmware
- AI + WiFi conflict: Use only one at a time to avoid memory overflow
Example: Face Recognition with LED Feedback
CODEBLOCK6
Features:
- - Face Enroll (A button):
ai.send_face_cmd(1) - Green LED - Face Recognition (automatic): Display ID, Green LED
- Unknown face: Red LED
- Delete all faces (B button):
ai.send_face_cmd(3) - Clear stored faces - Camera display: Show real-time camera feed on screen
Unihiker K10 - MicroPython
概述
Unihiker K10 板 MicroPython 编程的 CLI 工具包。核心原则:严格遵循参考文档——不进行任何即兴发挥。
使用场景
- - 向 K10 上传 MicroPython 代码
- 刷写 MicroPython 固件
- 查询 K10 MicroPython API(屏幕、传感器、RGB、音频、AI)
- 端口检测或连接问题
命令
| 命令 | 描述 |
|---|
| k10-micropython upload-mp <file.py> | 上传 MicroPython |
| k10-micropython flash-mp |
刷写 MicroPython 固件 |
| k10-micropython ports | 列出串行端口 |
| k10-micropython doctor | 环境诊断 |
编码
基本模板
python
from unihiker_k10 import screen
screen.init(dir=2)
screen.drawtext(text=Hello, x=10, y=0, fontsize=24, color=0xFF0000)
screen.show_draw()
重要提示:
常见问题
| 问题 | 解决方案 |
|---|
| MicroPython 代码不运行 | 只有 main.py 会自动运行。重命名文件或使用 REPL 运行 |
| 刷写失败 |
确保连接 USB 时按住 BOOT 按钮以进入下载模式 |
|
mpremote: 无法进入原始 REPL | K10 正在运行 Arduino,请先刷写 MicroPython 固件 |
| 未找到端口 | 运行 k10-micropython ports 或在连接时按住 BOOT |
|
AI + WiFi 冲突 | 在 V0.9.2 版本中只能使用其中一个 |
|
Windows PowerShell执行策略限制 | 运行 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser |
开发经验教训
2025-03-22 实践总结
与Arduino模式的区别:
- 1. 固件互斥: K10不能同时运行Arduino和MicroPython固件,需要刷写对应固件
- 入口文件: MicroPython只有main.py会自动运行,其他文件需要手动import
- 工具链不同: Arduino使用arduino-cli,MicroPython使用mpremote
刷写MicroPython固件步骤:
- 1. 按住BOOT按钮
- 按RST按钮重置
- 释放BOOT按钮
- 运行刷写命令
- 等待完成(30-60秒)
- 按RST重启
注意事项:
- - V0.9.2固件中AI功能和WiFi不能同时使用,会导致内存溢出
- 首次刷写后建议先上传简单的main.py测试
文件
unihiker-k10-micropython/
├── SKILL.md # 本文件
└── references/ # MicroPython API 文档
└── micropython-api.md # MicroPython API 参考
不使用 CLI 的手动操作:
bash
上传 MicroPython
bash path/to/unihiker-k10-micropython/scripts/upload-micropython.sh main.py /dev/cu.usbmodem2201
刷写 MicroPython 固件
bash path/to/unihiker-k10-micropython/scripts/flash-micropython.sh /dev/cu.usbmodem2201
MicroPython 代码执行
- - 自动执行:名为 main.py 的文件在上传和重置后自动运行
- 手动执行:其他文件名需要 REPL 交互:
bash
# 连接到 REPL
mpremote connect /dev/cu.usbmodem2201 repl
# 导入并运行您的模块
>>> import test
文件命名最佳实践:
your_project/
├── main.py # 入口点 - 启动时自动运行
├── test.py # 测试文件 - 必须通过 REPL 导入
└── heart.py # 其他文件 - 使用 import heart 导入
刷写 MicroPython 固件
方法 1:手动(推荐)
- 1. 按住 K10 上的 BOOT 按钮
- 按下 K10 上的 RST 按钮
- 释放 BOOT 按钮
- 运行:k10-micropython flash-mp 或 k10-micropython flash-mp --port /dev/cu.usbmodem2201
- 等待刷写完成(30-60 秒)
- 按下 K10 上的 RST 按钮重启
- 使用 k10-micropython upload-mp file.py 上传 Python 代码
方法 2:交互式
- 1. 运行:k10-micropython flash-mp
- 按照屏幕提示操作
- 按住 BOOT 按钮,连接 USB,释放 BOOT
快速开发工作流程
bash
1. 创建 MicroPython 脚本
echo from unihiker_k10 import screen
screen.init(dir=2)
screen.draw
text(text=Hello K10, x=10, y=0, fontsize=24, color=0xFFFFFF)
screen.show_draw() > main.py
2. 作为 main.py 上传以实现自动运行
k10-micropython upload-mp main.py
3. 或作为 test.py 测试并通过 REPL 运行
k10-micropython upload-mp test.py
mpremote connect /dev/cu.usbmodem2201 repl
>>> import test
关键功能
屏幕:
- - 屏幕初始化和方向控制
- 自定义字体大小和颜色的文本绘制
- 形状绘制:线条、圆形、矩形、点
- 从 TF 卡显示图像
- 二维码生成和显示
传感器:
- - 按钮 A/B(回调和状态检查)
- 加速度计(X、Y、Z 轴)
- 温湿度传感器(AHT20)
- 光传感器(ALS)
- 麦克风(录制到 TF 卡)
RGB LED 控制:
- - 单个 LED 控制(0、1、2)
- 所有 LED 控制(-1)
- 亮度控制(0-9)
音频:
- - 蜂鸣器控制(playTone)
- 麦克风录制到 TF 卡
AI 功能(V0.9.2):
- - 人脸检测:检测人脸,显示长度、宽度、中心坐标
- 人脸识别:录入人脸,识别人脸,显示 ID
- 猫识别:检测和分类猫(使用 TF 卡图像)
- 移动检测:可自定义阈值的运动检测
- 二维码扫描:扫描二维码并显示内容
- 语音识别:唤醒命令和语音命令
AI 说明:
- - V0.9.2 固件中 AI 功能资源消耗较大
- AI + WiFi 冲突:一次只能使用其中一个,以避免内存溢出
示例:带 LED 反馈的人脸识别
python
import ai
from unihiker_k10 import screen, rgb, button
import time
def callback(data):
if data == 1:
screen.drawtext(text=录入中..., x=10, y=90, fontsize=24, color=0xFFFF00)
elif data >= 0:
screen.drawtext(text=f人脸ID: {data}, x=10, y=90, fontsize=24, color=0x00FF00)
screen.show_draw()
screen.init(dir=2)
screen.show_bg(color=0x000000)
screen.drawtext(text=A: 录入, x=10, y=50, fontsize=18, color=0xFFFF00)
screen.drawtext(text=B: 删除全部, x=10, y=70, fontsize=18, color=0xFF0000)
screen.drawtext(text=LED: 红色=未知, x=10, y=110, fontsize=18, color=0xFF0000)
screen.drawtext(text= 绿色=已识别, x=10, y=130, fontsize=18, color=0x00FF00)
screen.show_draw()
rgb.brightness(9)
ai.init_ai()
ai.camera_start()
ai.facerecognizestart()
ai.sendfacecmd(2) # 识别模式
ai.setasrcallback(callback)