-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBoard.cs
228 lines (198 loc) · 8.01 KB
/
TestBoard.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using UnityEngine;
using System.Collections.Generic;
using System;
using TMPro;
using System.Collections.Concurrent;
using MychIO.Device;
using MychIO;
using MychIO.Event;
using System.Linq;
using MychIO.Connection.SerialDevice;
using MychIO.Device.TouchPanel;
public class TestBoard : MonoBehaviour
{
private ConcurrentQueue<Action> _executionQueue;
private GameObject[] _touchIndicators = null;
private Dictionary<TouchPanelZone, GameObject> _touchIndicatorMap;
private GameObject[] _buttonIndicators = null;
private Dictionary<ButtonRingZone, GameObject> _buttonIndicatorMap;
private IOManager _ioManager;
// Display
[SerializeReference]
private TextMeshProUGUI _eventText;
void Start()
{
_executionQueue = IOManager.ExecutionQueue;
// load debug menus
_eventText.text = "Waiting for events...";
// Load game objects
_touchIndicators = GameObject.FindGameObjectsWithTag("TouchIndicator");
_touchIndicatorMap = _touchIndicators.ToDictionary(go => Enum.Parse<TouchPanelZone>(go.name), go => go);
_buttonIndicators = GameObject.FindGameObjectsWithTag("ButtonIndicator");
_buttonIndicatorMap = _buttonIndicators
.ToDictionary(go => Enum.Parse<ButtonRingZone>(go.name), go => go);
_ioManager = new IOManager();
foreach (GameObject touchIndicator in _touchIndicators)
{
touchIndicator.SetActive(false);
}
foreach (GameObject buttonIndicator in _buttonIndicators)
{
buttonIndicator.SetActive(false);
}
}
public void connectDevices()
{
// Setup Adx Connection:
// Setup callbacks for TouchPanelZone
var touchPanelCallbacks = new Dictionary<TouchPanelZone, Action<TouchPanelZone, InputState>>();
foreach (TouchPanelZone touchZone in System.Enum.GetValues(typeof(TouchPanelZone)))
{
if (!_touchIndicatorMap.TryGetValue(touchZone, out var touchIndicator))
{
throw new Exception($"Failed to find GameObject for {touchZone}");
}
touchPanelCallbacks[touchZone] = (TouchPanelZone input, InputState state) =>
{
_executionQueue.Enqueue(() =>
{
touchIndicator.SetActive(state == InputState.On);
});
};
}
// Setup callbacks for ButtonRingZone
var buttonRingCallbacks = new Dictionary<ButtonRingZone, Action<ButtonRingZone, InputState>>();
foreach (ButtonRingZone buttonZone in System.Enum.GetValues(typeof(ButtonRingZone)))
{
if (!_buttonIndicatorMap.TryGetValue(buttonZone, out var buttonIndicator))
{
throw new Exception($"Failed to find GameObject for {buttonZone}");
}
buttonRingCallbacks[buttonZone] = (ButtonRingZone input, InputState state) =>
{
_executionQueue.Enqueue(() =>
{
buttonIndicator.SetActive(state == InputState.On);
});
};
}
// Setup Events
var eventCallbacks = new Dictionary<IOEventType, ControllerEventDelegate>{
{ IOEventType.Attach,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.ConnectionError,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.Debug,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.Detach,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.SerialDeviceReadError,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.InvalidDevicePropertyError,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
},
{ IOEventType.TouchPanelDeviceReadError,
(eventType, deviceType, message) =>
{
var text = $"eventType: {eventType} type: {deviceType} message: {message.Trim()}";
Debug.Log(text);
appendEventText(text);
}
}
};
_ioManager.Destroy(); // reset everything
_ioManager.SubscribeToEvents(eventCallbacks);
_ioManager.SubscribeToEvents(eventCallbacks);
var propertiesTouchPanel = new SerialDeviceProperties(
AdxTouchPanel.GetDefaultDeviceProperties(),
comPortNumber: "COM3",
debounceTimeMs: 5
).GetProperties();
try
{
_ioManager
.AddTouchPanel(
AdxTouchPanel.GetDeviceName(),
propertiesTouchPanel,
inputSubscriptions: touchPanelCallbacks
);
/*
_ioManager
.AddTouchPanel(
TouchPanelTouchPanel.GetDeviceName(),
propertiesTouchPanel,
inputSubscriptions: touchPanelCallbacks
);
*/
_ioManager.AddButtonRing(
AdxIO4ButtonRing.GetDeviceName(),
new Dictionary<string, dynamic>(){
{ "PollingRateMs", 0 },
{ "DebounceTimeMs", 5}
},
inputSubscriptions: buttonRingCallbacks
);
_ioManager.AddLedDevice(
AdxLedDevice.GetDeviceName()
);
}
catch (Exception e)
{
Debug.Log(e);
}
// TODO: Debug this
var result = _ioManager.GetDeviceProperties(DeviceClassification.ButtonRing);
Debug.Log(result);
}
private void appendEventText(string message)
{
_executionQueue.Enqueue(() =>
{
_eventText.text = $"\n{DateTime.Now.ToString("HH:mm:ss:fff")} - {message}";
}
);
}
// Update is called once per frame
void Update()
{
while (_executionQueue.TryDequeue(out var action))
{
action();
}
}
}