Game:bit

来自Labplus盛思维基百科
Tangliufeng讨论 | 贡献2018年1月19日 (五) 10:52的版本 无线遥控器
跳转至: 导航搜索
游戏手柄-01.png

概述

microbit拓展游戏手柄。拥有丰富的输入按键、双轴游戏摇杆、震动马达、无源蜂鸣器。结合microbit可以有多样的玩法,增加microbit编程的趣味性!

技术参数

  • 输入电源:3V(2节7号干电池)
  • PCI插槽可插入microbit作主控
  • 拥有丰富的按键、双轴摇杆、震动马达、无源蜂鸣器

使用教程

game:bit 说明 micro:bit引脚 Python示例
双轴按键游戏摇杆 三路模拟输出,输出值分别对应(X,Y)双轴偏移量和Z轴上按下的输出模拟量800~850,松开输出模拟量1023 X轴->P2
Y轴->P1
Z按键->P0
获取x轴偏移量: pin2.read_analog()
获取z按键模拟量: pin0.read_analog()
Y/X/B/A按键 当Y按下按键输出模拟量0~50, 松开输出模拟量1023

当X按下按键输出模拟量200~250, 松开输出模拟量1023
当B按下按键输出模拟量400~450, 松开输出模拟量1023
当A按下按键输出模拟量600~650, 松开输出模拟量1023

P0 pin0.read_analog()
「START」按键 「START」按键连接microbit的BUTTON_A,功能与microbit的A按键功能相同 BUTTON_A(P5) button_a.is_pressed()==True #当「START」按键按下
「SElECT」按键 「SElECT」按键连接microbit的BUTTON_B,功能与microbit的B按键功能相同 BUTTON_B(P11) button_b.is_pressed()==True #当「SELECT」按键按下
震动马达 输入高电平,触发震动马达,低电平停止震动 P16 Pin16.write_digital(1) #震动开启
Pin16.write_digital(0) #停止震动
无源蜂鸣器 设置频率或音阶,可发出对应频率的电子声音 P8 music.pitch(1000,1000,pin8) #蜂鸣器发出1KHz,持续1秒的声音

应用示例

无线遥控器

可利用microbit的无线功能,发送game:bit的按键数据。另外一块microbit接收数据后,作出响应。达到无线遥控的功能。可应用在无线遥控手柄的场景上。

  • game:bit & micro:bit发送端程序:
from microbit import *
import radio

def getKeyVal():
    key = 0
    btVal = pin0.read_analog()
    if (btVal < 70):
        key = 1    # button Y
    elif (btVal < 270 and btVal > 130):
        key = 2    # button X
    elif (btVal < 470 and btVal > 330):
        key = 3    # button B
    elif (btVal < 670 and btVal > 530):
        key = 4    # button A
    elif (btVal < 870 and btVal > 730):
        key = 5    # rocker button
    if button_a.is_pressed():
        key = 6    # button start
    if button_b.is_pressed():
        key = 7    # button select
    return key 

radio.config(length=32, queue=3, channel=7, power=0, data_rate=radio.RATE_1MBIT)
radio.on()
while True:
    sendBuff = [0,0,0,0,0,0]
    tmp = pin2.read_analog()    # axix x
    sendBuff[0] = tmp & 0xff
    sendBuff[1] = (tmp >> 8) & 0xff
    tmp = pin1.read_analog()  # axis y
    sendBuff[2] = tmp & 0xff
    sendBuff[3] = (tmp >> 8) & 0xff
    tmp = getKeyVal()           # button val
    sendBuff[4] = tmp & 0xff
    sendBuff[5] = (tmp >> 8) & 0xff
    radio.send_bytes(bytearray(sendBuff))
    #print(sendBuff)
    sleep(50)
  • micro:bit接收端程序:
from microbit import *
import radio

radio.config(length=32, queue=3, channel=7, power=0, data_rate=radio.RATE_1MBIT)
radio.on()
while True:
    tmp = radio.receive_bytes()
    if (tmp != None):
        x = (tmp[1]<<8)|tmp[0]
        y = (tmp[3]<<8)|tmp[2]
        key = (tmp[5]<<8)|tmp[4]
        #print(x)
        #print(y)
        #print(key)