Skip to content

Commit

Permalink
Merge pull request #3 from Vinz1911/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Vinz1911 authored Feb 17, 2021
2 parents 756c90a + 1576a84 commit ff331c7
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 1,432 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# PrimePowerUP

## Current Examples:
- Spike Prime + Powerup Remote
- 1: basic remote example for testing ble connection
- 2: example about lighting up dot's on spike prime on button press on remote
- 3: example to control the `advanced driving base` model from LEGO(R) with the PowerUP(TM) Remote

- **NOTE: If you're using the official LEGO Spike APP to upload the code, you should just upload it and don't run it with attached console, this can be ended up in a frozen Spike Prime**
- **NOTE: There is currently one problem: the ble code consumes lot of ressources from the spike prime, so it's necessary to restart the prime after a code execution because of mem alloc (bug)?**
`PrimePoweredUP` contains a library for Lego Spike Prime to connect to PoweredUP Remote (Handset) over BLE.
The core is based on the MicroPython ubluetooth low level api.

### Description
- the library use lot of memory. i recommend to pre compile the library from `remote/control` and install it on the prime hub.
a very good way to do that is using this awesome tool: [Spike Tools](https://github.com/XenseEducation/spiketools-release/releases)
pre compiled library can also be downloaded in releases section: [Pre-Compiled Library](https://github.com/Vinz1911/PrimePowerUP/releases)

- there are two examples in `examples` folder. The first one shows how to light up dot's on Prime Hub and the second one
shows how to control a motor pair with the remote. examples are created by using the control.py installed as pre compiled lib
(it's also possible to copy all together and load it on the hub)

### Known Problems:
- the ubluetooth class has some problems with event loop based functions from Lego. This means, if you run a event loop based
function within the button pressed callback, the entire hub will freeze. This is currently not possible to fix that, maybe with
a new firmare which supports uasyncio library. **Event based functions ?!** are functions like playing sound until end, wait for or
motor functions like run_to_position or run_for_degrees and so on.
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from remote.control import PowerUPRemote, PowerUPButtons, PowerUPColors
from remote.control import PoweredUPRemote, PoweredUPColors, PoweredUPButtons

"""
LEGO(R) SPIKE PRIME + POWERED UP
--------------------------------
This is a basic example:
This example let light up different dot's on
a prime/inventor hub for different buttons pressed
on the powered up remote
"""


def on_connect():
hub.status_light.on("azure")
"""
callback on connect
"""
hub.status_light.on("blue")


def on_disconnect():
"""
callback on disconnect
"""
hub.status_light.on("white")


def on_button(button):
"""
callback on button press
:param button: button id
"""
hub.light_matrix.off()
if button == PowerUPButtons.A_PLUS:
if button == PoweredUPButtons.LEFT_PLUS:
hub.light_matrix.set_pixel(0, 0, brightness=100)
elif button == PowerUPButtons.A_RED:
elif button == PoweredUPButtons.LEFT_RED:
hub.light_matrix.set_pixel(1, 0, brightness=100)
elif button == PowerUPButtons.A_MINUS:
elif button == PoweredUPButtons.LEFT_MINUS:
hub.light_matrix.set_pixel(2, 0, brightness=100)
elif button == PowerUPButtons.B_PLUS:
elif button == PoweredUPButtons.RIGHT_PLUS:
hub.light_matrix.set_pixel(3, 0, brightness=100)
elif button == PowerUPButtons.B_RED:
elif button == PoweredUPButtons.RIGHT_RED:
hub.light_matrix.set_pixel(4, 0, brightness=100)
elif button == PowerUPButtons.B_MINUS:
elif button == PoweredUPButtons.RIGHT_MINUS:
hub.light_matrix.set_pixel(0, 1, brightness=100)
elif button == PowerUPButtons.A_PLUS_B_PLUS:
elif button == PoweredUPButtons.LEFT_PLUS_RIGHT_PLUS:
hub.light_matrix.set_pixel(0, 2, brightness=100)
elif button == PowerUPButtons.CENTER:
remote.disconnect()
elif button == PowerUPButtons.RELEASED:
elif button == PoweredUPButtons.RELEASED:
hub.light_matrix.off()
else:
hub.light_matrix.off()
Expand All @@ -38,9 +57,9 @@ def on_button(button):
hub = PrimeHub()

# create remote and connect
remote = PowerUPRemote()
remote = PoweredUPRemote()
remote.debug = True
remote.on_connect(callback=on_connect)
remote.on_disconnect(callback=on_disconnect)
remote.on_button(callback=on_button)
remote.connect(color=PowerUPColors.RED)
remote.connect()
66 changes: 66 additions & 0 deletions examples/driving/driving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from remote.control import PoweredUPRemote, PoweredUPColors, PoweredUPButtons

"""
LEGO(R) SPIKE PRIME + POWERED UP
--------------------------------
This is a basic example:
This example let control a motor pair
with the powered up remote
"""


def on_connect():
"""
callback on connect
"""
hub.status_light.on("blue")


def on_disconnect():
"""
callback on disconnect
"""
hub.status_light.on("white")
motor_pair.stop()


def on_button(button):
"""
callback on button press
:param button: button id
"""
if button == PoweredUPButtons.RIGHT_PLUS:
motor_pair.start(speed=75)
elif button == PoweredUPButtons.RIGHT_MINUS:
motor_pair.start(speed=-75)
elif button == PoweredUPButtons.LEFT_PLUS_RIGHT_PLUS:
motor_pair.start(steering=-45, speed=75)
elif button == PoweredUPButtons.LEFT_MINUS_RIGHT_PLUS:
motor_pair.start(steering=45, speed=75)
elif button == PoweredUPButtons.LEFT_MINUS_RIGHT_MINUS:
motor_pair.start(steering=45, speed=-75)
elif button == PoweredUPButtons.LEFT_PLUS_RIGHT_MINUS:
motor_pair.start(steering=-45, speed=-75)
elif button == PoweredUPButtons.RELEASED:
motor_pair.stop()
else:
motor_pair.stop()


# set up hub
hub = PrimeHub()

# set up motors
motor_pair = MotorPair('A', 'B')
motor_pair.set_stop_action('coast')

# create remote and connect
remote = PoweredUPRemote()
remote.on_connect(callback=on_connect)
remote.on_disconnect(callback=on_disconnect)
remote.on_button(callback=on_button)
remote.connect()
Loading

0 comments on commit ff331c7

Please sign in to comment.