forked from PolyphasicDevTeam/SleePi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeg_reader.cpp
149 lines (133 loc) · 3.3 KB
/
eeg_reader.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <boost/python.hpp>
#include <iostream>
#include <stdio.h>
#include <fcntl.h> /* File Control Definitions */
#include <termios.h>/* POSIX Terminal Control Definitions*/
#include <unistd.h> /* UNIX Standard Definitions */
#include <errno.h> /* ERROR Number Definitions */
#include <fstream>
#define MAX_EEG_CHANNELS 6
typedef struct PACKETStruct
{
unsigned char readstate;
unsigned int extract_pos;
unsigned int info;
unsigned int requestedinfo;
unsigned int chn;
unsigned char number;
unsigned char old_number;
unsigned char switches;
unsigned char aux;
unsigned int buffer[MAX_EEG_CHANNELS*2];
unsigned int tempbuf[MAX_EEG_CHANNELS*2];
} PACKETStruct;
bool parse_byte_P2(unsigned char actbyte);
PACKETStruct PACKET;
int fd;
int eeg_init()
{
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY);
if(fd == 1)
{
printf("\n Error! in Opening ttyUSB0\n");
return 1;
}
else
{
printf("\n ttyUSB0 Opened Successfully\n");
}
struct termios tty;
tcgetattr(fd,&tty);
speed_t ispeed = cfgetispeed(&tty);
speed_t ospeed = cfgetospeed(&tty);
printf("baud rate in: 0%o\n", ispeed);
printf("baud rate out: 0%o\n", ospeed);
//tcgetattr(fd, &SerialPortSettings);
//cfsetispeed(&SerialPortSettings,B57600);
//cfsetospeed(&SerialPortSettings,B57600);
//SerialPortSettings.c_cflag |= CREAD | CLOCAL;
//SerialPortSettings.c_cc[VMIN] = 10; // Read 10 characters
//SerialPortSettings.c_cc[VTIME] = 0; // Wait indefinitely
//tcsetattr(fd,TCSANOW,&SerialPortSettings);
return 0;
}
int eeg_destroy()
{
close(fd);
return 0;
}
int ch_val[MAX_EEG_CHANNELS];
std::string eeg_get()
{
std::string ret = "";
unsigned char read_buffer[512];
int bytes_read = 0;
bytes_read = read(fd,&read_buffer,512);
for (size_t i = 0; i < bytes_read; ++i)
{
unsigned int tmp = read_buffer[i];
//std::cout << tmp << std::endl;
if(parse_byte_P2(read_buffer[i]))
{
for (size_t i = 0; i < MAX_EEG_CHANNELS; ++i)
{
if (i > 0) { ret += " "; }
ret += std::to_string(ch_val[i]);
}
ret += "X";
}
}
return ret;
}
bool parse_byte_P2(unsigned char actbyte)
{
bool ret = 0;
switch (PACKET.readstate)
{
case 0: if (actbyte==165) PACKET.readstate++;
break;
case 1: if (actbyte==90) PACKET.readstate++;
else PACKET.readstate=0;
break;
case 2: PACKET.readstate++;
break;
case 3: PACKET.number = actbyte;
//std::cout << "Frame number " << PACKET.number << std::endl;
PACKET.extract_pos=0;PACKET.readstate++;
break;
case 4:
if (PACKET.extract_pos < 12)
{
if ((PACKET.extract_pos & 1) == 0)
{
PACKET.buffer[PACKET.extract_pos>>1]=actbyte*256;
}
else
{
PACKET.buffer[PACKET.extract_pos>>1]+=actbyte;
}
PACKET.extract_pos++;
}
else
{
for (size_t i = 0; i < MAX_EEG_CHANNELS; ++i)
{
ch_val[i] = PACKET.buffer[i];
//std::cout << ch_val[i] << std::endl;
}
ret = 1;
PACKET.switches= actbyte;
PACKET.readstate=0;
}
break;
default: PACKET.readstate=0;
}
return ret;
}
BOOST_PYTHON_MODULE(openeeg)
{
using namespace boost::python;
def("eeg_init",eeg_init);
def("eeg_destroy",eeg_destroy);
def("eeg_get",eeg_get);
}