spikeble 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- app/__init__.py +3 -0
- app/bargraph.py +24 -0
- app/display.py +39 -0
- app/linegraph.py +39 -0
- app/music.py +51 -0
- app/sound.py +18 -0
- color/__init__.py +14 -0
- color_matrix/__init__.py +18 -0
- color_sensor/__init__.py +16 -0
- device/__init__.py +27 -0
- distance_sensor/__init__.py +30 -0
- force_sensor/__init__.py +65 -0
- hub/__init__.py +13 -0
- hub/button.py +37 -0
- hub/light.py +38 -0
- hub/light_matrix.py +249 -0
- hub/motion_sensor.py +142 -0
- hub/port.py +25 -0
- hub/sound.py +74 -0
- motor/__init__.py +398 -0
- motor_pair/__init__.py +555 -0
- orientation/__init__.py +12 -0
- runloop/__init__.py +85 -0
- spikeble/__init__.py +71 -0
- spikeble/_lib/cobs.py +125 -0
- spikeble/_lib/connection.py +22 -0
- spikeble/_lib/crc.py +11 -0
- spikeble/_lib/enumeration.py +79 -0
- spikeble/_lib/messages.py +290 -0
- spikeble/_lib/readme +2 -0
- spikeble/_utils.py +8 -0
- spikeble/micropython/app/__init__.py +3 -0
- spikeble/micropython/app/bargraph.py +24 -0
- spikeble/micropython/app/display.py +39 -0
- spikeble/micropython/app/linegraph.py +39 -0
- spikeble/micropython/app/music.py +51 -0
- spikeble/micropython/app/sound.py +18 -0
- spikeble/micropython/color/__init__.py +14 -0
- spikeble/micropython/color_matrix/__init__.py +18 -0
- spikeble/micropython/color_sensor/__init__.py +16 -0
- spikeble/micropython/device/__init__.py +27 -0
- spikeble/micropython/distance_sensor/__init__.py +30 -0
- spikeble/micropython/force_sensor/__init__.py +65 -0
- spikeble/micropython/hub/__init__.py +13 -0
- spikeble/micropython/hub/button.py +37 -0
- spikeble/micropython/hub/light.py +38 -0
- spikeble/micropython/hub/light_matrix.py +249 -0
- spikeble/micropython/hub/motion_sensor.py +142 -0
- spikeble/micropython/hub/port.py +25 -0
- spikeble/micropython/hub/sound.py +74 -0
- spikeble/micropython/motor/__init__.py +398 -0
- spikeble/micropython/motor_pair/__init__.py +555 -0
- spikeble/micropython/orientation/__init__.py +12 -0
- spikeble/micropython/runloop/__init__.py +85 -0
- spikeble/spike.py +243 -0
- spikeble-0.1.dist-info/METADATA +126 -0
- spikeble-0.1.dist-info/RECORD +59 -0
- spikeble-0.1.dist-info/WHEEL +4 -0
- spikeble-0.1.dist-info/licenses/LICENSE +13 -0
app/__init__.py
ADDED
app/bargraph.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"The bargraph module is used make bar graphs in the SPIKE App"
|
|
2
|
+
|
|
3
|
+
from typing import Awaitable
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def change(color: int, value: float) -> None:
|
|
7
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
8
|
+
assert 0 <= value <= 100, "Value must be between 0 and 100"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def clear_all() -> None:
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def get_value(color: int) -> Awaitable:
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def set_value(color: int, value: float) -> None:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def show(fullscreen: bool) -> None:
|
|
24
|
+
pass
|
app/display.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"The display module is used show images in the SPIKE App"
|
|
2
|
+
|
|
3
|
+
IMAGE_ROBOT_1 = 1
|
|
4
|
+
IMAGE_ROBOT_2 = 2
|
|
5
|
+
IMAGE_ROBOT_3 = 3
|
|
6
|
+
IMAGE_ROBOT_4 = 4
|
|
7
|
+
IMAGE_ROBOT_5 = 5
|
|
8
|
+
IMAGE_HUB_1 = 6
|
|
9
|
+
IMAGE_HUB_2 = 7
|
|
10
|
+
IMAGE_HUB_3 = 8
|
|
11
|
+
IMAGE_HUB_4 = 9
|
|
12
|
+
IMAGE_AMUSEMENT_PARK = 10
|
|
13
|
+
IMAGE_BEACH = 11
|
|
14
|
+
IMAGE_HAUNTED_HOUSE = 12
|
|
15
|
+
IMAGE_CARNIVAL = 13
|
|
16
|
+
IMAGE_BOOKSHELF = 14
|
|
17
|
+
IMAGE_PLAYGROUND = 15
|
|
18
|
+
IMAGE_MOON = 16
|
|
19
|
+
IMAGE_CAVE = 17
|
|
20
|
+
IMAGE_OCEAN = 18
|
|
21
|
+
IMAGE_POLAR_BEAR = 19
|
|
22
|
+
IMAGE_PARK = 20
|
|
23
|
+
IMAGE_RANDOM = 21
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def hide() -> None:
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def image(image: int) -> None:
|
|
31
|
+
assert 1 <= image <= 21, "Image must be between 1 and 21"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def show(fullscreen: bool) -> None:
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def text(text: str) -> None:
|
|
39
|
+
pass
|
app/linegraph.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"The linegraph module is used make line graphs in the SPIKE App"
|
|
2
|
+
|
|
3
|
+
from typing import Awaitable
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def clear(color: int) -> None:
|
|
7
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def clear_all() -> None:
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def get_average(color: int) -> Awaitable:
|
|
15
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def get_last(color: int) -> Awaitable:
|
|
19
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def get_max(color: int) -> Awaitable:
|
|
23
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def get_min(color: int) -> Awaitable:
|
|
27
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def hide() -> None:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def plot(color: int, x: float, y: float) -> None:
|
|
35
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def show(fullscreen: bool) -> None:
|
|
39
|
+
pass
|
app/music.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Drum constants
|
|
2
|
+
DRUM_SNARE = 1
|
|
3
|
+
DRUM_BASS = 2
|
|
4
|
+
DRUM_SIDE_STICK = 3
|
|
5
|
+
DRUM_CRASH_CYMBAL = 4
|
|
6
|
+
DRUM_OPEN_HI_HAT = 5
|
|
7
|
+
DRUM_CLOSED_HI_HAT = 6
|
|
8
|
+
DRUM_TAMBOURINE = 7
|
|
9
|
+
DRUM_HAND_CLAP = 8
|
|
10
|
+
DRUM_CLAVES = 9
|
|
11
|
+
DRUM_WOOD_BLOCK = 10
|
|
12
|
+
DRUM_COWBELL = 11
|
|
13
|
+
DRUM_TRIANGLE = 12
|
|
14
|
+
DRUM_BONGO = 13
|
|
15
|
+
DRUM_CONGA = 14
|
|
16
|
+
DRUM_CABASA = 15
|
|
17
|
+
DRUM_GUIRO = 16
|
|
18
|
+
DRUM_VIBRASLAP = 17
|
|
19
|
+
DRUM_CUICA = 18
|
|
20
|
+
|
|
21
|
+
# Instrument constants
|
|
22
|
+
INSTRUMENT_PIANO = 1
|
|
23
|
+
INSTRUMENT_ELECTRIC_PIANO = 2
|
|
24
|
+
INSTRUMENT_ORGAN = 3
|
|
25
|
+
INSTRUMENT_GUITAR = 4
|
|
26
|
+
INSTRUMENT_ELECTRIC_GUITAR = 5
|
|
27
|
+
INSTRUMENT_BASS = 6
|
|
28
|
+
INSTRUMENT_PIZZICATO = 7
|
|
29
|
+
INSTRUMENT_CELLO = 8
|
|
30
|
+
INSTRUMENT_TROMBONE = 9
|
|
31
|
+
INSTRUMENT_CLARINET = 10
|
|
32
|
+
INSTRUMENT_SAXOPHONE = 11
|
|
33
|
+
INSTRUMENT_FLUTE = 12
|
|
34
|
+
INSTRUMENT_WOODEN_FLUTE = 13
|
|
35
|
+
INSTRUMENT_BASSOON = 14
|
|
36
|
+
INSTRUMENT_CHOIR = 15
|
|
37
|
+
INSTRUMENT_VIBRAPHONE = 16
|
|
38
|
+
INSTRUMENT_MUSIC_BOX = 17
|
|
39
|
+
INSTRUMENT_STEEL_DRUM = 18
|
|
40
|
+
INSTRUMENT_MARIMBA = 19
|
|
41
|
+
INSTRUMENT_SYNTH_LEAD = 20
|
|
42
|
+
INSTRUMENT_SYNTH_PAD = 21
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def play_drum(drum: int) -> None:
|
|
46
|
+
assert 1 <= drum <= 18, "Drum must be between 1 and 18"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def play_instrument(instrument: int, note: int, duration: int) -> None:
|
|
50
|
+
assert 1 <= instrument <= 21, "Instrument must be between 1 and 21"
|
|
51
|
+
assert 1 <= note <= 130, "Note must be between 1 and 130"
|
app/sound.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from typing import Awaitable
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
async def play(
|
|
5
|
+
sound_name: str, volume: int = 100, pitch: int = 0, pan: int = 0
|
|
6
|
+
) -> Awaitable:
|
|
7
|
+
"""Play a sound in the SPIKE App"""
|
|
8
|
+
assert 0 <= volume <= 100, "Volume must be between 0 and 100"
|
|
9
|
+
assert -100 <= pan <= 100, "Pan must be between -100 and 100"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def set_attributes(volume: int, pitch: int, pan: int) -> None:
|
|
13
|
+
assert 0 <= volume <= 100, "Volume must be between 0 and 100"
|
|
14
|
+
assert -100 <= pan <= 100, "Pan must be between -100 and 100"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def stop() -> None:
|
|
18
|
+
pass
|
color/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"The color module contains all the color constants to use with the color_matrix, color_sensor and light modules."
|
|
2
|
+
|
|
3
|
+
BLACK = 0
|
|
4
|
+
MAGENTA = 1
|
|
5
|
+
PURPLE = 2
|
|
6
|
+
BLUE = 3
|
|
7
|
+
AZURE = 4
|
|
8
|
+
TURQUOISE = 5
|
|
9
|
+
GREEN = 6
|
|
10
|
+
YELLOW = 7
|
|
11
|
+
ORANGE = 8
|
|
12
|
+
RED = 9
|
|
13
|
+
WHITE = 10
|
|
14
|
+
UNKNOWN = -1
|
color_matrix/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
def clear(port: int) -> None:
|
|
2
|
+
"Turn off all pixels on a Color Matrix"
|
|
3
|
+
assert port in range(6), "Invalid port"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def get_pixel(port: int, x: int, y: int) -> tuple[int, int]:
|
|
7
|
+
"Retrieve a specific pixel represented as a tuple containing the color and intensity"
|
|
8
|
+
assert port in range(6), "Invalid port"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def set_pixel(port: int, x: int, y: int, pixel: tuple[int, int]) -> None:
|
|
12
|
+
"Change a single pixel on a Color Matrix"
|
|
13
|
+
assert port in range(6), "Invalid port"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def show(port: int, pixels: list[tuple[int, int]]) -> None:
|
|
17
|
+
"Change all pixels at once on a Color Matrix"
|
|
18
|
+
assert port in range(6), "Invalid port"
|
color_sensor/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"The `color_sensor` module enables you to write code that reacts to specific colors or the intensity of the reflected light."
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
async def color(port: int) -> int:
|
|
5
|
+
"Returns the color value of the detected color. Use the `color` module to map the color value to a specific color."
|
|
6
|
+
assert port in range(6), "Invalid port"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def reflection(port: int) -> int:
|
|
10
|
+
"Retrieves the intensity of the reflected light (0-100%)."
|
|
11
|
+
assert port in range(6), "Invalid port"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def rgbi(port: int) -> tuple[int, int, int, int]:
|
|
15
|
+
"Retrieves the overall color intensity and intensity of red, green and blue."
|
|
16
|
+
assert port in range(6), "Invalid port"
|
device/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"The `device` module enables you to write code to get information about devices plugged into the hub."
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
async def data(port: int) -> tuple[int]:
|
|
5
|
+
"Retrieve the raw LPF-2 data from a device."
|
|
6
|
+
assert port in range(6), "Invalid port"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def id(port: int) -> int:
|
|
10
|
+
"Retrieve the device id of a device. Each device has an id based on its type."
|
|
11
|
+
assert port in range(6), "Invalid port"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def get_duty_cycle(port: int) -> int:
|
|
15
|
+
"Retrieve the duty cycle for a device. Returned values is in range 0 to 10000"
|
|
16
|
+
assert port in range(6), "Invalid port"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def ready(port: int) -> bool:
|
|
20
|
+
"When a device is attached to the hub it might take a short amount of time before it's ready to accept requests. Use `ready` to test for the readiness of the attached devices."
|
|
21
|
+
assert port in range(6), "Invalid port"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
async def set_duty_cycle(port: int, duty_cycle: int) -> None:
|
|
25
|
+
"Set the duty cycle for a device. Accepted values are in range 0 to 10000."
|
|
26
|
+
assert port in range(6), "Invalid port"
|
|
27
|
+
assert duty_cycle in range(10001), "Invalid duty cycle"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""The distance_sensor module enables you to write code that reacts to specific distances or light up the Distance Sensor in different ways."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
async def clear(port: int) -> None:
|
|
5
|
+
"Turns off all the lights in the Distance Sensor connected to `port`."
|
|
6
|
+
assert port in range(6), "Invalid port"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def distance(port: int) -> int:
|
|
10
|
+
"Retrieve the distance in millimeters captured by the Distance Sensor connected to `port`. If the Distance Sensor cannot read a valid distance it will return -1."
|
|
11
|
+
assert port in range(6), "Invalid port"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def get_pixel(port: int, x: int, y: int) -> int:
|
|
15
|
+
"Retrieve the intensity of a specific light on the Distance Sensor connected to `port`."
|
|
16
|
+
assert port in range(6), "Invalid port"
|
|
17
|
+
assert x in range(4), "Invalid x coordinate"
|
|
18
|
+
assert y in range(4), "Invalid y coordinate"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def set_pixel(port: int, x: int, y: int, intensity: int) -> None:
|
|
22
|
+
"Changes the intensity of a specific light on the Distance Sensor connected to `port`."
|
|
23
|
+
assert port in range(6), "Invalid port"
|
|
24
|
+
assert x in range(4), "Invalid x coordinate"
|
|
25
|
+
assert y in range(4), "Invalid y coordinate"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def show(port: int, pixels: list[int]) -> None:
|
|
29
|
+
"Change all the lights at the same time."
|
|
30
|
+
assert port in range(6), "Invalid port"
|
force_sensor/__init__.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The `force_sensor` module contains all functions and constants to use the Force Sensor.
|
|
3
|
+
|
|
4
|
+
To use the Force Sensor module add the following import statement to your project:
|
|
5
|
+
|
|
6
|
+
>>> import force_sensor
|
|
7
|
+
|
|
8
|
+
All functions in the module should be called inside the force_sensor module as a prefix like so:
|
|
9
|
+
|
|
10
|
+
>>> force_sensor.force(port.A)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def force(port: int) -> int:
|
|
15
|
+
"""
|
|
16
|
+
Retrieves the measured force in decinewtons (0 to 100).
|
|
17
|
+
|
|
18
|
+
Example
|
|
19
|
+
-------
|
|
20
|
+
>>> from hub import port
|
|
21
|
+
>>> import force_sensor
|
|
22
|
+
>>> print(force_sensor.force(port.A))
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
port : int
|
|
27
|
+
A port from the `port` submodule in the hub module.
|
|
28
|
+
"""
|
|
29
|
+
assert port in range(6), "Invalid port"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
async def pressed(port: int) -> bool:
|
|
33
|
+
"""
|
|
34
|
+
Tests whether the button on the sensor is pressed. Returns true if the force sensor connected to port is pressed.
|
|
35
|
+
|
|
36
|
+
Example
|
|
37
|
+
-------
|
|
38
|
+
>>> from hub import port
|
|
39
|
+
>>> import force_sensor
|
|
40
|
+
>>> print(force_sensor.pressed(port.A))
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
port : int
|
|
45
|
+
A port from the `port` submodule in the hub module.
|
|
46
|
+
"""
|
|
47
|
+
assert port in range(6), "Invalid port"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def raw(port: int) -> int:
|
|
51
|
+
"""
|
|
52
|
+
Returns the raw, uncalibrated force value of the force sensor connected on `port`.
|
|
53
|
+
|
|
54
|
+
Example
|
|
55
|
+
-------
|
|
56
|
+
>>> from hub import port
|
|
57
|
+
>>> import force_sensor
|
|
58
|
+
>>> print(force_sensor.raw(port.A))
|
|
59
|
+
|
|
60
|
+
Parameters
|
|
61
|
+
----------
|
|
62
|
+
port : int
|
|
63
|
+
A port from the `port` submodule in the hub module.
|
|
64
|
+
"""
|
|
65
|
+
assert port in range(6), "Invalid port"
|
hub/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
def hardware_id() -> str:
|
|
2
|
+
"""Retrieve the hardware id."""
|
|
3
|
+
pass
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def power_off() -> int:
|
|
7
|
+
"""Turns off the hub."""
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def temperature() -> int:
|
|
12
|
+
"""Retrieve the hub temperature. Measured in decidegrees celsius (d°C) which is 1 / 10 of a degree celsius (°C)"""
|
|
13
|
+
pass
|
hub/button.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module allows you to react to buttons being pressed on the hub. You must first import the `button` module to use the buttons.
|
|
3
|
+
|
|
4
|
+
To use the Button module add the following import statement to your project:
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
>>> from hub import button
|
|
8
|
+
|
|
9
|
+
All functions in the module should be called inside the `button` module as a prefix like so:
|
|
10
|
+
|
|
11
|
+
>>> button.pressed(button.LEFT)
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
LEFT = 1
|
|
16
|
+
"""Left button next to the power button on the SPIKE Prime hub"""
|
|
17
|
+
RIGHT = 2
|
|
18
|
+
"""Right button next to the power button on the SPIKE Prime hub"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def pressed(button: int) -> int:
|
|
22
|
+
"""
|
|
23
|
+
```python
|
|
24
|
+
from hub import button
|
|
25
|
+
left_button_press_duration = 0
|
|
26
|
+
|
|
27
|
+
# Wait for the left button to be pressed
|
|
28
|
+
while not button.pressed(button.LEFT):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
# As long as the left button is being pressed, update the `left_button_press_duration` variable
|
|
32
|
+
while button.pressed(button.LEFT):
|
|
33
|
+
left_button_press_duration = button.pressed(button.LEFT)
|
|
34
|
+
|
|
35
|
+
print("Left button was pressed for " + str(left_button_press_duration) + " milliseconds")
|
|
36
|
+
```
|
|
37
|
+
"""
|
hub/light.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The light module includes functions to change the color of the light on the SPIKE Prime hub.
|
|
3
|
+
|
|
4
|
+
To use the Light module add the following import statement to your project:
|
|
5
|
+
|
|
6
|
+
>>> from hub import light
|
|
7
|
+
|
|
8
|
+
All functions in the module should be called inside the light module as a prefix like so:
|
|
9
|
+
|
|
10
|
+
>>> light.color(color.RED)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
POWER = 0
|
|
14
|
+
"""The power button. On SPIKE Prime it's the button between the left and right buttons."""
|
|
15
|
+
CONNECT = 1
|
|
16
|
+
"""The light around the Bluetooth connect button on SPIKE Prime."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def color(light: int, color: int) -> None:
|
|
20
|
+
"""
|
|
21
|
+
Change the color of a light on the hub.
|
|
22
|
+
```python
|
|
23
|
+
from hub import light
|
|
24
|
+
import color
|
|
25
|
+
|
|
26
|
+
# Change the light to red
|
|
27
|
+
light.color(light.POWER, color.RED)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
|
|
33
|
+
light : int
|
|
34
|
+
The light on the hub
|
|
35
|
+
color : int
|
|
36
|
+
A color from the `color` module
|
|
37
|
+
"""
|
|
38
|
+
assert 0 <= color <= 10, "Color must be between 0 and 10"
|
hub/light_matrix.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"""
|
|
2
|
+
To use the Light Matrix module add the following import statement to your project:
|
|
3
|
+
|
|
4
|
+
>>> from hub import light_matrix
|
|
5
|
+
|
|
6
|
+
All functions in the module should be called inside the light_matrix module as a prefix like so:
|
|
7
|
+
|
|
8
|
+
>>> light_matrix.write("Hello World")
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from typing import Awaitable
|
|
12
|
+
|
|
13
|
+
IMAGE_HEART = 1
|
|
14
|
+
IMAGE_HEART_SMALL = 2
|
|
15
|
+
IMAGE_HAPPY = 3
|
|
16
|
+
IMAGE_SMILE = 4
|
|
17
|
+
IMAGE_SAD = 5
|
|
18
|
+
IMAGE_CONFUSED = 6
|
|
19
|
+
IMAGE_ANGRY = 7
|
|
20
|
+
IMAGE_ASLEEP = 8
|
|
21
|
+
IMAGE_SURPRISED = 9
|
|
22
|
+
IMAGE_SILLY = 10
|
|
23
|
+
IMAGE_FABULOUS = 11
|
|
24
|
+
IMAGE_MEH = 12
|
|
25
|
+
IMAGE_YES = 13
|
|
26
|
+
IMAGE_NO = 14
|
|
27
|
+
IMAGE_CLOCK12 = 15
|
|
28
|
+
IMAGE_CLOCK1 = 16
|
|
29
|
+
IMAGE_CLOCK2 = 17
|
|
30
|
+
IMAGE_CLOCK3 = 18
|
|
31
|
+
IMAGE_CLOCK4 = 19
|
|
32
|
+
IMAGE_CLOCK5 = 20
|
|
33
|
+
IMAGE_CLOCK6 = 21
|
|
34
|
+
IMAGE_CLOCK7 = 22
|
|
35
|
+
IMAGE_CLOCK8 = 23
|
|
36
|
+
IMAGE_CLOCK9 = 24
|
|
37
|
+
IMAGE_CLOCK10 = 25
|
|
38
|
+
IMAGE_CLOCK11 = 26
|
|
39
|
+
IMAGE_ARROW_N = 27
|
|
40
|
+
IMAGE_ARROW_NE = 28
|
|
41
|
+
IMAGE_ARROW_E = 29
|
|
42
|
+
IMAGE_ARROW_SE = 30
|
|
43
|
+
IMAGE_ARROW_S = 31
|
|
44
|
+
IMAGE_ARROW_SW = 32
|
|
45
|
+
IMAGE_ARROW_W = 33
|
|
46
|
+
IMAGE_ARROW_NW = 34
|
|
47
|
+
IMAGE_GO_RIGHT = 35
|
|
48
|
+
IMAGE_GO_LEFT = 36
|
|
49
|
+
IMAGE_GO_UP = 37
|
|
50
|
+
IMAGE_GO_DOWN = 38
|
|
51
|
+
IMAGE_TRIANGLE = 39
|
|
52
|
+
IMAGE_TRIANGLE_LEFT = 40
|
|
53
|
+
IMAGE_CHESSBOARD = 41
|
|
54
|
+
IMAGE_DIAMOND = 42
|
|
55
|
+
IMAGE_DIAMOND_SMALL = 43
|
|
56
|
+
IMAGE_SQUARE = 44
|
|
57
|
+
IMAGE_SQUARE_SMALL = 45
|
|
58
|
+
IMAGE_RABBIT = 46
|
|
59
|
+
IMAGE_COW = 47
|
|
60
|
+
IMAGE_MUSIC_CROTCHET = 48
|
|
61
|
+
IMAGE_MUSIC_QUAVER = 49
|
|
62
|
+
IMAGE_MUSIC_QUAVERS = 50
|
|
63
|
+
IMAGE_PITCHFORK = 51
|
|
64
|
+
IMAGE_XMAS = 52
|
|
65
|
+
IMAGE_PACMAN = 53
|
|
66
|
+
IMAGE_TARGET = 54
|
|
67
|
+
IMAGE_TSHIRT = 55
|
|
68
|
+
IMAGE_ROLLERSKATE = 56
|
|
69
|
+
IMAGE_DUCK = 57
|
|
70
|
+
IMAGE_HOUSE = 58
|
|
71
|
+
IMAGE_TORTOISE = 59
|
|
72
|
+
IMAGE_BUTTERFLY = 60
|
|
73
|
+
IMAGE_STICKFIGURE = 61
|
|
74
|
+
IMAGE_GHOST = 62
|
|
75
|
+
IMAGE_SWORD = 63
|
|
76
|
+
IMAGE_GIRAFFE = 64
|
|
77
|
+
IMAGE_SKULL = 65
|
|
78
|
+
IMAGE_UMBRELLA = 66
|
|
79
|
+
IMAGE_SNAKE = 67
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def clear() -> None:
|
|
83
|
+
"""
|
|
84
|
+
Switches off all of the pixels on the Light Matrix.
|
|
85
|
+
|
|
86
|
+
Example
|
|
87
|
+
-------
|
|
88
|
+
```python
|
|
89
|
+
|
|
90
|
+
from hub import light_matrix
|
|
91
|
+
import time
|
|
92
|
+
# Update pixels to show an image on Light Matrix, and then turn them off using the clear function
|
|
93
|
+
|
|
94
|
+
# Show a small heart
|
|
95
|
+
light_matrix.show_image(2)
|
|
96
|
+
|
|
97
|
+
# Wait for two seconds
|
|
98
|
+
time.sleep_ms(2000)
|
|
99
|
+
|
|
100
|
+
# Switch off the heart
|
|
101
|
+
light_matrix.clear()
|
|
102
|
+
```
|
|
103
|
+
"""
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def get_orientation() -> int:
|
|
108
|
+
"""
|
|
109
|
+
Retrieve the current orientation of the Light Matrix.
|
|
110
|
+
Can be used with the following constants: `orientation.UP`, `orientation.LEFT`, `orientation.RIGHT`, `orientation.DOWN`
|
|
111
|
+
"""
|
|
112
|
+
pass
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def get_pixel(x: int, y: int) -> int:
|
|
116
|
+
"""
|
|
117
|
+
Retrieve the intensity of a specific pixel on the Light Matrix.
|
|
118
|
+
|
|
119
|
+
Example
|
|
120
|
+
-------
|
|
121
|
+
```python
|
|
122
|
+
|
|
123
|
+
from hub import light_matrix
|
|
124
|
+
|
|
125
|
+
# Show a heart
|
|
126
|
+
light_matrix.show_image(1)
|
|
127
|
+
|
|
128
|
+
# Print the value of the center pixel's intensity
|
|
129
|
+
print(light_matrix.get_pixel(2, 2))
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Parameters
|
|
133
|
+
----------
|
|
134
|
+
|
|
135
|
+
x : int
|
|
136
|
+
The X value, range (0 - 4)
|
|
137
|
+
y : int
|
|
138
|
+
The Y value, range (0 - 4)
|
|
139
|
+
"""
|
|
140
|
+
pass
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def set_orientation(top: int) -> int:
|
|
144
|
+
"""
|
|
145
|
+
Change the orientation of the Light Matrix. All subsequent calls will use the new orientation.
|
|
146
|
+
Can be used with the following constants: `orientation.UP`, `orientation.LEFT`, `orientation.RIGHT`, `orientation.DOWN`
|
|
147
|
+
|
|
148
|
+
Parameters
|
|
149
|
+
----------
|
|
150
|
+
top : int
|
|
151
|
+
The side of the hub to be the top
|
|
152
|
+
"""
|
|
153
|
+
pass
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def set_pixel(x: int, y: int, intensity: int) -> None:
|
|
157
|
+
"""
|
|
158
|
+
Sets the brightness of one pixel (one of the 25 LEDs) on the Light Matrix.
|
|
159
|
+
|
|
160
|
+
Example
|
|
161
|
+
-------
|
|
162
|
+
```python
|
|
163
|
+
from hub import light_matrix
|
|
164
|
+
# Turn on the pixel in the center of the hub
|
|
165
|
+
light_matrix.set_pixel(2, 2, 100)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Parameters
|
|
169
|
+
----------
|
|
170
|
+
x : int
|
|
171
|
+
The X value, range (0 - 4)
|
|
172
|
+
y : int
|
|
173
|
+
The Y value, range (0 - 4)
|
|
174
|
+
intensity : int
|
|
175
|
+
How bright to light up the pixel
|
|
176
|
+
"""
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def show(pixels: list[int]) -> None:
|
|
181
|
+
"""
|
|
182
|
+
Change all the lights at the same time.
|
|
183
|
+
|
|
184
|
+
Example
|
|
185
|
+
-------
|
|
186
|
+
```python
|
|
187
|
+
from hub import light_matrix
|
|
188
|
+
# Update all pixels on Light Matrix using the show function
|
|
189
|
+
|
|
190
|
+
# Create a list with 25 identical intensity values
|
|
191
|
+
pixels = [100] * 25
|
|
192
|
+
|
|
193
|
+
# Update all pixels to show same intensity
|
|
194
|
+
light_matrix.show(pixels)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
pixels : list[int]
|
|
200
|
+
A list containing light intensity values for all 25 pixels.
|
|
201
|
+
"""
|
|
202
|
+
pass
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def show_image(image: int) -> None:
|
|
206
|
+
"""
|
|
207
|
+
Display one of the built in images on the display.
|
|
208
|
+
|
|
209
|
+
Example
|
|
210
|
+
-------
|
|
211
|
+
```python
|
|
212
|
+
from hub import light_matrix
|
|
213
|
+
# Update pixels to show an image on Light Matrix using the show_image function
|
|
214
|
+
|
|
215
|
+
# Show a smiling face
|
|
216
|
+
light_matrix.show_image(light_matrix.IMAGE_HAPPY)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Parameters
|
|
220
|
+
----------
|
|
221
|
+
image : int
|
|
222
|
+
The id of the image to show. The range of available images is 1 to 67. There are consts on the `light_matrix` module for these.
|
|
223
|
+
"""
|
|
224
|
+
pass
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
async def write(
|
|
228
|
+
text: str, intensity: int = 100, time_per_character: int = 500
|
|
229
|
+
) -> Awaitable:
|
|
230
|
+
"""
|
|
231
|
+
Displays text on the Light Matrix, one letter at a time, scrolling from right to left except if there is a single character to show which will not scroll
|
|
232
|
+
|
|
233
|
+
Example
|
|
234
|
+
-------
|
|
235
|
+
```python
|
|
236
|
+
from hub import light_matrix
|
|
237
|
+
# Write a message to the hub
|
|
238
|
+
light_matrix.write("Hello, world!")
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Parameters
|
|
242
|
+
----------
|
|
243
|
+
text : str
|
|
244
|
+
The text to display
|
|
245
|
+
intensity : int
|
|
246
|
+
How bright to light up the pixel
|
|
247
|
+
time_per_character : int
|
|
248
|
+
How long to show each character on the display
|
|
249
|
+
"""
|