HandleControllerSession.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using AOT;
  6. using EZXR.Glass.Core;
  7. using EZXR.Glass.SixDof;
  8. using UnityEngine;
  9. namespace EZXR.Glass.Inputs
  10. {
  11. public enum HandleKeyCode
  12. {
  13. Home = 301,
  14. Return = 300,
  15. Primary = 303,
  16. Secondary = 302,
  17. Rocker = 304,
  18. Trigger = 305,
  19. Grid = 306
  20. }
  21. [ScriptExecutionOrder(-50)]
  22. public class HandleControllerSession : MonoBehaviour
  23. {
  24. //[StructLayout(LayoutKind.Sequential, Pack = 1)]
  25. [StructLayout(LayoutKind.Sequential)]
  26. private struct HandControllerResult
  27. {
  28. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
  29. public float[] pose; //xyz, wxyz
  30. public float is_3dof; //0: 6dof,1: 3dof
  31. public double timestamp;
  32. public int controllerId; //0左手柄,1右手柄
  33. }
  34. private static HandleControllerSession instance;
  35. public static HandleControllerSession Instance
  36. {
  37. get
  38. {
  39. return instance;
  40. }
  41. }
  42. public static Pose controllerPose_left = new Pose();
  43. public static Pose controllerPose_right = new Pose();
  44. private HandControllerResult controllerLeftResult = new HandControllerResult();
  45. private HandControllerResult controllerRightResult = new HandControllerResult();
  46. private Action<HandType, bool> bindingEventCallback;
  47. private Action<HandType, bool> connectEventCallback;
  48. private Action<HandType, HandleKeyCode, bool> buttonEventCallback;
  49. private Action<HandType, Vector2> axis2DEventCallback;
  50. private Action<HandType, HandleKeyCode, bool> holdEventCallback;
  51. private Action<HandType, bool> silenceEventCallback;
  52. private Action<HandType, int> trackingStateEventCallback;
  53. private Action<HandType, bool> upgradeOTAEventCallback;
  54. private Action<bool> serverReconnectEventCallback;
  55. //public Action<bool> dynamicModeChangedCallBack;
  56. private delegate void ControllerBindEventCallback(int value);
  57. private delegate void ControllerConnectEventCallback(bool connected, int controllerId);
  58. private delegate void ControllerButtonEventCallback(int keycode, int state, int controllerId);
  59. private delegate void ControllerAxis2DEventCallback(int x, int y, int controllerId);
  60. private delegate void ControllerHoldEventCallback(int keycode, bool isHeld, int controllerId);
  61. private delegate void ControllerSilenceEventCallback(bool sleepless, int controllerId);
  62. private delegate void ControllerHxrctUpdateCallback(int status, int controllerId);
  63. //private delegate void ControllerDynamicModeChangedCallback(int status);
  64. private delegate void ControllerServerReconnectedCallback(bool connected);
  65. private struct ControllerBindEventUserData { public int status; public bool changed; }
  66. private struct ControllerConnectEventUserData { public bool connected; public int controllerId; public bool changed; }
  67. private struct ControllerButtonEventUserData { public int keycode; public int state; public int controllerId; public bool changed; }
  68. private struct ControllerAxis2DEventUserData { public int x; public int y; public int controllerId; public bool changed; }
  69. private struct ControllerHoldEventUserData { public int keycode; public bool isHeld; public int controllerId; public bool changed; }
  70. private struct ControllerSilenceEventUserData { public bool sleepless; public int controllerId; public bool changed; }
  71. private struct ControllerHxrctUpdateUserData { public int status; public int controllerId; public bool changed; }
  72. private struct ControllerServerReconnectedUserData { public bool connected; public bool changed; }
  73. private static ControllerBindEventUserData bindEventUserData;
  74. private static ControllerConnectEventUserData connectEventUserData;
  75. private static ControllerButtonEventUserData buttonEventUserData;
  76. private static ControllerAxis2DEventUserData axis2DEventUserData;
  77. private static ControllerHoldEventUserData holdEventUserData;
  78. private static ControllerSilenceEventUserData silenceEventUserData;
  79. private static ControllerHxrctUpdateUserData hxrctUpdateUserData;
  80. private static ControllerServerReconnectedUserData serverReconnectedUserData;
  81. //private static bool isPaused;
  82. private static bool isRegisterActive;
  83. private void Awake()
  84. {
  85. instance = this;
  86. controllerLeftResult.controllerId = -1;
  87. controllerRightResult.controllerId = -1;
  88. controllerLeftResult.pose = new float[7];
  89. controllerRightResult.pose = new float[7];
  90. controllerLeftResult.is_3dof = -1;
  91. controllerRightResult.is_3dof = -1;
  92. RegisterNativeAPI(); //当应用内SceneChanged时不会触发OnApplicationPause
  93. //需要在Awake时就注册NativeAPI
  94. Debug.Log("HandleControllerSession Awake");
  95. }
  96. private void OnApplicationPause(bool pause)
  97. {
  98. //isPaused = pause;
  99. //Debug.Log($"xxxxxx, OnApplicationPause call: pause = {pause}");
  100. if (!pause)
  101. {
  102. RegisterNativeAPI();
  103. Debug.Log("HandleControllerSession OnApplicationResume, successfully register Events(8)");
  104. }
  105. else
  106. {
  107. UnregisterNativeAPI();
  108. Debug.Log("HandleControllerSession OnApplicationPause, successfully unregister Events(8)");
  109. }
  110. }
  111. private void OnDestroy()
  112. {
  113. UnregisterNativeAPI();
  114. Debug.Log("HandleControllerSession, destroy register Events(8)");
  115. }
  116. private void RegisterNativeAPI()
  117. {
  118. if (isRegisterActive) return;
  119. isRegisterActive = true;
  120. NativeAPI.registerControllerBondChangeCallback(OnControllerBindEventCallback);
  121. NativeAPI.registerControllerConnectEventCallback(OnControllerConnectEventCallback);
  122. NativeAPI.registerControllerTouchEvent(OnControllerButtonEventCallback);
  123. NativeAPI.registerControllerRockerTouchEvent(OnControllerAxis2DEventCallback);
  124. NativeAPI.registerControllerTouchChangeEventCallback(OnControllerHoldEventCallback);
  125. NativeAPI.registerControllerSleepChangeCallback(OnControllerSilenceEventCallback);
  126. NativeAPI.registerControllerHxrctUpdateCallback(OnControllerHxrctUpdateCallback);
  127. //NativeAPI.registerControllerDynamicModeChangedCallback(OnControllerDynamicModeChangedCallback);
  128. //NativeAPI.registerConnectChangedCallback(OnControllerServerReconnectedCallback);
  129. NativeTracking.RegisterConnectChangedCallback(OnControllerServerReconnectedCallback);
  130. Debug.Log("HandleControllerSession RegisterNativeAPI, successfully register Events(8)");
  131. }
  132. private void UnregisterNativeAPI()
  133. {
  134. isRegisterActive = false;
  135. NativeAPI.registerControllerBondChangeCallback(null);
  136. NativeAPI.registerControllerConnectEventCallback(null);
  137. NativeAPI.registerControllerTouchEvent(null);
  138. NativeAPI.registerControllerRockerTouchEvent(null);
  139. NativeAPI.registerControllerTouchChangeEventCallback(null);
  140. NativeAPI.registerControllerSleepChangeCallback(null);
  141. NativeAPI.registerControllerHxrctUpdateCallback(null);
  142. //NativeAPI.registerControllerDynamicModeChangedCallback(null);
  143. //NativeAPI.registerConnectChangedCallback(null);
  144. NativeTracking.UnregisterConnectChangedCallback(OnControllerServerReconnectedCallback);
  145. Debug.Log("HandleControllerSession UnregisterNativeAPI, successfully unregister Events(8)");
  146. }
  147. // Update is called once per frame
  148. private void Update()
  149. {
  150. /*
  151. if (Input.GetKeyDown(KeyCode.Space))
  152. {
  153. //Debug.Log("HandleControllerSession, Space KeyDown");
  154. Debug.Log($"HandleControllerSession, get Battery: L{NativeAPI.GetControllerBattery(0)}, R{NativeAPI.GetControllerBattery(1)}");
  155. Debug.Log($"HandleControllerSession, get ConnectState: L{NativeAPI.GetControllerConnectState(0)}, R{NativeAPI.GetControllerConnectState(1)}");
  156. }
  157. if (Input.GetKeyDown(KeyCode.C))
  158. {
  159. //Debug.Log("HandleControllerSession, C KeyDown");
  160. Debug.Log($"HandleControllerSession, cancel Binding: L{NativeAPI.ClearBond(0)}, R{NativeAPI.ClearBond(1)}");
  161. }
  162. if (Input.GetKeyDown(KeyCode.L))
  163. {
  164. //Debug.Log("HandleControllerSession, L KeyDown");
  165. Debug.Log($"HandleControllerSession, Left Binding: {NativeAPI.bondController(0)}");
  166. }
  167. if (Input.GetKeyDown(KeyCode.R))
  168. {
  169. //Debug.Log("HandleControllerSession, R KeyDown");
  170. Debug.Log($"HandleControllerSession, Right Binding: {NativeAPI.bondController(1)}");
  171. }
  172. if (Input.GetKeyDown(KeyCode.V))
  173. {
  174. //Debug.Log("HandleControllerSession, V KeyDown");
  175. Debug.Log($"HandleControllerSession, vibrate: {NativeAPI.vibrateController(1, 2, 200)}");
  176. }
  177. if (Input.GetKeyDown(KeyCode.B))
  178. {
  179. //Debug.Log("HandleControllerSession, B KeyDown");
  180. Debug.Log($"HandleControllerSession, bind status: {NativeAPI.GetControllerBondState() & 0xF0}");
  181. }
  182. if (Input.GetKeyDown(KeyCode.C))
  183. {
  184. //Debug.Log("HandleControllerSession, C KeyDown");
  185. Debug.Log($"HandleControllerSession, cancel Binding: L{NativeAPI.ClearBond(0)}, R{NativeAPI.ClearBond(1)}");
  186. }
  187. if (Input.GetKeyDown(KeyCode.R))
  188. {
  189. //Debug.Log("HandleControllerSession, R KeyDown");
  190. Debug.Log($"HandleControllerSession, Right Binding: {NativeAPI.bondController(1)}");
  191. }
  192. if (Input.GetKeyDown(KeyCode.Q))
  193. {
  194. Debug.Log($"HandleControllerSession, HostVersion: {NativeAPI.ControllerClient_GetVersion()}, " +
  195. $"ClientVersion: (L){NativeAPI.ControllerClient_GetCtrlVersion(0)}(R){NativeAPI.ControllerClient_GetCtrlVersion(1)}");
  196. }
  197. if (Input.GetKeyDown(KeyCode.U))
  198. {
  199. Debug.Log($"HandleControllerSession, HostUpdate: {NativeAPI.ControllerHxrUpgradeHost("/sdcard/Download/SA2101_52833_7.zip")}");
  200. }
  201. if (Input.GetKeyDown(KeyCode.W))
  202. {
  203. Debug.Log($"HandleControllerSession, ClientUpdate: (R){NativeAPI.updateHxrct("/sdcard/Download/hxrct_E7.zip", 1)}");
  204. }
  205. if (Input.GetKeyDown(KeyCode.G))
  206. {
  207. Debug.Log($"HandleControllerSession, GetCurrentControllerType: (R){NativeAPI.getCurrentHandType()}");
  208. }
  209. if (Input.GetKeyDown(KeyCode.M))
  210. {
  211. Debug.Log($"HandleControllerSession, GetControllerDeviceSN: (L){GetControllerDeviceSN(0)}(R){GetControllerDeviceSN(1)}");
  212. }
  213. */
  214. }
  215. /// <summary>
  216. /// 初始化注册回调
  217. /// </summary>
  218. public void InitRegistration(Action<HandType, bool> bindingEventCallback,
  219. Action<HandType, bool> connectEventCallback,
  220. Action<HandType, HandleKeyCode, bool> buttonEventCallback,
  221. Action<HandType, Vector2> axis2DEventCallback,
  222. Action<HandType, HandleKeyCode, bool> holdEventCallback,
  223. Action<HandType, bool> silenceEventCallback,
  224. Action<HandType, int> trackingStateEventCallback)
  225. {
  226. this.bindingEventCallback = bindingEventCallback;
  227. this.connectEventCallback = connectEventCallback;
  228. this.buttonEventCallback = buttonEventCallback;
  229. this.axis2DEventCallback = axis2DEventCallback;
  230. this.holdEventCallback = holdEventCallback;
  231. this.silenceEventCallback = silenceEventCallback;
  232. this.trackingStateEventCallback = trackingStateEventCallback;
  233. }
  234. /// <summary>
  235. /// 初始化注册OTA回调
  236. /// </summary>
  237. /// <param name="upgradeOTAEventCallback"></param>
  238. public void InitOTARegistration(Action<HandType, bool> upgradeOTAEventCallback)
  239. {
  240. this.upgradeOTAEventCallback = upgradeOTAEventCallback;
  241. }
  242. /// <summary>
  243. /// 初始化注册Server重连回调
  244. /// </summary>
  245. /// <param name="serverReconnectEventCallback"></param>
  246. public void InitServerReconnectRegistration(Action<bool> serverReconnectEventCallback)
  247. {
  248. this.serverReconnectEventCallback = serverReconnectEventCallback;
  249. }
  250. /// <summary>
  251. /// 进行手柄配对
  252. /// </summary>
  253. /// <param name="controllerId">0左手柄,1右手柄</param>
  254. /// <returns></returns>
  255. public bool BindHandle(int controllerId)
  256. {
  257. // 先解绑,再配对
  258. NativeAPI.ClearBond(controllerId);
  259. // 返回0是执行正常,成功与否要从回调中获得
  260. return NativeAPI.bondController(controllerId) >= 0;
  261. }
  262. /// <summary>
  263. /// 取消手柄配对
  264. /// </summary>
  265. /// <param name="controllerId">0左手柄,1右手柄</param>
  266. /// <returns></returns>
  267. public bool UnbindHandle(int controllerId)
  268. {
  269. // 返回0是执行正常,成功与否要从回调中获得
  270. return NativeAPI.ClearBond(controllerId) >= 0;
  271. }
  272. /// <summary>
  273. /// 获取绑定状态
  274. /// </summary>
  275. /// <param name="handType">指定手柄</param>
  276. /// <returns></returns>
  277. public bool GetBindState(HandType handType)
  278. {
  279. int value = NativeAPI.GetControllerBondState();
  280. if (handType == HandType.Left)
  281. {
  282. if ((value & 0x0F) == 0x00) return false; // unbind
  283. if ((value & 0x0F) == 0x04) return true; // bound
  284. }
  285. else
  286. {
  287. if ((value & 0xF0) == 0x00) return false; // unbind
  288. if ((value & 0xF0) == 0x40) return true; // bound
  289. }
  290. Debug.Log($"HandleControllerSession, GetBindState Exception: {value}");
  291. return false;
  292. }
  293. /// <summary>
  294. /// 获取连接状态
  295. /// </summary>
  296. /// <param name="controllerId">0左手柄,1右手柄</param>
  297. /// <returns></returns>
  298. public bool GetConnectState(int controllerId)
  299. {
  300. // 返回-1或0是未连接,返回1是已连接
  301. return NativeAPI.GetControllerConnectState(controllerId) > 0;
  302. }
  303. /// <summary>
  304. /// 获取手柄电量
  305. /// </summary>
  306. /// <param name="controllerId">0左手柄,1右手柄</param>
  307. /// <returns></returns>
  308. public float GetPowerStats(int controllerId)
  309. {
  310. // 电量0到6,转换成百分比
  311. return NativeAPI.GetControllerBattery(controllerId) / 6.0f; // 满电量6
  312. }
  313. /// <summary>
  314. /// 进行手柄震动
  315. /// </summary>
  316. /// <param name="controllerId">0左手柄,1右手柄</param>
  317. /// <param name="level">震动等级 1-8</param>
  318. /// <param name="time">震动时长 0-65535 ms</param>
  319. public bool VibrateHandle(int controllerId, int level, int time)
  320. {
  321. // 返回0是执行正常,没有回调
  322. return NativeAPI.vibrateController(controllerId, level, time) >= 0;
  323. }
  324. /// <summary>
  325. /// 切换控制器类型
  326. /// </summary>
  327. /// <param name="type">0头控,1手势,2手柄</param>
  328. public bool ChangeControllerType(int type)
  329. {
  330. if (SessionManager.Instance && SessionManager.Instance.IsInited)
  331. {
  332. NativeAPI.changeControllerType(type);
  333. return true;
  334. }
  335. return false;
  336. }
  337. /// <summary>
  338. /// 设置动态切换模式
  339. /// </summary>
  340. /// <param name="type">0关闭,1开启</param>
  341. //public void ChangeControllerDynamicMode(bool status)
  342. //{
  343. // Debug.Log($"HandleControllerSession, changeControllerDynamicMode: {status}");
  344. // NativeAPI.changeControllerDynamicMode(status ? 1 : 0);
  345. //}
  346. /// <summary>
  347. /// 获取动态切换模式(用于跨应用)
  348. /// </summary>
  349. /// <returns>0关闭,1开启,-1未定义</returns>
  350. //public int GetCurrentDynamicMode()
  351. //{
  352. // return NativeAPI.getCurrentDynamicMode();
  353. //}
  354. /// <summary>
  355. /// 获取当前控制器类型(用于跨应用)
  356. /// </summary>
  357. /// <returns></returns>
  358. //public int GetCurrentControllerType()
  359. //{
  360. // return NativeAPI.getCurrentHandType();
  361. //}
  362. /// <summary>
  363. /// 获取手柄设备SN号
  364. /// </summary>
  365. /// <param name="controllerId">0左手柄,1右手柄</param>
  366. /// <returns></returns>
  367. public string GetControllerDeviceSN(int controllerId)
  368. {
  369. string sn = Marshal.PtrToStringAnsi(NativeAPI.getControllerMac(controllerId));
  370. Debug.Log($"HandleControllerSession, GetControllerDeviceSN: {sn}");
  371. return sn;
  372. }
  373. /// <summary>
  374. /// 获取手柄固件版本号
  375. /// </summary>
  376. /// <param name="controllerId">0左手柄,1右手柄</param>
  377. /// <returns></returns>
  378. public int GetOTAVersion(int controllerId)
  379. {
  380. return NativeAPI.ControllerClient_GetCtrlVersion(controllerId);
  381. }
  382. /// <summary>
  383. /// 获取手柄Host固件版本号
  384. /// </summary>
  385. /// <returns></returns>
  386. public int GetHostOTAVersion()
  387. {
  388. return NativeAPI.ControllerClient_GetVersion();
  389. }
  390. /// <summary>
  391. /// 升级手柄固件
  392. /// </summary>
  393. /// <param name="controllerId">0左手柄,1右手柄</param>
  394. /// <param name="package">固件路径</param>
  395. /// <returns></returns>
  396. public bool UpgradeOTA(int controllerId, string package)
  397. {
  398. //return NativeAPI.updateHxrct(package, controllerId) == 1;
  399. Debug.Log($"HandleControllerSession, UpgradeOTA: controllerId = {controllerId}, package = {package}," +
  400. $"result = {NativeAPI.updateHxrct(package, controllerId)}");
  401. return true;
  402. }
  403. /// <summary>
  404. /// 升级手柄Host固件
  405. /// </summary>
  406. /// <param name="package">固件路径</param>
  407. /// <returns></returns>
  408. public bool UpgradeHostOTA(string package)
  409. {
  410. //return NativeAPI.ControllerHxrUpgradeHost(package) == 1;
  411. Debug.Log($"HandleControllerSession, UpgradeHostOTA: package = {package}," +
  412. $"result = {NativeAPI.ControllerHxrUpgradeHost(package)}");
  413. return true;
  414. }
  415. /// <summary>
  416. /// 更新手柄位姿状态
  417. /// </summary>
  418. public void UpdateHandlePose()
  419. {
  420. if (ARFrame.SessionStatus == EZVIOState.EZVIOCameraState_Tracking)
  421. {
  422. // 适配0dof、3dof、6dof
  423. var translation = HMDPoseTracker.Instance.transform.position;
  424. var quaternion = HMDPoseTracker.Instance.transform.rotation;
  425. // 适配空间计算
  426. if (HMDPoseTracker.Instance.UseLocalPose)
  427. {
  428. translation = HMDPoseTracker.Instance.transform.localPosition;
  429. quaternion = HMDPoseTracker.Instance.transform.localRotation;
  430. }
  431. var originHeadPose = new float[]
  432. {
  433. translation.x, translation.y,translation.z,
  434. quaternion.x, quaternion.y, quaternion.z, quaternion.w
  435. };
  436. //var originHeadPose = new float[7];
  437. //Array.Copy(ARFrame.OriginVIOResult.translation, 0, originHeadPose, 0, 3);
  438. //Array.Copy(ARFrame.OriginVIOResult.quaternion, 0, originHeadPose, 3, 4);
  439. ////Debug.Log($"HandControllerSession, feed originHeadPose: {originHeadPose[0]},{originHeadPose[1]},{originHeadPose[2]},{originHeadPose[3]},{originHeadPose[4]},{originHeadPose[5]},{originHeadPose[6]}");
  440. float left_is_3dof = controllerLeftResult.is_3dof, right_is_3dof = controllerRightResult.is_3dof;
  441. if (NativeAPI.getControllerResultWithHead(ref controllerLeftResult, ref controllerRightResult, originHeadPose, originHeadPose.Length))
  442. {
  443. #if UNITY_EDITOR
  444. var left_position = Vector3.zero;
  445. var left_rotation = Quaternion.identity;
  446. var right_position = Vector3.zero;
  447. var right_rotation = Quaternion.identity;
  448. #else
  449. var left_position = new Vector3(controllerLeftResult.pose[0], controllerLeftResult.pose[1], controllerLeftResult.pose[2]);
  450. var left_rotation = new Quaternion(controllerLeftResult.pose[4], controllerLeftResult.pose[5], controllerLeftResult.pose[6], controllerLeftResult.pose[3]);
  451. var right_position = new Vector3(controllerRightResult.pose[0], controllerRightResult.pose[1], controllerRightResult.pose[2]);
  452. var right_rotation = new Quaternion(controllerRightResult.pose[4], controllerRightResult.pose[5], controllerRightResult.pose[6], controllerRightResult.pose[3]);
  453. #endif
  454. controllerPose_left.position = left_position;
  455. controllerPose_left.rotation = left_rotation;
  456. //ConversionUtility.RecenterByOffset(controllerPose_left, ARFrame.accumulatedRecenterOffset4x4, ref controllerPose_left);
  457. //leftController.SetPositionAndRotation(controllerPose_left.position, controllerPose_left.rotation);
  458. controllerPose_right.position = right_position;
  459. controllerPose_right.rotation = right_rotation;
  460. //ConversionUtility.RecenterByOffset(controllerPose_right, ARFrame.accumulatedRecenterOffset4x4, ref controllerPose_right);
  461. //rightController.SetPositionAndRotation(controllerPose_right.position, controllerPose_right.rotation);
  462. if (left_is_3dof != controllerLeftResult.is_3dof)
  463. {
  464. Debug.Log($"HandleControllerSession, controllerLeftResult is_3dof changed to: {controllerLeftResult.is_3dof}");
  465. trackingStateEventCallback?.Invoke(HandType.Left, (int)controllerLeftResult.is_3dof);
  466. }
  467. if (right_is_3dof != controllerRightResult.is_3dof)
  468. {
  469. Debug.Log($"HandleControllerSession, controllerRightResult is_3dof changed to: {controllerRightResult.is_3dof}");
  470. trackingStateEventCallback?.Invoke(HandType.Right, (int)controllerRightResult.is_3dof);
  471. }
  472. }
  473. }
  474. }
  475. /// <summary>
  476. /// 通过Update将多线程或异步回调转化到主线程中处理
  477. /// </summary>
  478. public void UpdateHandleCallback()
  479. {
  480. if (bindEventUserData.changed)
  481. {
  482. bindEventUserData.changed = false;
  483. bool status = false;
  484. bool valid_left = false;
  485. int value = bindEventUserData.status;
  486. if ((value & 0x0F) == 0x00) { valid_left = true; status = false; }
  487. if ((value & 0x0F) == 0x04) { valid_left = true; status = true; }
  488. Debug.Log($"HandleControllerSession, UpdateHandleCallback bindingEventCallback: handType = Left, valid = {valid_left}, status = {status}");
  489. if (valid_left) HandleControllerSession.Instance.bindingEventCallback(HandType.Left, status);
  490. bool valid_right = false;
  491. if ((value & 0xF0) == 0x00) { valid_right = true; status = false; }
  492. if ((value & 0xF0) == 0x40) { valid_right = true; status = true; }
  493. Debug.Log($"HandleControllerSession, UpdateHandleCallback bindingEventCallback: handType = Right, valid = {valid_right}, status = {status}");
  494. if (valid_right) HandleControllerSession.Instance.bindingEventCallback(HandType.Right, status);
  495. }
  496. if (connectEventUserData.changed)
  497. {
  498. connectEventUserData.changed = false;
  499. var connected = connectEventUserData.connected;
  500. var controllerId = connectEventUserData.controllerId;
  501. Debug.Log($"HandleControllerSession, UpdateHandleCallback connectEventCallback: connected = {connected}, controllerId = {controllerId}");
  502. HandleControllerSession.Instance.connectEventCallback((HandType)controllerId, connected);
  503. }
  504. if (buttonEventUserData.changed)
  505. {
  506. buttonEventUserData.changed = false;
  507. var controllerId = buttonEventUserData.controllerId;
  508. var keycode = buttonEventUserData.keycode;
  509. var state = buttonEventUserData.state;
  510. //Debug.Log($"HandleControllerSession, UpdateHandleCallback buttonEventCallback: keycode = {keycode}, state = {state}, controllerId = {controllerId}");
  511. HandleControllerSession.Instance.buttonEventCallback((HandType)controllerId, (HandleKeyCode)keycode, state > 0);
  512. }
  513. if (axis2DEventUserData.changed)
  514. {
  515. axis2DEventUserData.changed = false;
  516. var x = axis2DEventUserData.x;
  517. var y = axis2DEventUserData.y;
  518. var controllerId = axis2DEventUserData.controllerId;
  519. //Debug.Log($"HandleControllerSession, UpdateHandleCallback axis2DEventCallback: x = {x}, y = {y}, controllerId = {controllerId}");
  520. float axis_x = x < 8 ? Mathf.Lerp(-1, 0, x / 8.0f) : Mathf.Lerp(0, 1, (x - 8) / (15.0f - 8.0f));
  521. float axis_y = y < 8 ? Mathf.Lerp(-1, 0, y / 8.0f) : Mathf.Lerp(0, 1, (y - 8) / (15.0f - 8.0f));
  522. HandleControllerSession.Instance.axis2DEventCallback((HandType)controllerId, new Vector2(axis_x, axis_y));
  523. }
  524. if (holdEventUserData.changed)
  525. {
  526. holdEventUserData.changed = false;
  527. var keycode = holdEventUserData.keycode;
  528. var isHeld = holdEventUserData.isHeld;
  529. var controllerId = holdEventUserData.controllerId;
  530. //Debug.Log($"HandleControllerSession, UpdateHandleCallback holdEventCallback: keycode = {keycode}, hold = {isHeld}, controllerId = {controllerId}");
  531. HandleControllerSession.Instance.holdEventCallback((HandType)controllerId, (HandleKeyCode)keycode, isHeld);
  532. }
  533. if (silenceEventUserData.changed)
  534. {
  535. silenceEventUserData.changed = false;
  536. var sleepless = silenceEventUserData.sleepless;
  537. var controllerId = silenceEventUserData.controllerId;
  538. //Debug.Log($"HandleControllerSession, UpdateHandleCallback silenceEventCallback: silent = {!sleepless}, controllerId = {controllerId}");
  539. HandleControllerSession.Instance.silenceEventCallback((HandType)controllerId, !sleepless);
  540. }
  541. if (hxrctUpdateUserData.changed)
  542. {
  543. hxrctUpdateUserData.changed = false;
  544. var status = hxrctUpdateUserData.status;
  545. var controllerId = hxrctUpdateUserData.controllerId;
  546. Debug.Log($"HandleControllerSession, UpdateHandleCallback upgradeOTAEventCallback: status = {status}, controllerId = {controllerId}");
  547. HandleControllerSession.Instance.upgradeOTAEventCallback((HandType)controllerId, status == 0);
  548. }
  549. if (serverReconnectedUserData.changed)
  550. {
  551. serverReconnectedUserData.changed = false;
  552. Debug.Log($"HandleControllerSession, UpdateHandleCallback serverReconnectEventCallback: connected = {serverReconnectedUserData.connected}");
  553. HandleControllerSession.Instance.serverReconnectEventCallback(serverReconnectedUserData.connected);
  554. }
  555. }
  556. //手柄绑定状态回调
  557. [MonoPInvokeCallback(typeof(ControllerBindEventCallback))]
  558. private static void OnControllerBindEventCallback(int value)
  559. {
  560. //if (isPaused) return;
  561. bindEventUserData.changed = true;
  562. bindEventUserData.status = value;
  563. //Debug.Log($"HandleControllerSession, OnControllerBindEventCallback bindEventUserData: changed = {bindEventUserData.changed}, status = {bindEventUserData.status}");
  564. }
  565. //手柄连接状态回调 左手0 右手1
  566. [MonoPInvokeCallback(typeof(ControllerConnectEventCallback))]
  567. private static void OnControllerConnectEventCallback(bool connected, int controllerId)
  568. {
  569. //if (isPaused) return;
  570. connectEventUserData.changed = true;
  571. connectEventUserData.connected = connected;
  572. connectEventUserData.controllerId = controllerId;
  573. //Debug.Log($"HandleControllerSession, OnControllerConnectEventCallback connectEventUserData: changed = {connectEventUserData.changed}, connected = {connectEventUserData.connected}, controllerId = {connectEventUserData.controllerId}");
  574. }
  575. //手柄按键事件回调
  576. //controllerId 0左手柄,1右手柄
  577. //state 1按下,0弹起
  578. //keycode 300返回,301主页,302BY键,303AX键,304摇杆键,305扳机键,306Grid键
  579. [MonoPInvokeCallback(typeof(ControllerButtonEventCallback))]
  580. private static void OnControllerButtonEventCallback(int keycode, int state, int controllerId)
  581. {
  582. //if (isPaused) return;
  583. buttonEventUserData.changed = true;
  584. buttonEventUserData.keycode = keycode;
  585. buttonEventUserData.state = state;
  586. buttonEventUserData.controllerId = controllerId;
  587. //Debug.Log($"HandleControllerSession, OnControllerButtonEventCallback buttonEventUserData: changed = {buttonEventUserData.changed}, keycode = {buttonEventUserData.keycode}, state = {buttonEventUserData.state}, controllerId = {buttonEventUserData.controllerId}");
  588. }
  589. //手柄摇杆事件回调
  590. //controllerId 0左手柄,1右手柄
  591. //xy整型,左下角[0,0],中间[8,8],右上角[15,15]
  592. [MonoPInvokeCallback(typeof(ControllerAxis2DEventCallback))]
  593. private static void OnControllerAxis2DEventCallback(int x, int y, int controllerId)
  594. {
  595. //if (isPaused) return;
  596. axis2DEventUserData.changed = true;
  597. axis2DEventUserData.x = x;
  598. axis2DEventUserData.y = y;
  599. axis2DEventUserData.controllerId = controllerId;
  600. //Debug.Log($"HandleControllerSession, OnControllerAxis2DEventCallback axis2DEventUserData: changed = {axis2DEventUserData.changed}, x = {axis2DEventUserData.x}, y = {axis2DEventUserData.y}, controllerId = {axis2DEventUserData.controllerId}");
  601. }
  602. //手柄握持事件回调
  603. //原理是按键的触摸反馈
  604. //controllerId 0左手柄,1右手柄
  605. //keycode 302BY键,303AX键,304摇杆键,305扳机键
  606. [MonoPInvokeCallback(typeof(ControllerAxis2DEventCallback))]
  607. private static void OnControllerHoldEventCallback(int keycode, bool isHeld, int controllerId)
  608. {
  609. //if (isPaused) return;
  610. holdEventUserData.changed = true;
  611. holdEventUserData.keycode = keycode;
  612. holdEventUserData.isHeld = isHeld;
  613. holdEventUserData.controllerId = controllerId;
  614. //Debug.Log($"HandleControllerSession, OnControllerHoldEventCallback holdEventUserData: changed = {holdEventUserData.changed}, keycode = {holdEventUserData.keycode}, isHeld = {holdEventUserData.isHeld}, controllerId = {holdEventUserData.controllerId}");
  615. }
  616. //手柄静置事件回调
  617. //原理是监测IMU是否静止
  618. //controllerId 0左手柄,1右手柄
  619. [MonoPInvokeCallback(typeof(ControllerSilenceEventCallback))]
  620. private static void OnControllerSilenceEventCallback(bool sleepless, int controllerId)
  621. {
  622. //if (isPaused) return;
  623. silenceEventUserData.changed = true;
  624. silenceEventUserData.sleepless = sleepless;
  625. silenceEventUserData.controllerId = controllerId;
  626. //Debug.Log($"HandleControllerSession, OnControllerSilenceEventCallback silenceEventUserData: changed = {silenceEventUserData.changed}, sleepless = {silenceEventUserData.sleepless}, controllerId = {silenceEventUserData.controllerId}");
  627. }
  628. //手柄Client固件更新回调
  629. //stauts 0成功
  630. //controllerId 0左手柄,1右手柄
  631. [MonoPInvokeCallback(typeof(ControllerHxrctUpdateCallback))]
  632. private static void OnControllerHxrctUpdateCallback(int status, int controllerId)
  633. {
  634. //if (isPaused) return;
  635. hxrctUpdateUserData.changed = true;
  636. hxrctUpdateUserData.status = status;
  637. hxrctUpdateUserData.controllerId = controllerId;
  638. Debug.Log($"HandleControllerSession, OnControllerHxrctUpdateCallback hxrctUpdateUserData: changed = {hxrctUpdateUserData.changed}, status = {hxrctUpdateUserData.status}, controllerId = {hxrctUpdateUserData.controllerId}");
  639. }
  640. //控制器dynamicMode状态更新回调
  641. //status 0关闭,1开启,-1未定义
  642. //[MonoPInvokeCallback(typeof(ControllerDynamicModeChangedCallback))]
  643. //private static void OnControllerDynamicModeChangedCallback(int status)
  644. //{
  645. // Debug.Log($"HandleControllerSession, OnControllerDynamicModeChangedCallback: status = {status}");
  646. // if (status >= 0 && HandleControllerSession.Instance.dynamicModeChangedCallBack != null)
  647. // {
  648. // HandleControllerSession.Instance.dynamicModeChangedCallBack(status > 0);
  649. // }
  650. //}
  651. //手柄Server重连状态回调
  652. //connected true已重连
  653. [MonoPInvokeCallback(typeof(Action<bool>))]
  654. private static void OnControllerServerReconnectedCallback(bool connected)
  655. {
  656. //if (isPaused) return;
  657. serverReconnectedUserData.changed = true;
  658. serverReconnectedUserData.connected = connected;
  659. Debug.Log($"HandleControllerSession, OnControllerServerReconnectedCallback serverReconnectedUserData: changed = {serverReconnectedUserData.changed}, connected = {serverReconnectedUserData.connected}");
  660. }
  661. private partial struct NativeAPI
  662. {
  663. #if !UNITY_EDITOR
  664. [DllImport(NativeConsts.NativeLibrary)]
  665. public static extern bool getControllerResultWithHead(ref HandControllerResult controllerLeftResult, ref HandControllerResult controllerRightResult, float[] headpose, int headposeSize);
  666. //注册手柄按键事件回调
  667. [DllImport(NativeConsts.NativeLibrary)]
  668. public static extern void registerControllerTouchEvent(ControllerButtonEventCallback callback);
  669. //注册手柄摇杆事件回调
  670. [DllImport(NativeConsts.NativeLibrary)]
  671. public static extern void registerControllerRockerTouchEvent(ControllerAxis2DEventCallback callback);
  672. //注册手柄握持事件回调
  673. [DllImport(NativeConsts.NativeLibrary)]
  674. public static extern void registerControllerTouchChangeEventCallback(ControllerHoldEventCallback callback);
  675. //注册手柄绑定状态回调
  676. [DllImport(NativeConsts.NativeLibrary)]
  677. public static extern void registerControllerBondChangeCallback(ControllerBindEventCallback callback);
  678. //注册手柄连接状态回调
  679. [DllImport(NativeConsts.NativeLibrary)]
  680. public static extern void registerControllerConnectEventCallback(ControllerConnectEventCallback callback);
  681. //注册手柄静置状态回调
  682. [DllImport(NativeConsts.NativeLibrary)]
  683. public static extern void registerControllerSleepChangeCallback(ControllerSilenceEventCallback callback);
  684. //注册手柄(手柄Client)固件更新状态回调
  685. [DllImport(NativeConsts.NativeLibrary)]
  686. public static extern void registerControllerHxrctUpdateCallback(ControllerHxrctUpdateCallback callback);
  687. ////注册手柄Server重连状态回调
  688. //[DllImport(NativeConsts.NativeLibrary)]
  689. //public static extern void registerConnectChangedCallback(ControllerServerReconnectedCallback callback);
  690. //进行手柄配对:左手0 右手1 (手柄同时按住扳机键和返回键)
  691. [DllImport(NativeConsts.NativeLibrary)]
  692. public static extern int bondController(int controllerId);
  693. //取消手柄配对:左手0 右手1
  694. [DllImport(NativeConsts.NativeLibrary)]
  695. public static extern int ClearBond(int controllerId);
  696. //获取手柄绑定状态
  697. [DllImport(NativeConsts.NativeLibrary)]
  698. public static extern int GetControllerBondState();
  699. //获取手柄连接状态:1连接,0未连接
  700. [DllImport(NativeConsts.NativeLibrary)]
  701. public static extern int GetControllerConnectState(int controllerId);
  702. //获取手柄电量,[0, 6]
  703. [DllImport(NativeConsts.NativeLibrary)]
  704. public static extern int GetControllerBattery(int controllerId);
  705. //进行手柄震动
  706. //level 震动等级 1-8
  707. //time 震动时长 0-65535 ms
  708. [DllImport(NativeConsts.NativeLibrary)]
  709. public static extern int vibrateController(int controllerId, int level, int time);
  710. //控制器类型切换时发送通知
  711. //type 0-头控,1-手势,2-手柄
  712. [DllImport(NativeConsts.NativeLibrary)]
  713. public static extern void changeControllerType(int type);
  714. //获取当前控制器类型切换到的类型
  715. //type 0-头控,1-手势,2-手柄 (default为-1)
  716. [DllImport(NativeConsts.NativeLibrary)]
  717. public static extern int getCurrentHandType();
  718. // 获取手柄序列号
  719. [DllImport(NativeConsts.NativeLibrary)]
  720. public static extern IntPtr getControllerMac(int controllerId);
  721. //获取眼镜(手柄Host)Nordic固件版本
  722. [DllImport(NativeConsts.NativeLibrary)]
  723. public static extern int ControllerClient_GetVersion();
  724. //获取手柄(手柄Client)固件版本
  725. [DllImport(NativeConsts.NativeLibrary)]
  726. public static extern int ControllerClient_GetCtrlVersion(int controllerId);
  727. //眼镜(手柄Host)Nordic固件更新
  728. [DllImport(NativeConsts.NativeLibrary)]
  729. public static extern int ControllerHxrUpgradeHost(string path);
  730. //手柄(手柄Client)固件更新
  731. [DllImport(NativeConsts.NativeLibrary)]
  732. public static extern int updateHxrct(string path, int controllerId);
  733. ////注册控制器dynamicMode状态更新回调
  734. //[DllImport(NativeConsts.NativeLibrary)]
  735. //public static extern void registerControllerDynamicModeChangedCallback(ControllerDynamicModeChangedCallback callback);
  736. ////设置控制器dynamicMode状态
  737. //[DllImport(NativeConsts.NativeLibrary)]
  738. //public static extern void changeControllerDynamicMode(int mode);
  739. ////获取控制器dynamicMode状态
  740. //[DllImport(NativeConsts.NativeLibrary)]
  741. //public static extern int getCurrentDynamicMode();
  742. #else
  743. public static bool getControllerResultWithHead(ref HandControllerResult controllerLeftResult, ref HandControllerResult controllerRightResult, float[] headpose, int headposeSize)
  744. {
  745. return false;
  746. }
  747. //注册手柄按键事件回调
  748. public static void registerControllerTouchEvent(ControllerButtonEventCallback callback) { }
  749. //注册手柄摇杆事件回调
  750. public static void registerControllerRockerTouchEvent(ControllerAxis2DEventCallback callback) { }
  751. //注册手柄握持事件回调
  752. public static void registerControllerTouchChangeEventCallback(ControllerHoldEventCallback callback) { }
  753. //注册手柄绑定状态回调
  754. public static void registerControllerBondChangeCallback(ControllerBindEventCallback callback) { }
  755. //注册手柄连接状态回调
  756. public static void registerControllerConnectEventCallback(ControllerConnectEventCallback callback) { }
  757. //注册手柄静置状态回调
  758. public static void registerControllerSleepChangeCallback(ControllerSilenceEventCallback callback) { }
  759. ////注册手柄Server重连状态回调
  760. //public static void registerConnectChangedCallback(ControllerServerReconnectedCallback callback) { }
  761. //进行手柄配对:左手0 右手1 (手柄同时按住扳机键和返回键)
  762. public static int bondController(int controllerId)
  763. {
  764. return -1;
  765. }
  766. //取消手柄配对:左手0 右手1
  767. public static int ClearBond(int controllerId)
  768. {
  769. return -1;
  770. }
  771. //获取手柄绑定状态
  772. public static int GetControllerBondState()
  773. {
  774. return -1;
  775. }
  776. //获取手柄连接状态:1连接,0未连接
  777. public static int GetControllerConnectState(int controllerId)
  778. {
  779. return -1;
  780. }
  781. //获取手柄电量,[0, 6]
  782. public static int GetControllerBattery(int controllerId)
  783. {
  784. return -1;
  785. }
  786. //进行手柄震动
  787. //level 震动等级 1-8
  788. //time 震动时长 0-65535 ms
  789. public static int vibrateController(int controllerId, int level, int time)
  790. {
  791. return -1;
  792. }
  793. //控制器类型切换时发送通知
  794. //type 0-头控,1-手势,2-手柄
  795. public static void changeControllerType(int type)
  796. {
  797. }
  798. //获取当前控制器类型切换到的类型
  799. //type 0-头控,1-手势,2-手柄 (default为-1)
  800. public static int getCurrentHandType()
  801. {
  802. return -1;
  803. }
  804. // 获取手柄序列号
  805. public static IntPtr getControllerMac(int controllerId)
  806. {
  807. return IntPtr.Zero;
  808. }
  809. //获取眼镜(手柄Host)Nordic固件版本
  810. public static int ControllerClient_GetVersion()
  811. {
  812. return -1;
  813. }
  814. //获取手柄(手柄Client)固件版本
  815. public static int ControllerClient_GetCtrlVersion(int controllerId)
  816. {
  817. return -1;
  818. }
  819. //眼镜(手柄Host)Nordic固件更新
  820. public static int ControllerHxrUpgradeHost(string path)
  821. {
  822. return -1;
  823. }
  824. //手柄(手柄Client)固件更新
  825. public static int updateHxrct(string path, int controllerId)
  826. {
  827. return -1;
  828. }
  829. //注册手柄(手柄Client)固件更新状态回调
  830. public static void registerControllerHxrctUpdateCallback(ControllerHxrctUpdateCallback callback) { }
  831. ////注册控制器dynamicMode状态更新回调
  832. //public static void registerControllerDynamicModeChangedCallback(ControllerDynamicModeChangedCallback callback) { }
  833. ////设置控制器dynamicMode状态
  834. //public static void changeControllerDynamicMode(int mode)
  835. //{
  836. //}
  837. ////获取控制器dynamicMode状态
  838. //public static int getCurrentDynamicMode()
  839. //{
  840. // return -1;
  841. //}
  842. #endif
  843. }
  844. }
  845. }