GPIO

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

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 direction

    • in, 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 if direction is set to "in".

    • none, none
    • rising, the electrical level rose from low to high
    • falling, the electrical level fell from high to low
    • both, both rising and falling
  • level, which defines initial electrical level of the interface, This argument takes effect only if direction is set to "out".

    • high, the initial electrical level is defined as high
    • low, the initial electrical level is defined as low

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.

'use strict';

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
'use strict';

var Level = require('gpio').Level;

var driver = require('ruff-driver');

module.exports = driver({

attach: function (inputs) {
this._gpio = inputs['gpio'];
},
exports: {
turnOn: function (callback) {
this._gpio.write(Level.high, callback);
},
turnOff: function (callback) {
this._gpio.write(Level.low, callback);
},
isOn: function (callback) {
var readCallback = callback && function (error, value) {
if (error) {
callback(error);
return;
}

callback(undefined, !!value);
};

this._gpio.read(readCallback);
}
}
});