知识点:什么是掌控板?
掌控板是一块普及STEAM创客教育、人工智能教育、机器人编程教育的开源智能硬件。它集成ESP-32高性能双核芯片,支持WiFi和蓝牙双模通信,可作为物联网节点,实现物联网应用。同时掌控板上集成了OLED显示屏、RGB灯、加速度计、麦克风、光线传感器、蜂鸣器、按键开关、触摸开关、金手指外部拓展接口,支持图形化及MicroPython代码编程,可实现智能机器人、创客智造作品等智能控制类应用。
掌控板硬件特性:
ESP-32主控
处理器:Tensilica LX6双核处理器(一核处理高速连接;一核独立应用开发)
主频:高达240MHz的时钟频率
SRAM:520KB
Flash:8MB
Wi-Fi标准:FCC/CE/TELEC/KCC
Wi-Fi协议:802.11 b/g/n/d/e/i/k/r (802.11n,速度高达150 Mbps),A-MPDU和A-MSDU聚合,支持0.4us防护间隔
频率范围:2.4~2.5 GHz
蓝牙协议:符合蓝牙v4.2 BR/EDR和BLE标准
蓝牙音频:CVSD和SBC音频低功耗:10uA
供电方式:Micro USB供电
工作电压:3.3V
最大工作电流:200mA
最大负载电流:1000mA
掌控板载
三轴加速度计MSA300,测量范围:±2/4/8/16G
地磁传感器MMC5983MA,测量范围:±8 Gauss;精度0.4mGz,电子罗盘误差±0.5°
光线传感器
麦克风
3 颗全彩ws2812灯珠
1.3英寸OLED显示屏,支持16*16字符显示,分辨率128x64
无源蜂鸣器
支持2个物理按键(A/B)、6个触摸按键
支持1路鳄鱼夹接口,可方便接入各种阻性传感器
拓展接口
20通道数字I/O, (其中支持12路PWM,6路触摸输入)
5通道12bit模拟输入ADC,P0~P4
1路的外部输入鳄鱼夹接口:EXT/GND
支持I2C、UART、SPI通讯协议
10、使用“摇晃”指令的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#使用“摇晃”指令的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#使用“摇晃”指令的计步器
from mpython import *
import framebuf
import font.digiface_30
import time
def on_button_a_down(_):
global a, b
time.sleep_ms(10)
if button_a.value() == 1: return
a = 1
def on_button_b_down(_):
global a, b
time.sleep_ms(10)
if button_b.value() == 1: return
a = 0
from machine import Timer
_is_shaked = _is_thrown = False
_last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0
def on_shaked():pass
def on_thrown():pass
tim11 = Timer(11)
def timer11_tick(_):
global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown
if _is_shaked:
_count_shaked += 1
if _count_shaked == 5: _count_shaked = 0
if _is_thrown:
_count_thrown += 1
if _count_thrown == 10: _count_thrown = 0
if _count_thrown > 0: return
x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()
_is_thrown = (x * x + y * y + z * z < 0.25)
if _is_thrown: on_thrown();return
if _last_x == 0 and _last_y == 0 and _last_z == 0:
_last_x = x; _last_y = y; _last_z = z; return
diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z
_last_x = x; _last_y = y; _last_z = z
if _count_shaked > 0: return
_is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)
if _is_shaked: on_shaked()
tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)
def on_shaked():
global a, b
if a == 1:
b = b + 1
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d[2]
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
a = 0
b = 0
while True:
if a == 0:
oled.fill(0)
oled.DispChar('计步器', 0, 0, 1)
oled.DispChar('每天1万步,活出好身体', 0, 16, 1)
oled.DispChar('按下A键开始计步', 0, 32, 1)
oled.DispChar('按下B键步数清零', 0, 48, 1)
oled.show()
b = 0
elif a == 1:
oled.fill(0)
oled.DispChar('步数', 0, 15, 1)
display_font(font.digiface_30, (str(b)), 30, 10, False, 2)
oled.show()
if b <= 10000:
oled.fill(0)
oled.DispChar((''.join([str(x) for x in ['还差', 10000 - b, '加油加油!']])), 0, 48, 1)
oled.show()
else:
oled.fill(0)
oled.DispChar('目标完成,你真棒!', 0, 48, 1)
oled.show()
time.sleep(1)
mPython 图形编程
11、可以调节感应灵敏度的计步器
调节变量 j 的条件阕值,即可灵活调整计步器的感应灵敏度,以适应不同情况。
#MicroPython动手做(20)——掌控板之三轴加速度
#可以调节感应灵敏度的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#可以调节感应灵敏度的计步器
import math
from mpython import *
import time
import framebuf
import font.digiface_it_42
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d[2]
k = 0
while True:
j = math.sqrt(accelerometer.get_x() * accelerometer.get_x() + (accelerometer.get_y() * accelerometer.get_y() + accelerometer.get_z() * accelerometer.get_z()))
if j >= 1.2:
k = k + 1
rgb[1] = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
display_font(font.digiface_it_42, (str(k)), 4, 10, False, 2)
oled.DispChar('步', 113, 48, 1)
oled.show()
12、四方向动态体感灯
#MicroPython动手做(20)——掌控板之三轴加速度
#四方向动态体感灯
#MicroPython动手做(20)——掌控板之三轴加速度
#四方向动态体感灯
from mpython import *
import time
while True:
oled.fill(0)
oled.DispChar("掌控体感灯", 36, 32, 1)
oled.show()
if accelerometer.get_x() < -0.3:
oled.fill(0)
oled.DispChar("向前倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(153), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_x() > 0.3:
oled.fill(0)
oled.DispChar("向后倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(51), int(51), int(255)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() > 0.3:
oled.fill(0)
oled.DispChar("向左倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(0), int(153), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() < -0.3:
oled.fill(0)
oled.DispChar("向右倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(204), int(153), int(51)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
mPython X 图形编程