Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions dotbot/joystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JoystickController:

def __init__(self, joystick_index, client, dotbot_address, application):
"""Initialize the joystick controller."""
self.api = rest_client
self.client = client
self.dotbots = []
self.dotbot_address = dotbot_address
self.application = APPLICATION_TYPE_MAP[application]
Expand Down Expand Up @@ -99,24 +99,20 @@ def pos_from_joystick(self):

async def fetch_active_dotbots(self):
while 1:
self.dotbots = await self.api.fetch_active_dotbots(
self.dotbots = await self.client.fetch_dotbots(
query=DotBotQueryModel(status=DotBotStatus.ACTIVE)
)
await asyncio.sleep(1)

async def start(self):
"""Starts to read continuously joystick positions."""
asyncio.create_task(
self.fetch_active_dotbots(
query=DotBotQueryModel(status=DotBotStatus.ACTIVE)
)
)
asyncio.create_task(self.fetch_active_dotbots())
while True:
# fetch positions from joystick
positions = self.pos_from_joystick()
if positions != NULL_POSITION or self.previous_positions != NULL_POSITION:
self._logger.info("refresh positions", positions=positions)
await self.api.send_move_raw_command(
await self.client.send_move_raw_command(
self.selected_dotbot,
self.application,
DotBotMoveRawCommandModel(
Expand Down
16 changes: 6 additions & 10 deletions dotbot/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class KeyboardEvent:
class KeyboardController:
"""Dotbot controller for a keyboard interface."""

def __init__(self, rest_client, dotbot_address, application):
def __init__(self, client, dotbot_address, application):
"""Initializes the keyboard controller."""
self.api = rest_client
self.client = client
self.dotbots = []
self.dotbot_address = dotbot_address
self.application = APPLICATION_TYPE_MAP[application]
Expand Down Expand Up @@ -175,7 +175,7 @@ def on_release(key):
if hasattr(event.key, "char") and event.key.char in COLOR_KEYS:
red, green, blue = rgb_from_key(event.key.char)
self._logger.info("color pressed", red=red, green=green, blue=blue)
await self.api.send_rgb_led_command(
await self.client.send_rgb_led_command(
self.selected_dotbot,
DotBotRgbLedCommandModel(red=red, green=green, blue=blue),
)
Expand Down Expand Up @@ -225,7 +225,7 @@ async def refresh_speeds(self):
left_speed, right_speed = self.speeds_from_keys()
if (left_speed, right_speed) != (0, 0) or self.previous_speeds != (0, 0):
self._logger.info("refresh speeds", left=left_speed, right=right_speed)
await self.api.send_move_raw_command(
await self.client.send_move_raw_command(
self.selected_dotbot,
self.application,
DotBotMoveRawCommandModel(
Expand All @@ -237,18 +237,14 @@ async def refresh_speeds(self):

async def fetch_active_dotbots(self):
while 1:
self.dotbots = await self.api.fetch_active_dotbots(
self.dotbots = await self.client.fetch_dotbots(
query=DotBotQueryModel(status=DotBotStatus.ACTIVE)
)
await asyncio.sleep(1)

async def start(self):
"""Starts to continuously listen on keyboard key press/release events."""
asyncio.create_task(
self.fetch_active_dotbots(
query=DotBotQueryModel(status=DotBotStatus.ACTIVE)
)
)
asyncio.create_task(self.fetch_active_dotbots())
asyncio.create_task(self.update_active_keys())
while 1:
await self.refresh_speeds()
Expand Down