Browse Source

优化方向盘连接,接入油门数据,测试电调,优化图标,增加前进倒档

caner 2 years ago
parent
commit
8f4c74d942
7 changed files with 66 additions and 39 deletions
  1. BIN
      electron/icon/playGame.png
  2. BIN
      electron/icon/playGame@2x.png
  3. 7 3
      electron/logiControl.js
  4. 4 3
      electron/main.js
  5. 14 12
      package.json
  6. 40 20
      src/App.vue
  7. 1 1
      src/components/gauge.vue

BIN
electron/icon/playGame.png


BIN
electron/icon/playGame@2x.png


+ 7 - 3
electron/logiControl.js

@@ -1,11 +1,15 @@
 const HID = require('node-hid');
 const HID = require('node-hid');
+let data = null
 try {
 try {
     const devices = HID.devices();
     const devices = HID.devices();
     const logitech = devices.filter(el => el.manufacturer === 'Logitech' && el.product.includes('G923'));
     const logitech = devices.filter(el => el.manufacturer === 'Logitech' && el.product.includes('G923'));
-    const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
+    data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
     data.on('data', (db) => {
     data.on('data', (db) => {
-        process.send(db)
+        if (process.connected){ process.send(db)}else{
+            throw {type:'err',data:'process.connected=false'}
+        }
     })
     })
 } catch (error) {
 } catch (error) {
-    process.send({ type: 'err' })
+    if (data) data.close()
+    process.send({ type: 'err', data: error + '' })
 }
 }

+ 4 - 3
electron/main.js

@@ -132,10 +132,11 @@ class MainSerivce {
   connectLogi() {
   connectLogi() {
     this.contrlEvent = fork(join(__dirname, './logiControl.js'));
     this.contrlEvent = fork(join(__dirname, './logiControl.js'));
     this.contrlEvent.on('message', msg => {
     this.contrlEvent.on('message', msg => {
-      if (msg.type === 'err') {
-        this.contrlEvent.kill(msg.pid)
+      if (!msg || msg.type === 'err') {
+        this.contrlEvent.disconnect()
+        this.contrlEvent.kill(msg.pid ?? 'SIGKILL')
         this.contrlEvent = null
         this.contrlEvent = null
-        dialog.showMessageBox(this.mainWin, { message: '请尝试旋转方向或重新插入USB!', type: 'error', title: '连接错误' }).then(async ({ response }) => {
+        dialog.showMessageBox(this.mainWin, { message: '请尝试旋转方向或重新插入USB或重启客户端!', type: 'error', title: '连接错误' }).then(async ({ response }) => {
           if (!response) {
           if (!response) {
             await sleep(2000)
             await sleep(2000)
             this.connectLogi()
             this.connectLogi()

+ 14 - 12
package.json

@@ -1,9 +1,10 @@
 {
 {
-  "name": "contrl-client",
-  "version": "1.0.0",
+  "name": "contrl",
+  "version": "0.1.0",
   "main": "electron/main.js",
   "main": "electron/main.js",
-  "author": "Caner",
-  "description": "控制端",
+  "author": "Caner <5658514@qq.com> (https://git.caner.top/NetworkCar/Control)",
+  "description": "Contrl Client",
+  "homepage": "https://caner.top",
   "scripts": {
   "scripts": {
     "dev": "vite",
     "dev": "vite",
     "build": "vue-tsc --noEmit ; vite build ; electron-builder build"
     "build": "vue-tsc --noEmit ; vite build ; electron-builder build"
@@ -16,7 +17,7 @@
     "@typescript-eslint/parser": "^5.40.0",
     "@typescript-eslint/parser": "^5.40.0",
     "@vitejs/plugin-vue": "^4.2.3",
     "@vitejs/plugin-vue": "^4.2.3",
     "echarts": "^5.4.2",
     "echarts": "^5.4.2",
-    "electron": "^23.1.2",
+    "electron": "^25.3.0",
     "electron-builder": "^23.6.0",
     "electron-builder": "^23.6.0",
     "eslint": "^8.40.0",
     "eslint": "^8.40.0",
     "eslint-config-airbnb-base": "^15.0.0",
     "eslint-config-airbnb-base": "^15.0.0",
@@ -33,17 +34,18 @@
     "vue-tsc": "^1.6.5"
     "vue-tsc": "^1.6.5"
   },
   },
   "build": {
   "build": {
+    "appId": "com.caner.contrl",
+    "npmRebuild": false,
+    "productName": "Contrl",
     "directories": {
     "directories": {
       "output": "out"
       "output": "out"
     },
     },
     "files": [
     "files": [
-      "dist/js",
-      "dist/assets",
-      "dist/index.html",
-      "electron",
-      "dist-electron"
+      "dist",
+      "electron"
     ],
     ],
     "asar": false,
     "asar": false,
-    "icon": "./icon/"
+    "icon": "electron/icon/",
+    "electronVersion": "25.3.0"
   }
   }
-}
+}

+ 40 - 20
src/App.vue

@@ -35,12 +35,32 @@ const warnAudio = ref(false)
 const quantity = ref(0)
 const quantity = ref(0)
 const error = ref('')
 const error = ref('')
 const conctrlData = ref({
 const conctrlData = ref({
-  v0: 0, v1: 0, v2: 0, v3: 0
+  v0: 128, v1: 128, v2: 128, v3: 128
 })
 })
-
-const speed = ref(1) // 1低速档 | 2 高速档
+const conctrlAnimation = ref(0)
+const conctrlGrears = ref(false)
+const conctrlNum = ref(0)
 const SpeedValue = ref(0)
 const SpeedValue = ref(0)
 
 
+// 方向盘数据
+function onContrlData() {
+  if (showLoading.value) return
+  socket.value?.emit('msg', { type: 'conctrl', conctrl: { ...conctrlData.value } })
+  conctrlAnimation.value = requestAnimationFrame(onContrlData)
+}
+
+// 档位计算
+function countContrlData(v: number) {
+  // 转10进制
+  const num = parseInt(v.toString(), 10)
+  if (conctrlNum.value % 2) {
+    // 倒档
+    return num <= 128 ? 0 : Math.abs((255 - num) - 128)
+  }
+  // 前进
+  return num <= 128 ? 255 : ((255 - num) + 128)
+}
+
 // 关闭
 // 关闭
 function close(err?: string) {
 function close(err?: string) {
   if (Peer.value) Peer.value?.close()
   if (Peer.value) Peer.value?.close()
@@ -53,6 +73,7 @@ function close(err?: string) {
   Peer.value = null
   Peer.value = null
   audioStateNum.value = 0
   audioStateNum.value = 0
   quantity.value = 0
   quantity.value = 0
+  cancelAnimationFrame(conctrlAnimation.value)
 }
 }
 
 
 // init socket
 // init socket
@@ -105,6 +126,7 @@ function intSoketRtc(host: string) {
         if (state === 'connected') {
         if (state === 'connected') {
           await sleep(3000)
           await sleep(3000)
           showLoading.value = false
           showLoading.value = false
+          onContrlData()
         }
         }
       }
       }
     } catch (error) {
     } catch (error) {
@@ -135,21 +157,22 @@ function intSoketRtc(host: string) {
 
 
 // 遥控数据
 // 遥控数据
 window.$electron?.on('contrlData', (db: Uint8Array) => {
 window.$electron?.on('contrlData', (db: Uint8Array) => {
-  console.log(1)
-
-  // 鸣笛
+  // 倒档 | 前进 =>右拨片
+  conctrlGrears.value = db[6] === 2
+  // 鸣笛 =>回车
   warnAudio.value = !!db[54]
   warnAudio.value = !!db[54]
-  // 录音
+  // 录音 =>左拨片
   micState.value = db[6] === 1
   micState.value = db[6] === 1
-  // 静音
+  // 静音 =>L3
   audioState.value = db[6] === 64
   audioState.value = db[6] === 64
   // 控制
   // 控制
   conctrlData.value = {
   conctrlData.value = {
-    v0: 0,
-    v1: 0,
-    v2: parseInt(db[44].toString(), 10), // 转10进制
-    v3: 0
+    v0: 128,
+    v1: 128,
+    v2: parseInt(db[44].toString(), 10), // 方向盘
+    v3: countContrlData(db[46]) // 油门
   }
   }
+  console.log(6, conctrlData.value)
 })
 })
 
 
 // 窗口事件
 // 窗口事件
@@ -178,21 +201,18 @@ async function login(data: { name: string, roomID: string }) {
 }
 }
 
 
 // 监听按钮状态
 // 监听按钮状态
-watch([ audioState, warnAudio ], () => {
+watch([ audioState, warnAudio, conctrlGrears ], () => {
   if (showLoading.value) return
   if (showLoading.value) return
   if (audioState.value) {
   if (audioState.value) {
     audioStateNum.value++
     audioStateNum.value++
     socket.value?.emit('msg', { type: 'contrlAudio', contrlAudio: !!(audioStateNum.value % 2) })
     socket.value?.emit('msg', { type: 'contrlAudio', contrlAudio: !!(audioStateNum.value % 2) })
   }
   }
+  if (conctrlGrears.value) {
+    conctrlNum.value++
+  }
   if (warnAudio.value) socket.value?.emit('msg', { type: 'warnAudio' })
   if (warnAudio.value) socket.value?.emit('msg', { type: 'warnAudio' })
 })
 })
 
 
-// 监听方向盘
-watch(conctrlData, () => {
-  if (showLoading.value) return
-  socket.value?.emit('msg', { type: 'conctrl', conctrl: { ...conctrlData.value } })
-})
-
 // 初始化
 // 初始化
 onMounted(() => intSoketRtc(HOST))
 onMounted(() => intSoketRtc(HOST))
 onUnmounted(() => close())
 onUnmounted(() => close())
@@ -256,7 +276,7 @@ window.$electron.send('close-loading')
     <div class="gauge">
     <div class="gauge">
       <Gauge
       <Gauge
         :value="SpeedValue"
         :value="SpeedValue"
-        :gears="speed"
+        :gears="conctrlNum % 2 ? '倒档' : '前进'"
       />
       />
     </div>
     </div>
     <Loading v-if="showLoading" />
     <Loading v-if="showLoading" />

+ 1 - 1
src/components/gauge.vue

@@ -11,7 +11,7 @@ import {
 echarts.use([ GaugeChart, LabelLayout, UniversalTransition, CanvasRenderer ])
 echarts.use([ GaugeChart, LabelLayout, UniversalTransition, CanvasRenderer ])
 const props = defineProps<{
 const props = defineProps<{
   value: number,
   value: number,
-  gears: number
+  gears: string
 }>()
 }>()
 const chartDom = ref()
 const chartDom = ref()
 const chart = ref(null as any)
 const chart = ref(null as any)