Skip to content

Add FFI Functions for Clamp Control#2559

Open
MichaelHuth wants to merge 3 commits into
mainfrom
feature/2559-mh_add_ffi_clamp_control
Open

Add FFI Functions for Clamp Control#2559
MichaelHuth wants to merge 3 commits into
mainfrom
feature/2559-mh_add_ffi_clamp_control

Conversation

@MichaelHuth

Copy link
Copy Markdown
Collaborator

close #2547

@MichaelHuth MichaelHuth self-assigned this Oct 30, 2025
@MichaelHuth MichaelHuth added FFI Foreign Function Interface PatchLink labels Oct 30, 2025
@t-b

t-b commented Oct 30, 2025

Copy link
Copy Markdown
Collaborator

Let's do your usual appraoch here where Luke/Tim can test the FFI functions before we write the tests.

@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch 2 times, most recently from 2f8e923 to d5bf06c Compare October 30, 2025 19:23
@MichaelHuth

Copy link
Copy Markdown
Collaborator Author

If the current clamp mode for the headstage in MIES is different from the specific calmp mode of an amplifier setting (e.g. auto bridge balance that applies only to IC) then it is ignored by MIES.

@campagnola

Copy link
Copy Markdown
Member

I gave this a try today:

  • I need a way to get the state of the clamp for any mode, not just the currently active mode. I suggest always returning all fields for both modes regardless of the current clamp mode (much simpler, but is there a cost to assembling these structures?). Alternatively, add a mode argument to FFI_GetClampState that can optionally be -1 to request the current mode.
  • missing GetHeadstageActive (or is there another way to get this?)
  • missing SetHoldingPotentialEnable and SetBiasCurrentEnable

@campagnola

Copy link
Copy Markdown
Member

If the current clamp mode for the headstage in MIES is different from the specific calmp mode of an amplifier setting (e.g. auto bridge balance that applies only to IC) then it is ignored by MIES.

That makes sense for any of the TriggerAuto* functions, but all other parameters I would expect to be settable even when the mode is not active (for example, I should be able to set the auto bias parameters before switching to IC)

@t-b

t-b commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator

I think this is on @MichaelHuth plate again.

@t-b t-b assigned t-b and unassigned MichaelHuth Feb 25, 2026
@t-b

t-b commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

@MichaelHuth I'm also taking over this one.

@t-b t-b assigned MichaelHuth and unassigned t-b Apr 30, 2026
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from d5bf06c to 93afc3b Compare May 7, 2026 15:33
Copilot AI review requested due to automatic review settings May 7, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new Foreign Function Interface (FFI) entry points intended for external (ACQ4/ZeroMQ) control/inspection of amplifier clamp settings, addressing issue #2547 by avoiding direct GUI element access.

Changes:

  • Added FFI functions to query clamp state and to set/trigger several clamp-related controls (mode, holding potential, bias current, auto bias, headstage active, and auto clamp actions).
  • Introduced DAP_SetClampMode as a shared helper and wired the clamp-mode checkbox handler to use it.
  • Exposed AI_AmpStorageControlToRowLabel for cross-module use and added a range assertion for the AutoBias Vcom parameter.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
Packages/MIES/MIES_ForeignFunctionInterface.ipf Adds clamp-control-related FFI functions, including FFI_GetClampState and several setters/triggers.
Packages/MIES/MIES_DAEphys.ipf Refactors clamp-mode setting into DAP_SetClampMode and uses it from the GUI checkbox handler.
Packages/MIES/MIES_AmplifierInteraction.ipf Adds AutoBias Vcom range assertion and makes AI_AmpStorageControlToRowLabel non-static for use by FFI.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_AmplifierInteraction.ipf Outdated
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from 93afc3b to f2c45fb Compare May 7, 2026 17:24
Copilot AI review requested due to automatic review settings May 8, 2026 14:42
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from f2c45fb to c4e9cc9 Compare May 8, 2026 14:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_DAEphys.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from c4e9cc9 to ddddce4 Compare May 8, 2026 16:48
Copilot AI review requested due to automatic review settings May 8, 2026 17:21
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from ddddce4 to 3b13ea8 Compare May 8, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf Outdated
Comment thread Packages/MIES/MIES_AmplifierInteraction.ipf Outdated
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
Comment thread Packages/MIES/MIES_ForeignFunctionInterface.ipf
@MichaelHuth
MichaelHuth force-pushed the feature/2559-mh_add_ffi_clamp_control branch from 3b13ea8 to 984406a Compare May 8, 2026 17:56
@MichaelHuth

Copy link
Copy Markdown
Collaborator Author

@campagnola I changed the following:

  • previous FFI_GetClampState is now FFI_GetCurrentClampState
  • New FFI_GetClampState returns now the Amplifier Storage Wave from MIES for the specified headstage with all amplifier settings. The wave has dimension labels and the function has docu.
  • FFI_SetHoldingPotential and FFI_SetBiasCurrent have an additional enable argument that can be 0 (disable) and 1 (enable). When you set enable the value is written first then it is enabled, if you disable then it is first disabled and then the value set.
  • FFI_SetAutoBias uses the same order for value setting and enabling as FFI_SetHoldingPotential and FFI_SetBiasCurrent
  • New FFI_GetHeadstageActive returns 1 or 0, depending if the given headstage is enabled or not.
  • FFI_SetHeadstageActive raises an assertion when attempting to activate a headstage while data acquisition is running
  • For FFI_SetClampMode the clamp mode change is deferred if data acquisition is running until it stopped. If that is problematic I can also add an assertion there for this case.

@MichaelHuth MichaelHuth assigned campagnola and unassigned MichaelHuth May 8, 2026
@t-b

t-b commented May 28, 2026

Copy link
Copy Markdown
Collaborator

@campagnola Friendly ping.

this was previously done directly in the GUI event handler
In MIES the range is limited through the corresponding GUi element.
However, when changed externally through a ZeroMQ function call then
the range was not checked.
Added the following functions:

FFI_GetCurrentClampState
returns a wave with the current clamp control values

FFI_GetClampState
returns a wave with the all clamp control values

FFI_SetClampMode

FFI_SetHoldingPotential
- allowed only in VC mode

FFI_SetBiasCurrent
- allowed only in IC mode

FFI_SetAutoBias
- allowed only in IC mode

FFI_SetHeadstageActive
- allowed only when no DAQ is running

FFI_GetHeadstageActive
- returns 1 is the given headstage is active, 0 otherwise

FFI_TriggerAutoClampControl
- wraps Pipette, Capacitance and Bridge Balance auto setting in a single call
Copilot AI review requested due to automatic review settings July 24, 2026 14:58
@t-b
t-b force-pushed the feature/2559-mh_add_ffi_clamp_control branch from 984406a to 17f5faf Compare July 24, 2026 14:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (3)

Packages/MIES/MIES_ForeignFunctionInterface.ipf:786

  • FFI_GetClampState says it returns a null wave for an "invalid" headstage, but invalid indices currently ASSERT in FFI_CheckValidDeviceAndHeadstage. Please align the API contract (either return null for invalid indices, or adjust docs to state that invalid input raises an error).
/// @param[in] device    Device title, e.g. "Dev1"
/// @param[in] headstage headstage number
/// @returns             wave with full clamp state, null wave for invalid/inactive headstage
Function/WAVE FFI_GetClampState(string device, variable headstage)

Packages/MIES/MIES_ForeignFunctionInterface.ipf:932

  • FFI_SetAutoBias currently forwards potential to the amplifier without validating it is finite and within the documented range (-99..99 mV). Adding explicit checks here provides a clearer FFI-level error (and avoids relying on downstream assertions).
	clampMode = DAG_GetHeadstageMode(device, headstage)
	ASSERT(clampMode == I_CLAMP_MODE, "Attempt to set auto bias but current clamp mode is not IC !")

Packages/MIES/MIES_ForeignFunctionInterface.ipf:618

  • The VC documentation table for FFI_GetCurrentClampState lists WholeCellCap twice (once in pF and again in a.u.), but the implementation filters out button-only controls via AI_AmpStorageControlToRowLabel and does not return an a.u. entry here. This duplicate label/unit is misleading for FFI consumers relying on dimension labels.
/// | PipetteOffsetVC        |                       | mV      |
/// +------------------------+-----------------------+---------+
/// | WholeCellCap           |                       | a.u.    |
/// +------------------------+-----------------------+---------+

Comment on lines +667 to +670
/// @param[in] device Device title, e.g. "Dev1"
/// @param[in] headstage headstage number
/// @returns wave with clamp state, null wave for invalid/inactive headstage
Function/WAVE FFI_GetCurrentClampState(string device, variable headstage)
Comment on lines +846 to +850
/// @param[in] device Device title, e.g. "Dev1"
/// @param[in] headstage headstage number
/// @param[in] potential holding potential in mV when current clamp mode of headstage is VC, bias current in pA when current clamp mode of headstage is IC
/// @param[in] enable when set to 1, enables Holding Potential after setting its value, when set to 0, disables Holding Potential before setting its values
Function FFI_SetHoldingPotential(string device, variable headstage, variable potential, variable enable)

static Function FFI_CheckValidDeviceAndHeadstage(string device, variable headstage)

ASSERT(!DAP_DeviceIsUnlocked(device), "Target device is not locked:" + device)
Comment on lines +5902 to +5906
Function DAP_SetClampMode(string device, variable headstage, variable mode)

NVAR dataAcqRunMode = $GetDataAcqRunMode(device)
if(dataAcqRunMode == DAQ_NOT_RUNNING)
DAP_ChangeHeadStageMode(device, mode, headstage, DO_MCC_MIES_SYNCING)
@t-b

t-b commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

@MichaelHuth After chatting with Tim we agreed to add some tests and then merge.

@t-b t-b assigned MichaelHuth and unassigned campagnola Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

FFI Foreign Function Interface PatchLink

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FFI for clamp control

4 participants