Skip to content

Commit 3dc72dd

Browse files
AdonaiVeraandrewjaykeller
authored andcommitted
Brain terminal option using python
1 parent 1ac04e0 commit 3dc72dd

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

examples/brain_terminal.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# You need to install the python dashing if you want to run this code
2+
try:
3+
from dashing import HSplit, VSplit, HGauge, VGauge, HBrailleChart
4+
except ImportError:
5+
print("The dashing module is not installed. Please install it using:")
6+
print("pip install dashing")
7+
exit(1) # Exit the script if dashing is not installed
8+
9+
import os
10+
from dotenv import load_dotenv
11+
from neurosity import NeurositySDK
12+
from time import sleep
13+
14+
# Load environment variables
15+
load_dotenv()
16+
17+
# Initialize Neurosity SDK with the device ID from environment variables
18+
neurosity = NeurositySDK({
19+
"device_id": os.getenv("NEUROSITY_DEVICE_ID")
20+
})
21+
22+
# UI field paths for displaying brainwave band powers
23+
FIELD_PATHS = {
24+
"alpha": (0, 1, 0),
25+
"beta": (0, 1, 1),
26+
"delta": (0, 1, 2),
27+
"gamma": (0, 1, 3),
28+
"theta": (0, 1, 4)
29+
}
30+
31+
def neurosity_login():
32+
"""Attempt to log into Neurosity."""
33+
try:
34+
neurosity.login({
35+
"email": os.getenv("NEUROSITY_EMAIL"),
36+
"password": os.getenv("NEUROSITY_PASSWORD")
37+
})
38+
print("Logged in successfully.")
39+
return True
40+
except Exception as e:
41+
print(f"Failed to log in: {e}")
42+
return False
43+
44+
def callback_focus(data):
45+
"""Handle focus data callback."""
46+
ui.items[0].items[0].items[0].value = int(data["probability"] * 100)
47+
48+
def callback_calm(data):
49+
"""Handle calm data callback."""
50+
ui.items[0].items[0].items[1].value = int(data["probability"] * 100)
51+
52+
def callback_brainwaves_power_by_band(data):
53+
"""Handle brainwaves power by band data callback."""
54+
for band, values in data['data'].items():
55+
average_power = round(sum(values) / len(values))
56+
path = FIELD_PATHS[band]
57+
ui.items[path[0]].items[path[1]].items[path[2]].value = average_power
58+
59+
def callback_brainwaves_raw(data):
60+
"""Handle raw brainwaves data callback."""
61+
for channel_index, channel in enumerate(data['data']):
62+
ui.items[1].items[channel_index].append(round(sum(channel) / len(channel)))
63+
64+
def setup_ui():
65+
"""Set up the user interface."""
66+
global ui
67+
ui = HSplit(
68+
VSplit(
69+
HSplit(
70+
VGauge(val=0, title="Focus", border_color=2, color=4),
71+
VGauge(val=5, title="Calm", border_color=2, color=3),
72+
),
73+
HSplit(
74+
VGauge(val=0, title="Alpha", border_color=2, color=1),
75+
VGauge(val=0, title="Beta", border_color=2, color=2),
76+
VGauge(val=0, title="Delta", border_color=2, color=3),
77+
VGauge(val=0, title="Gamma", border_color=2, color=4),
78+
VGauge(val=0, title="Theta", border_color=2, color=5),
79+
)
80+
),
81+
VSplit(
82+
HBrailleChart(border_color=2, color=1, title="CP6"),
83+
HBrailleChart(border_color=2, color=2, title="F6"),
84+
HBrailleChart(border_color=2, color=3, title="C4"),
85+
HBrailleChart(border_color=2, color=4, title="CP4"),
86+
HBrailleChart(border_color=2, color=5, title="CP3"),
87+
HBrailleChart(border_color=2, color=6, title="F5"),
88+
HBrailleChart(border_color=2, color=7, title="C3"),
89+
HBrailleChart(border_color=2, color=7, title="CP5"),
90+
),
91+
)
92+
93+
if __name__ == '__main__':
94+
95+
if neurosity_login():
96+
setup_ui()
97+
neurosity.calm(callback_calm)
98+
neurosity.focus(callback_focus)
99+
neurosity.brainwaves_power_by_band(callback_brainwaves_power_by_band)
100+
neurosity.brainwaves_raw(callback_brainwaves_raw)
101+
102+
try:
103+
while True:
104+
sleep(1/25) # Maintain a loop to keep the script running
105+
ui.display()
106+
except KeyboardInterrupt:
107+
print("Interrupted by user, shutting down.")
108+
finally:
109+
print("Cleaning up resources.")
110+
else:
111+
print("Failed to initialize Neurosity device.")

0 commit comments

Comments
 (0)