{
"pages": [
"bleLED/bleled"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#4b0963",
"navigationBarTitleText": "星星点灯",
"navigationBarTextStyle": "white"
},
"sitemapLocation": "sitemap.json"
}
<view>
<text>mCookie-BLE控制ColorLED</text>
</view>
<view class='title'>
<text>mCookie-BLE控制ColorLED</text>
<view class="line"></view>
</view>
.title {
color: #731496;
text-align: center;
margin-top: 20px;
}
.line {
margin-top: 20px;
width: 100%;
height: 1rpx;
background: #731496;
}
<view class='image-H'>
<image class='userinfo-avatar' src='../bleLED/snm.jpg'></image>
</view>
.image-H {
text-align: center;
margin-top: 110px;
}
.userinfo-avatar {
width: 166rpx;
height: 166rpx;
margin: 20rpx;
border-radius: 50%;
}
<view>
<button class='weui-btn' hover-class='other-button-hover' bindtap='sendData'>点灯</button>
</view>
.weui-btn {
margin-top: 128px;
width: 156rpx;
height: 156rpx;
border-radius: 50px;
padding-top: 15px;
text-align: center;
background-color: #8d1cb7;
color: #fff;
}
.other-button-hover {
background-color: #731496;
}
import bleComm from '../utils/bleComm.js';
var msg;
Page({
onUnload: function () {
bleComm.disConnect();
},
onHide: function () {
bleComm.disConnect();
},
onLoad: function (options) {
wx.showLoading({
title: '靠近连接',
mask: true
})
bleComm.connectDevice().then(res => {
wx.showToast({
title: '蓝牙连接成功',
icon: 'success',
duration: 300
})
});
},
sendData: function () {
msg = !msg;
console.log(msg);
if (msg)
bleComm.writeNumberValue(1);
else
bleComm.writeNumberValue(0);
},
})
import bleComm from '../utils/bleComm.js';
var msg;
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({
title: '靠近连接',
mask: true
})
bleComm.connectDevice().then(res => {
wx.showToast({
title: '蓝牙连接成功',
icon: 'success',
duration: 300
})
});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
bleComm.disConnect();
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
bleComm.disConnect();
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
sendData: function () {
msg = !msg;
console.log(msg);
if (msg)
bleComm.writeNumberValue(1);
else
bleComm.writeNumberValue(0);
}
})
//#define BLEMODULE_01_9600 //old-mCookie_Bluetooth
//#define BLEMODULE_45_9600 //Microduino_BLE new-mCookie_Bluetooth
#define BLEMODULE_01_57600 //mCenter+、BleUpload
uint8_t light = my_Serial.read(); //接收一位数字
if (sta)
strip.setPixelColor(0, 0XFF00FF); //设置紫色
else
strip.setPixelColor(0, 0X000000); //设置黑色(关灯)
strip.show(); //控制彩灯
//#define BLEMODULE_01_9600 //old-mCookie_Bluetooth
//#define BLEMODULE_45_9600 //Microduino_BLE new-mCookie_Bluetooth
#define BLEMODULE_01_57600 //mCenter+、BleUpload
/*定义串口0接口,波特率9600的蓝牙*/
#ifdef BLEMODULE_01_9600
#define my_Serial Serial
#define UARTSPEED 9600
#endif
/*定义串口0接口,波特率57600的蓝牙*/
#ifdef BLEMODULE_01_57600
#define my_Serial Serial
#define UARTSPEED 57600
#endif
/*定义软串口(4,5)接口,波特率9600的蓝牙*/
#ifdef BLEMODULE_45_9600
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5);
#define my_Serial mySerial
#define UARTSPEED 9600
#endif
#include <Microduino_ColorLED.h> //引用彩灯库
ColorLED strip = ColorLED(1, 6); //彩灯名字叫strip,共接了1个灯,接口6
void setup(void)
{
my_Serial.begin(UARTSPEED); //定义蓝牙接收的速度
strip.begin(); //彩灯开始
}
void loop(void)
{
//每收到一次信号,向通信另一端反馈一次
if (my_Serial.available() > 0) //如果串口有数据输入
{
uint8_t sta = my_Serial.read(); //将收到的数据赋给变量sta
if (sta)
strip.setPixelColor(0, 0XFF00FF); //设置紫色
else
strip.setPixelColor(0, 0X000000); //设置黑色(关灯)
strip.show(); //控制彩灯
}
}
switch(表达式)
{
case 值1:
表达式的值和 值1匹配上了,需要执行的代码;
break;
case 值2:
表达式的值和 值2匹配上了,需要执行的代码;
break;
case 值3:
表达式的值和 值3匹配上了,需要执行的代码;
break;
default:
如果表达式的值和以上的case后面的值都没有匹配上,那么就执行这里的代码。
break;
}
END
杨立斌
美科
解惑
创建时间:2017/12/22
24星星点灯