forked from robintibor/python-mindwave-mobile
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread_loop.py
More file actions
87 lines (70 loc) · 3.77 KB
/
read_loop.py
File metadata and controls
87 lines (70 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# read_mwm.py
# @author: Esteban Martínez
# TO-DO LIST
# - Add a periodic visual or sound alarm and set an amount of time the MindWave will run.
# - Implement digital filters.
# This module reads data from the Mindwave Mobile EEG headset and prints it
# to the console. It connects to the headset, reads data points,
# and displays the values for:
# - EEG Power Bands (Delta, Theta, Low Alpha, High Alpha, Low Beta, High Beta, Low Gamma, Mid Gamma)
# - Raw Data
# - Meditation
# - Attention
# - Amount of Noise (also known as Poor Signal Level)
# - Blink (Not working)
import time
import bluetooth
from mindwavemobile.MindwaveDataPoints import RawDataPoint, PoorSignalLevelDataPoint, AttentionDataPoint, MeditationDataPoint, BlinkDataPoint, EEGPowersDataPoint
from mindwavemobile.MindwaveDataPointReader import MindwaveDataPointReader
# Main execution block.
# Initializes the MindwaveDataPointReader, starts the connection,
# and continuously reads and prints data points.
if __name__ == '__main__':
mindwaveDataPointReader = MindwaveDataPointReader()
mindwaveDataPointReader.start()
if (mindwaveDataPointReader.isConnected()):
# Initialize all variables
rawValue = attention = meditation = amountOfNoise = blink = None
delta = theta = lowAlpha = highAlpha = lowBeta = highBeta = lowGamma = midGamma = None
data_header = "eeg_power;raw_value;attention;meditation;amount_of_noise"
# Print Header
# eeg_power = [Delta,Theta,LowAlpha,HighAlpha,LowBeta,HighBeta,LowGamma,MidGamma]
print(data_header)
# Endless read cycle
while(True):
# DataPoint is the class that reads the next data point from the headset.
dataPoint = mindwaveDataPointReader.readNextDataPoint()
# Checks if the dataPoint object belongs to one of the specified
# data point classes. If the dataPoint is an instance of one
# of these classes, the code then proceeds to extract
# the specific data value from that data point object.
if isinstance(dataPoint, (PoorSignalLevelDataPoint, AttentionDataPoint,
MeditationDataPoint, BlinkDataPoint,
RawDataPoint, EEGPowersDataPoint)):
if isinstance(dataPoint, PoorSignalLevelDataPoint):
amountOfNoise = dataPoint.amountOfNoise
elif isinstance(dataPoint, AttentionDataPoint):
attention = dataPoint.attentionValue
elif isinstance(dataPoint, MeditationDataPoint):
meditation = dataPoint.meditationValue
elif isinstance(dataPoint, BlinkDataPoint):
blink = dataPoint.blinkValue
elif isinstance(dataPoint, RawDataPoint):
rawValue = dataPoint.rawValue
elif isinstance(dataPoint, EEGPowersDataPoint):
delta, theta = dataPoint.delta, dataPoint.theta
lowAlpha, highAlpha = dataPoint.lowAlpha, dataPoint.highAlpha
lowBeta, highBeta = dataPoint.lowBeta, dataPoint.highBeta
lowGamma, midGamma = dataPoint.lowGamma, dataPoint.midGamma
# Prints on console all data collected in a cycle.
print(
f"[{delta},{theta},{lowAlpha},{highAlpha},"\
f"{lowBeta},{highBeta},{lowGamma},{midGamma}];"\
f"{rawValue};{attention};{meditation};{amountOfNoise}"
)
# Error message when device is not connected or couldn't be found.
else:
print(
"Exiting because the program could not connect "\
"to the MindWave Mobile device."
)