Skip to content

Commit

Permalink
Apply formatting to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tstenner committed Aug 27, 2021
1 parent 93eb0f2 commit 0589ec8
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 227 deletions.
4 changes: 2 additions & 2 deletions examples/GetAllStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* resolved and displayed. This is useful for browsing applications.
*/

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
try {
std::cout << "Here is a one-shot resolve of all current streams:" << std::endl;

Expand Down Expand Up @@ -54,7 +54,7 @@ int main(int argc, char* argv[]) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}

} catch (std::exception& e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
return 0;
Expand Down
14 changes: 7 additions & 7 deletions examples/GetFullinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* resolve functions only includes the core information otherwise.
*/

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
std::string field, value;
if (argc != 3) {
std::cout << "This connects to a stream which has a particular value for a given field and "
"displays its full stream_info contentss."
<< std::endl;
"displays its full stream_info contentss."
<< std::endl;
std::cout << "Please enter a field name and the desired value (e.g. \"type EEG\" (without "
"the quotes)):"
<< std::endl;
"the quotes)):"
<< std::endl;
std::cin >> field >> value;
} else {
field = argv[1];
Expand All @@ -36,11 +36,11 @@ int main(int argc, char* argv[]) {

// get & display the info
std::cout << "The information about this stream is displayed in the following: "
<< std::endl;
<< std::endl;
lsl::stream_info info = inlet.info();
std::cout << info.as_xml() << std::endl;

} catch (std::exception& e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
return 0;
Expand Down
14 changes: 7 additions & 7 deletions examples/GetTimeCorrection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* should be approx. 0 (up to some tolerance).
*/

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
std::string field, value;
if (argc != 3) {
std::cout << "This connects to a stream which has a particular value for a given field and "
"gets the time-synchronization information for it."
<< std::endl;
"gets the time-synchronization information for it."
<< std::endl;
std::cout << "Please enter a field name and the desired value (e.g. \"type EEG\" (without "
"the quotes)):"
<< std::endl;
"the quotes)):"
<< std::endl;
std::cin >> field >> value;
} else {
field = argv[1];
Expand All @@ -38,13 +38,13 @@ int main(int argc, char* argv[]) {

// start receiving & displaying the data
std::cout << "Press [Enter] to query a new correction value (clocks may drift)..."
<< std::endl;
<< std::endl;
while (true) {
std::cout << inlet.time_correction() << std::endl;
std::cin.get();
}

} catch (std::exception& e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
return 0;
Expand Down
32 changes: 16 additions & 16 deletions examples/HandleMetaData.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#include <iostream>
#include <lsl_cpp.h>

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
try {
// create a new stream_info and declare some meta-data (in accordance with XDF format)
const char* name = argc > 1 ? argv[1] : "MetaTester";
const char *name = argc > 1 ? argv[1] : "MetaTester";
lsl::stream_info info(name, "EEG", 8, 100, lsl::cf_float32, "myuid323457");
lsl::xml_element chns = info.desc().append_child("channels");
const char* labels[] = {"C3", "C4", "Cz", "FPz", "POz", "CPz", "O1", "O2"};
for (const char* label : labels)
const char *labels[] = {"C3", "C4", "Cz", "FPz", "POz", "CPz", "O1", "O2"};
for (const char *label : labels)
chns.append_child("channel")
.append_child_value("label", label)
.append_child_value("unit", "microvolts")
.append_child_value("type", "EEG");
.append_child_value("label", label)
.append_child_value("unit", "microvolts")
.append_child_value("type", "EEG");
info.desc().append_child_value("manufacturer", "SCCN");
info.desc()
.append_child("cap")
.append_child_value("name", "EasyCap")
.append_child_value("size", "54")
.append_child_value("labelscheme", "10-20");
.append_child("cap")
.append_child_value("name", "EasyCap")
.append_child_value("size", "54")
.append_child_value("labelscheme", "10-20");

// create outlet for the stream
lsl::stream_outlet outlet(info);
Expand All @@ -32,17 +32,17 @@ int main(int argc, char* argv[]) {
// get the full stream info (including custom meta-data) and dissect it
lsl::stream_info inf = inlet.info();
std::cout << "The stream's XML meta-data is: \n"
<< inf.as_xml()
<< "\nThe manufacturer is: " << inf.desc().child_value("manufacturer")
<< "\nThe cap circumference is: " << inf.desc().child("cap").child_value("size")
<< "\nThe channel labels are as follows:\n";
<< inf.as_xml()
<< "\nThe manufacturer is: " << inf.desc().child_value("manufacturer")
<< "\nThe cap circumference is: " << inf.desc().child("cap").child_value("size")
<< "\nThe channel labels are as follows:\n";
lsl::xml_element ch = inf.desc().child("channels").child("channel");
for (int k = 0; k < info.channel_count(); k++) {
std::cout << " " << ch.child_value("label") << std::endl;
ch = ch.next_sibling();
}

} catch (std::exception& e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
return 0;
Expand Down
65 changes: 33 additions & 32 deletions examples/HandleMetaDataC.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
#include <lsl_c.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
int c; /* channel index */
lsl_xml_ptr desc,chns,chn,cap; /* some xml element pointers */
lsl_outlet outlet; /* stream outlet */
lsl_streaminfo info,inf; /* streaminfo objects */
lsl_inlet inlet; /* a stream inlet to get samples from */
int errcode; /* error code (lsl_lost_error or timeouts) */
const char* labels[] = {"C3","C4","Cz","FPz","POz","CPz","O1","O2"};
int main(int argc, char *argv[]) {
int c; /* channel index */
lsl_xml_ptr desc, chns, chn, cap; /* some xml element pointers */
lsl_outlet outlet; /* stream outlet */
lsl_streaminfo info, inf; /* streaminfo objects */
lsl_inlet inlet; /* a stream inlet to get samples from */
int errcode; /* error code (lsl_lost_error or timeouts) */
const char *labels[] = {"C3", "C4", "Cz", "FPz", "POz", "CPz", "O1", "O2"};

/* create a new streaminfo and declare some meta-data (in accordance with XDF format) */
const char* name = argc > 1 ? argv[1] : "MetaTester";
info = lsl_create_streaminfo(name,"EEG",8,100,cft_float32,"myuid323457");
const char *name = argc > 1 ? argv[1] : "MetaTester";
info = lsl_create_streaminfo(name, "EEG", 8, 100, cft_float32, "myuid323457");
desc = lsl_get_desc(info);
chns = lsl_append_child(desc,"channels");
for (c=0;c<8;c++) {
chn = lsl_append_child(chns,"channel");
lsl_append_child_value(chn,"label",labels[c]);
lsl_append_child_value(chn,"unit","microvolts");
lsl_append_child_value(chn,"type","EEG");
chns = lsl_append_child(desc, "channels");
for (c = 0; c < 8; c++) {
chn = lsl_append_child(chns, "channel");
lsl_append_child_value(chn, "label", labels[c]);
lsl_append_child_value(chn, "unit", "microvolts");
lsl_append_child_value(chn, "type", "EEG");
}
lsl_append_child_value(desc,"manufacturer","SCCN");
cap = lsl_append_child(desc,"cap");
lsl_append_child_value(cap,"name","EasyCap");
lsl_append_child_value(cap,"size","54");
lsl_append_child_value(cap,"labelscheme","10-20");
lsl_append_child_value(desc, "manufacturer", "SCCN");
cap = lsl_append_child(desc, "cap");
lsl_append_child_value(cap, "name", "EasyCap");
lsl_append_child_value(cap, "size", "54");
lsl_append_child_value(cap, "labelscheme", "10-20");

/* create outlet for the stream */
outlet = lsl_create_outlet(info,0,360);
outlet = lsl_create_outlet(info, 0, 360);

/* === the following could run on another computer === */

/* resolve the stream and open an inlet */
lsl_resolve_byprop(&info,1,"name","MetaTester",1,LSL_FOREVER);
lsl_resolve_byprop(&info, 1, "name", "MetaTester", 1, LSL_FOREVER);
inlet = lsl_create_inlet(info, 360, LSL_NO_PREFERENCE, 1);
inf = lsl_get_fullinfo(inlet,LSL_FOREVER,&errcode);
inf = lsl_get_fullinfo(inlet, LSL_FOREVER, &errcode);
printf("The stream's XML meta-data is: \n");
printf("%s\n",lsl_get_xml(inf));
printf("The manufacturer is: %s\n",lsl_child_value_n(lsl_get_desc(inf),"manufacturer"));
printf("The cap circumference is: %s\n",lsl_child_value_n(lsl_child(lsl_get_desc(inf),"cap"),"size"));
printf("%s\n", lsl_get_xml(inf));
printf("The manufacturer is: %s\n", lsl_child_value_n(lsl_get_desc(inf), "manufacturer"));
printf("The cap circumference is: %s\n",
lsl_child_value_n(lsl_child(lsl_get_desc(inf), "cap"), "size"));
printf("The channel labels are as follows:\n");
chn = lsl_child(lsl_child(lsl_get_desc(inf),"channels"),"channel");
for (c=0; c<lsl_get_channel_count(inf); c++) {
printf(" %s\n",lsl_child_value_n(chn,"label"));
chn = lsl_next_sibling(chn);
}
chn = lsl_child(lsl_child(lsl_get_desc(inf), "channels"), "channel");
for (c = 0; c < lsl_get_channel_count(inf); c++) {
printf(" %s\n", lsl_child_value_n(chn, "label"));
chn = lsl_next_sibling(chn);
}

/* destroy objects and free memory */
lsl_destroy_streaminfo(inf);
Expand Down
28 changes: 13 additions & 15 deletions examples/ReceiveData.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
#include <thread>
#include <iostream>
#include <lsl_cpp.h>
#include <string>
#include <thread>


/**
* Example program that demonstrates how to resolve a specific stream on the lab
* network and how to connect to it in order to receive data.
*/

void printChunk(const std::vector<float>& chunk, std::size_t n_channels) {
for(std::size_t i=0; i < chunk.size(); ++i) {
void printChunk(const std::vector<float> &chunk, std::size_t n_channels) {
for (std::size_t i = 0; i < chunk.size(); ++i) {
std::cout << chunk[i] << ' ';
if (i % n_channels == n_channels - 1)
std::cout << '\n';
if (i % n_channels == n_channels - 1) std::cout << '\n';
}
}

void printChunk(const std::vector<std::vector<float>>& chunk) {
for(const auto& vec: chunk)
printChunk(vec, vec.size());
void printChunk(const std::vector<std::vector<float>> &chunk) {
for (const auto &vec : chunk) printChunk(vec, vec.size());
}

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
std::string field, value;
const int max_samples = argc > 3 ? std::stoi(argv[3]) : 10;
if (argc < 3) {
std::cout << "This connects to a stream which has a particular value for a "
"given field and receives data.\nPlease enter a field name and the desired "
"value (e.g. \"type EEG\" (without the quotes)):"
<< std::endl;
"given field and receives data.\nPlease enter a field name and the desired "
"value (e.g. \"type EEG\" (without the quotes)):"
<< std::endl;
std::cin >> field >> value;
} else {
field = argv[1];
Expand All @@ -39,7 +37,7 @@ int main(int argc, char* argv[]) {
// resolve the stream of interet
std::cout << "Now resolving streams..." << std::endl;
std::vector<lsl::stream_info> results = lsl::resolve_stream(field, value);
if(results.empty()) throw std::runtime_error("No stream found");
if (results.empty()) throw std::runtime_error("No stream found");

std::cout << "Here is what was resolved: " << std::endl;
std::cout << results[0].as_xml() << std::endl;
Expand All @@ -54,7 +52,7 @@ int main(int argc, char* argv[]) {
std::vector<float> sample;
std::vector<std::vector<float>> chunk_nested_vector;

for(int i=0; i < max_samples; ++i) {
for (int i = 0; i < max_samples; ++i) {
// pull a single sample
inlet.pull_sample(sample);
printChunk(sample, inlet.get_channel_count());
Expand All @@ -73,7 +71,7 @@ int main(int argc, char* argv[]) {
printChunk(sample, inlet.get_channel_count());
}

if(argc == 1) {
if (argc == 1) {
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
}
Expand Down
45 changes: 24 additions & 21 deletions examples/ReceiveDataC.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@
#include <stdio.h>

/**
* Example program that demonstrates how to resolve a specific stream on the lab network and how to connect to it in order to receive data.
*/
* Example program that demonstrates how to resolve a specific stream on the lab network and how to
* connect to it in order to receive data.
*/

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {

unsigned k,t; /* channel index */
lsl_streaminfo info; /* the streaminfo returned by the resolve call */
lsl_inlet inlet; /* a stream inlet to get samples from */
int errcode; /* error code (lsl_lost_error or timeouts) */
float cursample[8]; /* array to hold our current sample */
double timestamp; /* time stamp of the current sample (in sender time) */
unsigned k, t; /* channel index */
lsl_streaminfo info; /* the streaminfo returned by the resolve call */
lsl_inlet inlet; /* a stream inlet to get samples from */
int errcode; /* error code (lsl_lost_error or timeouts) */
float cursample[8]; /* array to hold our current sample */
double timestamp; /* time stamp of the current sample (in sender time) */

/* resolve the stream of interest (result array: info, array capacity: 1 element, type shall be EEG, resolve at least 1 stream, wait forever if necessary) */
/* resolve the stream of interest (result array: info, array capacity: 1 element, type shall be
* EEG, resolve at least 1 stream, wait forever if necessary) */
printf("Now waiting for an EEG stream...\n");
lsl_resolve_byprop(&info, 1, "type","EEG", 1, LSL_FOREVER);
lsl_resolve_byprop(&info, 1, "type", "EEG", 1, LSL_FOREVER);

/* make an inlet to read data from the stream (buffer max. 300 seconds of data, no preference regarding chunking, automatic recovery enabled) */
/* make an inlet to read data from the stream (buffer max. 300 seconds of data, no preference
* regarding chunking, automatic recovery enabled) */
inlet = lsl_create_inlet(info, 300, LSL_NO_PREFERENCE, 1);

/* subscribe to the stream (automatically done by push, but a nice way of checking early on that we can connect successfully) */
lsl_open_stream(inlet,LSL_FOREVER,&errcode);
if (errcode != 0)
return errcode;
/* subscribe to the stream (automatically done by push, but a nice way of checking early on that
* we can connect successfully) */
lsl_open_stream(inlet, LSL_FOREVER, &errcode);
if (errcode != 0) return errcode;

printf("Displaying data...\n");
for(t=0;t<100000000;t++) {
/* get the next sample form the inlet (read into cursample, 8 values, wait forever if necessary) and return the timestamp if we got something */
timestamp = lsl_pull_sample_f(inlet,cursample,8,LSL_FOREVER,&errcode);
for (t = 0; t < 100000000; t++) {
/* get the next sample form the inlet (read into cursample, 8 values, wait forever if
* necessary) and return the timestamp if we got something */
timestamp = lsl_pull_sample_f(inlet, cursample, 8, LSL_FOREVER, &errcode);

/* print the data */
for (k=0; k<8; ++k)
printf("\t%.2f", cursample[k]);
for (k = 0; k < 8; ++k) printf("\t%.2f", cursample[k]);
printf("\n");
}

Expand Down
6 changes: 3 additions & 3 deletions examples/ReceiveDataInChunks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdint.h>
#include <iostream>
#include <lsl_cpp.h>
#include <stdint.h>


// define a packed sample struct (here a stereo sample).
Expand All @@ -9,7 +9,7 @@ struct stereo_sample {
int16_t l, r;
};

int main(int, char* []) {
int main(int, char *[]) {
try {

// resolve the stream of interest & make an inlet
Expand All @@ -23,7 +23,7 @@ int main(int, char* []) {
std::cout << timestamp << std::endl; // only showing the time stamps here
}

} catch (std::exception& e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
std::cout << "Press any key to exit. " << std::endl;
std::cin.get();
return 0;
Expand Down
Loading

0 comments on commit 0589ec8

Please sign in to comment.