Skip to content

Commit 8c0016b

Browse files
authored
Merge pull request #3 from lucidcode/refactor-data-parser
Refactor data parser
2 parents a6c9fae + 06f7044 commit 8c0016b

File tree

3 files changed

+65
-71
lines changed

3 files changed

+65
-71
lines changed

Hypnodyne ZMax/PluginHandler.cs

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -143,82 +143,76 @@ private static void processData(String data)
143143
{
144144
try
145145
{
146-
string[] stringSeparators = new string[] { "\r\n", "\r", "\n", "\n\r" };
146+
if (!data.StartsWith("D")) return;
147+
string[] stringSeparators = new string[] { "\r\n", "\r", "\n", "\n\r", "." };
147148
string[] lines = data.Split(stringSeparators, StringSplitOptions.None);
148-
for (int linen = 0; linen < lines.Length; linen++)
149+
for (int i = 0; i < lines.Length; i++)
149150
{
150-
String line = lines[linen];
151-
bool isDataPacket = false;
151+
String line = lines[i];
152152
if (line != "")
153153
{
154-
if (line[0] == 'D')
154+
String dataline = line.Substring(1);
155+
string[] parts = dataline.Split('.');
156+
byte[] buf = HexToBytes(parts[1].Replace("-", ""));
157+
if (buf.Length == 0)
155158
{
156-
String dataline = line.Substring(1);
157-
string[] parts = dataline.Split('.');
158-
byte[] buf = HexToBytes(parts[1].Replace("-", ""));
159-
if (buf.Length > 0)
159+
continue;
160+
}
161+
162+
int packet_type = buf[0];
163+
if ((packet_type >= 1) && (packet_type <= 11))
164+
{
165+
if (buf.Length == 40)
160166
{
161-
int packet_type = buf[0];
162-
if ((packet_type >= 1) && (packet_type <= 11))
167+
int eegrv = buf[1] * 256 + buf[2];
168+
int eeglv = buf[3] * 256 + buf[4];
169+
int dx = buf[5] * 256 + buf[6];
170+
int dy = buf[7] * 256 + buf[8];
171+
int dz = buf[9] * 256 + buf[10];
172+
double scaled_eegr = ScaleEEG(eegrv);
173+
double scaled_eegl = ScaleEEG(eeglv);
174+
double scaled_dx = ScaleAccel(dx);
175+
double scaled_dy = ScaleAccel(dy);
176+
double scaled_dz = ScaleAccel(dz);
177+
178+
scaled_eegr += 500;
179+
scaled_eegl += 500;
180+
181+
m_dblX = (scaled_dx + 2) * 250;
182+
m_dblY = (scaled_dy + 2) * 250;
183+
m_dblZ = (scaled_dz + 2) * 250;
184+
185+
if (ClearDisplay)
163186
{
164-
if (buf.Length == 40)
165-
{
166-
isDataPacket = true;
167-
int eegrv = buf[1] * 256 + buf[2];
168-
int eeglv = buf[3] * 256 + buf[4];
169-
int dx = buf[5] * 256 + buf[6];
170-
int dy = buf[7] * 256 + buf[8];
171-
int dz = buf[9] * 256 + buf[10];
172-
double scaled_eegr = ScaleEEG(eegrv);
173-
double scaled_eegl = ScaleEEG(eeglv);
174-
double scaled_dx = ScaleAccel(dx);
175-
double scaled_dy = ScaleAccel(dy);
176-
double scaled_dz = ScaleAccel(dz);
177-
178-
scaled_eegr += 500;
179-
scaled_eegl += 500;
180-
181-
m_dblX = (scaled_dx + 2) * 250;
182-
m_dblY = (scaled_dy + 2) * 250;
183-
m_dblZ = (scaled_dz + 2) * 250;
184-
185-
if (ClearDisplay)
186-
{
187-
ClearDisplay = false;
188-
DisplayValue = 0;
189-
}
190-
191-
if (ClearHighscore)
192-
{
193-
ClearHighscore = false;
194-
DisplayValue = 0;
195-
}
196-
197-
double raw = (scaled_eegr + scaled_eegl) / 2;
198-
if (raw >= HighscoreValue)
199-
{
200-
HighscoreValue = (scaled_eegr + scaled_eegl) / 2;
201-
}
202-
203-
DisplayValue = (scaled_eegr + scaled_eegl) / 2;
204-
205-
if (ZMaxEEGRChanged != null)
206-
{
207-
ZMaxEEGRChanged(null, new ZMaxChangedEventArgs(Convert.ToInt32(scaled_eegr)));
208-
}
209-
if (ZMaxEEGLChanged != null)
210-
{
211-
ZMaxEEGLChanged(null, new ZMaxChangedEventArgs(Convert.ToInt32(scaled_eegl)));
212-
}
213-
}
187+
ClearDisplay = false;
188+
DisplayValue = 0;
189+
}
190+
191+
if (ClearHighscore)
192+
{
193+
ClearHighscore = false;
194+
DisplayValue = 0;
195+
}
196+
197+
double raw = (scaled_eegr + scaled_eegl) / 2;
198+
if (raw >= HighscoreValue)
199+
{
200+
HighscoreValue = (scaled_eegr + scaled_eegl) / 2;
201+
}
202+
203+
DisplayValue = (scaled_eegr + scaled_eegl) / 2;
204+
205+
if (ZMaxEEGRChanged != null)
206+
{
207+
ZMaxEEGRChanged(null, new ZMaxChangedEventArgs(Convert.ToInt32(scaled_eegr)));
208+
}
209+
if (ZMaxEEGLChanged != null)
210+
{
211+
ZMaxEEGLChanged(null, new ZMaxChangedEventArgs(Convert.ToInt32(scaled_eegl)));
214212
}
215213
}
216214
}
217215
}
218-
if (!isDataPacket)
219-
{
220-
221-
}
222216
}
223217
}
224218
catch (Exception ex)

Hypnodyne ZMax/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.1.0")]
36-
[assembly: AssemblyFileVersion("1.0.1.0")]
35+
[assembly: AssemblyVersion("1.0.2.0")]
36+
[assembly: AssemblyFileVersion("1.0.2.0")]

Installer/Lucid Scribe Hypnodyne ZMax.vdproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@
278278
{
279279
"Name" = "8:.NET Framework"
280280
"Message" = "8:[VSDNETMSG]"
281-
"FrameworkVersion" = "8:3.5.30729 "
281+
"FrameworkVersion" = "8:3.5.30729 "
282282
"AllowLaterVersions" = "11:FALSE"
283283
"InstallUrl" = "8:http://go.microsoft.com/fwlink/?LinkId=76617"
284284
}
@@ -929,15 +929,15 @@
929929
{
930930
"Name" = "8:Microsoft Visual Studio"
931931
"ProductName" = "8:Lucid Scribe Hypnodyne ZMax"
932-
"ProductCode" = "8:{16E930C9-7A96-4EE0-8095-DC3D569F7B13}"
933-
"PackageCode" = "8:{E9E9EB19-E3F7-4B4D-AF66-59A29B41D551}"
932+
"ProductCode" = "8:{FEB88E90-7A2F-4FA5-9876-E571DAEE597C}"
933+
"PackageCode" = "8:{3C64919A-8A76-4C99-B2E2-7B2098C9DCF4}"
934934
"UpgradeCode" = "8:{D968DAE8-1754-45B3-992D-C4CC6A7615B8}"
935935
"AspNetVersion" = "8:4.0.30319.0"
936936
"RestartWWWService" = "11:FALSE"
937937
"RemovePreviousVersions" = "11:TRUE"
938938
"DetectNewerInstalledVersion" = "11:TRUE"
939939
"InstallAllUsers" = "11:TRUE"
940-
"ProductVersion" = "8:1.0.1"
940+
"ProductVersion" = "8:1.0.2"
941941
"Manufacturer" = "8:lucidcode"
942942
"ARPHELPTELEPHONE" = "8:"
943943
"ARPHELPLINK" = "8:http://www.lucidcode.com/Contact"

0 commit comments

Comments
 (0)