微信小程序--树莓派(raspberry pi)小车控制的代码流程(树莓派 小程序)

网友投稿 907 2022-10-05

微信小程序--树莓派(raspberry pi)小车控制的代码流程(树莓派 小程序)

微信小程序--树莓派(raspberry pi)小车控制的代码流程(树莓派 小程序)

目录

1. 基本思路

2. 代码结构如下:

3. index目录

4. control目录

5. 工程全局控制

主要练习了一下微信小程序的开发。这里简单记录一下主要代码片段。也是趟过了许多的坑,例如:微信小程序不支持完全全屏,微信小程序不能横屏展示。所以开发过程中也用了一些非常手段。可以说这只是一个很基本的demo,所以里面很多东西,比如摄像头监控ip、页面元素定位我都使用了写死的值。特别是界面,我只是在iPhone 6上面做的实验,所以换到其他手机上时,界面就会变型了。

1. 基本思路

index页面如下:

程序员必备接口测试调试工具:立即使用Apipost = Postman + Swagger + Mock + Jmeter Api设计、调试、文档、自动化测试工具 后端、前端、测试,同时在线协作,内容实时同步

进去之后的页面如下(其中中间空白处会展示摄像头监控,不过我并没有启动,所以看不见):

2. 代码结构如下:

其中,index下面是首页,control是控制页面,res目录下存放的是图片资源

3. index目录

index.js

//index.js//获取应用实例const app = getApp()Page({ data: { logo: "/res/rasp-logo.png", welcome: "欢迎使用树莓小车", enterBtn: "进入", PromoteMsg: "Please enter the server address (eg: http://x.x.x.x:8080)", reqURL: "" }, // 从输入框中获取用户输入的服务器地址信息 getURL: function (e) { this.setData({ reqURL: e.detail.value }) }, enterClicked: function (e) { /* * 当按下进入按钮,需要做以下事情: * 1. 首先判断用户是否已经在输入框中输入完整的服务器地址 * 2. 发起一个到服务器的GET请求,并分析服务器的响应结果 * 3. 跳转到小车控制界面 */ console.log(this.data.reqURL) if (this.data.reqURL == '') { wx.showModal({ title: '提示', content: '请先输入正确的服务器地址!', }) return } // 发起到服务器的GET请求 wx.request({ url: this.data.reqURL, success: function (res) { // 在这里获取POST请求地址,以及视频流地址,然后赋值给全局变量,供control页面调用 console.log(res.data.match(/url = \"(\S*)\"/)[1]) console.log(res.data.match(/src=\"(\S*)\"/)[1]) app.globalData.postURL = res.data.match(/url = \"(\S*)\"/)[1] app.globalData.cameraURL = res.data.match(/src=\"(\S*)\"/)[1] // 跳转到control页面 wx.navigateTo({ url: '/pages/control/control', }) }, fail: function(res) { wx.showModal({ title: '提示', content: '请检查输入的服务器地址!', }) } }) }})-

index.json:无数据,只有一对打括号index.wxml

{{welcome}} {{enterBtn}}-

index.wxss

/**index.wxss**/.welcome{ display: flex; margin-top: 50rpx; flex-direction: column; align-items: center; justify-content: space-between;}.requestURL{ margin: 50rpx 10rpx 30rpx 10rpx; border: 1px solid gray; font-style: italic; font-size: small}.enter{ margin-right: 10rpx; width: 150rpx; height: 60rpx; font-size: small}-

4. control目录

control.js

control.json

{ "navigationBarBackgroundColor": "#ffffff", "navigationBarTextStyle": "black", "navigationBarTitleText": "树莓小车", "backgroundColor": "#eeeeee", "backgroundTextStyle": "light", "enablePullDownRefresh": false, "navigationStyle": "custom", "disableScroll": true}-

control.wxml

-

control.wxss

/* pages/control/control.wxss */.control { width: 100%; height: 100%; transform: rotate(90deg); background-color: #eee; justify-content: center;}.cameraView { margin-left: 0px; width: 603px; height: 375px; background-color: #eee; justify-content: center;}.button { height: 60px; width: 60px; opacity: 0.3;}#forward { left: 60px; top: 135px;}#left { left: 0px; top: 195px;}#right { left: 120px; top: 195px;}#backLeft { left: 0px; top: 255px;}#backRight { left: 120px; top: 255px;}#back { left: 60px; top: 315px;}#up { left: 480px; top: 195px;}#camLeft { left: 420px; top: 255px;}#camRight { left: 540px; top: 255px;}#down { left: 480px; top: 315px;}#reset{ left: 480px; top: 135px}-

5. 工程全局控制

app.js:实际似乎并没有用到,里面都是工程创建时的默认代码

//app.jsApp({ onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId } }) // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) }, globalData: { userInfo: null, postURL: null, cameraURL: null }})-

app.json:

{ "pages": [ "pages/index/index", "pages/control/control" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "树莓小车", "navigationBarTextStyle": "black", "showStatusBar": false }}-

app.wxss:

/**app.wxss**/.container { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 200rpx 0; box-sizing: border-box;}-

project.control.json:

{ "description": "项目配置文件。", "packOptions": { "ignore": [] }, "setting": { "urlCheck": false, "es6": true, "postcss": true, "minified": true, "newFeature": true }, "compileType": "miniprogram", "libVersion": "2.0.4", "appid": "wx18414b9f85bfc895", "projectname": "wechat-control", "isGameTourist": false, "condition": { "search": { "current": -1, "list": [] }, "conversation": { "current": -1, "list": [] }, "game": { "currentL": -1, "list": [] }, "miniprogram": { "current": -1, "list": [] } }}-

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:开发小程序程序「小程序开发发」
下一篇:bzoj1997 [Hnoi2010]Planar
相关文章

 发表评论

暂时没有评论,来抢沙发吧~