Skip to content

Commit 4ec4724

Browse files
Patch/pr 795 refactor (#7)
1 parent b0a8e19 commit 4ec4724

File tree

9 files changed

+370
-702
lines changed

9 files changed

+370
-702
lines changed

README.md

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -31,107 +31,6 @@ It provides a uniform SDK to work with biosensors with a primary focus on neuroi
3131
* Powerful CI/CD system which runs integrations tests for each commit automatically using BrainFlow's Emulator
3232
* Simplified process to add new boards and methods
3333

34-
## NeuroPawn Knight Board Docs
35-
36-
### [Python] Brainflow Simple Setup
37-
```
38-
import brainflow as bf
39-
import time
40-
41-
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
42-
43-
class KnightBoard:
44-
def __init__(self, serial_port: str, num_channels: int):
45-
"""Initialize and configure the Knight Board."""
46-
self.params = BrainFlowInputParams()
47-
self.params.serial_port = serial_port
48-
self.num_channels = num_channels
49-
50-
# Initialize board
51-
self.board_shim = BoardShim(BoardIds.NEUROPAWN_KNIGHT_BOARD.value, self.params)
52-
self.board_id = self.board_shim.get_board_id()
53-
self.eeg_channels = self.board_shim.get_exg_channels(self.board_id)
54-
self.sampling_rate = self.board_shim.get_sampling_rate(self.board_id)
55-
56-
def start_stream(self, buffer_size: int = 450000):
57-
"""Start the data stream from the board."""
58-
self.board_shim.prepare_session()
59-
self.board_shim.start_stream(buffer_size)
60-
print("Stream started.")
61-
time.sleep(2)
62-
for x in range(1, self.num_channels + 1):
63-
time.sleep(0.5)
64-
cmd = f"chon_{x}_12"
65-
self.board_shim.config_board(cmd)
66-
print(f"sending {cmd}")
67-
time.sleep(1)
68-
rld = f"rldadd_{x}"
69-
self.board_shim.config_board(rld)
70-
print(f"sending {rld}")
71-
time.sleep(0.5)
72-
73-
def stop_stream(self):
74-
"""Stop the data stream and release resources."""
75-
self.board_shim.stop_stream()
76-
self.board_shim.release_session()
77-
print("Stream stopped and session released.")
78-
79-
Knight_board = KnightBoard("COM3", 8)
80-
Knight_board.start_stream()
81-
82-
while True:
83-
data = Knight_board.board_shim.get_board_data()
84-
# do stuff with data
85-
86-
if keyboard.is_pressed('q'):
87-
Knight_board.stop_stream()
88-
break
89-
```
90-
### BrainFlow Configuration Commands
91-
92-
To configure the NeuroPawn Knight board with BrainFlow, pass the commands as strings into:
93-
94-
board_shim.config_board(<command>)
95-
96-
#### Command 1: Enable EEG Channel / Set Gain
97-
**Purpose**: Enables a specified channel with a specified gain, starting data acquisition on that channel. If the channel is already enabled, it will remain enabled, but will still update its gain.
98-
99-
f"chon_{channel}_{gain_value}"
100-
101-
##### Parameters:
102-
103-
- channel: The channel number to start the data acquisition. Replace this with the actual number of the channel you want to configure. One-indexed.
104-
105-
- gain: Specifies the gain value for the channel to be enabled. Allowable gain values are: [1, 2, 3, 4, 6, 8, 12 (recommended)]. The gain value controls the amplification level of the EEG signal on the specified channel.
106-
107-
#### Command 2: Disable EEG Channel
108-
**Purpose**: Disables a specified channel, stopping data acquisition on that channel.
109-
110-
f"choff_{channel_number}"
111-
112-
##### Parameters:
113-
114-
- channel_number: The channel number to **stop** the data acquisition. This is appended to *'choff'* to construct the configuration command. One-indexed.
115-
116-
#### Command 3: Toggle on RLD
117-
**Purpose**: Toogle **on** right leg drive for the specified channel.
118-
119-
f"rldadd_{channel_number}"
120-
121-
##### Parameters:
122-
123-
- channel_number: The channel number to toggle **on** the right leg drive. This number is converted to a string and appended to *'rldadd'* to create the configuration command. One-indexed.
124-
125-
126-
### Command 4: Toggle off RLD
127-
**Purpose**: Toogle **off** right leg drive for the specified channel.
128-
129-
f"rldremove_{channel}"
130-
131-
#### Parameters:
132-
133-
- channel_number: The channel number to toggle **off** the right leg drive. This number is converted to a string and appended to *'rldremove'* to create the configuration command. One-indexed.
134-
13534
## Resources
13635

13736
* [***BrainFlow Docs, Dev and User guides and other information***](https://brainflow.readthedocs.io)

docs/SupportedBoards.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ Knight Board
14251425
14261426
To create such board you need to specify the following board ID and fields of BrainFlowInputParams object:
14271427
1428-
- :code:`BoardIds.NEUROPAWN_KNIGHT_BOARD` or :code:`BoardIds.NEUROPAWN_KNIGHT_BOARD_IMU`
1428+
- :code:`BoardIds.NEUROPAWN_KNIGHT_BOARD`
14291429
- :code:`serial_port`, e.g. COM3, /dev/tty.*
14301430

14311431
Initialization Example:
@@ -1436,8 +1436,42 @@ Initialization Example:
14361436
params.serial_port = "COM3"
14371437
params.other_info = '{"gain": 6}' # optional: set gain to allowed values: 1, 2, 3, 4, 6, 8, 12 (default)
14381438
1439-
board = BoardShim(BoardIds.NEUROPAWN_KNIGHT_BOARD, params) # standard Knight Board
1440-
board = BoardShim(BoardIds.NEUROPAWN_KNIGHT_BOARD_IMU, params) # Knight Board IMU
1439+
board = BoardShim(BoardIds.NEUROPAWN_KNIGHT_BOARD, params)
1440+
1441+
**On Unix-like systems you may need to configure permissions for serial port or run with sudo.**
1442+
1443+
**On MacOS there are two serial ports for each device: /dev/tty..... and /dev/cu..... You HAVE to specify /dev/cu.....**
1444+
1445+
Supported platforms:
1446+
1447+
- Windows
1448+
- Linux
1449+
- MacOS
1450+
- Devices like Raspberry Pi
1451+
1452+
Knight IMU Board
1453+
~~~~~~~~~~~~~~~~~
1454+
1455+
.. image:: https://live.staticflickr.com/65535/54061606098_e223ab04a6_w.jpg
1456+
:width: 400px
1457+
:height: 274px
1458+
1459+
`NeuroPawn website <https://www.neuropawn.tech/>`_
1460+
1461+
To create such board you need to specify the following board ID and fields of BrainFlowInputParams object:
1462+
1463+
- :code:`BoardIds.NEUROPAWN_KNIGHT_BOARD_IMU`
1464+
- :code:`serial_port`, e.g. COM3, /dev/tty.*
1465+
1466+
Initialization Example:
1467+
1468+
.. code-block:: python
1469+
1470+
params = BrainFlowInputParams()
1471+
params.serial_port = "COM3"
1472+
params.other_info = '{"gain": 6}' # optional: set gain to allowed values: 1, 2, 3, 4, 6, 8, 12 (default)
1473+
1474+
board = BoardShim(BoardIds.NEUROPAWN_KNIGHT_BOARD_IMU, params)
14411475
14421476
**On Unix-like systems you may need to configure permissions for serial port or run with sudo.**
14431477

src/board_controller/build.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ SET (BOARD_CONTROLLER_SRC
8484
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/pieeg/pieeg_board.cpp
8585
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/synchroni/synchroni_board.cpp
8686
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/neuropawn/knight.cpp
87+
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/neuropawn/knight_base.cpp
8788
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/neuropawn/knightimu.cpp
8889
${CMAKE_CURRENT_SOURCE_DIR}/src/board_controller/biolistener/biolistener.cpp
8990
)
Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
11
#pragma once
22

3-
#include <set>
4-
#include <thread>
3+
#include "knight_base.h"
54

6-
#include "board.h"
7-
#include "board_controller.h"
8-
#include "serial.h"
9-
10-
class Knight : public Board
5+
class Knight : public KnightBase
116
{
127

138
protected:
14-
volatile bool keep_alive;
15-
bool initialized;
16-
bool is_streaming;
17-
std::thread streaming_thread;
18-
Serial *serial;
19-
20-
int min_package_size;
21-
int gain;
22-
23-
virtual int send_to_board (const char *msg);
24-
virtual int send_to_board (const char *msg, std::string &response);
25-
virtual std::string read_serial_response ();
26-
int open_port ();
27-
int set_port_settings ();
289
void read_thread ();
2910

30-
private:
31-
static const std::set<int> allowed_gains;
32-
3311
public:
3412
Knight (int board_id, struct BrainFlowInputParams params);
35-
~Knight ();
36-
37-
int prepare_session ();
38-
int start_stream (int buffer_size, const char *streamer_params);
39-
int stop_stream ();
40-
int release_session ();
41-
int config_board (std::string config, std::string &response);
42-
43-
static constexpr int start_byte = 0xA0;
44-
static constexpr int end_byte = 0xC0;
4513
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <set>
4+
#include <thread>
5+
6+
#include "board.h"
7+
#include "board_controller.h"
8+
#include "serial.h"
9+
10+
class KnightBase : public Board
11+
{
12+
13+
protected:
14+
volatile bool keep_alive;
15+
bool initialized;
16+
bool is_streaming;
17+
std::thread streaming_thread;
18+
Serial *serial;
19+
20+
int min_package_size;
21+
int gain;
22+
23+
virtual int send_to_board (const char *msg);
24+
virtual int send_to_board (const char *msg, std::string &response);
25+
virtual std::string read_serial_response ();
26+
virtual int open_port ();
27+
virtual int set_port_settings ();
28+
virtual void read_thread () = 0;
29+
30+
private:
31+
static const std::set<int> allowed_gains;
32+
33+
public:
34+
KnightBase (int board_id, struct BrainFlowInputParams params);
35+
virtual ~KnightBase ();
36+
37+
virtual int prepare_session ();
38+
virtual int start_stream (int buffer_size, const char *streamer_params);
39+
virtual int stop_stream ();
40+
virtual int release_session ();
41+
virtual int config_board (std::string config, std::string &response);
42+
43+
static constexpr int start_byte = 0xA0;
44+
static constexpr int end_byte = 0xC0;
45+
};
Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
11
#pragma once
22

3-
#include <set>
4-
#include <thread>
3+
#include "knight_base.h"
54

6-
#include "board.h"
7-
#include "board_controller.h"
8-
#include "serial.h"
9-
10-
class KnightIMU : public Board
5+
class KnightIMU : public KnightBase
116
{
127

138
protected:
14-
volatile bool keep_alive;
15-
bool initialized;
16-
bool is_streaming;
17-
std::thread streaming_thread;
18-
Serial *serial;
19-
20-
int min_package_size;
21-
int gain;
22-
23-
virtual int send_to_board (const char *msg);
24-
virtual int send_to_board (const char *msg, std::string &response);
25-
virtual std::string read_serial_response ();
26-
int open_port ();
27-
int set_port_settings ();
289
void read_thread ();
2910

30-
private:
31-
static const std::set<int> allowed_gains;
32-
3311
public:
3412
KnightIMU (int board_id, struct BrainFlowInputParams params);
35-
~KnightIMU ();
36-
37-
int prepare_session ();
38-
int start_stream (int buffer_size, const char *streamer_params);
39-
int stop_stream ();
40-
int release_session ();
41-
int config_board (std::string config, std::string &response);
42-
43-
static constexpr int start_byte = 0xA0;
44-
static constexpr int end_byte = 0xC0;
4513
};

0 commit comments

Comments
 (0)