Device (C++)

From Neurotech Software Development Kit
Revision as of 06:03, 26 April 2018 by NateColeman (talk | contribs) (Notes)
Jump to: navigation, search

Neuro::Device

Defined in header <device/device.h>

class Device final;

The Device class is an abstraction for NeuroMD BLE devices. This abstraction provides functions for changing of device state by executing commands and setting parameters. Each device have different sets of supported commands and parameters, Device class has functions designed to get information about these sets. Callibri and Braibit devices has different parameters sets and provides different ways to access them. Device class hides all differences behind its interface and provides universal way to read and write parameters, execute commands and receive biopotential signals.

Member functions

(constructor)[private] constructs device, inaccessible from user code, new device object could be constructed only by DeviceScanner instance
(destructor) destroys device
operator=[deleted] not copy assignable
Supported features
channels returns information about channels which could be created with this device, contains information about channels types and names
commands returns vector containing supported commands
parameters returns

Non-member functions

checkHasChannel returns true if device has channel with same channel information
checkHasCommand returns true if device supports specified command
checkHasParameter returns true if device has specified parameter
countChannelsWithType returns number of channels with specified type in channel info section
getParameterAccess if device has specified parameter returns access modifier for it, otherwise throws

Notes

In general all parameters read/write operations, command executions and data read operations through channels must be done on connected devices. Only Name, State and Address parameters could be read on disconnected device. Performing another operations on disconnected device cause device-dependent behavior. For Callibri device most of operations on disconnected device will cause exception throwing, for Brainbit some read operations may be performed but it is not guaranteed to be stable for all versions of SDK.

Example

This example shows how a mutex can be used to protect a <a href="/w/cpp/container/map" title="cpp/container/map">std::map</a> shared between two threads.

<span class="co2">#include <iostream></span>
<span class="co2">#include <map></span>
<span class="co2">#include <string></span>
<span class="co2">#include <chrono></span>
<span class="co2">#include <thread></span>
<span class="co2">#include <mutex></span>
 
<a href="http://en.cppreference.com/w/cpp/container/map"><span class="kw1281">std::<span class="me2">map</span></span></a><span class="sy1"><</span><a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a>, <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">></span> g_pages<span class="sy4">;</span>
std<span class="sy4">::</span><span class="me2">mutex</span> g_pages_mutex<span class="sy4">;</span>
 
<span class="kw4">void</span> save_page<span class="br0">(</span><span class="kw4">const</span> <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> <span class="sy3">&</span>url<span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// simulate a long page fetch</span>
    <a href="http://en.cppreference.com/w/cpp/thread/sleep_for"><span class="kw2149">std::<span class="me2">this_thread</span><span class="sy4">::</span><span class="me2">sleep_for</span></span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/cpp/chrono/duration"><span class="kw997">std::<span class="me2">chrono</span><span class="sy4">::</span><span class="me2">seconds</span></span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> result <span class="sy1">=</span> <span class="st0">"fake content"</span><span class="sy4">;</span>
 
    <a href="http://en.cppreference.com/w/cpp/thread/lock_guard"><span class="kw2165">std::<span class="me2">lock_guard</span></span></a><span class="sy1"><</span>std<span class="sy4">::</span><span class="me2">mutex</span><span class="sy1">></span> guard<span class="br0">(</span>g_pages_mutex<span class="br0">)</span><span class="sy4">;</span>
    g_pages<span class="br0">[</span>url<span class="br0">]</span> <span class="sy1">=</span> result<span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> 
<span class="br0">{</span>
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t1<span class="br0">(</span>save_page, <span class="st0">"http://foo"</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t2<span class="br0">(</span>save_page, <span class="st0">"http://bar"</span><span class="br0">)</span><span class="sy4">;</span>
    t1.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    t2.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// safe to access g_pages without lock now, as the threads are joined</span>
    <span class="kw1">for</span> <span class="br0">(</span><span class="kw4">const</span> <span class="kw4">auto</span> <span class="sy3">&</span>pair <span class="sy4">:</span> g_pages<span class="br0">)</span> <span class="br0">{</span>
        <a href="http://en.cppreference.com/w/cpp/io/cout"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> pair.<span class="me1">first</span> <span class="sy1"><<</span> <span class="st0">" => "</span> <span class="sy1"><<</span> pair.<span class="me1">second</span> <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span>

Output:

http://bar => fake content
http://foo => fake content


</div>

               </div>