瀏覽代碼

基础功能制作

基础快门控制功能
kindring 2 年之前
父節點
當前提交
c877a37990
共有 2 個文件被更改,包括 59 次插入41 次删除
  1. 56 39
      pages/index/index.js
  2. 3 2
      pages/index/index.wxml

+ 56 - 39
pages/index/index.js

@@ -6,6 +6,8 @@ const sleep = require("../../utils/sleep");
 const app = getApp();
 const ble = getBle();
 
+let services = [];
+
 const connectStateMap = {
   0: {text:'未连接',color:"red"},
   1:  {text:'扫描中',color:"greenYellow"},
@@ -42,10 +44,10 @@ const bleServerMap = {
       hold:{
         firstUuid: '0000FF01',
         command: {
-          cmd1:'0106',//SHUTTER_RELEASED
-          cmd2:'0107',//PRESS_TO_FOCUS
-          cmd3:'0108',//HOLD_FOCUS
-          cmd4:'0109',//TAKE_PICTURE
+          cmd1:'0107',//Focus Down
+          shutterDown:'0109',//PRESS_TO_FOCUS
+          shutterUp:'0108',//HOLD_FOCUS
+          cmd4:'0106',//Focus Up
         }
       }
     }
@@ -80,6 +82,8 @@ Page({
     remoteDev:{
       connect: false,
       devName: "",
+      devId: '',
+      shutter: false,
     },
     bleDevs:[],// 搜索到的蓝牙设备列表
     motto: 'Hello World',
@@ -119,6 +123,44 @@ Page({
   async holdFoucus(){
     console.log("发送对焦命令");
   },
+  async shutterHandle(){
+    let cmds,waitTime=10;
+    let code = bleServerMap.remote.code.hold;
+    let dev = this.data.bleDevs.find(dev=>this.data.remoteDev.devId===dev.deviceId);
+    let server = services.find(s=>s.uuid === bleServerMap.remote.uuid);
+    if(!server){console.log('无法找到远程控制服务')}
+    let characteristic = server.characteristics.find(c=>c.uuid.startsWith(code.firstUuid));
+    if(!characteristic){return console.log('无法匹配到特征')}
+    if(this.data.remoteDev.shutter){
+      console.log('快门抬起')
+      // 发送抬起快门
+      cmds =[code.command.shutterUp,code.command.shutterDown,code.command.shutterUp]
+    }else{
+      console.log('快门按下')
+      cmds =  [code.command.shutterDown]
+    }
+    this.setData({
+      remoteDev:{...this.data.remoteDev,shutter:!this.data.remoteDev.shutter}
+    });
+    // 获取对应的服务
+    // let server = 
+    for (let index = 0; index < cmds.length; index++) {
+      const cmd = cmds[index];
+      let [_err,_res] = await handle(ble.sendData(
+        dev.deviceId,
+        server.uuid,
+        characteristic.uuid,
+        cmd.replace(/\s/g,'')
+        ));
+        if(_err){
+          console.log('发送命令失败');
+          console.error(_err);
+        }
+        console.log('发送命令成功');
+        await sleep(waitTime);
+    }
+    
+  },
   async connectDev(e){
     let devId = e.currentTarget.dataset.dev;
     let dev = this.data.bleDevs.find(dev=>devId===dev.deviceId);
@@ -145,6 +187,13 @@ Page({
         })
       }
       console.log('设备连接成功');
+      this.setData({
+        remoteDev: {
+          ...this.data.remoteDev,
+          devId: devId,
+          devName: dev.name
+        }
+      });
       console.log(res);
       // 然后呢? 
       this.setData({
@@ -158,7 +207,6 @@ Page({
         });
         return console.log(err);
       }
-      console.log(res);
       res.forEach(async val=>{
         console.log('******');
         console.log(val.uuid);
@@ -226,44 +274,14 @@ Page({
           let code = bleServerMap.remote.code.hold
           let characteristic = val.characteristics.find(c=>c.uuid.startsWith(code.firstUuid));
           if(!characteristic){return console.log('无法配对')}
-          [_err,_res] = await handle(ble.sendData(
-            devId,
-            val.uuid,
-            characteristic.uuid,
-            code.command.cmd3.replace(/\s/g,'')
-            ));
-            await sleep(10);
-            [_err,_res] = await handle(ble.sendData(
-              devId,
-              val.uuid,
-              characteristic.uuid,
-              code.command.cmd1.replace(/\s/g,'')
-              ));
+          
+            
              
             if(_err){
               wx.showToast({
                 title: 'Err:发送快门控制命令失败',
               })
               // 尝试关闭设备
-              setTimeout(async ()=>{
-                console.log('发送释放快门命令');
-                [_err,_res] = await handle(ble.sendData(
-                  devId,
-                  val.uuid,
-                  characteristic.uuid,
-                  code.command.cmd2.replace(/\s/g,'')
-                  ));
-                  if(_err){
-                    console.error(_err)
-                    wx.showToast({
-                      title: 'Err:发送按住快门命令失败??',
-                    })
-                  } else{
-                    wx.showToast({
-                      title: '发送按住快门命令成功??',
-                    });
-                  }
-              },20000);
               
                console.log(_err);
             }
@@ -283,8 +301,7 @@ Page({
       wx.showToast({
         title: '蓝牙服务搜索成功',
       });
-    
-
+      services = res;
   },
   // 事件处理函数
   bindViewTap() {

+ 3 - 2
pages/index/index.wxml

@@ -35,10 +35,11 @@
   </view>
 
   <view class="conetrol" wx:if="{{ble.state === bleStateEnum.connected}}">
-    <button bindtap="holdFoucus">对焦?</button>
-  </view>
+    <button bindtap="holdFoucus">对焦</button>
+    <button bindtap="shutterHandle">{{remoteDev.shutter?'抬起':'按下'}}</button>
   
   <view class="usermotto">
     <text class="user-motto">{{motto}}</text>
   </view>
 </view>
+</view>