From 1db17b044cb8338b0df4fdc3160f9636c627ba12 Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Fri, 13 Mar 2015 23:41:28 +0100
Subject: [PATCH] Move electrode event channels to new format

---
 Source/Processors/Channel/Channel.cpp         | 25 +++++++++++++------
 Source/Processors/Channel/Channel.h           | 17 ++++++++++---
 .../GenericProcessor/GenericProcessor.h       |  2 +-
 .../SpikeDetector/SpikeDetector.cpp           | 19 +++-----------
 .../SpikeDisplayNode/SpikeDisplayNode.cpp     | 16 ++----------
 Source/Processors/SpikeSorter/SpikeSorter.cpp | 18 +++----------
 .../Processors/Visualization/SpikeObject.cpp  | 25 +++++++++++++++++++
 Source/Processors/Visualization/SpikeObject.h | 17 +++++++++++++
 8 files changed, 83 insertions(+), 56 deletions(-)

diff --git a/Source/Processors/Channel/Channel.cpp b/Source/Processors/Channel/Channel.cpp
index e3a20afb7..dee6db379 100644
--- a/Source/Processors/Channel/Channel.cpp
+++ b/Source/Processors/Channel/Channel.cpp
@@ -70,6 +70,7 @@ Channel::Channel(const Channel& ch)
     y = ch.y;
     z = ch.z;
     impedance = ch.impedance;
+	extraData = ch.extraData;
 
     setRecordState(false);
 }
@@ -138,14 +139,8 @@ void Channel::createDefaultName()
         case EVENT_CHANNEL:
             name = String("EVENT");
             break;
-        case SINGLE_ELECTRODE:
-            name = String("SE");
-            break;
-        case STEREOTRODE:
-            name = String("ST");
-            break;
-        case TETRODE:
-            name = String("TT");
+        case ELECTRODE_CHANNEL:
+            name = String("ELEC");
             break;
         case MESSAGE_CHANNEL:
             name = String("MSG");
@@ -153,3 +148,17 @@ void Channel::createDefaultName()
 
     name += index;
 }
+
+bool Channel::getRecordState()
+{
+	return isRecording;
+}
+
+ChannelExtraData::ChannelExtraData(void* ptr, int size)
+	: dataPtr(ptr), dataSize(size)
+{
+}
+
+ChannelExtraData::~ChannelExtraData()
+{
+}
\ No newline at end of file
diff --git a/Source/Processors/Channel/Channel.h b/Source/Processors/Channel/Channel.h
index af5377760..f7dceb166 100644
--- a/Source/Processors/Channel/Channel.h
+++ b/Source/Processors/Channel/Channel.h
@@ -32,6 +32,7 @@
 #include <stdio.h>
 
 class GenericProcessor;
+class ChannelExtraData;
 
 /**
 
@@ -87,10 +88,7 @@ public:
     void setRecordState(bool t); // {isRecording = t;}
 
     /** Sets whether or not the channel will record. */
-    bool getRecordState()
-    {
-        return isRecording;
-    }
+	bool getRecordState();
 
     /** Sets the bitVolts value for this channel. */
     void setBitVolts(float bitVolts);
@@ -154,6 +152,8 @@ public:
     /** Impedance of this channel. */
     float impedance;
 
+	/** For use with special event channels. */
+	ReferenceCountedObjectPtr<ChannelExtraData> extraData;
 
 private:
 
@@ -167,4 +167,13 @@ private:
 
 };
 
+class ChannelExtraData : public ReferenceCountedObject
+{
+public:
+	ChannelExtraData(void* data, int size);
+	virtual ~ChannelExtraData();
+	void* const dataPtr;
+	const int dataSize;
+};
+
 #endif  // __CHANNEL_H_DABDFE3F__
diff --git a/Source/Processors/GenericProcessor/GenericProcessor.h b/Source/Processors/GenericProcessor/GenericProcessor.h
index 9da3ee596..bf5cc3c4b 100755
--- a/Source/Processors/GenericProcessor/GenericProcessor.h
+++ b/Source/Processors/GenericProcessor/GenericProcessor.h
@@ -25,7 +25,7 @@
 #define __GENERICPROCESSOR_H_1F469DAF__
 
 enum ChannelType {HEADSTAGE_CHANNEL = 0, AUX_CHANNEL = 1, ADC_CHANNEL = 2, EVENT_CHANNEL = 3,
-                  SINGLE_ELECTRODE = 4, STEREOTRODE = 5, TETRODE = 6, MESSAGE_CHANNEL = 7
+                  ELECTRODE_CHANNEL = 4,  MESSAGE_CHANNEL = 5 
                  };
 
 #include "../../../JuceLibraryCode/JuceHeader.h"
diff --git a/Source/Processors/SpikeDetector/SpikeDetector.cpp b/Source/Processors/SpikeDetector/SpikeDetector.cpp
index bc53a8b64..c51bbc4dc 100755
--- a/Source/Processors/SpikeDetector/SpikeDetector.cpp
+++ b/Source/Processors/SpikeDetector/SpikeDetector.cpp
@@ -88,21 +88,10 @@ void SpikeDetector::updateSettings()
     for (int i = 0; i < electrodes.size(); i++)
     {
 
-        Channel* ch;
-
-        switch (electrodes[i]->numChannels)
-        {
-            case 1:
-                ch = new Channel(this, i, SINGLE_ELECTRODE);
-                break;
-            case 2:
-                ch = new Channel(this, i, STEREOTRODE);
-                break;
-            case 4:
-                ch = new Channel(this, i, TETRODE);
-                break;
-        }
-
+        Channel* ch = new Channel(this,i,ELECTRODE_CHANNEL);
+		ch->name = generateSpikeElectrodeName(electrodes[i]->numChannels, ch->index);
+		SpikeChannel* spk = new SpikeChannel(SpikeChannel::Plain, electrodes[i]->numChannels, NULL, 0);
+		ch->extraData = spk;
         eventChannels.add(ch);
     }
 
diff --git a/Source/Processors/SpikeDisplayNode/SpikeDisplayNode.cpp b/Source/Processors/SpikeDisplayNode/SpikeDisplayNode.cpp
index fa1c2f702..7b0253be6 100755
--- a/Source/Processors/SpikeDisplayNode/SpikeDisplayNode.cpp
+++ b/Source/Processors/SpikeDisplayNode/SpikeDisplayNode.cpp
@@ -60,24 +60,12 @@ void SpikeDisplayNode::updateSettings()
     {
         ChannelType type = eventChannels[i]->getType();
 
-        if (type == SINGLE_ELECTRODE || type == STEREOTRODE || type == TETRODE)
+        if (type == ELECTRODE_CHANNEL)
         {
 
             Electrode elec;
+			elec.numChannels = static_cast<SpikeChannel*>(eventChannels[i]->extraData.get())->numChannels;
 
-            switch (type)
-            {
-                case SINGLE_ELECTRODE:
-                    elec.numChannels = 1;
-                    break;
-                case STEREOTRODE:
-                    elec.numChannels = 2;
-                    break;
-                case TETRODE:
-                    elec.numChannels = 4;
-                    break;
-            }
-            
             elec.name = eventChannels[i]->getName();
             elec.currentSpikeIndex = 0;
             elec.mostRecentSpikes.ensureStorageAllocated(displayBufferSize);
diff --git a/Source/Processors/SpikeSorter/SpikeSorter.cpp b/Source/Processors/SpikeSorter/SpikeSorter.cpp
index 3fab6eb2a..c31b2b9d6 100644
--- a/Source/Processors/SpikeSorter/SpikeSorter.cpp
+++ b/Source/Processors/SpikeSorter/SpikeSorter.cpp
@@ -192,20 +192,10 @@ void SpikeSorter::updateSettings()
     for (int i = 0; i < electrodes.size(); i++)
     {
 
-        Channel* ch;
-
-        switch (electrodes[i]->numChannels)
-        {
-            case 1:
-                ch = new Channel(this, i, SINGLE_ELECTRODE);
-                break;
-            case 2:
-                ch = new Channel(this, i, STEREOTRODE);
-                break;
-            case 4:
-                ch = new Channel(this, i, TETRODE);
-                break;
-        }
+        Channel* ch = new Channel(this,i,ELECTRODE_CHANNEL);
+		ch->name = generateSpikeElectrodeName(electrodes[i]->numChannels, ch->index);
+		SpikeChannel* spk = new SpikeChannel(SpikeChannel::Sorted, electrodes[i]->numChannels, electrodes[i], sizeof(Electrode));
+		ch->extraData = spk;
 
         eventChannels.add(ch);
     }
diff --git a/Source/Processors/Visualization/SpikeObject.cpp b/Source/Processors/Visualization/SpikeObject.cpp
index 702b9cc53..ce61eb9ba 100755
--- a/Source/Processors/Visualization/SpikeObject.cpp
+++ b/Source/Processors/Visualization/SpikeObject.cpp
@@ -400,3 +400,28 @@ int microSecondsToSpikeTimeBin(SpikeObject *s, float t, int ch)
 }
 
 
+SpikeChannel::SpikeChannel(SpikeDataType type, int nChans, void* ptr, int size)
+	: dataType(type), numChannels(nChans), ChannelExtraData(ptr, size)
+{
+}
+
+String generateSpikeElectrodeName(int numChannels, int index)
+{
+	String name;
+	switch (numChannels)
+	{
+	case 1:
+		name = String("SE");
+		break;
+	case 2:
+		name = String("ST");
+		break;
+	case 4:
+		name = String("TT");
+		break;
+	default:
+		name = String("ELEC");
+		break;
+	}
+	return name + String(index);
+}
\ No newline at end of file
diff --git a/Source/Processors/Visualization/SpikeObject.h b/Source/Processors/Visualization/SpikeObject.h
index a386ee0ef..c3a06c073 100755
--- a/Source/Processors/Visualization/SpikeObject.h
+++ b/Source/Processors/Visualization/SpikeObject.h
@@ -29,6 +29,8 @@
 #include <stdint.h>
 #include <math.h>
 
+#include "../Channel/Channel.h"
+
 #define SPIKE_METADATA_SIZE 42
 #define MAX_NUMBER_OF_SPIKE_CHANNELS 4
 #define MAX_NUMBER_OF_SPIKE_CHANNEL_SAMPLES 80
@@ -37,6 +39,21 @@
 #define MAX_SPIKE_BUFFER_LEN 512 // max length of spike buffer in bytes
                                  // the true max calculated from the spike values below is actually 507
 
+/** Class to store spike data in event channels */
+class SpikeChannel : public ChannelExtraData
+{
+public:
+	enum SpikeDataType { Plain = 0, Sorted = 1};
+
+	SpikeChannel(SpikeDataType type, int nChans, void* ptr, int size);
+
+	const SpikeDataType dataType;
+	const int numChannels;
+};
+
+/** Simple generic function to name spike electrode channels */
+String generateSpikeElectrodeName(int numChannels, int index);
+
 #define SPIKE_BASE_CODE 100
 
 /**
-- 
GitLab