Skip to content

Commit baa0a35

Browse files
committed
Add support for handling incoming events inside the Python module
1 parent 548bfa9 commit baa0a35

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

Modules/template/processor_template.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
class PyProcessor:
55

66
def __init__(self, num_channels, sample_rate):
7-
""" A new processor is initialized whenever the plugin settings are updated """
7+
"""
8+
A new processor is initialized whenever the plugin settings are updated
9+
10+
Parameters:
11+
num_channels (int): number of input channels in the selected stream.
12+
sample_rate (float): sample rate of the selected stream
13+
"""
814
print("Num Channels: ", num_channels, " | Sample Rate: ", sample_rate)
915
# pass
1016

@@ -27,13 +33,26 @@ def start_acquisition(self):
2733
def stop_acquisition(self):
2834
""" Called when acquisition is stopped """
2935
pass
36+
37+
def handle_ttl_event(self, source_node, channel, sample_number, line, state):
38+
"""
39+
Handle each incoming ttl event.
40+
41+
Parameters:
42+
source_node (int): id of the processor this event was generated from
43+
channel (str): name of the event channel
44+
sample_number (int): sample number of the event
45+
line (int): the line on which event was generated (0-255)
46+
state (int): event state 0 (OFF) or 1 (ON)
47+
"""
48+
pass
3049

3150
def start_recording(self, recording_dir):
3251
"""
3352
Called when recording starts
3453
3554
Parameters:
36-
recording_dir - recording directory to be used by future record nodes.
55+
recording_dir (str): recording directory to be used by future record nodes.
3756
"""
3857
pass
3958

Source/PythonProcessor.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void PythonProcessor::process(AudioBuffer<float>& buffer)
9191
if( !moduleReady )
9292
return;
9393

94-
// checkForEvents(true);
94+
checkForEvents(true);
9595

9696
for (auto stream : getDataStreams())
9797
{
@@ -138,25 +138,29 @@ void PythonProcessor::process(AudioBuffer<float>& buffer)
138138
}
139139
}
140140

141-
// void PythonProcessor::handleTTLEvent(TTLEventPtr event)
142-
// {
143-
// // Get ttl info
144-
// const int state = event->getState() ? 1 : 0;
145-
// const int64 sampleNumber = event->getSampleNumber();
146-
// const int channel = event->getChannelIndex();
147-
// const uint8 line = event->getLine();
148-
// const uint16 streamId = event->getStreamId();
149-
150-
// // Give to python
151-
// // py::gil_scoped_acquire acquire;
141+
void PythonProcessor::handleTTLEvent(TTLEventPtr event)
142+
{
143+
if (event->getStreamId() == currentStream)
144+
{
145+
// Get ttl info
146+
auto chanInfo = event->getChannelInfo();
147+
auto channelName = chanInfo->getName();
148+
const int sourceNodeId = chanInfo->getSourceNodeId();
149+
const int64 sampleNumber = event->getSampleNumber();
150+
const uint8 line = event->getLine();
151+
const int state = event->getState() ? 1 : 0;
152+
153+
// Give to python
154+
// py::gil_scoped_acquire acquire;
152155

153-
// try {
154-
// pyObject->attr("handle_ttl_event")(state, sampleNumber, channel, line, streamId);
155-
// }
156-
// catch (py::error_already_set& e) {
157-
// handlePythonException(e);
158-
// }
159-
// }
156+
try {
157+
pyObject->attr("handle_ttl_event")(sourceNodeId, channelName.toRawUTF8(), sampleNumber, line, state);
158+
}
159+
catch (py::error_already_set& e) {
160+
handlePythonException(e);
161+
}
162+
}
163+
}
160164

161165

162166
// void PythonProcessor::handleSpike(SpikePtr event)

Source/PythonProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class PythonProcessor : public GenericProcessor
9090
// /** Handles events received by the processor
9191
// Called automatically for each received event whenever checkForEvents() is called from
9292
// the plugin's process() method */
93-
// void handleTTLEvent(TTLEventPtr event) override;
93+
void handleTTLEvent(TTLEventPtr event) override;
9494

9595
// /** Handles spikes received by the processor
9696
// Called automatically for each received spike whenever checkForEvents(true) is called from

0 commit comments

Comments
 (0)