Acroname BrainStem Software Control Package

2.12.2 · active · verified Thu Apr 16

The `brainstem` Python package provides a robust interface for controlling and interacting with Acroname BrainStem hardware devices, such as programmable USB hubs and switches. Currently at version 2.12.2, the library enables Python applications to discover, connect to, and manage BrainStem modules. It is actively maintained with updates typically released alongside new BrainStem Development Kits, ensuring compatibility with various operating systems and Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code demonstrates how to discover an Acroname BrainStem USB module, connect to it, and then blink its user-programmable LED. This example requires a physical BrainStem USB module to be connected to your computer for successful execution. The code handles discovery, connection, a simple LED toggle loop, and proper disconnection.

import brainstem
import time

try:
    # Discover the first USB BrainStem module
    print("Discovering modules...")
    specs = brainstem.discover.findAllModules(brainstem.link.Spec.USB)
    if not specs:
        print("No BrainStem modules found. Please connect a device.")
        exit()
    
    # Connect to the first discovered USB module
    spec = specs[0]
    print(f"Found module: {str(spec)}")
    stem = brainstem.stem.USBStem()
    error = stem.connectFromSpec(spec)

    if error == brainstem.result.Result.NO_ERROR:
        print(f"Connected to {brainstem.defs.model_info(stem.system.getModel().value)}")
        print("Blinking user LED for 5 seconds...")
        for i in range(10):
            stem.system.setLED(i % 2) # Toggle LED (0 for off, 1 for on)
            time.sleep(0.5)
        stem.system.setLED(0) # Ensure LED is off at the end
        print("LED blinking complete.")
    else:
        print(f"Failed to connect to module: {error}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if 'stem' in locals() and stem.isConnected():
        stem.disconnect()
        print("Disconnected from BrainStem module.")

view raw JSON →