# Bittle()

### Attributes

* **id (UUID):** Bittle's unique id.
* **bluetoothManager (BluetoothManager):** Manager for sending messages to Bittle through Bluetooth.
* **wifiManager (WifiManager):** Manager for sending messages to Bittle through WiFi.
* **serialManager (SerialManager):** Manager for sending messages to Bittle through Serial communication.
* **gait (Gait):** Current gait (WALK by default); used to send movement commands.
* **commands ({Command : str}):** Map with avaliable commands and its Serial translation.

### Methods

* **connect\_bluetooth(get\_first\_bittle=True):** Connects to Bittle through Bluetooth connection. Returns True if connected, False otherwise. If get\_first\_bittle is enabled, searchs for *BitleSPP* among paired devices and connect to it.
* **send\_command\_bluetooth(command):** Sends a command to Bittle through Bluetooth connection.
* **send\_msg\_bluetooth(message):** Sends a custom message to Bittle through Bluetooth connection.
* **receive\_msg\_bluetooth(buffer\_size=1014):** Returns received message from Bittle through Bluetooth connection (bytes).
* **disconnect\_bluetooth():** Closes Bluetooth connection with Bittle.
* **has\_wifi\_connection():** Checks wether there is connection with REST API. Returns True if there is connection, False otherwise.
* **send\_command\_wifi(command):** Sends a command to Bittle through WiFi connection. Returns REST API response code (int), -1 if there is no connection.
* **send\_msg\_wifi(message):** Sends a custom message to Bittle through WiFi connection. Returns REST API response code (int), -1 if there is no connection.
* **send\_movement\_bluetooth(direction):** Sends a movement command to Bittle through Bluetooth connection. This method sends current gait plus direction, only WALK gait is avaliable for backward directions.
* **send\_movement\_wifi(direction):** Sends a movement command to Bittle through WiFi connection. This method sends current gait plus direction, only WALK gait is avaliable for backward directions.
* **connect\_serial(discover\_port=True):** Connects to Bittle through Serial. Return True if connected, False otherwise. If discover\_port is enabled, search among avaliable communication ports the one associated to CH340 USB driver, which is used by Bittle and connect to it.
* **send\_command\_serial(command):** Sends a command to Bittle through Serial connection.
* **send\_msg\_serial(message):** Sends a custom message to Bittle through Serial connection.
* **receive\_msg\_serial(buffer\_size):** Returns received message from Bittle through Serial connection (bytes).
* **send\_movement\_serial(direction):** Sends a movement command to Bittle through Serial connection. Thid method sends current gait plus direction, only WALK gait is avaliable for backward directions.
* **disconnect\_serial():** Closes Serial connection with Bittle.

```python
"""Example: Making Bittle trot forward-left for 5 seconds and stop afterward.
"""
import pyBittle

bittle = pyBittle.Bittle()
bittle.gait = pyBittle.Gait.TROT

bittle.send_movement_wifi(pyBittle.Direction.FORWARDLEFT)
time.sleep(5)
bittle.send_command_wifi(pyBittle.Command.BALANCE)  # BALANCE is Command type!
```

### Examples

The following example shows the complete process of creating a Bittle instance and communicating with it through Bluetooth. If connection is successful, send GREETING and REST commands to check whether Bittle receives and replies to them:

```python
import pyBittle
import time


greet_command = pyBittle.Command.GREETING
rest_command = pyBittle.Command.REST

bittle = pyBittle.Bittle()  # Create Bittle instance

isConnected = bittle.connect_bluetooth()
if isConnected:
    bittle.send_command_bluetooth(greet_command)  # Send 'khi' message
    received_msg = bittle.receive_msg_bluetooth()
    decoded_msg = received_msg.decode('utf-8')
    decoded_msg = decoded_msg.replace('\r\n', '')
    print(f"Received message: {decoded_msg}, expected: k")
    time.sleep(6)

    bittle.send_command_bluetooth(rest_command)  # Send 'd' message
    received_msg = bittle.receive_msg_bluetooth()
    decoded_msg = received_msg.decode('utf-8')
    decoded_msg = decoded_msg.replace('\r\n', '')
    print(f"Received message: {decoded_msg}, expected: d")
    time.sleep(5)

    bittle.disconnect_bluetooth()
else:
    print("Can't connect to Bittle!")

```

The following example shows the complete process of creating a Bittle instance and communicating with it through WiFi. If connection is successful, send GREETING and REST commands to check whether Bittle receives and replies to them:

```python
import pyBittle
import time


greet_command = pyBittle.Command.GREETING
rest_command = pyBittle.Command.REST

bittle = pyBittle.Bittle()  # Create Bittle instance
bittle.wifiManager.ip = '192.168.1.131'  # Set REST API IP address

if bittle.has_wifi_connection():
    response = bittle.send_command_wifi(greet_command)  # Send 'khi' message
    print(f"Received response: {response}, expected: 200")
    time.sleep(6)
    
    response = bittle.send_command_wifi(rest_command)  # Send 'd' message
    print(f"Received response: {response}, expected: 200")
    time.sleep(5)
else:
    print("Can't connect to Bittle!")
```

The following example shows the complete process of creating a Bittle instance and communicating with it through Serial. If connection is successful, send GREETING and REST commands to check wether Bittle receives and replies to them:

```python
import pyBittle
import time


sit_command = pyBittle.Command.SIT
rest_command = pyBittle.Command.REST

bittle = pyBittle.Bittle()  # Create Bittle instance

is_connected = bittle.connect_serial()
if is_connected:
    bittle.send_command_serial(sit_command)  # Send 'ksit' message
    received = bittle.receive_msg_serial()
    decoded_msg = received.decode('utf-8')
    decoded_msg = decoded_msg.replace('\r\n', '')
    print(f"Received message: {decoded_msg}, expected: 'k'")
    time.sleep(5)
    
    bittle.send_command_serial(rest_command)  # Send 'd' message
    received = bittle.receive_msg_serial()
    decoded_msg = received.decode('utf-8')
    decoded_msg = decoded_msg.replace('\r\n', '')
    print(f"Received message: {decoded_msg}, expected: 'd'")
    time.sleep(5)
    
    bittle.disconnect_serial()
else:
    print("Bittle not found!")
```
