BaseChannel (Java)

From Neurotech Software Development Kit
Jump to: navigation, search

ru.neuromd.neurosdk.channels

Class BaseChannel

Related Classes

ru.neuromd.neurosdk.Device ru.neuromd.neurosdk.DeviceInfo

Classes hierarchy

  • java.lang.Object
    • ru.neuromd.neurosdk.channels.BaseChannel

Class definition

public abstract class BaseChannel<SampleType>

Base channel class is intended for creating various channels of device and for build flexible system with different signal processing classes representing their data in same way. Inherit from this class to create you own channel

Fields Summary

Modifier and Type Field and Description
SubscribersNotifier&ltLong> dataLengthChanged

Subscribe this event to receive notifications about changes of total length of data

Constructor Summary

You are not able to create BaseChannel directly. This is the abstract base class for all channels.

Methods Summary

Modifier and Type Method and Description
ChannelInfo info()

Returns channel information

SampleType[] readData()

Requests data from channel buffer with specified offset and length, which must be set in terms of totalLength parameter.

If there is no enough data to read from buffer, nonexistent samples will be filled with zeros

long totalLength()

Returns total length of data been added to channel buffer. This value indicates how much data SHOULD be in buffer, but does not guarantee that all of these data samples are actually in buffer. How many data samples are stored in buffer, whether they saved on hard disc or not is depends on implementation of channel class. Basically channel data samples are stored in RAM and after totalLength exceeds buffer size, old data are discarded and cannot be restored. In this case actual data length stored in buffer could be clarified by calling bufferSize() method. For channels which do not have internal buffer or save data on disc return values of this method and bufferSize method will be equal and no data samples are discarded. To check whether channel has buffer overflow or not call bufferSize() method

long bufferSize()

Returns size in samples of internal channel buffer. If there is no internal buffer or it does not have maximum size this method returns zero

float samplingFrequency()

Returns sampling frequency of data returned by readData() method.

void setSamplingFrequency(float frequency)

Attempts to set sampling frequency for data in channel. Whether it possible or not depends on implementation of channel. May throw if setting of sampling frequency is impossible.

Device underlyingDevice()

Returns device this channel belongs to. May throw if channel does not have representation in any device

Field Detail

dataLengthChanged

public final SubscribersNotifier<Long> dataLengthChanged

Subscribe this event to receive notifications about changes of total length of data

Method Detail

connect

public void connect()

Tries to establish connection with device. Check DeviceState parameter or subscribe parameterChanged event for operation result.

disconnect

public void disconnect()

Disconnects from device. Check DeviceState parameter or subscribe parameterChanged event for operation result

channels

public ChannelInfo[] channels()

Returns information about supported channels.

Check this information before creation of channel. If device does not support channel, channel object won't be initialized with it.

Returns:
Array of channel info objects

commands

public Command[] commands()

Returns supported commands of device

Returns:
Array of supported commands

parameters

public Parameter[] parameters()

Returns all available parameters of device, their types and access rights

Returns:
Array of available parameters

execute

public boolean execute(Command cmd)

Tries to execute command and returns value indicating operations success.

Will throw if device does not support specified command. To get supported commands call commands() method.

Parameters:
cmd - Command to execute
Returns:
Operation success indicator
Throws:
java.lang.UnsupportedOperationException

readParam

public<ParamType> ParamType readParam(ParameterName param)

Return value of specified parameter of device.

Will throw if parameter does not present in device. To get supported parameters and type information for parameter call parameters() method. It returns Parameter object which consists of parameter name, type and access mode.

Parameters:
param - ParameterName to read
Returns:
Parameter value
Throws:
java.lang.UnsupportedOperationException

setParam

public boolean setParam(ParameterName param, Object value)

Sets value for specified parameter and returns value indicating success of operation.

Will throw if parameter does not present in device or has only Read access mode. To get supported parameters and type information for parameter call parameters() method. It returns Parameter object which consists of parameter name, type and access mode

Parameters:
param - Name of parameter to set
value - Parameter value
Returns:
Operation success indicator
Throws:
java.lang.UnsupportedOperationException

Example

This example shows how to find devices and list their features

import com.neuromd.common.INotificationCallback;
import com.neuromd.neurosdk.Device;
import com.neuromd.neurosdk.DeviceScanner;
import com.neuromd.neurosdk.channels.ChannelInfo;
import com.neuromd.neurosdk.parameters.Command;
import com.neuromd.neurosdk.parameters.Parameter;
import com.neuromd.neurosdk.parameters.ParameterName;
import com.neuromd.neurosdk.parameters.types.DeviceState;

public class DeviceInfoActivity extends AppCompatActivity
{
    private DeviceScanner mScanner;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_info);
    
        requestPermissions();
        enableBtAndGeolocation();
        initScanner();
        initButtons();
    }
    
    private void initScanner()
    {
        mScanner = new DeviceScanner(getApplicationContext());
        mScanner.scanStateChanged.subscribe(new INotificationCallback<Boolean>()
        {
            @Override
            public void onNotify(Object o, final Boolean isScanning)
            {
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Button startButton = findViewById(R.id.startScanButton);
                        Button stopButton = findViewById(R.id.stopScanButton);
                        startButton.setEnabled(!isScanning);
                        stopButton.setEnabled(isScanning);
                    }
                });
            }
        });
        mScanner.deviceFound.subscribe(new INotificationCallback<Device>()
        {
            @Override
            public void onNotify(Object o, Device device)
            {
                onDeviceFound(device);
            }
        });
    }
    
    private void requestPermissions(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }
    
    private void enableBtAndGeolocation(){
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
            if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                Intent enableGeoIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(enableGeoIntent, 1);
            }
        }
    }
    
    private void initButtons()
    {
        Button startButton = findViewById(R.id.startScanButton);
        startButton.setEnabled(true);
        startButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mScanner.startScan(0); //zero is for infinity
            }
        });
    
        Button stopButton = findViewById(R.id.stopScanButton);
        stopButton.setEnabled(false);
        stopButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mScanner.stopScan();
            }
        });
    }
    
    private void onDeviceFound(final Device device)
    {
        device.parameterChanged.subscribe(new INotificationCallback<ParameterName>()
        {
            @Override
            public void onNotify(Object o, ParameterName parameterName)
            {
                if (parameterName == ParameterName.State){
                    DeviceState state = device.readParam(ParameterName.State);
                    if (state == DeviceState.Connected){
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                onDeviceConnected(device);
                            }
                        });
                    }
                }
            }
        });
        device.connect();
    }
    
    private void onDeviceConnected(Device device)
    {
        EditText deviceInfoText = findViewById(R.id.infoText);
        
        String deviceName = device.readParam(ParameterName.Name);
        String deviceAddress = device.readParam(ParameterName.Address);
        deviceInfoText.append(String.format("Found device: %s [%s]\n", deviceName, deviceAddress));
    
        deviceInfoText.append("Supported params:\n");
        Parameter[] deviceParams = device.parameters();
        for (Parameter param : deviceParams)
        {
            String paramName = param.getName().toString();
            String accessMode = param.getAccess().toString();
            deviceInfoText.append(String.format("-%s {%s}\n", paramName, accessMode));
        }
    
        deviceInfoText.append("\nSupported commands:\n");
        Command[] deviceCommands = device.commands();
        for (Command cmd : deviceCommands)
        {
            deviceInfoText.append(String.format("-%s \n", cmd.toString()));
        }
    
        deviceInfoText.append("\nSupported channels:\n");
        ChannelInfo[] deviceChannels = device.channels();
        for (ChannelInfo channel : deviceChannels)
        {
            String channelName = channel.getName();
            deviceInfoText.append(String.format("-%s \n", channelName));
        }
    }
}

Possible output:

Android deviceinfo example 1.png

See also

NeuroMD SDK Manual

Device (C++)