-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.js
178 lines (158 loc) · 5.4 KB
/
common.js
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
// air light 在 检查设备 => 连接设备 => 获取实例 => 判断设备类型 进行整合
// 不同类型升级提供俩个接口,通过判断设备类型分别调用不同的SDK包,再进行升级后续操作
let curGlasses = null;
const DEBUG = true;
import GlassesLight from './js_light/glasses.js';
import * as managerLight from './js_light/manager.js'
import GlassesAir from './js_air/glasses.js'
import * as managerAir from './js_air/manager.js'
// *******************
export function isNrealDevice(device) {
// 涉及俩种设备的区分协议(符合 air 和 light 俩种Nreal设备)
// console.log({
// pid_hex: device.productId.toString(16),
// pid_dec: device.productId
// })
return device.productId == 0x0423
|| device.productId == 0x573C
|| device.productId == 0x0424;
}
export function addHidListener() {
navigator.hid.onconnect = function (event) {
let device = event.device;
if (device.productId == 1059 || device.productId == 1060) {
console.log('connected',{device});
managerAir.canCommand(device).then(result => {
if (result) {
curGlasses = new GlassesAir(device);
}
});
}
if (device.productId == 22332 || device.productId == 22336) {
managerLight.canCommand(device).then(result => {
if (result) {
curGlasses = new GlassesLight(device);
}
});
}
}
navigator.hid.ondisconnect = function (event) {
if (curGlasses && curGlasses.device == event.device) {
curGlasses = null;
}
}
}
window.curGlassesArray = [];
export function checkConnection() {
if (curGlasses) {
return curGlasses;
}
return navigator.hid.getDevices().then(devices => {
// console.warn('filtering', devices);
// filters out devices that are nreal devices.
return devices.filter(isNrealDevice);
}).then(async devices => {
console.log('filtered:', devices)
for (let device of devices) {
// if(curGlasses){
// console.warn('skipping',device)
// continue;
// }
if (device.productId == 1059 || device.productId == 1060) {
console.log({air:device});
if (await managerAir.canCommand(device)) {
curGlasses = new GlassesAir(device);
curGlassesArray.push(curGlasses);
return curGlasses;
}
}
if (device.productId == 22332 || device.productId == 22336) {
if (await managerLight.canCommand(device)) {
curGlasses = new GlassesLight(device);
return curGlasses;
}
}
}
});
}
export function requestDevice() {
return navigator.hid.requestDevice({
filters: [{
vendorId: 0x0486, // ? ASUS Computers Inc. ?
}, {
vendorId: 0x0483, // STMicroelectronics ?
}, {
vendorId: 0x0482, // Kyocera Corporation ?
}, {
vendorId: 0x3318, // Gleaming Reality (Wuxi) Technology Co., LTD ?
}]
}).then(async devices => {
for (let device of devices) {
if (deviceIsAir(device)) {
// can command checks isMcu
// air shows up as 3 separate HID devices
// but we only select 1 that can be commanded
// this code was originally from their activation/update page
// so, maybe devices OTHER than MCU can be communicated with via JS?
// I'm not sure yet...
if (await managerAir.canCommand(device)) {
curGlasses = new GlassesAir(device);
return curGlasses;
}
} else if (deviceIsLight(device)) {
// NOTE i don't have a Light to test with,
// so all my work is focusedd on the Air Protocol for now...
if (await managerLight.canCommand(device)) {
curGlasses = new GlassesLight(device);
return curGlasses;
}
}
}
});
}
export async function connectDevice() {
let glasses = await checkConnection();
if (glasses) {
return glasses;
}
return await requestDevice();
}
export async function disconnectDevice() {
if(curGlasses){
curGlasses._device.close()
curGlasses = null;
}
}
export function deviceIsAir(device){
// 0x423 || 0x424
return device.productId == 1059 || device.productId == 1060
}
export function deviceIsLight(device){
// 0x573C || 0x5740
return device.productId == 22332 || device.productId == 22336
}
// check glasses is air or light?
export function isAirOrLight() {
if (!curGlasses) {
return 'not found device';
}
if (deviceIsAir(curGlasses.device)) {
return 1
}
if (deviceIsLight(curGlasses.device)) {
return 2
}
return null
}
export function hexStream2int8Array(captureString){
return new Uint8Array(captureString.match(/.{1,2}/g).map((b)=>{
// console.log(b);
return b //.parseInt(16)
}))
}
export function parseHexString(captureString){
return glasses.protocol.parse_rsp(hexStream2int8Array(captureString))
}
export {
curGlasses
}