-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cs
168 lines (146 loc) · 5.08 KB
/
Engine.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2012 Daniel A. Spilker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using EmIR.Models;
using Emotiv;
using UsbUirt;
namespace EmIR
{
public class Engine : INotifyPropertyChanged, IDisposable
{
private Thread thread;
private volatile bool running;
private IRCode lastIRCode;
private string status;
public string Status
{
get { return status; }
set
{
if (status != value)
{
status = value;
NotifyPropertyChanged("Status");
}
}
}
public void Start()
{
App.CurrentApp.EmotivInterface.PropertyChanged += InterfacePropertyChanged;
App.CurrentApp.USBUIRTInterface.PropertyChanged += InterfacePropertyChanged;
UpdateStatus();
}
public void Dispose()
{
App.CurrentApp.EmotivInterface.PropertyChanged -= InterfacePropertyChanged;
App.CurrentApp.USBUIRTInterface.PropertyChanged -= InterfacePropertyChanged;
if (running)
{
Stop();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
void Run()
{
if (!running)
{
running = true;
thread = new Thread((ThreadStart)delegate
{
while (running)
{
App.CurrentApp.EmotivInterface.EmoEngine.ProcessEvents(100);
}
});
thread.Name = "EmIR Engine Thread";
thread.Start();
App.CurrentApp.EmotivInterface.EmoEngine.CognitivEmoStateUpdated += CognitivEmoStateUpdated;
App.CurrentApp.USBUIRTInterface.Controller.TransmitCompleted += TransmitCompleted;
}
}
void Stop()
{
if (running)
{
if (App.CurrentApp.USBUIRTInterface.Controller != null)
{
App.CurrentApp.USBUIRTInterface.Controller.TransmitCompleted -= TransmitCompleted;
}
App.CurrentApp.EmotivInterface.EmoEngine.CognitivEmoStateUpdated -= CognitivEmoStateUpdated;
running = false;
thread.Join();
}
}
void UpdateStatus()
{
if (App.CurrentApp.EmotivInterface.Connected && App.CurrentApp.USBUIRTInterface.Connected)
{
Run();
Status = "Ready";
}
else
{
Stop();
string message = "";
if (!App.CurrentApp.EmotivInterface.Connected)
{
message = "EmoEngine not connected";
}
if (!App.CurrentApp.USBUIRTInterface.Connected)
{
if (message.Length > 0)
{
message += ", ";
}
message += "USB-UIRT not connected";
}
Status = message;
}
}
void InterfacePropertyChanged(object sender, PropertyChangedEventArgs e)
{
UpdateStatus();
}
void CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e)
{
Rule rule = App.CurrentApp.Rules.FirstOrDefault(r => r.Headset == e.userId && r.Action == e.emoState.CognitivGetCurrentAction() && r.IRCode != null);
if (rule != null && e.emoState.CognitivGetCurrentActionPower() >= rule.Threshold)
{
if (rule.IRCode != lastIRCode)
{
Status = "Sending IR Code: " + rule.IRCode.Name;
App.CurrentApp.USBUIRTInterface.Controller.TransmitAsync(rule.IRCode.Code, CodeFormat.Pronto, 2, TimeSpan.Zero);
lastIRCode = rule.IRCode;
}
}
else
{
lastIRCode = null;
}
}
void TransmitCompleted(object sender, TransmitCompletedEventArgs e)
{
Status = e.Error == null ? "Ready" : "IR Transmission Error: " + e.Error.Message;
}
}
}