# tdmitchell787/hp8903 architecture

> Architecture diagram of the GitHub repository tdmitchell787/hp8903, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tdmitchell787/hp8903
- Repository: https://github.com/tdmitchell787/hp8903

## Overview

##### Project Type and Purpose
- This is a desktop Python-based instrumentation control application.
- It provides a GUI front end to automate sweep measurements on an HP 8903 Audio Analyzer via GPIB controllers.

##### File Structure
- Top‐level script: **hp8903.py** (the main application).
- **hardware_tests/** directory with simple test scripts for supported GPIB controllers (galvant.py, nigpib232cva.py).
- No separate “src” or “lib” folders—most logic lives in hp8903.py, with hardware abstraction inside.

##### Review the README for Technical Stack
- Python 2.7+
- GUI: GTK3 via gobject
- Plotting: matplotlib
- Data handling: numpy
- Serial communication: pyserial
- Planned future support: pyvisa, additional controller plugins

##### Extract the Main Architectural Components
a. **GUI Layer**
- Built with GTK3/gobject, handles menus, parameter forms, “Connect”/“Start” buttons, status bar.
b. **Application Controller**
- Orchestrates user actions → triggers hardware commands and updates the view.
- Implements an MVC‐style controller.
c. **Hardware Abstraction / Driver Layer**
- Defines a common interface for GPIB controllers (Galvant, NI‐232CV-A, future Prologix/VISA).
- Each controller script (in hardware_tests) or class implements connect(), send(), receive().
d. **Communication Layer**
- Uses pyserial to communicate over the serial port to the GPIB adapter.
e. **Data Acquisition Module**
- Formats HP 8903 commands, sends them, reads raw binary/text responses.
f. **Data Processing Module**
- Uses numpy to parse and process raw measurement data (THD+n, amplitude vs. frequency).
- Manages filter control and sweep parameter calculations.
g. **Visualization Module**
- Uses matplotlib to plot time/frequency data and render THD+n vs. frequency graphs.
h. **Persistence / I/O**
- Saves raw data and plot images to disk (file‐system).
i. **External Hardware**
- HP 8903 Audio Analyzer
- GPIB controller devices

##### Relationships and Data Flows
- User clicks “Connect” in **GUI** → **Controller** calls **Hardware Abstraction** → **Communication Layer** opens serial to GPIB → GPIB adapter ↔ HP 8903.
- User starts a sweep → **Controller** → **Data Acquisition** → hardware → raw data back up → **Data Processing** → **Visualization** → GUI display.
- On demand, **Persistence** module writes data/images to disk.
- **hardware_tests** scripts bypass GUI and call **Hardware Abstraction** directly for low‐level testing.

##### Architectural Patterns
- Layered MVC‐style architecture for separation of concerns.
- Plugin/driver pattern for supporting multiple GPIB adapters.
- Clear modular boundaries: GUI, logic, hardware interfaces, processing, visualization.

##### Final Checklist
- All main components are present and clearly labeled.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "UI/UX Layer"
        GUI["GUI Layer\n(Gtk3/gobject)"]:::ui
    end

    subgraph "Core Logic" 
        Controller["Application Controller"]:::core
        Acquisition["Data Acquisition Module"]:::core
    end

    subgraph "Hardware Interfaces" 
        DriverInterface["Driver Interface\n(extension point)"]:::hw
        galvant["Galvant Driver"]:::hw
        nigpib["NI GPIB232CVA Driver"]:::hw
        Comm["Communication Layer\n(pyserial)"]:::hw
    end

    subgraph "Processing & Visualization"
        Processing["Data Processing Module\n(numpy)"]:::proc
        Visualization["Visualization Module\n(matplotlib)"]:::proc
        Persistence["Persistence / I/O\n(save data/images)"]:::proc
    end

    subgraph "External Hardware" 
        GPIBAdapter["GPIB Adapter"]:::ext
        HP8903["HP 8903 Audio Analyzer"]:::ext
    end

    Disk["File System"]:::disk

    %% Command flows
    GUI -->|"user action"| Controller
    Controller -->|"invoke"| DriverInterface
    DriverInterface -.-> galvant
    DriverInterface -.-> nigpib
    Controller -->|"start sweep"| Acquisition
    Acquisition -->|"SCPI command"| Comm
    Comm -->|"serial data"| GPIBAdapter
    GPIBAdapter -->|"GPIB handshake"| HP8903

    %% Data flows
    HP8903 -.->|"raw measurement data"| GPIBAdapter
    GPIBAdapter -->|"serial data"| Comm
    Comm -->|"raw data"| Acquisition
    Acquisition -->|"numpy array"| Processing
    Processing -->|"matplotlib figure"| Visualization
    Visualization -->|"render"| GUI

    %% Persistence
    Processing -->|"save raw"| Persistence
    Visualization -->|"save plot"| Persistence
    Persistence -->|"write"| Disk

    %% Click Events
    click GUI "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Controller "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Comm "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Acquisition "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Processing "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Visualization "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click Persistence "https://github.com/tdmitchell787/hp8903/blob/master/hp8903.py"
    click galvant "https://github.com/tdmitchell787/hp8903/blob/master/hardware_tests/galvant.py"
    click nigpib "https://github.com/tdmitchell787/hp8903/blob/master/hardware_tests/nigpib232cva.py"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085
    classDef core fill:#d4edda,stroke:#155724,color:#155724
    classDef hw fill:#fff3cd,stroke:#856404,color:#856404
    classDef proc fill:#ffeeba,stroke:#856404,color:#856404
    classDef ext fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef disk fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
