本节熟悉一下xdk下的Grove套件的使用,主要体验模拟量的输入、数字量的输出输入,中断的处理等。
一,模拟量的输入:温度传感器数据的读取
var mraa = require('mraa'); //打开mraa库
console.log('MRAA Version: ' + mraa.getVersion()); //打印mraa版本
var analogPin0 = new mraa.Aio(0);//温度传感器接在A0接口
var analogValue = analogPin0.read();//读取传感器值
二,数字接口的读取
var myDigitalPin6 = new mraa.Gpio(6); //接在D6接口
myDigitalPin6.dir(mraa.DIR_IN);
var myDigitalValue = myDigitalPin6.read();
三,数字接口的输出
var myDigitalPin5 = new mraa.Gpio(5); //接在D5接口
myDigitalPin5.dir(mraa.DIR_OUT);
myDigitalPin5.write(1);
四,中断的处理
var x = new mraa.Gpio(8); //接在D8接口
x.dir(mraa.DIR_IN);
x.isr(mraa.EDGE_BOTH, a());//注册中断函数
function a() {
var d = new Date();
var time = d.getTime();
console.log("The time is " + time);
}
综上整理为按键产生中断,在中断函数中读取按键值,1s周期的周期性的读取温度传感器值和led闪烁
var mraa = require('mraa');
console.log('MRAA Version: ' + mraa.getVersion());
var button = new mraa .Gpio(8);
button .dir(mraa.DIR_IN);
button .isr(m.EDGE_BOTH, a());
var analogPin0 = new mraa.Aio(0);
var LED= new mraa.Gpio(5);
LED.dir(mraa.DIR_OUT);
int tick = true;
function a() {
var d = new Date();
var time = d.getTime();
console.log("The time is " + time);
var myDigitalValue = button .read();
console.log("button:" + myDigitalValue );
}
periodicActivity();
function periodicActivity()
{
LED.write(tick?1:0);
tick = !tick;
var analogValue = analogPin0.read();
console.log("analogValue " + analogValue );
setTimeout(periodicActivity,1000); // 1 second (1000 milliseconds)
}