Run this cell before running any other cells
%load_ext autoreload
%autoreload 2
from ble import get_ble_controller
from base_ble import LOG
from cmd_types import CMD
import time
import numpy as np
LOG.propagate = False
You can use the print() function in Python to print messages to the screen.
The message can be a string, or any other object, the object will be converted into a string before it is written to the screen.
You could use the logging module that is setup in utils.py.
It prints to both your screen (standard output) as well as to log files (ble.log) in the logs directory.
This is the recommended way to output messages, since the log files can help with debugging.
The logging module also provides different log levels as shown below, each formatted with a different color for increased visibility.
NOTE: You may notice that the DEBUG message is not printed to the screen but is printed in the log file. This is because the logging level for the screen is set to INFO and for the file is set to DEBUG. You can change the default log levels in utils.py (STREAM_LOG_LEVEL and FILE_LOG_LEVEL).
To format your strings, you may use %-formatting, str.format() or f-strings.
The most "pythonic" way would be to use f-strings. Here is a good tutorial on f-strings.
LOG.debug("debug")
LOG.info("info")
LOG.warning("warning")
LOG.error("error")
LOG.critical("critical")
2023-02-07 01:20:52,086 | INFO |: info 2023-02-07 01:20:52,092 | WARNING |: warning 2023-02-07 01:20:52,094 | ERROR |: error 2023-02-07 01:20:52,098 | CRITICAL |: critical
The class ArtemisBLEController (defined in ble.py) provides member functions to handle various BLE operations to send and receive data to/from the Artemis board, provided the accompanying Arduino sketch is running on the Artemis board.
Member Functions | Description |
---|---|
reload_config() | Reload changes made in connection.yaml. |
connect() | Connect to the Artemis board, whose MAC address is specified in connection.yaml. |
disconnect() | Disconnect from the Artemis board. |
is_connected() | Return a boolean indicating whether your controller is connected to the Artemis board or not. |
send_command(cmd_type, data) | Send the command cmd_type (integer) with data (string) to the Artemis board. |
receive_float(uuid) receive_string(uuid) receive_int(uuid) |
Read the GATT characteristic (specified by its uuid) of type float, string or int. The type of the GATT characteristic is determined by the classes BLEFloatCharacteristic, BLECStringCharacteristic or BLEIntCharacteristic in the Arduino sketch. |
start_notify(uuid, notification_handler) | Activate notifications on the GATT characteristic (specified by its uuid). notification_handler is a function callback which must accept two inputs; the first will be a uuid string object and the second will be the bytearray of the characteristic value. |
bytearray_to_float(byte_array) bytearray_to_string(byte_array) bytearray_to_int(byte_array) |
Convert the bytearray to float, string or int, respectively. You may use these functions inside your notification callback function. |
stop_notify(uuid) | Stop notifications on the GATT characteristic (specified by its uuid). |
Member Variables | Description |
---|---|
uuid | A dictionary that stores the UUIDs of the various characteristics specified in connection.yaml. |
In the below cell, we create an ArtemisBLEController object using get_ble_controller() (defined in ble.py), which creates and/or returns a single instance of ArtemisBLEController.
NOTE: Do not use the class directly to instantiate an object.
from uuid import uuid4
uuid4()
UUID('0c081978-00cc-40e9-8aea-73532ef2ce69')
# Get ArtemisBLEController object
ble = get_ble_controller()
# Connect to the Artemis Device
ble.connect()
2023-02-09 15:59:04,806 | INFO |: Looking for Artemis Nano Peripheral Device: c0:83:48:6a:84:3c 2023-02-09 15:59:08,331 | INFO |: Connected to c0:83:48:6a:84:3c
The cell below shows examples of reading different types (as defined in the Arduino sketch) of GATT characteristics.
# Read a float GATT Charactersistic
f = ble.receive_float(ble.uuid['RX_FLOAT'])
print(f)
5.0
# Read a string GATT Charactersistic
s = ble.receive_string(ble.uuid['RX_STRING'])
print(s)
[->9.0<-]
Send the PING command and read the reply string from the string characteristic RX_STRING.
NOTE: The send_command() essentially sends a string data to the GATT characteristic (TX_CMD_STRING). The GATT characteristic in the Arduino sketch is of type BLECStringCharacteristic.
ble.send_command(CMD.PING, "")
s = ble.receive_string(ble.uuid['RX_STRING'])
print(s)
PONG
The cell below shows an example of the SEND_TWO_INTS command.
The two values in the data are separated by a delimiter "|".
Refer Lab 2 documentation for more information on the command protocol.
ble.send_command(CMD.SEND_TWO_INTS, "2|-6")
The Artemis board should print the two integers to the serial monitor in the ArduinoIDE.
# Disconnect
ble.disconnect()
2023-02-09 15:01:11,621 | INFO |: Disconnected from C0:83:48:6A:84:3C