Simple test

Ensure your device works with this simple test.

examples/ble_berrymed_pulse_oximeter_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Read data from a BerryMed pulse oximeter, model BM1000C, BM1000E, etc.
 6"""
 7
 8# Protocol defined here:
 9#     https://github.com/zh2x/BCI_Protocol
10# Thanks as well to:
11#     https://github.com/ehborisov/BerryMed-Pulse-Oximeter-tool
12#     https://github.com/ScheindorfHyenetics/berrymedBluetoothOxymeter
13#
14# The sensor updates the readings at 100Hz.
15
16import _bleio
17import adafruit_ble
18from adafruit_ble.advertising.standard import Advertisement
19from adafruit_ble.services.standard.device_info import DeviceInfoService
20from adafruit_ble_berrymed_pulse_oximeter import BerryMedPulseOximeterService
21
22# CircuitPython <6 uses its own ConnectionError type. So, is it if available. Otherwise,
23# the built in ConnectionError is used.
24connection_error = ConnectionError
25if hasattr(_bleio, "ConnectionError"):
26    connection_error = _bleio.ConnectionError
27
28# PyLint can't find BLERadio for some reason so special case it here.
29ble = adafruit_ble.BLERadio()  # pylint: disable=no-member
30
31pulse_ox_connection = None
32
33while True:
34    print("Scanning...")
35    for adv in ble.start_scan(Advertisement, timeout=5):
36        name = adv.complete_name
37        if not name:
38            continue
39        # "BerryMed" devices may have trailing nulls on their name.
40        if name.strip("\x00") == "BerryMed":
41            pulse_ox_connection = ble.connect(adv)
42            print("Connected")
43            break
44
45    # Stop scanning whether or not we are connected.
46    ble.stop_scan()
47    print("Stopped scan")
48
49    try:
50        if pulse_ox_connection and pulse_ox_connection.connected:
51            print("Fetch connection")
52            if DeviceInfoService in pulse_ox_connection:
53                dis = pulse_ox_connection[DeviceInfoService]
54                try:
55                    manufacturer = dis.manufacturer
56                except AttributeError:
57                    manufacturer = "(Manufacturer Not specified)"
58                try:
59                    model_number = dis.model_number
60                except AttributeError:
61                    model_number = "(Model number not specified)"
62                print("Device:", manufacturer, model_number)
63            else:
64                print("No device information")
65            pulse_ox_service = pulse_ox_connection[BerryMedPulseOximeterService]
66            while pulse_ox_connection.connected:
67                print(pulse_ox_service.values)
68    except connection_error:
69        try:
70            pulse_ox_connection.disconnect()
71        except connection_error:
72            pass
73        pulse_ox_connection = None