@@ -42,7 +42,10 @@ void CytonDaisyWifi::read_thread ()
42
42
*/
43
43
int res;
44
44
unsigned char b[OpenBCIWifiShieldBoard::package_size];
45
+ double package[30 ] = {0 .};
46
+ bool first_sample = true ;
45
47
double accel[3 ] = {0 .};
48
+ unsigned char last_sample_id = 0 ;
46
49
while (keep_alive)
47
50
{
48
51
res = server_socket->recv (b, OpenBCIWifiShieldBoard::package_size);
@@ -60,11 +63,9 @@ void CytonDaisyWifi::read_thread ()
60
63
continue ;
61
64
}
62
65
63
- double package[30 ] = {0 .};
64
- bool first_sample = false ;
65
66
if (b[0 ] != START_BYTE)
66
67
{
67
- break ; // drop entire transaction for daisy
68
+ continue ;
68
69
}
69
70
unsigned char *bytes = b + 1 ; // for better consistency between plain cyton and wifi, in
70
71
// plain cyton index is shifted by 1
@@ -75,8 +76,34 @@ void CytonDaisyWifi::read_thread ()
75
76
continue ;
76
77
}
77
78
79
+
80
+ // For Cyton Daisy Wifi, sample IDs are repeated twice
81
+ // (0, 0, 1, 1, 2, 2, 3, 3, ...) so when the sample id
82
+ // changes, that's how we know it's the first sample
83
+ if (last_sample_id != bytes[0 ])
84
+ {
85
+ first_sample = true ;
86
+ }
87
+ last_sample_id = bytes[0 ];
88
+
78
89
// place unprocessed bytes to other_channels for all modes
79
- if ((bytes[0 ] % 2 == 0 ) && (first_sample))
90
+ if (first_sample)
91
+ {
92
+ package[0 ] = (double )bytes[0 ];
93
+ // eeg
94
+ for (int i = 0 ; i < 8 ; i++)
95
+ {
96
+ package[i + 1 ] = eeg_scale * cast_24bit_to_int32 (bytes + 1 + 3 * i);
97
+ }
98
+ // other_channels
99
+ package[21 ] = (double )bytes[25 ];
100
+ package[22 ] = (double )bytes[26 ];
101
+ package[23 ] = (double )bytes[27 ];
102
+ package[24 ] = (double )bytes[28 ];
103
+ package[25 ] = (double )bytes[29 ];
104
+ package[26 ] = (double )bytes[30 ];
105
+ }
106
+ else
80
107
{
81
108
// eeg
82
109
for (int i = 0 ; i < 8 ; i++)
@@ -98,23 +125,6 @@ void CytonDaisyWifi::read_thread ()
98
125
package[26 ] /= 2.0 ;
99
126
package[20 ] = (double )bytes[31 ];
100
127
}
101
- else
102
- {
103
- first_sample = true ;
104
- package[0 ] = (double )bytes[0 ];
105
- // eeg
106
- for (int i = 0 ; i < 8 ; i++)
107
- {
108
- package[i + 1 ] = eeg_scale * cast_24bit_to_int32 (bytes + 1 + 3 * i);
109
- }
110
- // other_channels
111
- package[21 ] = (double )bytes[25 ];
112
- package[22 ] = (double )bytes[26 ];
113
- package[23 ] = (double )bytes[27 ];
114
- package[24 ] = (double )bytes[28 ];
115
- package[25 ] = (double )bytes[29 ];
116
- package[26 ] = (double )bytes[30 ];
117
- }
118
128
119
129
// place processed accel data
120
130
if (bytes[31 ] == END_BYTE_STANDARD)
@@ -124,7 +134,19 @@ void CytonDaisyWifi::read_thread ()
124
134
accel_temp[1 ] = cast_16bit_to_int32 (bytes + 27 );
125
135
accel_temp[2 ] = cast_16bit_to_int32 (bytes + 29 );
126
136
127
- if ((bytes[0 ] % 2 == 0 ) && (first_sample))
137
+ if (first_sample)
138
+ {
139
+ package[0 ] = (double )bytes[0 ];
140
+
141
+ // accel
142
+ if (accel_temp[0 ] != 0 )
143
+ {
144
+ accel[0 ] = accel_scale * accel_temp[0 ];
145
+ accel[1 ] = accel_scale * accel_temp[1 ];
146
+ accel[2 ] = accel_scale * accel_temp[2 ];
147
+ }
148
+ }
149
+ else
128
150
{
129
151
// need to average accel data
130
152
if (accel_temp[0 ] != 0 )
@@ -140,19 +162,6 @@ void CytonDaisyWifi::read_thread ()
140
162
141
163
package[20 ] = (double )bytes[31 ];
142
164
}
143
- else
144
- {
145
- first_sample = true ;
146
- package[0 ] = (double )bytes[0 ];
147
-
148
- // accel
149
- if (accel_temp[0 ] != 0 )
150
- {
151
- accel[0 ] = accel_scale * accel_temp[0 ];
152
- accel[1 ] = accel_scale * accel_temp[1 ];
153
- accel[2 ] = accel_scale * accel_temp[2 ];
154
- }
155
- }
156
165
157
166
package[17 ] = accel[0 ];
158
167
package[18 ] = accel[1 ];
@@ -161,7 +170,15 @@ void CytonDaisyWifi::read_thread ()
161
170
// place processed analog data
162
171
if (bytes[31 ] == END_BYTE_ANALOG)
163
172
{
164
- if ((bytes[0 ] % 2 == 0 ) && (first_sample))
173
+ if (first_sample)
174
+ {
175
+ package[0 ] = (double )bytes[0 ];
176
+ // analog
177
+ package[27 ] = cast_16bit_to_int32 (bytes + 25 );
178
+ package[28 ] = cast_16bit_to_int32 (bytes + 27 );
179
+ package[29 ] = cast_16bit_to_int32 (bytes + 29 );
180
+ }
181
+ else
165
182
{
166
183
// need to average analog data
167
184
package[27 ] += cast_16bit_to_int32 (bytes + 25 );
@@ -172,22 +189,15 @@ void CytonDaisyWifi::read_thread ()
172
189
package[29 ] /= 2 .0f ;
173
190
package[20 ] = (double )bytes[31 ]; // cyton end byte
174
191
}
175
- else
176
- {
177
- first_sample = true ;
178
- package[0 ] = (double )bytes[0 ];
179
- // analog
180
- package[27 ] = cast_16bit_to_int32 (bytes + 25 );
181
- package[28 ] = cast_16bit_to_int32 (bytes + 27 );
182
- package[29 ] = cast_16bit_to_int32 (bytes + 29 );
183
- }
184
192
}
185
193
// commit package
186
- if ((bytes[ 0 ] % 2 == 0 ) && ( first_sample) )
194
+ if (! first_sample)
187
195
{
188
196
double timestamp = get_timestamp ();
189
197
db->add_data (timestamp, package);
190
198
streamer->stream_data (package, 30 , timestamp);
191
199
}
200
+
201
+ first_sample = false ;
192
202
}
193
203
}
0 commit comments