Skip to content

Commit

Permalink
Added disconnect and RollMode. (#7)
Browse files Browse the repository at this point in the history
Semi-breaking change to roll command by adding parameter that has default.
Version 0.0.3
  • Loading branch information
irvinec committed Feb 20, 2019
1 parent 252714a commit 7ba4674
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup
import sys

__version__ = '0.0.2'
__version__ = '0.0.3'

ext_modules = []

Expand Down
35 changes: 32 additions & 3 deletions spheropy/spheropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@

# TODO: Need more parameter validation on functions and throughout.

class RollMode(enum.Enum):
NORMAL = enum.auto()
IN_PLACE_ROTATE = enum.auto()
FAST_ROTATE = enum.auto()

# region Sphero

class Sphero(object):
Expand Down Expand Up @@ -109,6 +114,12 @@ async def connect(self,
self._bluetooth_interface.connect(num_retry_attempts=num_retry_attempts)
print('Connected to Sphero.')

def disconnect(self):
"""Disconnect from the Sphero.
"""
if self._bluetooth_interface:
self._bluetooth_interface.disconnect()

async def ping(self,
wait_for_response=True,
reset_inactivity_timeout=True,
Expand Down Expand Up @@ -725,6 +736,7 @@ async def set_back_led(self,
async def roll(self,
speed,
heading_in_degrees,
mode=RollMode.NORMAL,
wait_for_response=True,
reset_inactivity_timeout=True,
response_timeout_in_seconds=None):
Expand All @@ -747,6 +759,8 @@ async def roll(self,
heading_in_degrees (int):
The relative heading in degrees.
The valid range is [0, 359]
mode (spheropy.RollMode):
Indicates the mode to use when rolling.
wait_for_response (bool, True):
If True, will wait for a response from the Sphero
reset_inactivity_timeout (bool, True):
Expand All @@ -759,6 +773,7 @@ async def roll(self,
command = _create_roll_command(
speed,
heading_in_degrees,
mode,
sequence_number=self._get_and_increment_command_sequence_number(),
wait_for_response=wait_for_response,
reset_inactivity_timeout=reset_inactivity_timeout)
Expand Down Expand Up @@ -1700,15 +1715,29 @@ def _create_set_back_led_output_command(
def _create_roll_command(
speed,
heading_in_degrees,
mode,
sequence_number,
wait_for_response,
reset_inactivity_timeout):
"""
"""
if heading_in_degrees < 0 or heading_in_degrees > 359:
raise ValueError(
"heading_in_degrees must be in the range [0, 359]. heading was {}"
.format(heading_in_degrees))
f'heading_in_degrees must be in the range [0, 359]. heading was {heading_in_degrees}')

state = 0
if mode == RollMode.IN_PLACE_ROTATE:
speed = 0
state = 1
elif mode == RollMode.FAST_ROTATE:
state = 2
elif mode == RollMode.NORMAL:
if speed > 0:
state = 1
else:
state = 0
else:
raise ValueError('Unknown RollMode.')

return _ClientCommandPacket(
device_id=_DEVICE_ID_SPHERO,
Expand All @@ -1718,7 +1747,7 @@ def _create_roll_command(
speed,
_get_byte_at_index(heading_in_degrees, 1),
_get_byte_at_index(heading_in_degrees, 0),
1 if speed > 0 else 0 # This is the STATE value that was originally used in CES firmware.
state
],
wait_for_response=wait_for_response,
reset_inactivity_timeout=reset_inactivity_timeout)
Expand Down

0 comments on commit 7ba4674

Please sign in to comment.