GPIO, aka General Purpose Input Output, is a common hardware interface, which usually acts as a switch.
To know more about GPIO, please visit here.
Prerequisites
- Visit here if you have no idea how to write application with Ruff.
- Visit Driver Getting Started and Driver Model, if you have no idea about driver development.
Using GPIO
Please read your datasheet to confirm your interface is GPIO.
Configuring driver.json
Please declare your interface is GPIO in driver.json. The declaration should be inputs with type as GPIO.{
...
"inputs": {
"gpio-in": {
"type": "gpio",
"args": {
"direction": "in",
"edge": "both"
}
},
"gpio-out": {
"type": "gpio",
"args": {
"direction": "out",
"level": "low"
}
}
}
}
Interface Properties
The following properties is accepted for GPIO:
direction
, which defines interface directionin
, input mode, which reads data from interface, or responses a particular interrut. For instance, a button could do something for user input, or read current state to know if the button is pressed.out
, output mode, to control device. For instance, LED could be defined as output to turn on/off LED.
edge
, which controls interrupt events, could be one of"none"
,"rising"
,"falling"
or"both"
.
This argument takes effect only ifdirection
is set to"in"
.none
, nonerising
, the electrical level rose from low to highfalling
, the electrical level fell from high to lowboth
, both rising and falling
level
, which defines initial electrical level of the interface, This argument takes effect only ifdirection
is set to"out"
.high
, the initial electrical level is defined ashigh
-
low
, the initial electrical level is defined aslow
Writing a Driver
As a Input Device
Interrupt will be triggered in GPIO in
mode. The driver could trigger different event based on different input value. An example is as follows:// Driver Code
gpio.on('interrupt',function(value){
if (that._value === value) {
return;
}
that._value = value;
if (value === 0) {
that.emit('event_0');
} else {
that.emit('event_1');
}
});// Application Code
$('#device').on('event_0', function() {
console.log('event_0');
}
$('#device').on('event_1', function() {
console.log('event_1');
}
As a Output Device
The driver could write Level.high
or Level.low
to interface in GPIO out
mode. An example is as follows:var Level = require('gpio').Level;
...
function turnOn(callback) {
gpio.write(Level.high, function(error) {
if (error) {
callback(error);
return;
}
});
}
Please visit GPIO API document to know more defails.
Application
Push Button Module (CK002)
Push button module has an input GPIO with both edge. You can declare it in driver.json."models": [
"CK002"
],
"inputs": {
"gpio": {
"type": "gpio",
"args": {
"direction": "in",
"edge": "both"
}
}
}
Push button module could emit push
or release
event. You will listen interrupt
event and trigger push
or release
based on button current state. ;
var driver = require('ruff-driver');
var ButtonState = {
pushed: 0,
released: 1
};
module.exports = driver({
attach: function (inputs) {
var that = this;
this._gpio = inputs['gpio'];
this._state = ButtonState.released;
this._gpio.on('interrupt', function (state) {
if (that._state === state) {
return;
}
that._state = state;
if (state === ButtonState.pushed) {
that.emit('push');
} else {
that.emit('release');
}
});
}
});
Buzzer (FC-49)
Buzzer has an output GPIO, trigged with high electrical level. You can declare it in driver.json: type as GPIO, direction as out, level as low."models": [
"FC-49"
],
"inputs": {
"gpio": {
"type": "gpio",
"args": {
"direction": "out",
"level": "low"
}
}
}
We provides buzzer with three methods: turn on the buzzer, turn off the buzzer and judge if buzzer is on:
- turnOn, write Level.high to GPIO to turn on the buzzer
- turnOff, write Level.low to GPIO to turn off the buzzer
- isOn, read GPIO to know if buzzer is on
; |