Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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.
Expand Down
4 changes: 3 additions & 1 deletion docs/NA500_AmpServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions mock/include/NA500Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class NA500Server {
std::atomic<bool> streaming_{false};
std::atomic<bool> dinEnabled_{false};
std::atomic<bool> 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<bool> bncOutManual_{false};
std::atomic<int> bncOutPulses_{0};
long long bncOutWidthUs_ = 0;
std::atomic<bool> comRefMode_{false}; // cmd_SetCOMAndRefForMeasurement 1: EEG reads 0
// The signal generator latches amplitude and frequency when switched on.
bool oscillatorOn_ = false;
Expand Down
22 changes: 20 additions & 2 deletions mock/src/NA500Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void NA500Server::session(const std::atomic<bool>& running) {
// input configuration does not.
streaming_ = false;
bncInEvents_ = false;
bncOutManual_ = false;
comRefMode_ = false;
bufferedReference_ = false;
amp_->setPower(false);
Expand Down Expand Up @@ -280,6 +281,21 @@ std::vector<std::string> 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};
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/egiamp/AmpServerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <chrono>
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>

#include <asio.hpp>
Expand Down Expand Up @@ -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<bool> connected_{false};
std::string serverAddress_;
uint16_t dataPort_{0};
Expand Down
10 changes: 10 additions & 0 deletions src/core/include/egiamp/EGIAmpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions src/core/src/AmpServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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
Expand Down
25 changes: 25 additions & 0 deletions src/core/src/EGIAmpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
16 changes: 12 additions & 4 deletions src/gui/EGIAmpWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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.");
}
}
1 change: 1 addition & 0 deletions src/gui/EGIAmpWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private slots:
void linkAmpserver();
void shutdownAmpServer();
void toggleImpedanceMode(bool enabled);
void triggerBncOut();
void displayError(QString description);
void unlockUI();
void lockUI();
Expand Down
13 changes: 13 additions & 0 deletions src/gui/EGIAmpWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="triggerButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>BNC Trigger</string>
</property>
<property name="toolTip">
<string>Send a 1 ms TTL pulse on the amplifier's BNC output (Net Amps 500 only)</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down
Loading