首先,我根本不是一个开发人员,我只是想让事情像我想要的那样运作。无法理解这一点:
一方面,这是我在Ubuntu PC上的python脚本,它将按钮输入从Playstation 4操纵杆发送到微: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编辑器中闪现到micropython的简单的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:
H 1115(如果没有任何东西)
但是LED矩阵上的图标永远不会出现。
到目前为止,我可以显示一些图标,除非我在脚本末尾再添加一条“one”语句,但这是正常的,因为它是“one”。例如,最后两行如下:
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)
非常感谢事先,我将很高兴提供任何额外的信息需要。
发布于 2020-04-14 06:29:28
我在另一个网站上得到了答案:https://forum.micropython.org/viewtopic.php?f=2&t=8153,所以我想在这里发布它,也许它会对其他有同样疑问的人有所帮助。所有的学分都在micropyhton论坛上的“jimmo”上,也多亏了“Iyassou”的帮助。
下面是mycropython的微:位代码,它将改变LED矩阵上的图标(在我的例子中是箭头),根据串行输入。当然,您需要以某种方式发送这些输入,我用上面的python脚本和PS4操纵杆发送它。
from microbit import *
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)
https://stackoverflow.com/questions/61156574
复制相似问题