# toio.py
[](https://pypi.org/project/toio-py/)
This is a library for controlling [toio™Core Cube](https://toio.io/platform/cube/) from Python.
Based on [toio Core Cube Specifications](https://toio.github.io/toio-spec/en/) v2.3.0.
**(日本語版 README.md は[こちら](https://github.com/toio/toio.py/blob/main/README.ja.md))**
## Features
- Uses [bleak](https://github.com/hbldh/bleak) for Bluetooth communication
- Supports Python 3.11 and later versions
- Multi-platform (Windows, Linux, macOS)
- No dedicated Bluetooth dongle required
- Asynchronous API (ToioCoreCube API) based on the toio Core Cube Specifications and synchronous API (SimpleCube API) for easy cube control
- Scanning function by specifying BLE addresses and cube-specific names
- API to control cube functions classified by characteristics (ToioCoreCube API)
- Ability to scan paired cubes (Windows only)
## System requirements
### Primaly tested environment
- Windows: Windows 10 (21H2)
### Secondary tested environment
- Linux: Ubuntu22.04
- macOS: macOS 12(Monterey)
## Setup and tutorial
See below for instructions on how to set up and run the tutorial.
- [Setup Guide (English)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.en.md)
- [Setup Guide (Japanese)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.ja.md)
- [Setup Guide (Chinese)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.zh.md)
---
## SimpleCube API
See [SIMPLE_API.en.md](https://github.com/toio/toio.py/blob/main/SIMPLE_API.en.md) for information on the SimpleCube API for easily controlling toio Core Cubes.
- [SIMPLE_API.en.md (English)](https://github.com/toio/toio.py/blob/main/SIMPLE_API.en.md)
- [SIMPLE_API.ja.md (Japanese)](https://github.com/toio/toio.py/blob/main/SIMPLE_API.ja.md)
---
## API document
- [API Document](https://toio.github.io/toio.py/)
---
## Implementation overview
toio.py consists of two classes, Scanner and ToioCoreCube.
### Scanner
Class for scanning cubes via the BLE interface.
You can scan for cubes in the following ways:
- Scan for nearby cubes
- Scan for a specific cube by name (the last 3 characters of the toio Core Cube name)
The following is available only for Windows and Linux:
- Scan for a specific cube by BLE address
The following is available only for Windows:
- Scan for cubes registered (paired) with OS
### ToioCoreCube
Class for controlling the cube.
ToioCoreCube has subclasses corresponding to the characteristics described in [toio CoreCube Specifications](https://toio.github.io/toio-spec/). You access the various functions of the cube via these subclasses.
## Sample code
### Scan and connect
Use `BLEScanner.scan()`.
The argument is the number of cubes to find in the scan.
If the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of the number of cubes found at the time of the timeout.
The following sample scans and connects nearby cubes.
Disconnects 3 seconds after connecting.
```Python
import asyncio
from toio import *
async def scan_and_connect():
dev_list = await BLEScanner.scan(num=1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
await asyncio.sleep(3)
await cube.disconnect()
return 0
if __name__ == "__main__":
asyncio.run(scan_and_connect())
```
### Scan and connect (scan by cube name)
Use `BLEScanner.scan_with_id()`.
The argument is [set (set type)](https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset), a 3-digit string at the end of the cube.
The argument is given as set even if only one unit is to be scanned.
If the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of cubes found at the time of the timeout.
```Python
dev_list = await BLEScanner.scan_with_id(cube_id={"C7f"})
```
Scan and connect `toio Core Cube-C7f`.
Disconnects 3 seconds after connecting.
```Python
import asyncio
from toio import *
async def scan_and_connect():
dev_list = await BLEScanner.scan_with_id(cube_id={"C7f"})
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
await asyncio.sleep(3)
await cube.disconnect()
return 0
if __name__ == "__main__":
asyncio.run(scan_and_connect())
```
### Scan and connect (scan paired cubes: supported on Windows only)
Windows only, paired cubes can be scanned.
Use `BLEScanner.scan_registered_cubes()`.
The argument is the number of cubes to find in the scan.
If the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of the number of cubes found at the time of the timeout.
```Python
dev_list = await BLEScanner.scan_registered_cubes()
```
Scan for paired cubes and connect them. (Pair the cube with Windows using "Add Bluetooth Device" before doing this.)
Disconnects 3 seconds after connecting.
```Python
import asyncio
from toio import *
async def scan_and_connect():
dev_list = await BLEScanner.scan_registered_cubes(num=1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
await asyncio.sleep(3)
await cube.disconnect()
return 0
if __name__ == "__main__":
asyncio.run(scan_and_connect())
```
### Get the cube location
Use the class `ToioCoreCube.api.id_information` to get the location information of the cube.
This class provides access to the [read sensor characteristic](https://toio.github.io/toio-spec/en/docs/ble_id).
The following code reads and displays the cube ID information 200 times.
It uses `read()` to read to the characteristic.
```Python
import asyncio
from toio import *
async def read_id():
device_list = await BLEScanner.scan(1)
assert len(device_list)
cube = ToioCoreCube(device_list[0].interface)
await cube.connect()
for n in range(200):
pos = await cube.api.id_information.read()
print("%4d:%s" % (n, str(pos)))
await cube.disconnect()
if __name__ == "__main__":
asyncio.run(read_id())
```
### Get the cube location (using notification)
You can receive notifications from the cube by registering a notification handler with `register_notification_handler()`.
Notifications are per each characteristic. A notification handler registered with `ToioCoreCube.api.id_information.register_notification_handler()` receives
receive only notifications from the read sensor.
The following code reads the ID by notification.
After 10 seconds, the handler is unregistered and disconnected.
```Python
import asyncio
from toio import *
# Notification handler
def notification_handler(payload: bytearray):
id_info = IdInformation.is_my_data(payload)
print(str(id_info))
async def read_id():
# connect to a cube
dev_list = await BLEScanner.scan(1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
# add notification handler
await cube.api.id_information.register_notification_handler(notification_handler)
await asyncio.sleep(10)
# remove notification handler
await cube.api.id_information.unregister_notification_handler(
notification_handler
)
await cube.disconnect()
return 0
if __name__ == "__main__":
asyncio.run(read_id())
```
The complete code that keeps displaying ID information until Ctlr-C is pressed is [examples/read_position.py](https://github.com/toio/toio.py/blob/main/examples/read_position.py).
### Motor control
The `ToioCoreCube.api.motor` class is used to control the motor.
This class provides access to the [motor's characteristic](https://toio.github.io/toio-spec/en/docs/ble_motor).
The following code uses `motor_control()` to rotate the cube in place for 2 seconds.
```Python
import asyncio
from toio import *
async def motor_1():
# connect to a cube
dev_list = await BLEScanner.scan(1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
# go
await cube.api.motor.motor_control(10, -10)
await asyncio.sleep(2)
# stop
await cube.api.motor.motor_control(0, 0)
await cube.disconnect()
return 0
if __name__ == "__main__":
asyncio.run(motor_1())
```
### Motor control (move to specified position)
Use `motor.motor_control_target()` to move the cube to a specified position on the mat.
```Python
import asyncio
from toio import *
# Notification handler
def notification_handler(payload: bytearray):
id_info = IdInformation.is_my_data(payload)
print(str(id_info))
async def motor_2():
# connect to a cube
dev_list = await BLEScanner.scan(1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
await cube.api.motor.register_notification_handler(notification_handler)
await cube.api.motor.motor_control_target(
timeout=5,
movement_type=MovementType.Linear,
speed=Speed(
max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
target=TargetPosition(
cube_location=CubeLocation(point=Point(x=200, y=200), angle=0),
rotation_option=RotationOption.AbsoluteOptimal,
),
)
await asyncio.sleep(4)
await cube.disconnect()
if __name__ == "__main__":
asyncio.run(motor_2())
```
### Motor control (move to multiple specified positions)
Use `motor.motor_control_multiple_targets()` to move the cube to a specified position on multiple mats.
```Python
import asyncio
from toio import *
async def motor_3():
# connect to a cube
dev_list = await BLEScanner.scan(1)
assert len(dev_list)
cube = ToioCoreCube(dev_list[0].interface)
await cube.connect()
targets = [
TargetPosition(
cube_location=CubeLocation(point=Point(x=250, y=250), angle=0), rotation_option=RotationOption.AbsoluteOptimal
),
TargetPosition(
cube_location=CubeLocation(point=Point(x=120, y=170), angle=0), rotation_option=RotationOption.AbsoluteOptimal
),
]
await cube.api.motor.motor_control_multiple_targets(
timeout=5,
movement_type=MovementType.Linear,
speed=Speed(
max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),
mode=WriteMode.Overwrite,
target_list=targets,
)
await asyncio.sleep(5)
await cube.disconnect()
if __name__ == "__main__":
asyncio.run(motor_3())
```
Raw data
{
"_id": null,
"home_page": "https://toio.io/",
"name": "toio-py",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "toio",
"author": "Sony Interactive Entertainment",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/6f/2f/f6586f281db5640ddb9c04c6615bb18f78e3f2db27dec6788048f9afb413/toio_py-1.0.2.tar.gz",
"platform": null,
"description": "\ufeff# toio.py\n\n[](https://pypi.org/project/toio-py/)\n\nThis is a library for controlling [toio\u2122Core Cube](https://toio.io/platform/cube/) from Python.\n\nBased on [toio Core Cube Specifications](https://toio.github.io/toio-spec/en/) v2.3.0.\n\n**\uff08\u65e5\u672c\u8a9e\u7248 README.md \u306f[\u3053\u3061\u3089](https://github.com/toio/toio.py/blob/main/README.ja.md)\uff09**\n\n## Features\n\n- Uses [bleak](https://github.com/hbldh/bleak) for Bluetooth communication\n- Supports Python 3.11 and later versions\n- Multi-platform (Windows, Linux, macOS)\n- No dedicated Bluetooth dongle required\n- Asynchronous API (ToioCoreCube API) based on the toio Core Cube Specifications and synchronous API (SimpleCube API) for easy cube control\n- Scanning function by specifying BLE addresses and cube-specific names\n- API to control cube functions classified by characteristics (ToioCoreCube API)\n- Ability to scan paired cubes (Windows only)\n\n## System requirements\n\n### Primaly tested environment\n\n- Windows: Windows 10 (21H2)\n\n### Secondary tested environment\n\n- Linux: Ubuntu22.04\n- macOS: macOS 12(Monterey)\n\n## Setup and tutorial\n\nSee below for instructions on how to set up and run the tutorial.\n\n- [Setup Guide (English)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.en.md)\n- [Setup Guide (Japanese)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.ja.md)\n- [Setup Guide (Chinese)](https://github.com/toio/toio.py/blob/main/SETUP_GUIDE.zh.md)\n\n---\n\n## SimpleCube API\n\nSee [SIMPLE_API.en.md](https://github.com/toio/toio.py/blob/main/SIMPLE_API.en.md) for information on the SimpleCube API for easily controlling toio Core Cubes.\n\n- [SIMPLE_API.en.md (English)](https://github.com/toio/toio.py/blob/main/SIMPLE_API.en.md)\n- [SIMPLE_API.ja.md (Japanese)](https://github.com/toio/toio.py/blob/main/SIMPLE_API.ja.md)\n\n---\n\n## API document\n\n- [API Document](https://toio.github.io/toio.py/)\n\n---\n\n## Implementation overview\n\ntoio.py consists of two classes, Scanner and ToioCoreCube.\n\n### Scanner\n\nClass for scanning cubes via the BLE interface.\n\nYou can scan for cubes in the following ways:\n\n- Scan for nearby cubes\n- Scan for a specific cube by name (the last 3 characters of the toio Core Cube name)\n\nThe following is available only for Windows and Linux:\n\n- Scan for a specific cube by BLE address\n\nThe following is available only for Windows:\n\n- Scan for cubes registered (paired) with OS\n\n### ToioCoreCube\n\nClass for controlling the cube.\n\nToioCoreCube has subclasses corresponding to the characteristics described in [toio CoreCube Specifications](https://toio.github.io/toio-spec/). You access the various functions of the cube via these subclasses.\n\n## Sample code\n\n### Scan and connect\n\nUse `BLEScanner.scan()`.\n\nThe argument is the number of cubes to find in the scan.\n\nIf the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of the number of cubes found at the time of the timeout.\n\nThe following sample scans and connects nearby cubes.\n\nDisconnects 3 seconds after connecting.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def scan_and_connect():\n dev_list = await BLEScanner.scan(num=1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n await asyncio.sleep(3)\n\n await cube.disconnect()\n return 0\n\nif __name__ == \"__main__\":\n asyncio.run(scan_and_connect())\n```\n\n### Scan and connect (scan by cube name)\n\nUse `BLEScanner.scan_with_id()`.\n\nThe argument is [set (set type)](https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset), a 3-digit string at the end of the cube.\nThe argument is given as set even if only one unit is to be scanned.\n\nIf the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of cubes found at the time of the timeout.\n\n```Python\n dev_list = await BLEScanner.scan_with_id(cube_id={\"C7f\"})\n```\n\nScan and connect `toio Core Cube-C7f`.\n\nDisconnects 3 seconds after connecting.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def scan_and_connect():\n dev_list = await BLEScanner.scan_with_id(cube_id={\"C7f\"})\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n await asyncio.sleep(3)\n\n await cube.disconnect()\n return 0\n\nif __name__ == \"__main__\":\n asyncio.run(scan_and_connect())\n```\n\n### Scan and connect (scan paired cubes: supported on Windows only)\n\nWindows only, paired cubes can be scanned.\n\nUse `BLEScanner.scan_registered_cubes()`.\n\nThe argument is the number of cubes to find in the scan.\n\nIf the specified number of cubes are not found by the timeout (default value is 5 seconds), it returns a list of the number of cubes found at the time of the timeout.\n\n```Python\n dev_list = await BLEScanner.scan_registered_cubes()\n```\n\nScan for paired cubes and connect them. (Pair the cube with Windows using \"Add Bluetooth Device\" before doing this.)\n\nDisconnects 3 seconds after connecting.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def scan_and_connect():\n dev_list = await BLEScanner.scan_registered_cubes(num=1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n await asyncio.sleep(3)\n\n await cube.disconnect()\n return 0\n\nif __name__ == \"__main__\":\n asyncio.run(scan_and_connect())\n```\n\n### Get the cube location\n\nUse the class `ToioCoreCube.api.id_information` to get the location information of the cube.\nThis class provides access to the [read sensor characteristic](https://toio.github.io/toio-spec/en/docs/ble_id).\n\nThe following code reads and displays the cube ID information 200 times.\nIt uses `read()` to read to the characteristic.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def read_id():\n device_list = await BLEScanner.scan(1)\n assert len(device_list)\n cube = ToioCoreCube(device_list[0].interface)\n await cube.connect()\n for n in range(200):\n pos = await cube.api.id_information.read()\n print(\"%4d:%s\" % (n, str(pos)))\n await cube.disconnect()\n\nif __name__ == \"__main__\":\n asyncio.run(read_id())\n```\n\n### Get the cube location (using notification)\n\nYou can receive notifications from the cube by registering a notification handler with `register_notification_handler()`.\nNotifications are per each characteristic. A notification handler registered with `ToioCoreCube.api.id_information.register_notification_handler()` receives\nreceive only notifications from the read sensor.\n\nThe following code reads the ID by notification.\n\nAfter 10 seconds, the handler is unregistered and disconnected.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\n# Notification handler\ndef notification_handler(payload: bytearray):\n id_info = IdInformation.is_my_data(payload)\n print(str(id_info))\n\n\nasync def read_id():\n # connect to a cube\n dev_list = await BLEScanner.scan(1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n # add notification handler\n await cube.api.id_information.register_notification_handler(notification_handler)\n await asyncio.sleep(10)\n\n # remove notification handler\n await cube.api.id_information.unregister_notification_handler(\n notification_handler\n )\n await cube.disconnect()\n return 0\n\nif __name__ == \"__main__\":\n asyncio.run(read_id())\n```\n\nThe complete code that keeps displaying ID information until Ctlr-C is pressed is [examples/read_position.py](https://github.com/toio/toio.py/blob/main/examples/read_position.py).\n\n### Motor control\n\nThe `ToioCoreCube.api.motor` class is used to control the motor.\nThis class provides access to the [motor's characteristic](https://toio.github.io/toio-spec/en/docs/ble_motor).\n\nThe following code uses `motor_control()` to rotate the cube in place for 2 seconds.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def motor_1():\n # connect to a cube\n dev_list = await BLEScanner.scan(1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n # go\n await cube.api.motor.motor_control(10, -10)\n await asyncio.sleep(2)\n # stop\n await cube.api.motor.motor_control(0, 0)\n\n await cube.disconnect()\n return 0\n\nif __name__ == \"__main__\":\n asyncio.run(motor_1())\n```\n\n### Motor control (move to specified position)\n\nUse `motor.motor_control_target()` to move the cube to a specified position on the mat.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\n# Notification handler\ndef notification_handler(payload: bytearray):\n id_info = IdInformation.is_my_data(payload)\n print(str(id_info))\n\nasync def motor_2():\n # connect to a cube\n dev_list = await BLEScanner.scan(1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n await cube.api.motor.register_notification_handler(notification_handler)\n await cube.api.motor.motor_control_target(\n timeout=5,\n movement_type=MovementType.Linear,\n speed=Speed(\n max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),\n target=TargetPosition(\n cube_location=CubeLocation(point=Point(x=200, y=200), angle=0),\n rotation_option=RotationOption.AbsoluteOptimal,\n ),\n )\n\n await asyncio.sleep(4)\n await cube.disconnect()\n\nif __name__ == \"__main__\":\n asyncio.run(motor_2())\n```\n\n### Motor control (move to multiple specified positions)\n\nUse `motor.motor_control_multiple_targets()` to move the cube to a specified position on multiple mats.\n\n```Python\nimport asyncio\n\nfrom toio import *\n\nasync def motor_3():\n # connect to a cube\n dev_list = await BLEScanner.scan(1)\n assert len(dev_list)\n cube = ToioCoreCube(dev_list[0].interface)\n await cube.connect()\n\n targets = [\n TargetPosition(\n cube_location=CubeLocation(point=Point(x=250, y=250), angle=0), rotation_option=RotationOption.AbsoluteOptimal\n ),\n TargetPosition(\n cube_location=CubeLocation(point=Point(x=120, y=170), angle=0), rotation_option=RotationOption.AbsoluteOptimal\n ),\n ]\n await cube.api.motor.motor_control_multiple_targets(\n timeout=5,\n movement_type=MovementType.Linear,\n speed=Speed(\n max=100, speed_change_type=SpeedChangeType.AccelerationAndDeceleration),\n mode=WriteMode.Overwrite,\n target_list=targets,\n )\n\n await asyncio.sleep(5)\n await cube.disconnect()\n\nif __name__ == \"__main__\":\n asyncio.run(motor_3())\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Control toio\u2122Core Cube",
"version": "1.0.2",
"split_keywords": [
"toio"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9f86485f30b58becd7ec52c1a376b906ce76e7fa38f215a3a0155fbbb1b1bb41",
"md5": "d3e11550297ec6f930e3a9a3cbeefc17",
"sha256": "07df754840872140bc45630a8c13dff433a94b1d53f2b808ab22bdbe52a745a3"
},
"downloads": -1,
"filename": "toio_py-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d3e11550297ec6f930e3a9a3cbeefc17",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 39636,
"upload_time": "2023-04-26T00:37:34",
"upload_time_iso_8601": "2023-04-26T00:37:34.163491Z",
"url": "https://files.pythonhosted.org/packages/9f/86/485f30b58becd7ec52c1a376b906ce76e7fa38f215a3a0155fbbb1b1bb41/toio_py-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6f2ff6586f281db5640ddb9c04c6615bb18f78e3f2db27dec6788048f9afb413",
"md5": "0fa9170318fea39b4065dc24378f1956",
"sha256": "ec7d1d8ac0b5e0a289f937b20110a5c2f28363301c2efb48d1a36eb3a2e95c3f"
},
"downloads": -1,
"filename": "toio_py-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "0fa9170318fea39b4065dc24378f1956",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 31625,
"upload_time": "2023-04-26T00:37:35",
"upload_time_iso_8601": "2023-04-26T00:37:35.953365Z",
"url": "https://files.pythonhosted.org/packages/6f/2f/f6586f281db5640ddb9c04c6615bb18f78e3f2db27dec6788048f9afb413/toio_py-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-26 00:37:35",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "toio-py"
}