From 7509c66e527eb21fd19b8c4e08a388f766342339 Mon Sep 17 00:00:00 2001 From: Chadwick Boulay Date: Sat, 7 Nov 2026 18:10:37 -0500 Subject: [PATCH 1/3] NA500: trigger a TTL pulse on the BNC output EGIAmpClient::triggerBncOut(widthUs) puts the NA500's BNC output in manual mode (cmd_SetBncOutMode 1), sets the pulse width (100-1600 us, the range Net Station offers) and fires it with cmd_SetTriggerBncManualOut. Mode and width are sent once per connection, so a trigger normally costs one command round trip. The GUI gets a "BNC Trigger" button (1 ms pulse), enabled while linked to an NA500. AmpServerConnection::sendCommand is now serialized: the UI thread and the impedance scan thread can send commands concurrently. The NA500 mock accepts the BNC-out commands, logs each pulse, and loops it back to its BNC input so it appears in the _DIN stream. Also fixes Unlink in the GUI leaving "Shutdown Amp Server" disabled and the impedance checkbox enabled. Verified on a GES 500 (fw 0.4.9) with BNC out cabled to BNC in: 0 BNC-in events in 10 s without triggers; 30 triggers produced exactly 30 events, each within +/-1 ms of the command. Co-Authored-By: Claude --- README.md | 1 + mock/include/NA500Server.h | 6 +++++ mock/src/NA500Server.cpp | 22 ++++++++++++++-- src/core/include/egiamp/AmpServerConnection.h | 2 ++ src/core/include/egiamp/EGIAmpClient.h | 10 ++++++++ src/core/src/AmpServerConnection.cpp | 1 + src/core/src/EGIAmpClient.cpp | 25 +++++++++++++++++++ src/gui/EGIAmpWindow.cpp | 16 +++++++++--- src/gui/EGIAmpWindow.h | 1 + src/gui/EGIAmpWindow.ui | 13 ++++++++++ 10 files changed, 91 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c27e803..734dccb 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ Differences from the NA400 that this app handles automatically: - **Front end**: the NA500's EEG reads full scale until its buffered reference is switched on, every `cmd_Stop` switches it off, and switch commands sent within ~20 ms of power-on are lost; the app waits briefly after power-on and enables the buffered reference before each `cmd_Start`. - **Physio16**: the NA500 never reports whether a Physio16 is attached, so the app always streams its 16 physio channels (PIB1-16) after the EEG. Physio gains persist on the amplifier, so the app sets them to 12, the power-on default, before each start; the physio is then scaled like the EEG (±375 mV range) and needs no realignment delay. - **Triggers**: the app enables the NA500's DB9 digital inputs (without that they read as all 16 active) and its BNC input. Each rising edge on BNC in appears in the `_DIN` stream as bit 16 (value `0x10000`) for 1 ms, alongside the 16 DB9 bits. The EEG trails a BNC-in event by about 1.5 samples (the ADC filter's delay); no correction is applied, as in Net Station. +- **BNC output trigger**: the GUI's *BNC Trigger* button (enabled while linked to an NA500) sends a 1 ms TTL pulse on the BNC output; `EGIAmpClient::triggerBncOut()` takes widths of 100-1600 µs. Cable BNC out to BNC in to see each pulse in the `_DIN` stream. The mock (`--amp-type NA500`) loops its BNC output back to its BNC input. - **Clock**: the NA500 stamps packets with its own clock, which is not synchronized; the app sets it with `cmd_SetAmpTime` at power-on, as Net Station does. - **Single client**: while Net Station (or any other client) is linked to the NA500, this app's connections are accepted but not serviced — and vice versa. Unlink Net Station before using this app; concurrent monitoring is not possible on the NA500 (unlike the NA400). - **Recovery**: the NA500 sends `ntn_AmpStarted` only in reply to `cmd_GetStartTime`, not when streaming starts, so external-restart recovery uses data-port polling instead. diff --git a/mock/include/NA500Server.h b/mock/include/NA500Server.h index ae46647..3848df8 100644 --- a/mock/include/NA500Server.h +++ b/mock/include/NA500Server.h @@ -55,6 +55,12 @@ class NA500Server { std::atomic streaming_{false}; std::atomic dinEnabled_{false}; std::atomic bncInEvents_{false}; // cmd_SetBncInMode 1; reset when the session ends + // BNC out: cmd_SetBncOutMode 1 (manual) arms cmd_SetTriggerBncManualOut; + // reset when the session ends, like the BNC input. Each pulse is looped back to the BNC input, + // as if a cable joined the two, so it shows up in the data stream. + std::atomic bncOutManual_{false}; + std::atomic bncOutPulses_{0}; + long long bncOutWidthUs_ = 0; std::atomic comRefMode_{false}; // cmd_SetCOMAndRefForMeasurement 1: EEG reads 0 // The signal generator latches amplitude and frequency when switched on. bool oscillatorOn_ = false; diff --git a/mock/src/NA500Server.cpp b/mock/src/NA500Server.cpp index 2dc1086..40a4727 100644 --- a/mock/src/NA500Server.cpp +++ b/mock/src/NA500Server.cpp @@ -172,6 +172,7 @@ void NA500Server::session(const std::atomic& running) { // input configuration does not. streaming_ = false; bncInEvents_ = false; + bncOutManual_ = false; comRefMode_ = false; bufferedReference_ = false; amp_->setPower(false); @@ -280,6 +281,21 @@ std::vector NA500Server::execute(const std::string& name, long long bncInEvents_ = value == 1; return {OK}; } + if (name == "cmd_SetBncOutMode") { + bncOutManual_ = value == 1; + return {OK}; + } + if (name == "cmd_SetBncOutPulseWidth") { + bncOutWidthUs_ = value; + return {OK}; + } + if (name == "cmd_SetTriggerBncManualOut") { + if (bncOutManual_) { + ++bncOutPulses_; + std::cout << "[NA500] BNC out pulse, " << bncOutWidthUs_ << " us" << std::endl; + } + return {OK}; + } if (name == "cmd_SetDinEnable") { dinEnabled_ = true; // the device ignores the value return {OK}; @@ -367,9 +383,11 @@ void NA500Server::streamLoop() { std::fill(std::begin(packet.eegData), std::end(packet.eegData), -8388608); } // BNC in (event mode) reports a 1 ms event at each rising edge; the mock - // drives it from the test-jig pulse that also pulls DIN 1 low. + // drives it from the test-jig pulse that also pulls DIN 1 low, and from + // BNC out pulses (looped back). const uint16_t din = packet.digitalInputs; - if (bncInEvents_ && (prevDin & 1) && !(din & 1)) { + const bool bncOutPulse = bncOutPulses_.exchange(0) > 0; + if (bncInEvents_ && (((prevDin & 1) && !(din & 1)) || bncOutPulse)) { bncSamplesLeft = rate >= 1000 ? rate / 1000 : 1; } prevDin = din; diff --git a/src/core/include/egiamp/AmpServerConnection.h b/src/core/include/egiamp/AmpServerConnection.h index 321297d..f215468 100644 --- a/src/core/include/egiamp/AmpServerConnection.h +++ b/src/core/include/egiamp/AmpServerConnection.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,7 @@ class AmpServerConnection { socket_stream commandStream_; socket_stream notificationStream_; socket_stream dataStream_; + std::mutex commandMutex_; // commands come from several threads (UI, impedance scan) std::atomic connected_{false}; std::string serverAddress_; uint16_t dataPort_{0}; diff --git a/src/core/include/egiamp/EGIAmpClient.h b/src/core/include/egiamp/EGIAmpClient.h index 19a023f..8d98d88 100644 --- a/src/core/include/egiamp/EGIAmpClient.h +++ b/src/core/include/egiamp/EGIAmpClient.h @@ -58,6 +58,13 @@ class EGIAmpClient { void stopImpedanceMode(); bool isImpedanceModeActive() const; + // NA500 only: fire one TTL pulse of the given width on the BNC output. + // Needs a running stream. Net Station offers 100-1600 us. + static constexpr int BNC_OUT_MIN_PULSE_US = 100; + static constexpr int BNC_OUT_MAX_PULSE_US = 1600; + static constexpr int BNC_OUT_DEFAULT_PULSE_US = 1000; + bool triggerBncOut(int widthUs); + // Shutdown the Amp Server process (cmd_Exit) // Warning: This terminates the Amp Server and will disconnect all clients bool shutdownAmpServer(); @@ -147,6 +154,9 @@ class EGIAmpClient { uint64_t lastPacketCounterWithTimeStamp_ = 0; uint16_t lastDINValue_ = 0xFFFF; // DINs are active-low; 0xFFFF = idle bool lastBncIn_ = false; // NA500 BNC input, reported as DIN bit 16 + // BNC output pulse width last sent on this connection; 0 = output not yet + // put in manual mode on this connection. + int bncOutWidthUs_ = 0; StatusCallback statusCallback_; ErrorCallback errorCallback_; diff --git a/src/core/src/AmpServerConnection.cpp b/src/core/src/AmpServerConnection.cpp index 39d7f05..7fc3605 100644 --- a/src/core/src/AmpServerConnection.cpp +++ b/src/core/src/AmpServerConnection.cpp @@ -67,6 +67,7 @@ bool AmpServerConnection::isConnected() const { std::string AmpServerConnection::sendCommand(const std::string& command, int ampId, int channel, const std::string& value) { + std::lock_guard lock(commandMutex_); char response[4096]; // Build the whole command first and write it in a single insertion: the // NA500's AmpServer parses each TCP segment as a complete message, so a diff --git a/src/core/src/EGIAmpClient.cpp b/src/core/src/EGIAmpClient.cpp index b23cde3..e3d197b 100644 --- a/src/core/src/EGIAmpClient.cpp +++ b/src/core/src/EGIAmpClient.cpp @@ -49,6 +49,7 @@ bool EGIAmpClient::connect() { } dataListening_ = false; // Fresh data connection, not yet listening + bncOutWidthUs_ = 0; // New session: configure the BNC output again before triggering // Create notification stream — lives as long as the connection std::string streamName = config_.streamName(); @@ -894,6 +895,30 @@ bool EGIAmpClient::isImpedanceModeActive() const { return impedanceModeActive_; } +bool EGIAmpClient::triggerBncOut(const int widthUs) { + if (!isStreaming() || details_.amplifierType != AmplifierType::NA500) { + emitError("BNC output trigger needs a streaming Net Amps 500"); + return false; + } + try { + // Mode and width only once per session (or width change), so a trigger + // normally costs a single command round trip. + if (bncOutWidthUs_ != widthUs) { + if (bncOutWidthUs_ == 0) { + connection_.sendCommand("cmd_SetBncOutMode", config_.amplifierId, 0, "1"); // manual + } + connection_.sendCommand("cmd_SetBncOutPulseWidth", config_.amplifierId, 0, + std::to_string(widthUs)); + bncOutWidthUs_ = widthUs; + } + return commandCompleted( + connection_.sendCommand("cmd_SetTriggerBncManualOut", config_.amplifierId, 0, "0")); + } catch (const std::exception& ex) { + emitError(std::string("BNC output trigger failed: ") + ex.what()); + return false; + } +} + bool EGIAmpClient::shutdownAmpServer() { try { emitStatus("Sending shutdown command to Amp Server...\n"); diff --git a/src/gui/EGIAmpWindow.cpp b/src/gui/EGIAmpWindow.cpp index f166271..fa24436 100644 --- a/src/gui/EGIAmpWindow.cpp +++ b/src/gui/EGIAmpWindow.cpp @@ -45,6 +45,7 @@ EGIAmpWindow::EGIAmpWindow(QWidget* parent, const std::string& configFile) // Impedance mode checkbox connect(ui->impedanceCheckBox, &QCheckBox::toggled, this, &EGIAmpWindow::toggleImpedanceMode); + connect(ui->triggerButton, &QPushButton::clicked, this, &EGIAmpWindow::triggerBncOut); // Signal connections for cross-thread communication connect(this, &EGIAmpWindow::appendStatusMessage, @@ -178,10 +179,7 @@ void EGIAmpWindow::linkAmpserver() { joinLinkThread(); client_->stopStreaming(); client_->disconnect(); - ui->statusBar->clearMessage(); - ui->statusBar->setStyleSheet(""); - emit fieldsEnabled(true); - emit setLinkButtonText("Link"); + unlockUI(); // also re-enables Shutdown and disables impedance/trigger emit sensorLayoutUpdated(""); } else { // Link — disable button immediately to prevent double-click @@ -275,6 +273,7 @@ void EGIAmpWindow::unlockUI() { ui->actionShutdown_Amp_Server->setEnabled(true); ui->impedanceCheckBox->setEnabled(false); ui->impedanceCheckBox->setChecked(false); + ui->triggerButton->setEnabled(false); } void EGIAmpWindow::lockUI() { @@ -283,6 +282,8 @@ void EGIAmpWindow::lockUI() { emit fieldsEnabled(false); ui->actionShutdown_Amp_Server->setEnabled(false); ui->impedanceCheckBox->setEnabled(true); + ui->triggerButton->setEnabled( + client_->amplifierDetails().amplifierType == egiamp::AmplifierType::NA500); } void EGIAmpWindow::shutdownAmpServer() { @@ -408,3 +409,10 @@ int EGIAmpWindow::findSampleRateIndex(int rate, bool native) const { } return defaultSampleRateIndex(); // fallback to "1000 Hz" } + +void EGIAmpWindow::triggerBncOut() { + // Failures are reported through the client's error callback. + if (client_->triggerBncOut(egiamp::EGIAmpClient::BNC_OUT_DEFAULT_PULSE_US)) { + emit appendStatusMessage("BNC trigger sent."); + } +} diff --git a/src/gui/EGIAmpWindow.h b/src/gui/EGIAmpWindow.h index 3776957..a7efe3c 100644 --- a/src/gui/EGIAmpWindow.h +++ b/src/gui/EGIAmpWindow.h @@ -27,6 +27,7 @@ private slots: void linkAmpserver(); void shutdownAmpServer(); void toggleImpedanceMode(bool enabled); + void triggerBncOut(); void displayError(QString description); void unlockUI(); void lockUI(); diff --git a/src/gui/EGIAmpWindow.ui b/src/gui/EGIAmpWindow.ui index 341e34f..3e8dea0 100644 --- a/src/gui/EGIAmpWindow.ui +++ b/src/gui/EGIAmpWindow.ui @@ -153,6 +153,19 @@ + + + + false + + + BNC Trigger + + + Send a 1 ms TTL pulse on the amplifier's BNC output (Net Amps 500 only) + + + From e6c77f307d13b24001e542f3f4e52b60dbe589a0 Mon Sep 17 00:00:00 2001 From: Chadwick Boulay Date: Sat, 7 Nov 2026 18:39:23 -0500 Subject: [PATCH 2/3] CI: move macOS jobs to macos-15 GitHub no longer runs the macos-14 image ("The configuration 'macos-14-arm64-us-default' is not supported"). Co-Authored-By: Claude --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b2098e..305b7be 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,7 +40,7 @@ jobs: config: - { name: "Ubuntu 22.04", os: ubuntu-22.04 } - { name: "Ubuntu 24.04", os: ubuntu-24.04 } - - { name: "macOS", os: macos-14, cmake_extra: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"' } + - { name: "macOS", os: macos-15, cmake_extra: '-DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"' } - { name: "Windows", os: windows-latest } steps: @@ -132,7 +132,7 @@ jobs: name: Sign & Notarize (macOS) needs: build if: github.event_name == 'release' - runs-on: macos-14 + runs-on: macos-15 steps: - name: Checkout @@ -141,7 +141,7 @@ jobs: - name: Download macOS Artifact uses: actions/download-artifact@v4 with: - name: package-macos-14 + name: package-macos-15 path: packages - name: Extract Package From 1729b3d304b943a24ad5151dee7a507c3a88f555 Mon Sep 17 00:00:00 2001 From: Chadwick Boulay Date: Sat, 7 Nov 2026 19:16:06 -0500 Subject: [PATCH 3/3] NA500: document the measured BNC output pulse 0 to 5.0 V, about 6.7% (16/15) longer than the set width, measured with a USB-1608FS-Plus at 100 kHz on a GES 500 (fw 0.4.9). Co-Authored-By: Claude --- README.md | 2 +- docs/NA500_AmpServer.md | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 734dccb..dccfa59 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ Differences from the NA400 that this app handles automatically: - **Front end**: the NA500's EEG reads full scale until its buffered reference is switched on, every `cmd_Stop` switches it off, and switch commands sent within ~20 ms of power-on are lost; the app waits briefly after power-on and enables the buffered reference before each `cmd_Start`. - **Physio16**: the NA500 never reports whether a Physio16 is attached, so the app always streams its 16 physio channels (PIB1-16) after the EEG. Physio gains persist on the amplifier, so the app sets them to 12, the power-on default, before each start; the physio is then scaled like the EEG (±375 mV range) and needs no realignment delay. - **Triggers**: the app enables the NA500's DB9 digital inputs (without that they read as all 16 active) and its BNC input. Each rising edge on BNC in appears in the `_DIN` stream as bit 16 (value `0x10000`) for 1 ms, alongside the 16 DB9 bits. The EEG trails a BNC-in event by about 1.5 samples (the ADC filter's delay); no correction is applied, as in Net Station. -- **BNC output trigger**: the GUI's *BNC Trigger* button (enabled while linked to an NA500) sends a 1 ms TTL pulse on the BNC output; `EGIAmpClient::triggerBncOut()` takes widths of 100-1600 µs. Cable BNC out to BNC in to see each pulse in the `_DIN` stream. The mock (`--amp-type NA500`) loops its BNC output back to its BNC input. +- **BNC output trigger**: the GUI's *BNC Trigger* button (enabled while linked to an NA500) sends a 1 ms TTL pulse (0 to 5 V) on the BNC output; `EGIAmpClient::triggerBncOut()` takes widths of 100-1600 µs. The amplifier makes each pulse about 6.7% longer than set (a 1 ms setting measures 1.07 ms); the app sends the width as is, as Net Station does. Cable BNC out to BNC in to see each pulse in the `_DIN` stream. The mock (`--amp-type NA500`) loops its BNC output back to its BNC input. - **Clock**: the NA500 stamps packets with its own clock, which is not synchronized; the app sets it with `cmd_SetAmpTime` at power-on, as Net Station does. - **Single client**: while Net Station (or any other client) is linked to the NA500, this app's connections are accepted but not serviced — and vice versa. Unlink Net Station before using this app; concurrent monitoring is not possible on the NA500 (unlike the NA400). - **Recovery**: the NA500 sends `ntn_AmpStarted` only in reply to `cmd_GetStartTime`, not when streaming starts, so external-restart recovery uses data-port polling instead. diff --git a/docs/NA500_AmpServer.md b/docs/NA500_AmpServer.md index 2d215c4..1eafa84 100644 --- a/docs/NA500_AmpServer.md +++ b/docs/NA500_AmpServer.md @@ -83,7 +83,9 @@ data on 9879). Only the differences that matter to a client are listed. the first reserved byte: one sample at 1000 Hz, four at 4000 Hz, whatever the pulse length. Mode 0 is off. The mode is reset when the session ends. - **BNC out**: `cmd_SetBncOutMode 1` (manual) makes `cmd_SetTriggerBncManualOut` fire one pulse of the - width set by `cmd_SetBncOutPulseWidth <µs>`. + width set by `cmd_SetBncOutPulseWidth <µs>`. Measured with a 100 kHz DAQ: a 0 to 5.0 V pulse, rising in + under 10 µs, about 6.7% (16/15) longer than set: 100 µs gives 108, 1000 µs gives 1069, 1500 µs gives + 1600, 1600 µs gives 1706. - Bits 0 and 1 of the same byte (photocell and audio inputs) read 1 with nothing connected. - A BNC pulse fed to the EEG inputs starts moving the EEG on the same sample as the BNC-in event and reaches half amplitude about 1.5 samples later (the ADC filter's delay), at 1000 and 4000 Hz.