Skip to main content

Module for handling connections with the Lego Mario toy

Project description

pyLegoMario

pip install pyLegoMario

pyLegoMario is a script that connects to Lego Mario and reads its acceleromter, tile sensor, and pants data. To connect, you have to turn Mario on and then press the Bluetooth Button. See mario_sample.py for a nice sample. To disconnect you have to await Mario.disconnect() or await Mario.turn_off(), as well as set Mario.autoReconnect = False

TL;DR; "just show me how to use it!"

The Basics

from pyLegoMario import Mario, MarioWindow, run
# Initialize Mario
mario = Mario()
# Create GUI
MarioWindow(mario)
# call run() at the end of your program to keep the asyncio loop running
run()

Use Mario in Your Own Programs With Callback Functions

def my_pants_hook(mario: Mario, powerup: str) -> None:
    print(f"I'm wearing {powerup} pants!")

mario.add_pants_hook(my_pants_hook)

Use Mario as a Controller in Pygame!

import pygame
from pyLegoMario import PygameMario, AsyncClock
from pyLegoMario import ACC_EVENT, PANTS_EVENT, RGB_EVENT

pygame.init()
mario = PygameMario()
# use AsyncClock instead of pygame.time.Clock
clock = AsyncClock()
screen = pygame.display.set_mode(1200,600)
font = pygame.font.SysFont(None, 48)

while True:
    clock.tick()
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == RGB_EVENT:
            screen.fill((0,0,0))
            # write the current camera value onto the screen
            text = font.render(event.value, True, (255,255,255))
            screen.blit(text, (10,10))
# no need to call run() here, AsyncClock handles this

You Can Do a Lot More!

Sample scripts can be found in the Github Repository

Tweet at me: @Jackomatrus

On Windows you will need Python 3.9 or higher for Bluetooth sockets to work properly.

I tested this on Mac and Windows. I have not tested this on Linux.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylegomario-1.1.tar.gz (112.6 kB view details)

Uploaded Source

Built Distribution

pyLegoMario-1.1-py3-none-any.whl (110.9 kB view details)

Uploaded Python 3

File details

Details for the file pylegomario-1.1.tar.gz.

File metadata

  • Download URL: pylegomario-1.1.tar.gz
  • Upload date:
  • Size: 112.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for pylegomario-1.1.tar.gz
Algorithm Hash digest
SHA256 578a56c3018f3784ae00882ad232d439d48db55ae4bf9a24c120e1aa7cca8745
MD5 4bf7dfe58a544a70581832d4c9ef03a1
BLAKE2b-256 b172e5cb207e97b87ce4ebc3ef0430ff467fbab0279373a96b2cc2e8bdd8fe6d

See more details on using hashes here.

File details

Details for the file pyLegoMario-1.1-py3-none-any.whl.

File metadata

  • Download URL: pyLegoMario-1.1-py3-none-any.whl
  • Upload date:
  • Size: 110.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for pyLegoMario-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1dce9dc1c2ceb62e2e3099afd5b7114438c0f5ba33328e6e5c503e55b87396fc
MD5 b4bf0a9e7b7a36df9759296d17c3b2d0
BLAKE2b-256 60ca895f677f379538be8790c83a85a0eb73f372d495990affe585d100b4178f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page