星火微课系统客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuoBoPenUtil.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using Common.system;
  2. using RobotpenGateway;
  3. using System;
  4. using System.Threading;
  5. using XHWK.WKTool.Utils.pen;
  6. namespace XHWK.WKTool.Utils.luobo
  7. {
  8. internal class LuoBoPenUtil
  9. {
  10. #region 罗博智慧笔
  11. #region 值初始化
  12. private PenEventI penEvent = null;
  13. [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  14. public static extern void OutputDebugString(string message);
  15. public robotpenController.returnPointData Date { get => date; set => date = value; }
  16. public bool usbIsConnected = false;
  17. private eDeviceType deviceType = eDeviceType.Unknow;
  18. private int m_nDeviceW = 22600;
  19. private int m_nDeviceH = 16650;
  20. private robotpenController.returnPointData date = null;
  21. public bool isOpen = true;
  22. /// <summary>
  23. /// 是否为笔模式
  24. /// </summary>
  25. private bool IsPen = false;
  26. #endregion 值初始化
  27. public LuoBoPenUtil(PenEventI penEvent)
  28. {
  29. this.penEvent = penEvent;
  30. }
  31. /// <summary>
  32. /// 笔模式
  33. /// </summary>
  34. public void lb_device_hand()
  35. {
  36. IsPen = true;
  37. if (deviceType == eDeviceType.T8B_D2 || deviceType == eDeviceType.T8Y || deviceType == eDeviceType.T8S_LQ || deviceType == eDeviceType.T8C)
  38. {
  39. robotpenController.GetInstance().setDeviceMode(eDeviceMode.DEVICE_HAND);
  40. }
  41. else
  42. {
  43. //获取当前设备模式,根据模式切换鼠标和笔
  44. robotpenController.GetInstance()._Send(cmdId.SearchMode);
  45. }
  46. }
  47. /// <summary>
  48. /// 鼠标模式
  49. /// </summary>
  50. public void lb_device_mouse()
  51. {
  52. IsPen = false;
  53. if (deviceType == eDeviceType.T8B_D2 || deviceType == eDeviceType.T8Y || deviceType == eDeviceType.T8S_LQ || deviceType == eDeviceType.T8C)
  54. {
  55. robotpenController.GetInstance().setDeviceMode(eDeviceMode.DEVICE_MOUSE);
  56. }
  57. else
  58. {
  59. //获取当前设备模式,根据模式切换鼠标和笔
  60. robotpenController.GetInstance()._Send(cmdId.SearchMode);
  61. }
  62. }
  63. private void luobo_searchModeEvt(int modeType)
  64. {
  65. if (App.IsOutputInfoLog)
  66. {
  67. if (modeType == 1)
  68. {
  69. LogHelper.Loginfo.Info("当前为笔模式");
  70. }
  71. else
  72. {
  73. LogHelper.Loginfo.Info("当前为鼠标模式");
  74. }
  75. }
  76. }
  77. // 子节点设备状态改变事件
  78. private void luobo_nodeStatusEvt(NODE_STATUS ns)
  79. {
  80. string strStatus = string.Empty;
  81. switch (ns)
  82. {
  83. case NODE_STATUS.DEVICE_POWER_OFF:
  84. {
  85. strStatus = "DEVICE_POWER_OFF";
  86. }
  87. break;
  88. case NODE_STATUS.DEVICE_STANDBY:
  89. {
  90. strStatus = "DEVICE_STANDBY";
  91. }
  92. break;
  93. case NODE_STATUS.DEVICE_INIT_BTN:
  94. {
  95. strStatus = "DEVICE_INIT_BTN";
  96. }
  97. break;
  98. case NODE_STATUS.DEVICE_OFFLINE:
  99. {
  100. strStatus = "DEVICE_OFFLINE";
  101. }
  102. break;
  103. case NODE_STATUS.DEVICE_ACTIVE:
  104. {
  105. strStatus = "DEVICE_ACTIVE";
  106. //deviceHand();
  107. }
  108. break;
  109. case NODE_STATUS.DEVICE_LOW_POWER_ACTIVE:
  110. {
  111. strStatus = "DEVICE_LOW_POWER_ACTIVE";
  112. }
  113. break;
  114. case NODE_STATUS.DEVICE_OTA_MODE:
  115. {
  116. strStatus = "DEVICE_OTA_MODE";
  117. }
  118. break;
  119. case NODE_STATUS.DEVICE_OTA_WAIT_SWITCH:
  120. {
  121. strStatus = "DEVICE_OTA_WAIT_SWITCH";
  122. }
  123. break;
  124. case NODE_STATUS.DEVICE_DFU_MODE:
  125. {
  126. strStatus = "DEVICE_DFU_MODE";
  127. }
  128. break;
  129. case NODE_STATUS.DEVICE_TRYING_POWER_OFF:
  130. {
  131. strStatus = "DEVICE_TRYING_POWER_OFF";
  132. }
  133. break;
  134. case NODE_STATUS.DEVICE_FINISHED_PRODUCT_TEST:
  135. {
  136. strStatus = "DEVICE_FINISHED_PRODUCT_TEST";
  137. }
  138. break;
  139. case NODE_STATUS.DEVICE_SYNC_MODE:
  140. {
  141. strStatus = "DEVICE_SYNC_MODE";
  142. }
  143. break;
  144. default:
  145. {
  146. strStatus = "UNKNOW";
  147. }
  148. break;
  149. }
  150. }
  151. private void luobo_switchModeEvt(int modeType)
  152. {
  153. if (modeType == 1)
  154. {
  155. if (App.IsOutputInfoLog)
  156. {
  157. LogHelper.Loginfo.Info("当前为笔模式");
  158. }
  159. if (!IsPen)
  160. {
  161. lb_device_mouse();
  162. }
  163. }
  164. else
  165. {
  166. if (App.IsOutputInfoLog)
  167. {
  168. LogHelper.Loginfo.Info("当前为鼠标模式");
  169. }
  170. if (IsPen)
  171. {
  172. lb_device_hand();
  173. }
  174. }
  175. }
  176. //初始化笔服务
  177. public void InitlbPen()
  178. {
  179. try
  180. {
  181. robotpenController.GetInstance()._ConnectInitialize(eDeviceType.Gateway, IntPtr.Zero);
  182. robotpenController.GetInstance().deviceChangeEvt += new robotpenController.DeviceChange(luobo_deviceChangeEvt);
  183. robotpenController.GetInstance().searchModeEvt += luobo_searchModeEvt;
  184. robotpenController.GetInstance().nodeStatusEvt += luobo_nodeStatusEvt;
  185. robotpenController.GetInstance().switchModeEvt += luobo_switchModeEvt;
  186. robotpenController.GetInstance().keyPressEvt += new robotpenController.KeyPress(luobo_keyPressEvt);
  187. robotpenController.returnPointData datad = new robotpenController.returnPointData(luobo_bigDataReportEvt1);
  188. robotpenController.GetInstance().initDeletgate(ref datad);
  189. CheckUsbConnect();
  190. }
  191. catch (Exception ex)
  192. {
  193. LogHelper.Logerror.Error("设备初始化失败:" + ex.Message, ex);
  194. }
  195. }
  196. /// <summary>
  197. /// 设备插拔消息,更新listview
  198. /// </summary>
  199. /// <param name="bStatus">
  200. /// </param>
  201. /// <param name="uPid">
  202. /// </param>
  203. private void luobo_deviceChangeEvt(bool bStatus, ushort uPid)
  204. {
  205. //throw new NotImplementedException();
  206. Console.WriteLine(@"luobo_deviceChangeEvt");
  207. try
  208. {
  209. if (App.IsOutputInfoLog)
  210. {
  211. LogHelper.Loginfo.Info
  212. (
  213. string.Format
  214. (
  215. "设备状态{0} PID = {1}",
  216. bStatus,
  217. uPid
  218. )
  219. );
  220. }
  221. }
  222. catch (Exception)
  223. {
  224. }
  225. CheckUsbConnect();
  226. }
  227. /// <summary>
  228. /// 书写
  229. /// </summary>
  230. /// ///
  231. /// <param name="bIndex">
  232. /// </param>
  233. /// <param name="bPenStatus">
  234. /// </param>
  235. /// <param name="bx">
  236. /// </param>
  237. /// <param name="by">
  238. /// </param>
  239. /// <param name="bPress">
  240. /// </param>
  241. private void luobo_bigDataReportEvt1(byte bIndex, byte bPenStatus, short bx, short by, short bPress)
  242. {
  243. if (bx == 0 && by == 0 && bPenStatus == 0 && bPress == 0)
  244. {
  245. return;
  246. }
  247. if (penEvent != null)
  248. {
  249. penEvent.penPointEvent(bIndex, bPenStatus, bx, by, bPress, m_nDeviceW, m_nDeviceH);
  250. }
  251. }
  252. /// <summary>
  253. /// 判断是否有设备连接
  254. /// </summary>
  255. public void CheckUsbConnect()
  256. {
  257. usbIsConnected = false;
  258. Thread.Sleep(200);
  259. int nDeviceCount = robotpenController.GetInstance()._GetDeviceCount();
  260. if (App.IsOutputInfoLog)
  261. {
  262. LogHelper.Loginfo.Info(string.Format("当前有 {0} 个设备", nDeviceCount));
  263. }
  264. if (nDeviceCount > 0)
  265. {
  266. for (int i = 0; i < nDeviceCount; ++i)
  267. {
  268. ushort npid = 0;
  269. ushort nvid = 0;
  270. string strDeviceName = string.Empty;
  271. eDeviceType dtype = eDeviceType.Unknow;
  272. if (robotpenController.GetInstance()._GetAvailableDevice(i, ref npid, ref nvid, ref strDeviceName, ref dtype))
  273. {
  274. if (!usbIsConnected)
  275. {
  276. usbIsConnected = true;
  277. deviceType = dtype;
  278. robotpenController.GetInstance()._ConnectInitialize(deviceType, IntPtr.Zero);
  279. int nRes = robotpenController.GetInstance()._ConnectOpen();
  280. if (nRes != 0)
  281. {
  282. //LogHelper.Loginfo.Info(string.Format(@"x={0},y={1},s={2},p={3}", bx, by, bPenStatus, bPress));
  283. LogHelper.Logerror.Error("设备自动连接失败,请重新插拔设备或尝试手动连接!", null);
  284. usbIsConnected = false;
  285. break;
  286. }
  287. robotpenController.GetInstance()._Send(cmdId.GetConfig);
  288. App.BoardStatus = true;
  289. try
  290. {
  291. int banWidth = robotpenController.GetInstance().getWidth();
  292. int banHeight = robotpenController.GetInstance().getHeight();
  293. if (banWidth > 0 && banHeight > 0)
  294. {
  295. if (banWidth > banHeight)
  296. {
  297. m_nDeviceW = banWidth;
  298. m_nDeviceH = banHeight;
  299. }
  300. else
  301. {
  302. m_nDeviceW = banHeight;
  303. m_nDeviceH = banWidth;
  304. }
  305. }
  306. if (App.IsOutputInfoLog)
  307. {
  308. LogHelper.Loginfo.Info
  309. (
  310. string.Format
  311. (
  312. "手写板宽度为:{0} 高度为:{1}",
  313. m_nDeviceW,
  314. m_nDeviceH
  315. )
  316. );
  317. }
  318. }
  319. catch (Exception ex)
  320. {
  321. LogHelper.Logerror.Error("手写板大小获取失败:" + ex.Message, ex);
  322. }
  323. UpdateDevStatus();
  324. if (App.IsOutputInfoLog)
  325. {
  326. LogHelper.Loginfo.Info(string.Format("设备类型:{0}", deviceType.ToString()));
  327. }
  328. new Thread(new ThreadStart(new Action(() =>
  329. {
  330. Thread.Sleep(500);
  331. robotpenController.GetInstance()._Send(cmdId.SearchMode);
  332. }))).Start();
  333. }
  334. }
  335. }
  336. }
  337. else
  338. {
  339. App.BoardStatus = false;
  340. UpdateDevStatus();
  341. }
  342. }
  343. /// <summary>
  344. /// 按键回调函数
  345. /// </summary>
  346. /// <param name="Value">
  347. /// </param>
  348. private void luobo_keyPressEvt(eKeyPress Value)
  349. {
  350. switch (Value)
  351. {
  352. case eKeyPress.CLICK:
  353. break;
  354. case eKeyPress.DBCLICK:
  355. break;
  356. case eKeyPress.PAGEUP:
  357. //last_button_Click(null, null);
  358. break;
  359. case eKeyPress.PAGEDOWN:
  360. //next_btn_Click(null, null);
  361. break;
  362. case eKeyPress.CREATEPAGE://关机键
  363. break;
  364. case eKeyPress.KEY_A:
  365. break;
  366. case eKeyPress.KEY_B:
  367. break;
  368. case eKeyPress.KEY_C:
  369. break;
  370. case eKeyPress.KEY_D:
  371. break;
  372. case eKeyPress.KEY_E:
  373. break;
  374. case eKeyPress.KEY_F:
  375. break;
  376. case eKeyPress.KEY_UP:
  377. //last_button_Click(null, null);
  378. break;
  379. case eKeyPress.KEY_DOWN:
  380. //next_btn_Click(null, null);
  381. break;
  382. case eKeyPress.KEY_YES:
  383. break;
  384. case eKeyPress.KEY_NO:
  385. break;
  386. case eKeyPress.KEY_CANCEL:
  387. break;
  388. case eKeyPress.KEY_OK:
  389. break;
  390. case eKeyPress.PAGEUPCLICK://上一页
  391. //last_button_Click(null, null);
  392. break;
  393. case eKeyPress.PAGEUPDBCLICK://双击上一页
  394. break;
  395. case eKeyPress.PAGEUPPRESS://长按上一页
  396. break;
  397. case eKeyPress.PAGEDOWNCLICK://下一页
  398. //next_btn_Click(null, null);
  399. break;
  400. case eKeyPress.PAGEDOWNDBCLICK://双击下一页
  401. break;
  402. case eKeyPress.PAGEDOWNPRESS://长按下一页
  403. break;
  404. }
  405. }
  406. /// <summary>
  407. /// 更新设备状态显示
  408. /// </summary>
  409. public void UpdateDevStatus()
  410. {
  411. if (penEvent != null)
  412. {
  413. penEvent.penStatusEvent();
  414. }
  415. }
  416. #endregion 罗博智慧笔
  417. }
  418. }