From one side, this is my python script on Ubuntu PC which sending button inputs from Playstation 4 joystick to serial port of micro:bit (joystick is connected to Ubuntu via bluetooth):
Code: Select all
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()

Code: Select all
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)
- 1 for joystick button 7 (R2)
2 for joystick button 6 (L2)
3 for joystick axis 0 left position (left analog on joystick)
3 for joystick axis 0 right position (left analog on joystick)
5 if nothing is pressed/moved
Till now, it was possible for me to display some icon, only if I add one more "else" statement at the end of script, but this is normal cause it is "else".
E.g.
Code: Select all
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)

Many thanks in advance, I would be glad to provide any additional info needed.