Difference between revisions of "Device (C++)"

From Neurotech Software Development Kit
Jump to: navigation, search
(Neuro::Device)
 
(39 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=<span style="font-size:0.7em; line-height:130%">Neuro::</span>Device=
 
=<span style="font-size:0.7em; line-height:130%">Neuro::</span>Device=
{|
+
 
|Defined in header [https://github.com/NeurotechLtd/neuro-sdk/blob/master/core/include/device/device.h <device/device.h>]
+
Defined in header [https://github.com/NeurotechLtd/neuro-sdk/blob/master/core/include/device/device.h <span style="color:#0033CC;"><device/device.h></span>]
 +
 
 +
'''<span style="font-size:1.3em; line-height:130%"><span style="color:#0033CC;">class</span> Device <span style="color:#0033CC;">final</span></span>''';
 +
 
 +
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==
 +
{|class="wikitable"
 +
|<span style="color:#0033CC;">(constructor)</span><span style="color:#888888;">[private]</span>
 +
|constructs device, inaccessible from user code, new device object could be constructed only by DeviceScanner instance
 +
|-
 +
|<span style="color:#0033CC;">(destructor)</span>
 +
|destroys device
 +
|-
 +
|<span style="color:#0033CC;">'''operator='''</span><span style="color:#888888;">[deleted]</span>
 +
|not copy assignable
 +
|-
 +
! scope="row" colspan="2"|'''Supported features'''
 +
|-
 +
|<span style="color:#0033CC;">'''channels'''</span>
 +
|returns information about channels which could be created with this device, contains information about channels types and names
 +
|-
 +
|<span style="color:#0033CC;">'''commands'''</span>
 +
|returns [http://en.cppreference.com/w/cpp/container/vector vector] containing supported commands
 +
|-
 +
|<span style="color:#0033CC;">'''parameters'''</span>
 +
|returns
 +
|}
 +
 
 +
==Non-member functions==
 +
{|class="wikitable"
 +
|<span style="color:#0033CC;">'''checkHasChannel'''</span>
 +
|returns true if device has channel with same channel information
 +
|-
 +
|<span style="color:#0033CC;">'''checkHasCommand'''</span>
 +
|returns true if device supports specified command
 +
|-
 +
|<span style="color:#0033CC;">'''checkHasParameter'''</span>
 +
|returns true if device has specified parameter
 +
|-
 +
|<span style="color:#0033CC;">'''countChannelsWithType'''</span>
 +
|returns number of channels with specified type in channel info section
 
|-
 
|-
|class Device final;
+
|<span style="color:#0033CC;">'''getParameterAccess'''</span>
 +
|if device has specified parameter returns access modifier for it, otherwise throws
 
|}
 
|}
  
 +
==Notes==
 +
In general all parameter 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 a disconnected device. Performing another operations on a 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 to find device, establish connection with it and to list device features
 +
<syntaxhighlight lang="c++">
 +
#include <iostream>
 +
#include <vector>
 +
#include "device_scanner.h"
 +
#include "device/param_values.h"
  
<tr class="t-dcl t-since-cxx11">
+
std::vector<std::shared_ptr<Neuro::Device>> FoundDevices;
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">class</span> mutex<span class="sy4">;</span></span></div></td>
 
<td class="t-dcl-nopad">  </td>
 
<td> <span class="t-mark-rev t-since-cxx11">(since C++11)</span> </td>
 
</tr>
 
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
 
</tbody></table>
 
<p>The <code>mutex</code> class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.
 
</p><p><code>mutex</code> offers exclusive, non-recursive ownership semantics:
 
</p>
 
<ul><li> A calling thread <i>owns</i> a <code>mutex</code> from the time that it successfully calls either <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a> until it calls <a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"><code>unlock</code></a>.
 
</li><li> When a thread owns a <code>mutex</code>, all other threads will block (for calls to <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a>) or receive a <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> return value (for <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a>) if they attempt to claim ownership of the <code>mutex</code>.
 
</li><li> A calling thread must not own the <code>mutex</code> prior to calling <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a>.
 
</li></ul>
 
<p>The behavior of a program is undefined if a <code>mutex</code> is destroyed while still owned by any threads, or a thread terminates while owning a <code>mutex</code>.  The <code>mutex</code> class satisfies all requirements of <a href="/w/cpp/concept/Mutex" title="cpp/concept/Mutex"><code>Mutex</code></a> and <a href="/w/cpp/concept/StandardLayoutType" title="cpp/concept/StandardLayoutType"><code>StandardLayoutType</code></a>.
 
</p><p><code>std::mutex</code> is neither copyable nor movable.
 
</p>
 
<table id="toc" class="toc"><tbody><tr><td><div id="toctitle"><h2>Contents</h2><span class="toctoggle">&nbsp;[<a href="#" class="internal" id="togglelink">hide</a>]&nbsp;</span></div>
 
<ul>
 
<li class="toclevel-1 tocsection-1"><a href="#Member_types"><span class="tocnumber">1</span> <span class="toctext">Member types</span></a></li>
 
<li class="toclevel-1 tocsection-2"><a href="#Member_functions"><span class="tocnumber">2</span> <span class="toctext">Member functions</span></a>
 
<ul>
 
<li class="toclevel-2"><a href="#Locking"><span class="tocnumber">2.1</span> <span class="toctext">Locking</span></a></li>
 
<li class="toclevel-2"><a href="#Native_handle"><span class="tocnumber">2.2</span> <span class="toctext">Native handle</span></a></li>
 
</ul>
 
</li>
 
<li class="toclevel-1 tocsection-3"><a href="#Notes"><span class="tocnumber">3</span> <span class="toctext">Notes</span></a></li>
 
<li class="toclevel-1 tocsection-4"><a href="#Example"><span class="tocnumber">4</span> <span class="toctext">Example</span></a></li>
 
</ul>
 
</td></tr></tbody></table>
 
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=1" title="Edit section: Member types">edit</a>]</span> <span class="mw-headline" id="Member_types">Member types</span></h3>
 
<table class="t-dsc-begin">
 
  
<tbody><tr class="t-dsc-hitem">
+
template <typename T>
<td>  Member type
+
void displayDeviceFeatures(T&& device_ptr){
</td>
+
    using Neuro::to_string;
<td>  Definition
 
</td></tr>
 
  
 +
    std::cout << "Device can execute:" << std::endl;
 +
    auto commands = device_ptr->commands();
 +
    for (auto& cmd : commands){
 +
        std::cout << "-" << to_string(cmd) << std::endl;
 +
    }
 +
std::cout << std::endl;
  
<tr class="t-dsc">
+
    std::cout << "Device has parameters:" << std::endl;
<td>  <code>native_handle_type</code><span class="t-mark">(optional)</span>
+
    auto params = device_ptr->parameters();
</td>
+
    for (auto& paramPair : params){
<td>  <i>implementation-defined</i>
+
        std::cout << "-" << to_string(paramPair.first) << " {" << to_string(paramPair.second) << "}" <<std::endl;
</td></tr>
+
    }
 +
std::cout << std::endl;
  
</tbody></table>
+
    std::cout << "Device has channels:" << std::endl;
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=2" title="Edit section: Member functions">edit</a>]</span> <span class="mw-headline" id="Member_functions">Member functions</span></h3>
+
    auto channels = device_ptr->channels();
<table class="t-dsc-begin">
+
    for (auto& channel : channels){
 +
        std::cout << "-" << channel.getName() << std::endl;
 +
    }
 +
std::cout << std::endl;
 +
}
  
<tbody><tr class="t-dsc">
 
<td>  <div class="t-dsc-member-div t-dsc-member-nobold-div"><div><a href="/w/cpp/thread/mutex/mutex" title="cpp/thread/mutex/mutex"> <span class="t-lines"><span>(constructor)</span></span></a></div></div>
 
</td>
 
<td>  constructs the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_constructor&amp;action=edit">[edit]</a></span>
 
</td></tr>
 
  
<tr class="t-dsc">
+
template <typename T>
<td>  <div class="t-dsc-member-div t-dsc-member-nobold-div"><div><a href="/w/cpp/thread/mutex/%7Emutex" title="cpp/thread/mutex/~mutex"> <span class="t-lines"><span>(destructor)</span></span></a></div></div>
+
void connectDevice(T&& device_ptr){
</td>
+
    using Neuro::Parameter;
<td>  destroys the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_destructor&amp;action=edit">[edit]</a></span>
+
std::cout << "Connecting device ["  
</td></tr>
+
  << device_ptr->readParam<Parameter::Address>()  
 +
  << "]" << std::endl;
  
<tr class="t-dsc">
+
using device_t = typename std::remove_reference_t<decltype(device_ptr)>::element_type;
<td> <div class="t-dsc-member-div"><div><span class="t-lines"><span>operator=</span></span></div><div><span class="t-lines"><span><span class="t-cmark">[deleted]</span></span></span></div></div>
+
auto weakDevice = std::weak_ptr<device_t>(device_ptr);
</td>
+
    device_ptr->setParamChangedCallback([weakDevice](auto param){
<td>  not copy-assignable <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_operator%3D&amp;action=edit">[edit]</a></span>
+
        if (param == Parameter::State){
</td></tr>
+
auto device = weakDevice.lock();
 +
if (device != nullptr) {
 +
auto state = device->readParam<Parameter::State>();
 +
if (state == Neuro::DeviceState::Connected) {
 +
std::cout << "Device ["
 +
<< device->readParam<Parameter::Address>()
 +
<< "] connected" << std::endl;
 +
displayDeviceFeatures(device);
 +
}
 +
}
 +
        }
 +
    });
 +
device_ptr->connect();
 +
FoundDevices.push_back(device_ptr);
 +
}
  
 +
template <typename T>
 +
void onDeviceFound(T&& device_ptr){
 +
    using Neuro::Parameter;
 +
    using Neuro::DeviceState;
 +
    using Neuro::to_string;
  
<tr>
+
    auto deviceName = device_ptr->readParam<Parameter::Name>();
<td colspan="2"> <h5> <span class="mw-headline" id="Locking">  Locking </span></h5>
+
    auto deviceAddress = device_ptr->readParam<Parameter::Address>();
</td></tr>
+
    auto deviceState = device_ptr->readParam<Parameter::State>();
 +
    std::cout << deviceName
 +
              << " [" << deviceAddress << "] "
 +
              << to_string(deviceState)
 +
              << std::endl;
  
<tr class="t-dsc">
+
using device_t = typename std::remove_reference_t<decltype(device_ptr)>::element_type;
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"> <span class="t-lines"><span>lock</span></span></a></div></div>
+
auto sharedDevice = std::shared_ptr<device_t>(std::forward<T>(device_ptr));
</td>
+
if (deviceState != DeviceState::Connected) {
<td>  locks the mutex, blocks if the mutex is not available <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_lock&amp;action=edit">[edit]</a></span>
+
connectDevice(sharedDevice);
</td></tr>
+
}
 +
else{
 +
displayDeviceFeatures(sharedDevice);
 +
}
 +
}
  
<tr class="t-dsc">
+
int main(int argc, char *argv[]){
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"> <span class="t-lines"><span>try_lock</span></span></a></div></div>
+
    auto scanner = Neuro::createDeviceScanner();
</td>
+
    scanner->subscribeDeviceFound([](auto&& device_ptr){
<td>  tries to lock the mutex, returns if the mutex is not available <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_try_lock&amp;action=edit">[edit]</a></span>
+
        onDeviceFound(std::forward<decltype(device_ptr)>(device_ptr));
</td></tr>
+
    });
 +
    scanner->startScan(0);//zero timeout for infinity
 +
    while (std::cin.get() != '\n');
 +
}
  
<tr class="t-dsc">
+
</syntaxhighlight>
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"> <span class="t-lines"><span>unlock</span></span></a></div></div>
 
</td>
 
<td>  unlocks the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_unlock&amp;action=edit">[edit]</a></span>
 
</td></tr>
 
  
 +
Possible output:
 +
<syntaxhighlight lang="bash">
 +
Neurotech_Callibri_R [2a:90:37:c0:ec:d4] Disconnected
 +
Connecting device [2a:90:37:c0:ec:d4]
 +
Device [2a:90:37:c0:ec:d4] connected
 +
Device can execute:
 +
-FindMe
 +
-StartSignal
 +
-StopSignal
  
<tr>
+
Device has parameters:
<td colspan="2"> <h5> <span class="mw-headline" id="Native_handle">  Native handle </span></h5>
+
-Name {Read}
</td></tr>
+
-State {ReadNotify}
 +
-Address {Read}
 +
-SerialNumber {Read}
 +
-FirmwareMode {Read}
 +
-SamplingFrequency {ReadWrite}
 +
-ADCInputState {ReadWrite}
 +
-ExternalSwitchState {ReadWrite}
 +
-HardwareFilterState {ReadWrite}
 +
-Gain {ReadWrite}
 +
-Offset {ReadWrite}
 +
-AccelerometerSens {ReadWrite}
 +
-GyroscopeSens {ReadWrite}
  
<tr class="t-dsc">
+
Device has channels:
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/native_handle" title="cpp/thread/mutex/native handle"> <span class="t-lines"><span>native_handle</span></span></a></div></div>
+
-Signal
</td>
+
-ElectrodesState
<td>  returns the underlying implementation-defined native handle object <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_native_handle&amp;action=edit">[edit]</a></span>
+
-ConnectionStats
</td></tr>
+
-Battery
</tbody></table>
+
</syntaxhighlight>
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=3" title="Edit section: Notes">edit</a>]</span> <span class="mw-headline" id="Notes">Notes</span></h3>
 
<p><code>std::mutex</code> is usually not accessed directly: <span class="t-lc"><a href="/w/cpp/thread/unique_lock" title="cpp/thread/unique lock">std::unique_lock</a></span>, <span class="t-lc"><a href="/w/cpp/thread/lock_guard" title="cpp/thread/lock guard">std::lock_guard</a></span>, <span class="t-rev-inl t-since-cxx17"><span> or <span class="t-lc">std::scoped_lock</span> </span> <span><span class="t-mark-rev t-since-cxx17">(since C++17)</span></span></span> manage locking in a more exception-safe manner.
 
</p>
 
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=4" title="Edit section: Example">edit</a>]</span> <span class="mw-headline" id="Example">Example</span></h3>
 
<div class="t-example"><p> This example shows how a <code>mutex</code> can be used to protect a <span class="t-lc"><a href="/w/cpp/container/map" title="cpp/container/map">std::map</a></span> shared between two threads.
 
</p><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
 
<div dir="ltr" class="mw-geshi t-example-code" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
 
<span class="co2">#include &lt;map&gt;</span>
 
<span class="co2">#include &lt;string&gt;</span>
 
<span class="co2">#include &lt;chrono&gt;</span>
 
<span class="co2">#include &lt;thread&gt;</span>
 
<span class="co2">#include &lt;mutex&gt;</span>
 
&nbsp;
 
<a href="http://en.cppreference.com/w/cpp/container/map"><span class="kw1281">std::<span class="me2">map</span></span></a><span class="sy1">&lt;</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">&gt;</span> g_pages<span class="sy4">;</span>
 
std<span class="sy4">::</span><span class="me2">mutex</span> g_pages_mutex<span class="sy4">;</span>
 
&nbsp;
 
<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">&amp;</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>
 
&nbsp;
 
    <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">&lt;</span>std<span class="sy4">::</span><span class="me2">mutex</span><span class="sy1">&gt;</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>
 
&nbsp;
 
<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>
 
&nbsp;
 
    <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">&amp;</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">&lt;&lt;</span> pair.<span class="me1">first</span> <span class="sy1">&lt;&lt;</span> <span class="st0">" =&gt; "</span> <span class="sy1">&lt;&lt;</span> pair.<span class="me1">second</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
    <span class="br0">}</span>
 
<span class="br0">}</span></pre></div></div>
 
<p>Output:
 
</p>
 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">http://bar =&gt; fake content
 
http://foo =&gt; fake content</pre></div></div>
 
</div>
 
  
<!--
+
==See also==
NewPP limit report
+
[[NeuroMD SDK Manual]]
Preprocessor visited node count: 5285/1000000
 
Preprocessor generated node count: 10355/1000000
 
Post‐expand include size: 159462/2097152 bytes
 
Template argument size: 30567/2097152 bytes
 
Highest expansion depth: 20/40
 
Expensive parser function count: 0/100
 
-->
 
  
<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:2291-0!*!0!!en!*!* and timestamp 20180411183737 -->
+
[[Device (Java)]]
</div>                    <!-- /bodycontent -->
 
                                        <!-- printfooter -->
 
                    <div class="printfooter">
 
                    Retrieved from "<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/thread/mutex&amp;oldid=89024">http://en.cppreference.com/mwiki/index.php?title=cpp/thread/mutex&amp;oldid=89024</a>"                    </div>
 
                    <!-- /printfooter -->
 
                                                            <!-- catlinks -->
 
                    <div id="catlinks" class="catlinks catlinks-allhidden"></div>                    <!-- /catlinks -->
 
                                                            <div class="visualClear"></div>
 
                    <!-- debughtml -->
 
                                        <!-- /debughtml -->
 
                </div>
 

Latest revision as of 03:43, 1 May 2018

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 parameter 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 a disconnected device. Performing another operations on a 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 to find device, establish connection with it and to list device features

#include <iostream>
#include <vector>
#include "device_scanner.h"
#include "device/param_values.h"

std::vector<std::shared_ptr<Neuro::Device>> FoundDevices;

template <typename T>
void displayDeviceFeatures(T&& device_ptr){
    using Neuro::to_string;

    std::cout << "Device can execute:" << std::endl;
    auto commands = device_ptr->commands();
    for (auto& cmd : commands){
        std::cout << "-" << to_string(cmd) << std::endl;
    }
	std::cout << std::endl;

    std::cout << "Device has parameters:" << std::endl;
    auto params = device_ptr->parameters();
    for (auto& paramPair : params){
        std::cout << "-" << to_string(paramPair.first) << " {" << to_string(paramPair.second) << "}" <<std::endl;
    }
	std::cout << std::endl;

    std::cout << "Device has channels:" << std::endl;
    auto channels = device_ptr->channels();
    for (auto& channel : channels){
        std::cout << "-" << channel.getName() << std::endl;
    }
	std::cout << std::endl;
}


template <typename T>
void connectDevice(T&& device_ptr){
    using Neuro::Parameter;
	std::cout << "Connecting device [" 
			  << device_ptr->readParam<Parameter::Address>() 
			  << "]" << std::endl;

	using device_t = typename std::remove_reference_t<decltype(device_ptr)>::element_type;
	auto weakDevice = std::weak_ptr<device_t>(device_ptr);
    device_ptr->setParamChangedCallback([weakDevice](auto param){
        if (param == Parameter::State){
			auto device = weakDevice.lock();
			if (device != nullptr) {
				auto state = device->readParam<Parameter::State>();
				if (state == Neuro::DeviceState::Connected) {
					std::cout << "Device ["
						<< device->readParam<Parameter::Address>()
						<< "] connected" << std::endl;
					displayDeviceFeatures(device);
				}
			}
        }
    });
	device_ptr->connect();
	FoundDevices.push_back(device_ptr);
}

template <typename T>
void onDeviceFound(T&& device_ptr){
    using Neuro::Parameter;
    using Neuro::DeviceState;
    using Neuro::to_string;

    auto deviceName = device_ptr->readParam<Parameter::Name>();
    auto deviceAddress = device_ptr->readParam<Parameter::Address>();
    auto deviceState = device_ptr->readParam<Parameter::State>();
    std::cout << deviceName
              << " [" << deviceAddress << "] "
              << to_string(deviceState)
              << std::endl;

	using device_t = typename std::remove_reference_t<decltype(device_ptr)>::element_type;
	auto sharedDevice = std::shared_ptr<device_t>(std::forward<T>(device_ptr));
	if (deviceState != DeviceState::Connected) {
		connectDevice(sharedDevice);
	}
	else{
		displayDeviceFeatures(sharedDevice);
	}
}

int main(int argc, char *argv[]){
    auto scanner = Neuro::createDeviceScanner();
    scanner->subscribeDeviceFound([](auto&& device_ptr){
        onDeviceFound(std::forward<decltype(device_ptr)>(device_ptr));
    });
    scanner->startScan(0);//zero timeout for infinity
    while (std::cin.get() != '\n');
}

Possible output:

Neurotech_Callibri_R [2a:90:37:c0:ec:d4] Disconnected
Connecting device [2a:90:37:c0:ec:d4]
Device [2a:90:37:c0:ec:d4] connected
Device can execute:
-FindMe
-StartSignal
-StopSignal

Device has parameters:
-Name {Read}
-State {ReadNotify}
-Address {Read}
-SerialNumber {Read}
-FirmwareMode {Read}
-SamplingFrequency {ReadWrite}
-ADCInputState {ReadWrite}
-ExternalSwitchState {ReadWrite}
-HardwareFilterState {ReadWrite}
-Gain {ReadWrite}
-Offset {ReadWrite}
-AccelerometerSens {ReadWrite}
-GyroscopeSens {ReadWrite}

Device has channels:
-Signal
-ElectrodesState
-ConnectionStats
-Battery

See also

NeuroMD SDK Manual

Device (Java)