Open Ephys GUI
 All Classes Functions Variables Pages
SpikeObject.h
1 /*
2  ------------------------------------------------------------------
3 
4  This file is part of the Open Ephys GUI
5  Copyright (C) 2012 Open Ephys
6 
7  ------------------------------------------------------------------
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifndef SPIKEOBJECT_H_
25 #define SPIKEOBJECT_H_
26 
27 #include <stdint.h>
28 #include <math.h>
29 
30 #define MAX_NUMBER_OF_SPIKE_CHANNELS 4
31 #define MAX_NUMBER_OF_SPIKE_CHANNEL_SAMPLES 256
32 #define CHECK_BUFFER_VALIDITY true
33 #define SPIKE_EVENT_CODE 4;
34 
35 struct SpikeObject{
36 
37  uint8_t eventType;
38  uint64_t timestamp;
39  uint16_t source;
40  uint16_t nChannels;
41  uint16_t nSamples;
42  uint16_t data[MAX_NUMBER_OF_SPIKE_CHANNELS * MAX_NUMBER_OF_SPIKE_CHANNEL_SAMPLES];
43  uint16_t gain[MAX_NUMBER_OF_SPIKE_CHANNELS];
44  uint16_t threshold[MAX_NUMBER_OF_SPIKE_CHANNELS];
45 
46 };
47 
48 /*
49  For transmission between processors SpikeObjects must be packaged up into buffers that can fit into MidiEvents
50  The following two methods can be used to package the above spike object into a buffer and unpackage a buffer
51  into a SpikeObject.
52 
53  The buffer is LittleEndian (thank Intel) and the byte order is the same as the SpikeObject definition.
54  IE. the first 2 bytes are the timestamp, the next two bytes are the source identifier, etc... with the last
55  set of bytes corresponding to the thresholds of the different channels.
56 
57  Finally the buffer will have an additional byte on the end that is used to check the integerity of the entire package.
58  The way this works is the buffer is divivded up into a series of 16 bit unsigned integers. The sum of all these integers
59  (except the last 16 bit integer) is taken and the sum should equal that 16 bit integer. If not then the data is corrupted
60  and should be dropped or dealt with another way.
61 */
62 
63 // Simple method for serializing a SpikeObject into a string of bytes, returns true is the packaged spike buffer is valid
64 int packSpike(SpikeObject *s, uint8_t* buffer, int bufferLength);
65 
66 // Simple method for deserializing a string of bytes into a Spike object, returns true is the provided spike buffer is valid
67 bool unpackSpike(SpikeObject *s, uint8_t* buffer, int bufferLength);
68 
69 // Checks the validity of the buffer, this should be run before unpacking the buffer
70 bool isBufferValid(uint8_t *buffer, int bufferLength);
71 
72 // Computes the validity value for the buffer, this should be called after packing the buffer
73 void makeBufferValid(uint8_t *buffer, int bufferLength);
74 
75 // Help function for generating fake spikes in the absence of a real spike source.
76 // Can be used to generate a sign wave with a fixed Frequency of 1000 hz or a basic spike waveform
77 // Additionally noise can be added to the waveform for help in diagnosing projection plots
78 void generateSimulatedSpike(SpikeObject *s, uint64_t timestamp, int noise);
79 
80 // Define the << operator for the SpikeObject
81 // std::ostream& operator<<(std::ostream &strm, const SpikeObject s);
82 
83 // Helper function for zeroing out a spike object with a specified number of channels
84 void generateEmptySpike(SpikeObject *s, int nChannels);
85 
86 
87 #endif //SPIKEOBJECT_H_