Skip to content
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

Closed
yonghuming opened this issue Mar 14, 2018 · 9 comments
Closed

it will be intresting use microbit to control pygame zero #388

yonghuming opened this issue Mar 14, 2018 · 9 comments

Comments

@yonghuming
Copy link
Contributor

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.

@ntoll
Copy link
Member

ntoll commented Mar 14, 2018

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.

@tjguk
Copy link
Collaborator

tjguk commented Mar 14, 2018

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

@ukBaz
Copy link

ukBaz commented Mar 14, 2018

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).
lancaster-university/microbit-dal#249

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.

@illume
Copy link
Contributor

illume commented Mar 16, 2018

Perhaps consider custom pygame events?
Below is how I post serial port data into pygame events in my arduino projects.
It uses a line based protocol on the serial port.

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)

@ntoll
Copy link
Member

ntoll commented Mar 16, 2018

@illume you're a ⭐ Thank you for posting this!

@ntoll
Copy link
Member

ntoll commented Mar 23, 2018

@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). ;-)

@illume
Copy link
Contributor

illume commented Mar 23, 2018

I hereby grant all, all permissions on those bytes to do as they wish. ceremonial bow and handwave

@ntoll
Copy link
Member

ntoll commented Mar 24, 2018

Stonking stuff... ceremonial bow and handwave back at'cha. ;-)

@ntoll
Copy link
Member

ntoll commented Jun 4, 2018

Closing since this is more to do with PyGame than Mu. :-) Thanks for all the input everybody.

@ntoll ntoll closed this as completed Jun 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants