diff --git a/README.md b/README.md index 4f68429..ea7f0f8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/demo/test.py b/demo/test.py index 270f9f1..a9a6e1f 100644 --- a/demo/test.py +++ b/demo/test.py @@ -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, diff --git a/demo/test_keybindings.py b/demo/test_keybindings.py index 5ab36d7..6df963e 100644 --- a/demo/test_keybindings.py +++ b/demo/test_keybindings.py @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/pyproject.toml b/pyproject.toml index bb36497..41f49f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ @@ -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" diff --git a/weallcode_robot/commands.py b/weallcode_robot/commands.py index 502cf37..4755a01 100644 --- a/weallcode_robot/commands.py +++ b/weallcode_robot/commands.py @@ -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 diff --git a/weallcode_robot/robot.py b/weallcode_robot/robot.py index 5993695..51bd768 100644 --- a/weallcode_robot/robot.py +++ b/weallcode_robot/robot.py @@ -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', diff --git a/weallcode_robot/robot_ui.py b/weallcode_robot/robot_ui.py index 77bb8bb..3161e2b 100644 --- a/weallcode_robot/robot_ui.py +++ b/weallcode_robot/robot_ui.py @@ -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.") @@ -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 @@ -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