Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
59 changes: 50 additions & 9 deletions Source/EphysSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void EphysSocket::registerParameters()
addFloatParameter (Parameter::PROCESSOR_SCOPE, "sample_rate", "Sample Rate", "Sample rate of incoming data", "Hz", DEFAULT_SAMPLE_RATE, MIN_SAMPLE_RATE, MAX_SAMPLE_RATE, 1.0f);
addFloatParameter (Parameter::PROCESSOR_SCOPE, "data_scale", "Scale", "Scale of incoming data", "", DEFAULT_DATA_SCALE, MIN_DATA_SCALE, MAX_DATA_SCALE, 0.1f);
addFloatParameter (Parameter::PROCESSOR_SCOPE, "data_offset", "Offset", "Offset of incoming data", "", DEFAULT_DATA_OFFSET, MIN_DATA_OFFSET, MAX_DATA_OFFSET, 1.0f);
addStringParameter (Parameter::PROCESSOR_SCOPE, "connection_state", "Connection State", "Set the socket connection state (CONNECTED/DISCONNECTED)", DEFAULT_CONNECTION_STATE);
Comment thread
tzijnge marked this conversation as resolved.
Outdated
}

void EphysSocket::disconnectSocket()
Expand All @@ -54,14 +55,17 @@ void EphysSocket::disconnectSocket()
getParameter ("sample_rate")->setEnabled (true);
getParameter ("data_scale")->setEnabled (true);
getParameter ("data_offset")->setEnabled (true);
getParameter ("connection_state")->setNextValue (CONNECTION_STATE_DISCONNECTED);

if (sn->getEditor() != nullptr) // check if headless
static_cast<EphysSocketEditor*> (sn->getEditor())->disconnected();
}

bool EphysSocket::connectSocket (bool printOutput)
{
if (socket.connectSocket (port, printOutput))
const bool connected = socket.connectSocket (port, printOutput);

if (connected)
{
getParameter ("port")->setEnabled (false);
getParameter ("sample_rate")->setEnabled (false);
Expand All @@ -70,11 +74,11 @@ bool EphysSocket::connectSocket (bool printOutput)

if (sn->getEditor() != nullptr) // check if headless
static_cast<EphysSocketEditor*> (sn->getEditor())->connected();

return true;
}

return false;
getParameter ("connection_state")->setNextValue (connected ? CONNECTION_STATE_CONNECTED : CONNECTION_STATE_DISCONNECTED);

return connected;
}

bool EphysSocket::errorFlag()
Expand Down Expand Up @@ -176,6 +180,14 @@ void EphysSocket::parameterValueChanged (Parameter* parameter)
{
data_offset = (float) parameter->getValue();
}
else if (parameter->getName() == "connection_state")
{
// This is mainly useful when the application settings have
// for some reason saved connection_state DISCONNECTED. At
// startup, the connection state will be synced with the actual
// state of the socket
parameter->setNextValue (socket.isConnected() ? CONNECTION_STATE_CONNECTED : CONNECTION_STATE_DISCONNECTED);
Comment thread
tzijnge marked this conversation as resolved.
Outdated
}
}

bool EphysSocket::startAcquisition()
Expand Down Expand Up @@ -283,17 +295,13 @@ String EphysSocket::handleConfigMessage (const String& msg)
// ES OFFSET <data_offset> - Updates the offset to data_offset
// ES PORT <port> - Updates the port number that EphysSocket connects to
// ES FREQUENCY <sample_rate> - Updates the sampling rate
// ES CONNECTED <connected> - Updates the connection state
Comment thread
tzijnge marked this conversation as resolved.
Outdated

if (CoreServices::getAcquisitionStatus())
{
return "Ephys Socket plugin cannot update settings while acquisition is active.";
}

if (socket.isConnected())
{
return "Ephys Socket plugin cannot update settings while connected to an active socket.";
}

StringArray parts = StringArray::fromTokens (msg, " ", "");

if (parts.size() > 0)
Expand All @@ -302,6 +310,11 @@ String EphysSocket::handleConfigMessage (const String& msg)
{
if (parts.size() == 3)
{
if (socket.isConnected() && ! parts[1].equalsIgnoreCase ("CONNECTION_STATE"))
Comment thread
tzijnge marked this conversation as resolved.
Outdated
{
return "Ephys Socket plugin cannot update settings while connected to an active socket.";
}

if (parts[1].equalsIgnoreCase ("SCALE"))
{
float scale = parts[2].getFloatValue();
Expand Down Expand Up @@ -354,6 +367,34 @@ String EphysSocket::handleConfigMessage (const String& msg)

return "Invalid frequency requested. Frequency can be set between '" + String (MIN_SAMPLE_RATE) + "' and '" + String (MAX_SAMPLE_RATE) + "'";
}
else if (parts[1].equalsIgnoreCase ("CONNECTION_STATE"))
{
bool connected { false };
const bool connected_state = parts[2].equalsIgnoreCase (CONNECTION_STATE_CONNECTED);
const bool disconnected_state = parts[2].equalsIgnoreCase (CONNECTION_STATE_DISCONNECTED);

if ((! connected_state) && (! disconnected_state))
{
LOGC ("Invalid connection state: ", parts[2]);
return String ("Connection state must be ") + CONNECTION_STATE_CONNECTED + " or " + CONNECTION_STATE_CONNECTED;
}

if (connected_state)
{
LOGC ("Request socket connect");
connected = connectSocket();
LOGC (connected ? "Connection success" : "Connection failed");
}
else
{
LOGC ("Request socket disconnect");
disconnectSocket();
LOGC ("Socket disconnected");
connected = false;
}

return connected ? CONNECTION_STATE_CONNECTED : CONNECTION_STATE_DISCONNECTED;
}
else
{
return "ES command " + parts[1] + "not recognized.";
Expand Down
35 changes: 20 additions & 15 deletions Source/EphysSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ namespace EphysSocketNode
class EphysSocket : public DataThread
{
public:
/** Connection states */
static const constexpr char* CONNECTION_STATE_CONNECTED { "CONNECTED" };
static const constexpr char* CONNECTION_STATE_DISCONNECTED { "DISCONNECTED" };

/** Default parameters */
const int DEFAULT_PORT = 9001;
const float DEFAULT_SAMPLE_RATE = 30000.0f;
const float DEFAULT_DATA_SCALE = 1.0f; // 0.195f for Intan devices
const float DEFAULT_DATA_OFFSET = 0.0f; // 32768.0f for Intan devices
static constexpr int DEFAULT_PORT { 9001 };
static constexpr float DEFAULT_SAMPLE_RATE { 30000.0f };
static constexpr float DEFAULT_DATA_SCALE { 1.0f }; // 0.195f for Intan devices
static constexpr float DEFAULT_DATA_OFFSET { 0.0f }; // 32768.0f for Intan devices
static constexpr char const* DEFAULT_CONNECTION_STATE { CONNECTION_STATE_DISCONNECTED };

/** Parameter limits */
const float MIN_DATA_SCALE = 0.0f;
const float MAX_DATA_SCALE = 9999.9f;
const float MIN_DATA_OFFSET = 0;
const float MAX_DATA_OFFSET = 65536;
const float MIN_PORT = 1023;
const float MAX_PORT = 65535;
const float MIN_SAMPLE_RATE = 0;
const float MAX_SAMPLE_RATE = 50000.0f;
static constexpr float MIN_DATA_SCALE { 0.0f };
static constexpr float MAX_DATA_SCALE { 9999.9f };
static constexpr float MIN_DATA_OFFSET { 0 };
static constexpr float MAX_DATA_OFFSET { 65536 };
static constexpr float MIN_PORT { 1023 };
static constexpr float MAX_PORT { 65535 };
static constexpr float MIN_SAMPLE_RATE { 0 };
static constexpr float MAX_SAMPLE_RATE { 50000.0f };

/** Constructor */
EphysSocket (SourceNode* sn);
Expand All @@ -34,7 +39,7 @@ class EphysSocket : public DataThread
~EphysSocket();

/** Creates custom editor */
std::unique_ptr<GenericEditor> createEditor (SourceNode* sn);
std::unique_ptr<GenericEditor> createEditor (SourceNode* sn) override;

/** Create the DataThread object*/
static DataThread* createDataThread (SourceNode* sn);
Expand All @@ -51,13 +56,13 @@ class EphysSocket : public DataThread
OwnedArray<SpikeChannel>* spikeChannels,
OwnedArray<DataStream>* sourceStreams,
OwnedArray<DeviceInfo>* devices,
OwnedArray<ConfigurationObject>* configurationObjects);
OwnedArray<ConfigurationObject>* configurationObjects) override;

/** Handles parameter value changes */
void parameterValueChanged (Parameter* parameter) override;

/** Resizes buffers when input parameters are changed*/
void resizeBuffers();
void resizeBuffers() override;

/** Disconnects the socket */
void disconnectSocket();
Expand Down