Skip to content

UI and button #22

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

Merged
merged 11 commits into from
May 25, 2024
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ robot.move(0, 0) # Stop the robot

robot.led(0, 0, 0) # Turn off the LED

a = robot.set_key_binding('a').led(0, 0, 255, 1) # set LED color one button A
w = robot.set_key_binding('w').move(100, 100) # bind move forward to key W
a = robot.setKeyBinding('a').led(0, 0, 255, 1) # set LED color one button A
w = robot.setKeyBinding('w').move(100, 100) # bind move forward to key W
```

## API
Expand All @@ -52,8 +52,8 @@ robot = Robot("WAC")

#### Methods

- `led(r: int, g: int, b: int)`: Sets the LED color. Values should be integers between 0 and 255.
- `move(right: int, left: int)`: Sets the motor speeds. Values should be integers between -100 and 100.
- `led(red: int, green: int, blue: int, duration: int)`: Sets the LED color. Values should be integers between 0 and 255.
- `move(left: int, right: int, duration: int)`: Sets the motor speeds. Values should be integers between -100 and 100.
- `wait(duration: float)`: Adds a wait command with a given duration in seconds.
- `run()`: Executes the commands in the order they were added.

Expand Down
13 changes: 7 additions & 6 deletions demo/test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from weallcode_robot import Robot

# Create a robot
name = "WAC-C9B9"

name = "dink"
robot = Robot(name)

# assign button A to set LED to blue for 1 second
robot.button_a.led(0, 0, 255, 1)
robot.buttonA.led(0, 0, 255, 1)

# assign button B to set LED to red & buzz at 440Hz for 0.5 seconds
robot.button_b.led(255, 0, 0)
robot.buttonB.led(255, 0, 0)

a = robot.set_key_binding('a').led(0, 0, 255, 1)
# a = robot.setKeyBinding('a').led(0, 0, 255, 1)

# Display the robot's name (uppercased) on the robot's display for 2.5 seconds
robot.displayText(name.upper(), 2.5)

robot.move(92,100, 1.5)
# Display bullseye for 1 second
robot.displayDots(
# fmt: off
# fmt: offa
[
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
Expand Down
26 changes: 17 additions & 9 deletions demo/test_keybindings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from weallcode_robot import Robot

# Create a robot
name = "dink"
name = "WAC-2463"
robot = Robot(name)

robot.led(0,0,255,1)
# a = robot.setKeyBinding('a').led(0, 0, 255, 1)
# b = robot.setKeyBinding('b').led(255, 0, 0, 1)
# c = robot.setKeyBinding('c').led(0, 255, 0, 1)

a = robot.set_key_binding('a').led(0, 0, 255, 1)
b = robot.set_key_binding('b').led(255, 0, 0, 1)
c = robot.set_key_binding('c').led(0, 255, 0, 1)
x = robot.setKeyBinding('x')
x.displayText('x')

w = robot.set_key_binding('w').move(100, 100).displayDots(

w = robot.setKeyBinding('w').move(100, 100).displayDots(
# fmt: off
[
0, 0, 1, 0, 0,
Expand All @@ -20,7 +22,9 @@
0, 0, 1, 0, 0,
]
)
a = robot.set_key_binding('a').move(100, 0).displayDots(

a = robot.setKeyBinding('a').move(100, 0).displayDots(

# fmt: off
[
0, 0, 0, 0, 0,
Expand All @@ -30,7 +34,9 @@
0, 0, 0, 0, 0,
]
)
s = robot.set_key_binding('d').move(0, 100).displayDots(

s = robot.setKeyBinding('d').move(0, 100).displayDots(

# fmt: off
[
0, 0, 0, 0, 0,
Expand All @@ -40,7 +46,9 @@
0, 0, 0, 0, 0,
]
)
d = robot.set_key_binding('s').move(-100, -100).displayDots(

d = robot.setKeyBinding('s').move(-100, -100).displayDots(

# fmt: off
[
0, 0, 1, 0, 0,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "weallcode_robot"
version = "3.0.3"
version = "3.1.0"
description = "Micro:bit TinyBit BLE Python Library"
license = "MIT"
authors = [
Expand Down Expand Up @@ -38,7 +38,7 @@ profile = "black"

[tool.poetry.dependencies]
python = "^3.11"
bleak = "^0.20.1"
bleak = "^0.22.1"

[tool.poetry.group.dev.dependencies]
black = "^23.3.0"
Expand Down
2 changes: 1 addition & 1 deletion weallcode_robot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def led(self, red, green, blue, duration: float = 0):
self.wait(duration)
return self

def move(self, right, left, duration: float = 0):
def move(self, left, right, duration: float = 0):
self.put(MoveCommand(left, right))
self.wait(duration)
return self
Expand Down
42 changes: 21 additions & 21 deletions weallcode_robot/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ def __init__(self, name, debug=False):
self.buzz = self.main_queue.buzz
self.clear = self.main_queue.clear

self.button_a = DynamicObject()
self.button_a.led = self.button_a_queue.led
self.button_a.move = self.button_a_queue.move
self.button_a.stop = self.button_a_queue.stop
self.button_a.wait = self.button_a_queue.wait
self.button_a.displayText = self.button_a_queue.displayText
self.button_a.displayDots = self.button_a_queue.displayDots
self.button_a.clearDisplay = self.button_a_queue.clearDisplay
self.button_a.buzz = self.button_a_queue.buzz
self.button_a.clear = self.button_a_queue.clear
self.buttonA = DynamicObject()
self.buttonA.led = self.button_a_queue.led
self.buttonA.move = self.button_a_queue.move
self.buttonA.stop = self.button_a_queue.stop
self.buttonA.wait = self.button_a_queue.wait
self.buttonA.displayText = self.button_a_queue.displayText
self.buttonA.displayDots = self.button_a_queue.displayDots
self.buttonA.clearDisplay = self.button_a_queue.clearDisplay
self.buttonA.buzz = self.button_a_queue.buzz
self.buttonA.clear = self.button_a_queue.clear

self.button_b = DynamicObject()
self.button_b.led = self.button_b_queue.led
self.button_b.move = self.button_b_queue.move
self.button_b.stop = self.button_b_queue.stop
self.button_b.wait = self.button_b_queue.wait
self.button_b.displayText = self.button_b_queue.displayText
self.button_b.displayDots = self.button_b_queue.displayDots
self.button_b.clearDisplay = self.button_b_queue.clearDisplay
self.button_b.buzz = self.button_b_queue.buzz
self.button_b.clear = self.button_b_queue.clear
self.buttonB = DynamicObject()
self.buttonB.led = self.button_b_queue.led
self.buttonB.move = self.button_b_queue.move
self.buttonB.stop = self.button_b_queue.stop
self.buttonB.wait = self.button_b_queue.wait
self.buttonB.displayText = self.button_b_queue.displayText
self.buttonB.displayDots = self.button_b_queue.displayDots
self.buttonB.clearDisplay = self.button_b_queue.clearDisplay
self.buttonB.buzz = self.button_b_queue.buzz
self.buttonB.clear = self.button_b_queue.clear

atexit.register(self.ui.run)

def set_key_binding(self, key) -> CommandQueue:
def setKeyBinding(self, key) -> CommandQueue:
valid_keys = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
Expand Down
18 changes: 9 additions & 9 deletions weallcode_robot/robot_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,10 @@ def bind(self, key) -> CommandQueue:
return self.key_commands[key]

async def _connect_and_run(self):
scanner = BleakScanner()
self.update_status(f'scanning for {self.robot.display_name} ...')
devices = await scanner.discover(timeout=2.0, return_adv=False)
device = await BleakScanner.find_device_by_name(self.robot.name, timeout=10.0)

self.update_status(f'connecting to {self.robot.display_name} ...')
device = None
for d in devices:
if d.name is not None and d.name.lower() == self.robot.name:
device = d
break

if device is None:
self.update_status(f"device {self.robot.name} not found. Quit and try again.")
Expand Down Expand Up @@ -129,6 +123,9 @@ def _button_handler_callback(characteristic: BleakGATTCharacteristic, data: byte

await self.execute(commands)

if self.robot.button_a_queue.empty() and self.robot.button_b_queue.empty() and not self.key_commands:
self.exit()

async def execute(self, commands):
if self.running == 1: return

Expand All @@ -138,8 +135,11 @@ async def execute(self, commands):
# self.update_status(f"executing {command.__class__.__name__} ...")
await command.execute(self.client)

self.update_status('idle')
self.running = 0
if not self.key_commands and self.robot.button_a_queue.empty() and self.robot.button_b_queue.empty():
return
else:
self.update_status('idle')
self.running = 0

async def clear(self):
self.running = 1
Expand Down