已解决-如何使用来自串行端口的输入更改micro:bit LED矩阵上的图标

问题描述 投票:0回答:1

首先,我根本不是开发人员,只是想让事情按我的意愿工作。无法理解:

从一侧看,这是我在Ubuntu PC上的python脚本,该脚本将来自Playstation 4游戏杆的按钮输入发送到micro:bit的串行端口(游戏杆通过蓝牙连接到Ubuntu):

import serial
import pygame

pygame.init()
pygame.joystick.init()

joystick = pygame.joystick.Joystick(0)
joystick.init()

screen = pygame.display.set_mode((100,100))

device = serial.Serial('/dev/ttyACM0', 115200)

try:
    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.JOYBUTTONDOWN:
                if event.button == 7:
                    device.write(b"1\r\n")
                elif event.button == 6:
                    device.write(b"2\r\n")
            elif event.type == pygame.JOYBUTTONUP:
                if joystick.get_button(7) == 1:
                    device.write(b"1\r\n")
                elif joystick.get_button(6) == 1:
                    device.write(b"2\r\n")
                elif joystick.get_axis(0) < 0:
                    device.write(b"3\r\n")
                elif joystick.get_axis(0) > 0:
                    device.write(b"4\r\n")
                else:
                    device.write(b"5\r\n")
            if event.type == pygame.JOYAXISMOTION:
                if event.axis == 0:
                    if event.value < 0:
                        device.write(b"3\r\n")
                    elif event.value > 0:
                        device.write(b"4\r\n")
                    elif event.value == 0:
                        if joystick.get_button(7) == 1:
                            device.write(b"1\r\n")
                        elif joystick.get_button(6) == 1:
                            device.write(b"2\r\n")
                        else:
                            device.write(b"5\r\n")
except KeyboardInterrupt:
    print("EXITING NOW")
    joystick.quit()
device.close()

[从另一面看,这是我从Mu编辑器中刷到micro:bit的简单的micropython代码,并期望它能正常工作,但它不是:lol:

from microbit import *

uart.init(baudrate=115200)

while True:
    joyinput = uart.read()
    if joyinput == "1":
        display.show(Image.ARROW_N)
    elif joyinput == "2":
        display.show(Image.ARROW_S)
    elif joyinput == "3":
        display.show(Image.ARROW_W)
    elif joyinput == "4":
        display.show(Image.ARROW_E)
    elif joyinput == "5":
        display.show(Image.HAPPY)

[在Mu编辑器的REPL控制台中,我可以看到通信进展顺利,即,只要我按住某个按钮或操纵杆轴移动,我就会获得具有适当编号的REPL:

  • 操纵杆按钮7(R2)的1
  • 2用于操纵杆按钮6(L2)
  • 3用于操纵杆轴0的左位置(操纵杆上的左模拟量)
  • 3用于操纵杆轴0的右位置(操纵杆上的左模拟量)
  • 5,如果没有按下/移动任何内容

但是LED矩阵上的图标永远不会出现。

到目前为止,只有在脚本末尾再添加一个“ else”语句时,我才可能显示一些图标,但这是正常的原因,因为它是“ else”。例如,下面的最后两行:

from microbit import *

uart.init(baudrate=115200)

while True:
    joyinput = uart.read()
    if joyinput == "1":
        display.show(Image.ARROW_N)
    elif joyinput == "2":
        display.show(Image.ARROW_S)
    elif joyinput == "3":
        display.show(Image.ARROW_W)
    elif joyinput == "4":
        display.show(Image.ARROW_E)
    elif joyinput == "5":
        display.show(Image.HAPPY)
    else:
        display.show(Image.HAPPY)

enter image description here

非常感谢,我很乐意提供所需的任何其他信息。

python pyserial joystick micropython bbc-microbit
1个回答
0
投票

我在另一个网站上获得了答案:https://forum.micropython.org/viewtopic.php?f=2&t=8153,所以我想在这里发布它,也许对其他有同样疑问的人会有所帮助。所有积分都归于micropyhton论坛上的@jimmo,也要感谢@Iyassou试图提供帮助。

下面是用于micro:bit的mycropython代码,它将根据串行输入更改LED矩阵上的图标(在我的情况下为箭头)。当然,您需要以某种方式发送这些输入,我将在上面的文章和PS4游戏杆中使用python脚本发送它。

from microbit import *
import time

uart.init(baudrate=115200)

while True:
    joyinput = uart.readline()
    if not joyinput:
        continue
    joyinput = str(joyinput.strip(), 'utf-8')
    if joyinput == "1":
        display.show(Image.ARROW_N)
    elif joyinput == "2":
        display.show(Image.ARROW_S)
    elif joyinput == "3":
        display.show(Image.ARROW_W)
    elif joyinput == "4":
        display.show(Image.ARROW_E)
    elif joyinput == "5":
        display.show(Image.HAPPY)
© www.soinside.com 2019 - 2024. All rights reserved.