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
20
21from adafruit_ble_berrymed_pulse_oximeter import BerryMedPulseOximeterService
22
23# CircuitPython <6 uses its own ConnectionError type. So, is it if available. Otherwise,
24# the built in ConnectionError is used.
25connection_error = ConnectionError
26if hasattr(_bleio, "ConnectionError"):
27    connection_error = _bleio.ConnectionError
28
29# PyLint can't find BLERadio for some reason so special case it here.
30ble = adafruit_ble.BLERadio()
31
32pulse_ox_connection = None
33
34while True:
35    print("Scanning...")
36    for adv in ble.start_scan(Advertisement, timeout=5):
37        name = adv.complete_name
38        if not name:
39            continue
40        # "BerryMed" devices may have trailing nulls on their name.
41        if name.strip("\x00") == "BerryMed":
42            pulse_ox_connection = ble.connect(adv)
43            print("Connected")
44            break
45
46    # Stop scanning whether or not we are connected.
47    ble.stop_scan()
48    print("Stopped scan")
49
50    try:
51        if pulse_ox_connection and pulse_ox_connection.connected:
52            print("Fetch connection")
53            if DeviceInfoService in pulse_ox_connection:
54                dis = pulse_ox_connection[DeviceInfoService]
55                try:
56                    manufacturer = dis.manufacturer
57                except AttributeError:
58                    manufacturer = "(Manufacturer Not specified)"
59                try:
60                    model_number = dis.model_number
61                except AttributeError:
62                    model_number = "(Model number not specified)"
63                print("Device:", manufacturer, model_number)
64            else:
65                print("No device information")
66            pulse_ox_service = pulse_ox_connection[BerryMedPulseOximeterService]
67            while pulse_ox_connection.connected:
68                print(pulse_ox_service.values)
69    except connection_error:
70        try:
71            pulse_ox_connection.disconnect()
72        except connection_error:
73            pass
74        pulse_ox_connection = None