|
beagblebone之node.js的LED流水灯设计、网页控制LED灯(初步)
[复制链接]
A基于node.js的LED流水灯设计
1、前提:
已建立PC与beagblebone之间的网络连接
2、效果描述:
采用beaglebone的node.js开发环境,即在浏览器输入http://IP_ADDR_OF_BB:3000,参照自带的blinkled程序,做一个将beagblebone的4个USR-LED流水灯效果的程序
3、代码
如下,注释已经很明显
4、效果
实现了4个LED逐个依次点亮的效果。具体效果做过单片机的LED流水灯的都懂得。- // 本程序从采用node.js实现beagblebone的四个LED流水灯效果
- // get the beaglebone variables and functions
- var bb = require('./bonescript');
- // the delay duration between two LEDs, in microseconds
- var delayMs = 200;
- // the whole number of LEDS
- var NUM_LEDS = 4;
- // 4 LEDs
- var ledPin0 = bone.USR0;
- var ledPin1 = bone.USR1;
- var ledPin2 = bone.USR2;
- var ledPin3 = bone.USR3;
- // 4 LED status
- var led0 = LOW;
- var led1 = LOW;
- var led2 = LOW;
- var led3 = LOW;
- // the counter used for couting for the current lighting LED
- var counter = 0;
- // setup function set the pin mode of the 4 LEDS
- setup = function() {
- pinMode(ledPin0, OUTPUT);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- };
- /* the loop function increase the counter, and divided by 4
- * using the remain to judge the number of the LED to be lighted
- */
- loop = function() {
- // Firstly unlighting all LEDS
- led0 = LOW;
- led1 = LOW;
- led2 = LOW;
- led3 = LOW;
- // increase the counter and divided by 4, to
- counter ++;
- var remain = counter % NUM_LEDS;
- // using the remain to judge the LED to be lighted, set the respond led status to be HIGH
- if(0 == remain) {
- led0 = HIGH;
- }
- else if(1 == remain) {
- led1 = HIGH;
- }
- else if(2 == remain) {
- led2 = HIGH;
- }
- else if(3 == remain) {
- led3 = HIGH;
- }
- // output the status to the LED
- digitalWrite(ledPin0, led0);
- digitalWrite(ledPin1, led1);
- digitalWrite(ledPin2, led2);
- digitalWrite(ledPin3, led3);
- // delay for the required duration
- delay(delayMs);
- };
- bb.run();
复制代码B 基于node.js的网页控制LED设计
1、前提:
已建立PC与beagblebone之间的网络连接
2、效果描述:
采用beaglebone的node.js开发环境,即在浏览器输入http://IP_ADDR_OF_BB:3000,参照自带的blinkled程序,做一个将beagblebone的USR0-LED通过网页刷新来控制亮和灭,
3、代码
如下,注释已经很明显
4、效果
即在另一个浏览器窗口输入http://IP_ADDR_OF_BB:8000,刷新一次,则LED-USR0则改变一次亮灭。
5、亮点和进一步创新点
在网页上实现LED的控制。
进一步可采取node.js的一些功能比如(nowjs的server与client的通讯功能,在网页上动态控制LED、显示模拟输入或数字输入量。
- // 打开浏览器;输入http://IP_Addr_if_Beaglebone:8000,刷新,则LED-USR0相应亮灭
- //=======================================
- // 采用下述代码取代引用到bonescript的一些常量和函数
- var bb = require('./bonescript');
- // 选取LED0作为显示
- var ledPin = bone.USR0;
- //用于LED亮灭的循环次数技术
- var counter = 0;
- // 设置LED的引脚模式为输出
- setup = function() {
- pinMode(ledPin, OUTPUT);
- var http = require('http');
-
- // 创建http服务器,每次有新的http请求(刷新浏览器),则counter加1,除以2的余数作为LED的状态
- var s = http.createServer(
- function(req, res) {
- counter ++;
- if(0 == counter%2) {
- digitalWrite(ledPin, HIGH);
- }
- else {
- digitalWrite(ledPin, LOW);
- }
- res.writeHead(200, { 'Content-type' : 'text/plain'} );
- res.write("Hello, beaglebone!");
- res.write("\ncounter = " + counter);
- res.end("\n================\n");
- }
- );
- s.listen(8000);
- };
- // 循环函数无执行
- loop = function() {
- // Your function to run in a loop
- };
- // 启动bb
- bb.run();
复制代码
[ 本帖最后由 mars4zhu 于 2012-10-12 00:13 编辑 ]
|
|