Skip to content

Commit

Permalink
reformat code, bump version to 0.10, close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
superfashi committed Sep 3, 2021
1 parent c33848f commit e86f951
Show file tree
Hide file tree
Showing 21 changed files with 480 additions and 473 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ An unofficial Python library for [Sphero](https://sphero.com/) toys that support
- [x] Sphero BB-8
- [x] Sphero BB-9E
- [x] Sphero R2-D2 / R2-Q5
- [o] Sphero BOLT (In Progress)
- [ ] Sphero SPRK+ / SPRK 2.0
- [ ] Sphero Mini
- [x] Sphero BOLT (In Progress)
- [x] Sphero SPRK+ / SPRK 2.0
- [x] Sphero Mini
- [x] Sphero RVR

The logic is written based on reverse-engineering the official [Sphero Edu for Android](https://play.google.com/store/apps/details?id=com.sphero.sprk), with the help from available documentation and other unofficial community-based Sphero libraries like [igbopie/spherov2.js](https://github.com/igbopie/spherov2.js) and [EnotYoyo/pysphero](https://github.com/EnotYoyo/pysphero).
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
author = 'Hanbang Wang'

# The short X.Y version
version = '0.9'
version = '0.10'
# The full version, including alpha/beta/rc tags
release = '0.9'
release = '0.10'

# -- General configuration ---------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='spherov2',
version='0.9',
version='0.10',
author='Hanbang Wang', # and 'Elionardo Feliciano',
author_email='[email protected]', # and '[email protected]',
license='MIT',
Expand Down
1 change: 1 addition & 0 deletions spherov2/adapter/tcp_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def recvall(s, size):

def get_tcp_adapter(host: str, port: int = 50004):
"""Gets an anonymous ``TCPAdapter`` with the given address and port."""

class TCPAdapter:
@staticmethod
def scan_toys(timeout=5.0):
Expand Down
1 change: 0 additions & 1 deletion spherov2/commands/async_.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from enum import IntEnum, IntFlag
import struct
from enum import IntEnum, IntFlag

Expand Down
6 changes: 4 additions & 2 deletions spherov2/commands/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from enum import IntEnum, IntFlag
from enum import IntEnum

from spherov2.commands import Commands


class BleCentralStates(IntEnum):
NOT_YET_INITIALIZED = 0
DISCONNECTED = 1
Expand All @@ -10,7 +12,7 @@ class BleCentralStates(IntEnum):
CONNECTED = 5
DISCONNECTING = 6
CONFIGURING = 7


class BleCentralStates(IntEnum):
NOT_YET_INITIALIZED = 0
Expand Down
7 changes: 4 additions & 3 deletions spherov2/commands/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def set_compressed_frame_player_frame_rotation(toy, b_arr, j, b, proc=None): #
@staticmethod
def set_compressed_frame_player_text_scrolling(toy, b_arr, j, b, proc=None): # Untested / Unknown Param Names
toy._execute(IO._encode(toy, 59, proc, [*b_arr, j, b]))

set_compressed_frame_player_text_scrolling_notify = (26, 60, 0xff), lambda listener, p: listener(p.data[0]) # Untested / Unknown Param Names


set_compressed_frame_player_text_scrolling_notify = (26, 60, 0xff), lambda listener, p: listener(
p.data[0]) # Untested / Unknown Param Names

@staticmethod
def draw_compressed_frame_player_line(toy, x1, y1, x2, y2, r, g, b, proc=None):
toy._execute(IO._encode(toy, 61, proc, [x1, y1, x2, y2, r, g, b]))
Expand Down
2 changes: 1 addition & 1 deletion spherov2/commands/system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_board_revision(toy, proc=None):
@staticmethod
def get_mac_address(toy, proc=None):
return toy._execute(SystemInfo._encode(toy, 6, proc)).data

@staticmethod
def get_model_number(toy, proc=None):
return toy._execute(SystemInfo._encode(toy, 18, proc)).data
Expand Down
3 changes: 2 additions & 1 deletion spherov2/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from spherov2.toy import Toy
from spherov2.toy.bb8 import BB8
from spherov2.toy.bb9e import BB9E
from spherov2.toy.bolt import BOLT
from spherov2.toy.mini import Mini
from spherov2.toy.ollie import Ollie
from spherov2.toy.r2d2 import R2D2
from spherov2.toy.r2q5 import R2Q5
from spherov2.toy.rvr import RVR
from spherov2.toy.bolt import BOLT


class ToyNotFoundError(Exception):
...
Expand Down
25 changes: 14 additions & 11 deletions spherov2/sphero_edu.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,30 +385,33 @@ def strobe(self, color: Color, period: float, count: int):
time.sleep(period)

# TODO Sphero BOLT Lights
def set_matrix_pixel(self, x:int, y:int, color: Color):
def set_matrix_pixel(self, x: int, y: int, color: Color):
"""For Sphero BOLT: Changes the color of BOLT's matrix at X and Y value. 8x8
"""
strMapLoc:str = str(x) + ':' + str(y)
self.__leds[strMapLoc] = bound_color(color, self.__leds[strMapLoc]) #TODO: Do this in a way that works with line and fill
strMapLoc: str = str(x) + ':' + str(y)
self.__leds[strMapLoc] = bound_color(color, self.__leds[
strMapLoc]) # TODO: Do this in a way that works with line and fill
ToyUtil.set_matrix_pixel(self.__toy, x, y, **self.__leds[strMapLoc]._asdict(), is_user_color=False)

def set_matrix_line(self, x1: int, y1: int, x2: int, y2: int, color: Color):
"""For Sphero BOLT: Changes the color of BOLT's matrix from x1,y1 to x2,y2 in a line. 8x8
"""
strMapLoc:str = str(x1) + 'x' + str(y1) + '|' + str(x2) + 'x' + str(y2)
self.__leds[strMapLoc] = bound_color(color, self.__leds[strMapLoc]) #TODO: Do this in a way that works with pixel and fill (needs to be accurate to diagonal lines)
strMapLoc: str = str(x1) + 'x' + str(y1) + '|' + str(x2) + 'x' + str(y2)
self.__leds[strMapLoc] = bound_color(color, self.__leds[
strMapLoc]) # TODO: Do this in a way that works with pixel and fill (needs to be accurate to diagonal lines)
ToyUtil.set_matrix_line(self.__toy, x1, y1, x2, y2, **self.__leds[strMapLoc]._asdict(), is_user_color=False)

def set_matrix_fill(self, x1: int, y1: int, x2: int, y2: int, color: Color):
"""For Sphero BOLT: Changes the color of BOLT's matrix from x1,y1 to x2,y2 in a box. 8x8
"""
strMapLoc:str = str(x1) + 'x' + str(y1) + '[]' + str(x2) + 'x' + str(y2)
self.__leds[strMapLoc] = bound_color(color, self.__leds[strMapLoc]) #TODO: Do this in a way that works with pixel and line
strMapLoc: str = str(x1) + 'x' + str(y1) + '[]' + str(x2) + 'x' + str(y2)
self.__leds[strMapLoc] = bound_color(color, self.__leds[
strMapLoc]) # TODO: Do this in a way that works with pixel and line
ToyUtil.set_matrix_fill(self.__toy, x1, y1, x2, y2, **self.__leds[strMapLoc]._asdict(), is_user_color=False)
def register_matrix_animation(self, s, s2, z, s3, s_arr, i, i_arr): #TODO: fix this function

def register_matrix_animation(self, s, s2, z, s3, s_arr, i, i_arr): # TODO: fix this function
ToyUtil.register_matrix_animation(self.__toy, s, s2, z, s3, s_arr, i, i_arr);

def play_matrix_animation(self, s):
ToyUtil.play_matrix_animation(self.__toy, s);

Expand Down
17 changes: 9 additions & 8 deletions spherov2/test/BB8Test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#python3
# python3
import time

from spherov2 import scanner
from spherov2.sphero_edu import SpheroEduAPI
from spherov2.types import Color
Expand All @@ -11,15 +12,15 @@
print("Connected.")
with SpheroEduAPI(toy) as droid:
print("Testing Main LED")
droid.set_main_led(Color(r=255, g=0, b=0)) #Sets Main LED
droid.set_main_led(Color(r=255, g=0, b=0)) # Sets Main LED
time.sleep(1)

print("Testing Aiming LED")
droid.set_back_led(255) #Sets Aiming light on
droid.set_back_led(255) # Sets Aiming light on
time.sleep(1)

print("Testing Roll")
droid.roll(0,100,5)
droid.roll(0, 100, 5)
time.sleep(1)
print("Testing End!")

print("Testing End!")
45 changes: 23 additions & 22 deletions spherov2/test/BoltTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#python3
# python3
import time

from spherov2 import scanner
from spherov2.sphero_edu import SpheroEduAPI
from spherov2.types import Color
Expand All @@ -11,36 +12,36 @@
print("Connected.")
with SpheroEduAPI(toy) as droid:
print("Testing Main LED")
droid.set_main_led(Color(r=0, g=0, b=255)) #Sets whole Matrix
droid.set_main_led(Color(r=0, g=0, b=255)) # Sets whole Matrix
time.sleep(1)
print("Testing Front LED")
droid.set_front_led(Color(r=0, g=255, b=0)) #Sets front LED
droid.set_front_led(Color(r=0, g=255, b=0)) # Sets front LED
time.sleep(1)
print("Testing Back LED")
droid.set_back_led(Color(r=255, g=0, b=0)) #Sets back LED
droid.set_back_led(Color(r=255, g=0, b=0)) # Sets back LED
time.sleep(1)
print("Set Matrix Pixel")
droid.set_matrix_pixel(0, 0, Color(r=255, g=255, b=0)) #Set Matrix Pixel
droid.set_matrix_pixel(0, 0, Color(r=255, g=255, b=0)) # Set Matrix Pixel
time.sleep(1)
print("Set Matrix Line")
droid.set_matrix_line(1, 0, 1, 7, Color(r=255, g=0, b=255)) #Set Matrix Line
droid.set_matrix_line(1, 0, 1, 7, Color(r=255, g=0, b=255)) # Set Matrix Line
time.sleep(1)
print("Set Matrix Fill")
droid.set_matrix_fill(2, 0, 6, 6, Color(r=0, g=255, b=255)) #Set Matrix Box
droid.set_matrix_fill(2, 0, 6, 6, Color(r=0, g=255, b=255)) # Set Matrix Box
time.sleep(2)
print("Testing End!")
#Smile (manual)
#droid.set_main_led(Color(r=0, g=0, b=0)) #Sets whole Matrix
#droid.set_matrix_line(2, 7, 5, 7, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(1, 6, 6, 6, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(0, 5, 1, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(3, 5, 4, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(6, 5, 7, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(0, 4, 7, 4, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_line(0, 3, 7, 3, Color(r=255, g=0, b=255)) #Set Matrix Line
#droid.set_matrix_pixel(0, 2, Color(r=255, g=0, b=255)) #Set Matrix Pixel
#droid.set_matrix_pixel(7, 2, Color(r=255, g=0, b=255)) #Set Matrix Pixel
#droid.set_matrix_pixel(1, 1, Color(r=255, g=0, b=255)) #Set Matrix Pixel
#droid.set_matrix_pixel(6, 1, Color(r=255, g=0, b=255)) #Set Matrix Pixel
#droid.set_matrix_line(2, 0, 5, 0, Color(r=255, g=0, b=255)) #Set Matrix Line

# Smile (manual)
# droid.set_main_led(Color(r=0, g=0, b=0)) #Sets whole Matrix
# droid.set_matrix_line(2, 7, 5, 7, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(1, 6, 6, 6, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(0, 5, 1, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(3, 5, 4, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(6, 5, 7, 5, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(0, 4, 7, 4, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_line(0, 3, 7, 3, Color(r=255, g=0, b=255)) #Set Matrix Line
# droid.set_matrix_pixel(0, 2, Color(r=255, g=0, b=255)) #Set Matrix Pixel
# droid.set_matrix_pixel(7, 2, Color(r=255, g=0, b=255)) #Set Matrix Pixel
# droid.set_matrix_pixel(1, 1, Color(r=255, g=0, b=255)) #Set Matrix Pixel
# droid.set_matrix_pixel(6, 1, Color(r=255, g=0, b=255)) #Set Matrix Pixel
# droid.set_matrix_line(2, 0, 5, 0, Color(r=255, g=0, b=255)) #Set Matrix Line
17 changes: 9 additions & 8 deletions spherov2/test/MiniTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#python3
# python3
import time

from spherov2 import scanner
from spherov2.sphero_edu import SpheroEduAPI
from spherov2.types import Color
Expand All @@ -11,15 +12,15 @@
print("Connected.")
with SpheroEduAPI(toy) as droid:
print("Testing Main LED")
droid.set_main_led(Color(r=255, g=0, b=0)) #Sets Main LED
droid.set_main_led(Color(r=255, g=0, b=0)) # Sets Main LED
time.sleep(1)

print("Testing Aiming LED")
droid.set_back_led(255) #Sets Aiming light on
droid.set_back_led(255) # Sets Aiming light on
time.sleep(1)

print("Testing Roll")
droid.roll(0,100,5)
droid.roll(0, 100, 5)
time.sleep(1)
print("Testing End!")

print("Testing End!")
22 changes: 11 additions & 11 deletions spherov2/toy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ class ToyV2(Toy):
_packet = PacketV2
_handshake = []
_response_uuid = _send_uuid = '00010002-574f-4f20-5370-6865726f2121'
#_response_uuid = _send_uuid = '00010002-574f-4f20-5370-6865726f2121' #Available responses
#_response_uuid = _send_uuid = '00010003-574f-4f20-5370-6865726f2121'
#_response_uuid = _send_uuid = '00020004-574f-4f20-5370-6865726f2121'
#_response_uuid = _send_uuid = '00020005-574f-4f20-5370-6865726f2121'
#_response_uuid = _send_uuid = '00020002-574f-4f20-5370-6865726f2121'
#_response_uuid = _send_uuid = '22bb746f-2bbd-7554-2d6f-726568705327'
#_response_uuid = _send_uuid = '22bb746f-2bb2-7554-2d6f-726568705327'
#_response_uuid = _send_uuid = '22bb746f-2bbf-7554-2d6f-726568705327'
#_response_uuid = _send_uuid = '22bb746f-2ba1-7554-2d6f-726568705327'
#_response_uuid = _send_uuid = '22bb746f-2ba6-7554-2d6f-726568705327'
#_response_uuid = _send_uuid = '22bb746f-2bb0-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '00010002-574f-4f20-5370-6865726f2121' #Available responses
# _response_uuid = _send_uuid = '00010003-574f-4f20-5370-6865726f2121'
# _response_uuid = _send_uuid = '00020004-574f-4f20-5370-6865726f2121'
# _response_uuid = _send_uuid = '00020005-574f-4f20-5370-6865726f2121'
# _response_uuid = _send_uuid = '00020002-574f-4f20-5370-6865726f2121'
# _response_uuid = _send_uuid = '22bb746f-2bbd-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '22bb746f-2bb2-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '22bb746f-2bbf-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '22bb746f-2ba1-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '22bb746f-2ba6-7554-2d6f-726568705327'
# _response_uuid = _send_uuid = '22bb746f-2bb0-7554-2d6f-726568705327'
46 changes: 19 additions & 27 deletions spherov2/toy/bb8.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
from collections import OrderedDict
from enum import IntEnum
from functools import partialmethod, lru_cache
from enum import IntEnum
from functools import lru_cache

from spherov2.commands.async_ import Async
from spherov2.commands.bootloader import Bootloader
from spherov2.commands.core import Core
from spherov2.commands.power import Power
from spherov2.commands.sensor import Sensor
from spherov2.commands.sphero import Sphero
from spherov2.controls.v1 import DriveControl, FirmwareUpdateControl, LedControl, SensorControl, StatsControl
from spherov2.toy import ToyV2, ToySensor, Toy
from spherov2.controls.v1 import FirmwareUpdateControl
from spherov2.toy.sphero import Sphero as SPHERO
from spherov2.types import ToyType


class BB8(SPHERO):
toy_type = ToyType('BB-8', 'BB-', 'BB', .06)
#_send_uuid = '22bb746f-2ba1-7554-2d6f-726568705327'
#_response_uuid = '22bb746f-2ba6-7554-2d6f-726568705327'
#_handshake = [('22bb746f-2bbd-7554-2d6f-726568705327', bytearray(b'011i3')),

# _send_uuid = '22bb746f-2ba1-7554-2d6f-726568705327'
# _response_uuid = '22bb746f-2ba6-7554-2d6f-726568705327'
# _handshake = [('22bb746f-2bbd-7554-2d6f-726568705327', bytearray(b'011i3')),
# ('22bb746f-2bb2-7554-2d6f-726568705327', bytearray([7]))]

class LEDs(IntEnum):
BODY_RED = 0
BODY_GREEN = 1
BODY_BLUE = 2
AIMING = 3
HEAD = 4

class Animations(IntEnum):
EMOTE_ALARM = 0
EMOTE_NO = 1
Expand Down Expand Up @@ -76,21 +72,17 @@ class Animations(IntEnum):
EYE_2 = 46
EYE_3 = 47
EYE_4 = 48


#Async - Sphero
#Bootloader - Sphero
#Core
get_factory_config_block_crc = Core.get_factory_config_block_crc #GetFactorConfigBlockCrcCommand


#Sphero
get_sku = Sphero.get_sku #GetSkuCommand


#Controls - Sphero

# Async - Sphero
# Bootloader - Sphero
# Core
get_factory_config_block_crc = Core.get_factory_config_block_crc # GetFactorConfigBlockCrcCommand

# Sphero
get_sku = Sphero.get_sku # GetSkuCommand

# Controls - Sphero
@property
@lru_cache(None)
def firmware_update_control(self):
return FirmwareUpdateControl(self)

Loading

0 comments on commit e86f951

Please sign in to comment.