Skip to content

Commit

Permalink
Add full variable support
Browse files Browse the repository at this point in the history
  • Loading branch information
ZACHSTRIVES committed Oct 1, 2021
1 parent 6ae5d8d commit 05229fb
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 73 deletions.
17 changes: 9 additions & 8 deletions INFO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ Run the following command: ``pip install PyNeuro``

Usage
-----

1. Importing the module: ``from PyNeuro.PyNeuro import PyNeuro``
2. Initializing: ``pn = PyNeuro()``
3. After initializing, if required the callbacks can be set
4. Then call ``pn.connect()`` method, it will connect with TCP Socket
1. Before you start, make sure you have downloaded `Nuerosky Mindware Developer Tools <https://store.neurosky.com/collections/developer-tools>`__ and turned on Thinkgear Connecter. Please keep Thinkgear Connecter on while the program is running.
2. Importing the module: ``from PyNeuro.PyNeuro import PyNeuro``
3. Initializing: ``pn = PyNeuro()``
4. After initializing, if required the callbacks can be set
5. Then call ``pn.connect()`` method, it will connect with TCP Socket
server.
5. Then call ``pn.start()`` method, it will be start fetching data.
6. To stop call ``pn.close()``
6. Then call ``pn.start()`` method, it will be start fetching data.
7. To stop call ``pn.close()``

Obtaining Data from Device
~~~~~~~~~~~~~~~~~~~~~~~~~~

- **Obtaining value:** ``attention = pn.attention`` #to get value of
attention\_ >\ **Other Variable** attention, meditation,
blinkStrength, delta, theta, will be added more soon.
blinkStrength, delta, theta, lowAlpha, highAlpha, lowBeta, highBeta, lowGamma, highGamma.

- **Setting callback:** A call back can be associated with all the
above variables so that a function is called when the variable is
Expand All @@ -38,6 +38,7 @@ Obtaining Data from Device
pn.set_attention_callback(callback_function1)
pn.set_meditation_callback(callback_function2)
pn.set_blinkStrength_callback(callback_function3)
....

You can add any number of callback functions to a variable..

Expand Down
223 changes: 167 additions & 56 deletions PyNeuro/PyNeuro.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class PyNeuro:

__delta = 0
__theta = 0
__lowAlpha = 0
__highAlpha = 0
__lowBeta = 0
__highBeta = 0
__lowGamma = 0
__highGamma = 0

__attention_records = []
__meditation_records = []
Expand All @@ -42,6 +48,12 @@ class PyNeuro:
__delta__callbacks = []
__theta__callbacks = []
__status__callbacks = []
__lowAlpha__callbacks = []
__highAlpha__callbacks = []
__lowBeta__callbacks = []
__highBeta__callbacks = []
__lowGamma__callbacks = []
__highGamma__callbacks = []

callBacksDictionary = {} # keep a track of all callbacks

Expand Down Expand Up @@ -84,43 +96,54 @@ def close(self):
:return:
"""
self.__threadRun = False
self.__parserThread.join()

def __packetParser(self):
while True:
line = self.__telnet.read_until(b'\r');
if len(line) > 20:
try:
raw_str = (str(line).rstrip("\\r'").lstrip("b'"))
data = json.loads(raw_str)
if "status" in data.keys():
if self.__status != data["status"]:
self.__status = data["status"]
if data["status"] == "scanning":
print("[PyNeuro] Scanning device..")
else:
print("[PyNeuro] Connection lost, trying to reconnect..")
else:
if "eSense" in data.keys():
if data["eSense"]["attention"] + data["eSense"]["meditation"] == 0:
if self.__status != "fitting":
self.__status = "fitting"
print("[PyNeuro] Fitting Device..")

else:
if self.__status != "connected":
self.__status = "connected"
print("[PyNeuro] Successfully Connected ..")
self.attention = data["eSense"]["attention"]
self.meditation = data["eSense"]["meditation"]
self.theta = data['eegPower']['theta']
self.delta = data['eegPower']['delta']
self.__attention_records.append(data["eSense"]["attention"])
self.__attention_records.append(data["eSense"]["meditation"])
elif "blinkStrength" in data.keys():
self.blinkStrength = data["blinkStrength"]
self.__blinkStrength_records.append(data["blinkStrength"])
except:
print()
try:
while True:
line = self.__telnet.read_until(b'\r');
if len(line) > 20:
try:
raw_str = (str(line).rstrip("\\r'").lstrip("b'"))
data = json.loads(raw_str)
if "status" in data.keys():
if self.__status != data["status"]:
self.__status = data["status"]
if data["status"] == "scanning":
print("[PyNeuro] Scanning device..")
else:
print("[PyNeuro] Connection lost, trying to reconnect..")
else:
if "eSense" in data.keys():
print(data["eegPower"])
if data["eSense"]["attention"] + data["eSense"]["meditation"] == 0:
if self.__status != "fitting":
self.__status = "fitting"
print("[PyNeuro] Fitting Device..")

else:
if self.__status != "connected":
self.__status = "connected"
print("[PyNeuro] Successfully Connected ..")
self.attention = data["eSense"]["attention"]
self.meditation = data["eSense"]["meditation"]
self.theta = data['eegPower']['theta']
self.delta = data['eegPower']['delta']
self.lowAlpha = data['eegPower']['lowAlpha']
self.highAlpha = data['eegPower']['highAlpha']
self.lowBeta = data['eegPower']['lowBeta']
self.highBeta = data['eegPower']['highBeta']
self.lowGamma = data['eegPower']['lowGamma']
self.highGamma = data['eegPower']['highGamma']
self.__attention_records.append(data["eSense"]["attention"])
self.__attention_records.append(data["eSense"]["meditation"])
elif "blinkStrength" in data.keys():
self.blinkStrength = data["blinkStrength"]
self.__blinkStrength_records.append(data["blinkStrength"])
except:
print()
except:
print("[PyNeuro] Stop Packet Parser")

def set_attention_callback(self, callback):
"""
Expand All @@ -140,32 +163,49 @@ def set_meditation_callback(self, callback):

def set_blinkStrength_callback(self, callback):
"""
Set callback function of meditation value
Set callback function of blinkStrength value
:param callback: function(blinkStrength: int)
"""

self.__blinkStrength__callbacks.append(callback)

def set_delta_callback(self, callback):
"""
Set callback function of meditation value
:param callback: function(blinkStrength: int)
"""

self.__delta__callbacks.append(callback)

def set_theta_callback(self, callback):
"""
Set callback function of meditation value
:param callback: function(blinkStrength: int)
"""

self.__theta__callbacks.append(callback)

def set_lowAlpha_callback(self, callback):

self.__lowAlpha__callbacks.append(callback)

def set_highAlpha_callback(self, callback):

self.__highAlpha__callbacks.append(callback)

def set_lowBeta_callback(self, callback):

self.__lowBeta__callbacks.append(callback)

def set_highBeta_callback(self, callback):

self.__highBeta__callbacks.append(callback)

def set_lowGamma_callback(self, callback):

self.__lowGamma__callbacks.append(callback)

def set_highGamma_callback(self, callback):

self.__highGamma__callbacks.append(callback)


# attention
@property
def attention(self):
"Get value for attention"
"""Get value for attention"""
return self.__attention

@attention.setter
Expand All @@ -179,7 +219,7 @@ def attention(self, value):
# meditation
@property
def meditation(self):
"Get value for meditation"
"""Get value for meditation"""
return self.__meditation

@meditation.setter
Expand All @@ -193,7 +233,7 @@ def meditation(self, value):
# blinkStrength
@property
def blinkStrength(self):
"Get value for blinkStrength"
"""Get value for blinkStrength"""
return self.__blinkStrength

@blinkStrength.setter
Expand All @@ -205,7 +245,7 @@ def blinkStrength(self, value):

@property
def delta(self):
"Get value for delta"
"""Get value for delta"""
return self.__delta

@delta.setter
Expand All @@ -217,28 +257,99 @@ def delta(self, value):

@property
def theta(self):
"Get value for theta"
"""Get value for theta"""
return self.__theta

@theta.setter
def theta(self,value):
def theta(self, value):
self.__theta = value
# if callback has been set, execute the function
for callback in self.__theta__callbacks:
callback(self.__theta)

# lowBeta
# lowAlpha

@delta.setter
def delta(self, value):
self.__delta = value
@property
def lowAlpha(self):
"""Get value for lowAlpha"""
return self.__lowAlpha

@lowAlpha.setter
def lowAlpha(self, value):
self.__lowAlpha = value
# if callback has been set, execute the function
for callback in self.__delta__callbacks:
callback(self.__delta)
for callback in self.__lowAlpha__callbacks:
callback(self.__lowAlpha)

# highAlpha
@property
def highAlpha(self):
"""Get value for highAlpha"""
return self.__highAlpha

@highAlpha.setter
def highAlpha(self, value):
self.__highAlpha = value
# if callback has been set, execute the function
for callback in self.__highAlpha__callbacks:
callback(self.__highAlpha)

@property
def lowBeta(self):
"""Get value for lowBeta"""
return self.__lowBeta

@lowBeta.setter
def lowBeta(self, value):
self.__lowBeta = value
# if callback has been set, execute the function
for callback in self.__lowBeta__callbacks:
callback(self.__lowBeta)

# highBeta
@property
def highBeta(self):
"""Get value for highBeta"""
return self.__highBeta

@highBeta.setter
def highBeta(self, value):
self.__highBeta = value
# if callback has been set, execute the function
for callback in self.__highBeta__callbacks:
callback(self.__highBeta)

# lowGamma
@property
def lowGamma(self):
"""Get value for lowGamma"""
return self.__lowGamma

@lowGamma.setter
def lowGamma(self, value):
self.__lowGamma = value
# if callback has been set, execute the function
for callback in self.__lowGamma__callbacks:
callback(self.__lowGamma)

# highGamma
@property
def highGamma(self):
"""Get value for midGamma"""
return self.__highGamma

@highGamma.setter
def highGamma(self, value):
self.__highGamma = value
# if callback has been set, execute the function
for callback in self.__highGamma__callbacks:
callback(self.__highGamma)

# status
@property
def status(self):
"Get status"
"""Get status"""
return self.__status

@status.setter
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ The library is tested with Mindwave Mobie2 Headset, and runs stably.
Run the following command: `pip install PyNeuro`

## Usage

1. Importing the module: `from PyNeuro.PyNeuro import PyNeuro`
2. Initializing: `pn = PyNeuro()`
3. After initializing, if required the callbacks can be set
4. Then call `pn.connect()` method, it will connect with TCP Socket server.
5. Then call `pn.start()` method, it will be start fetching data.
6. To stop call `pn.close()`
1. Before you start, make sure you have downloaded [Nuerosky Mindware Developer Tools](https://store.neurosky.com/collections/developer-tools) and turned on Thinkgear Connecter. Please keep Thinkgear Connecter on while the program is running.
2. Importing the module: `from PyNeuro.PyNeuro import PyNeuro`
3. Initializing: `pn = PyNeuro()`
4. After initializing, if required the callbacks can be set
5. Then call `pn.connect()` method, it will connect with TCP Socket server.
6. Then call `pn.start()` method, it will be start fetching data.
7. To stop call `pn.close()`

### Obtaining Data from Device

* **Obtaining value:** `attention = pn.attention` \#to get value of attention_
>**Other Variable** attention, meditation, blinkStrength, delta, theta will be added more soon.
>**Other Variable** attention, meditation, blinkStrength, delta, , lowAlpha, highAlpha, lowBeta, highBeta, lowGamma, highGamma.
* **Setting callback:** A call back can be associated with all the above variables so that a function is called when the variable is updated. Syntax:

Expand All @@ -29,6 +29,8 @@ Run the following command: `pip install PyNeuro`
pn.set_blinkStrength_callback(callback_function3)
pn.set_delta_callback(callback_function4)
pn.set_theta_callback(callback_function5)
....
```
>You can add any number of callback functions to a variable..
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


setup(name="PyNeuro",
version="1.2.1",
version="1.3.0",
description="Library for connect with Neurosky's Mindwave EEG headset via TCP Socket",
author="Zach Wang",
author_email="[email protected]",
Expand Down

0 comments on commit 05229fb

Please sign in to comment.