Skip to main content

Control LEGO(tm) BluetoothLE Hubs, Motors, and Sensors using Async Python

Project description

image_pypi image_downloads image_license passing quality Coverage Status

BrickNil [*] provides an easy way to connect to and program LEGO® Bluetooth hubs (including the newer 60197 and 60198 train sets) using Python on OS X and Linux. This work was inspired by this EuroBricks thread, and the NodeJS Powered-Up library. It requires modern Python (designed and tested for 3.7) and uses asynchronous event programming built on top of the Curio async library. As an aside, the choice of async library is fairly arbitrary; and conceivably enabling another library such as asyncio or Trio should be straightforward.

An example BrickNil program for controlling the Train motor speed is shown below:

from curio import sleep
from bricknil import attach, start
from bricknil.hub import PoweredUpHub
from bricknil.sensor import TrainMotor

@attach(TrainMotor, name='motor')
class Train(PoweredUpHub):

    async def run(self):
        for i in range(2):  # Repeat this control two times
            await self.motor.ramp_speed(80,5000) # Ramp speed to 80 over 5 seconds
            await sleep(6)
            await self.motor.ramp_speed(0,1000)  # Brake to 0 over 1 second
            await sleep(2)

async def system():
    train = Train('My train')

if __name__ == '__main__':
    start(system)

Features

  • Supports the following LEGO® Bluetooth systems:
    • PoweredUp hubs for trains

    • Boost Move hub

  • Supports the following actuators/sensors:
    • Internal motors

    • Train motors

    • Hub LED color

    • Boost vision sensor (color, distance)

    • Internal tilt/orientation/accelerometer

    • Hub buttons

  • Fully supports Python asynchronous keywords and coroutines
    • Allows expressive concurrent programming using async/await syntax

    • The current implementation uses the async library Curio by David Beazley

  • Cross-platform
    • Uses the Adafruit Bluefruit BluetoothLE library for Mac OS X

    • Uses the Bleak Bluetooth library for Linux and Win10[], tested on Raspberry Pi

Building a simple train controller

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

bricknil-0.5.1-py3-none-any.whl (59.9 kB view details)

Uploaded Python 3

File details

Details for the file bricknil-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: bricknil-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 59.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for bricknil-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a1aa618fa6b1bbbc75123294cdb8294c47687450fa04ee5cad292847d5467840
MD5 4f23758e8d5c1c9c893099827eed6d09
BLAKE2b-256 feb9b9c3de7e37fefe9cb5d62b3f4aa112c490720c635fdfc9ebd2bf89955050

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