-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
it will be intresting use microbit to control pygame zero #388
Comments
Heh... so this is definitely possible. The thing to do would be to see how PyGame consumes USB serial messages (i.e. from devices like the micro:bit). I know for certain that PyGame supports various game controller / USB devices. It's something I've thought about too, and may use April's PyWeek (see: https://pyweek.org/) as an excuse to try to build something. In the worst case, PySerial could be an option to read and write data to the device. |
Pygame's use of USB controllers is via the joystick module. I assume it would be easier to insert your own serial-handler into the pygame loop than to try to make the microbit mimic the joystick data |
FYI: There is work going on over on the micro:bit DAL to get the micro:bit to look like a Bluetooth Hardware Interface Device (HID). I know Bluetooth cannot be used by micropython on the micro:bit but to use the micro:bit as a wireless controller in Pygame this would probably make it easier to deal with. |
Perhaps consider custom pygame events? import pygame as pg
import serial
ser = serial.Serial('/dev/tty.usbmodem1411', 9600, timeout = 0)
serial_buffer = ''
# custom event type is an int based on USEREVENT.
GIGGLE = pg.USEREVENT + 1
SERIAL = pg.USEREVENT + 2
def send_giggle():
e = pg.event.Event(GIGGLE, something='lalala', giggle=True)
pg.event.post(e)
_, going = pg.init(), pg.display.set_mode((320,200))
pg.display.set_caption('g key to giggle')
while going:
serial_data = ser.read()
while serial_data:
serial_buffer += serial_data
if '\r\n' in serial_buffer:
evt = pg.event.Event(SERIAL, line=serial_buffer)
pg.event.post(evt)
serial_buffer = ''
serial_data = ser.read()
for e in [pg.event.wait()] + pg.event.get():
if e.type == pg.QUIT: going = False
elif e.type == pg.KEYDOWN and e.key == pg.K_g: send_giggle()
elif e.type == GIGGLE: print('Got a giggle! %s' % e)
elif e.type == SERIAL: print('Line from serial: %s' % e) |
@illume you're a ⭐ Thank you for posting this! |
@illume do you have any problem with me taking the code you provided and putting it in an example somewhere on Mu's (new and currently under development) website..? I'll credit you and refer back to this issue so people can see the source (there's no license associated with the code fragment above, so I have to ask). ;-) |
I hereby grant all, all permissions on those bytes to do as they wish. ceremonial bow and handwave |
Stonking stuff... ceremonial bow and handwave back at'cha. ;-) |
Closing since this is more to do with PyGame than Mu. :-) Thanks for all the input everybody. |
maybe pygame zero mode should be a sublclass of microbit mode class.
or we need to create a new mode so we can use microbit to contorl pygame
i am trying.
The text was updated successfully, but these errors were encountered: