轉自:http://www.myexception.cn/web/1615075.html
樹莓派學習筆記——webiopi網頁控制LED
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
<title>WebIOPi | Demo</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
// GPIO24和GPIO25爲輸出
webiopi().setFunction(24, "out");
webiopi().setFunction(25, "out");
var content, button;
content = $("#content");
// 創建一個不同按鈕,按鈕ID爲hold,名稱爲LED1,並綁定按下和鬆開事件
button = webiopi().createButton("hold", "LED1", mousedown, mouseup);
content.append(button);
// 創建一個GPIO按鈕,編號爲GPIO24,按鈕名稱爲LED2
button = webiopi().createGPIOButton(25, "LED2");
content.append(button); // append button to content div
// 不自動刷新界面
webiopi().refreshGPIO(false);
});
function mousedown() {
// GPIO24 輸出高電平
webiopi().digitalWrite(24, 1);
}
function mouseup() {
// GPIO24 輸出低電平
webiopi().digitalWrite(24, 0);
}
</script>
<style type="text/css">
button {
display: block;
margin: 5px 5px 5px 5px;
width: 160px;
height: 45px;
font-size: 24pt;
font-weight: bold;
color: black;
}
.LOW {
background-color: White;
}
.HIGH {
background-color: Red;
}
</style>
</head>
<body>
<div id="content" align="center"></div>
</body>
</html>
<body>
<div id="content" align="center"></div>
</body>
GPIOPort.prototype.setFunction = function(channel, func, callback) {
var name = this.name;
$.post(this.url + "/" + channel + "/function/" + func, function(data) {
callback(name, channel, data);
});
}
訪問REST API爲 /24/function/out。意爲GPIO24端口爲輸出狀態。WebIOPi.prototype.createButton = function (id, label, callback, callbackUp) {
var button = $('<button type="button" class="Default">');
button.attr("id", id);
button.text(label);
if (callback != undefined) {
button.bind(BUTTON_DOWN, callback);
}
if (callbackUp != undefined) {
button.bind(BUTTON_UP, callbackUp);
}
return button;
}
該代碼可創建一個button標籤——<button
type="button" class="Default" id="hold">LED1</button>WebIOPi.prototype.createGPIOButton = function (gpio, label) {
var button = w().createButton("gpio" + gpio, label);
button.bind(BUTTON_DOWN, function(event) {
w().toggleValue(gpio);
});
return button;
}
從實現代碼中可以看出,GPIOButton本質爲一個Button,只是這個Button的ID變爲gpio25。除此之外還給該button綁定了一個mousedown事件。WebIOPi.prototype.createGPIOButton = function (gpio, label) {
var button = w().createButton("gpio" + gpio, label);
button.bind(BUTTON_DOWN, function(event) {
w().toggleValue(gpio);
});
return button;
}
實現部分的代碼調用了digitalWrite。(見【8】)WebIOPi.prototype.refreshGPIO = function (repeat) {
$.getJSON(w().context + "*", function(data) {
w().updateALT(w().ALT.I2C, data["I2C"]);
w().updateALT(w().ALT.SPI, data["SPI"]);
w().updateALT(w().ALT.UART, data["UART"]);
w().updateALT(w().ALT.ONEWIRE, data["ONEWIRE"]);
$.each(data["GPIO"], function(gpio, data) {
w().updateFunction(gpio, data["function"]);
if ( ((gpio != 4) && ((data["function"] == "IN") || (data["function"] == "OUT"))
|| ((gpio == 4) && (w().ALT.ONEWIRE["enabled"] == false)))){
w().updateValue(gpio, data["value"]);
}
else if (data["function"] == "PWM") {
w().updateSlider(gpio, "ratio", data["ratio"]);
w().updateSlider(gpio, "angle", data["angle"]);
}
});
});
if (repeat === true) {
setTimeout(function(){w().refreshGPIO(repeat)}, 1000);
}
}
該部分實現較爲複雜,請注意updateValue函數——webiopi.jsWebIOPi.prototype.updateValue = function (gpio, value) {
w().GPIO[gpio].value = value;
var style = (value == 1) ? "HIGH" : "LOW";
$("#gpio"+gpio).attr("class", style);
}
選擇一個ID爲gpio??的標籤,然後加入一個class,樣式名爲HIGH或者LOW。 function mousedown() {
// GPIO24 輸出高電平
webiopi().digitalWrite(24, 1);
}
function mouseup() {
// GPIO24 輸出低電平
webiopi().digitalWrite(24, 0);
}
其中digitalWrite實現部分如下WebIOPi.prototype.digitalWrite = function (gpio, value, callback) {
if (w().GPIO[gpio].func.toUpperCase()=="OUT") {
$.post(w().context + 'GPIO/' + gpio + "/value/" + value, function(data) {
w().updateValue(gpio, data);
if (callback != undefined) {
callback(gpio, data);
}
});
}
else {
//console.log(w().GPIO[gpio].func);
}
}
使用jquery中的$.post方法,REST
API爲 button {
display: block;
margin: 5px 5px 5px 5px;
width: 160px;
height: 45px;
font-size: 24pt;
font-weight: bold;
color: black;
}
【10】 .LOW {
background-color: White;
}
.HIGH {
background-color: Red;
}