Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pymzor/node-gpio

A simple node.js platform-agnostic library to control GPIO pins, without running as root user.
https://github.com/pymzor/node-gpio

Last synced: 3 days ago
JSON representation

A simple node.js platform-agnostic library to control GPIO pins, without running as root user.

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/PymZoR/node-gpio.svg)](https://travis-ci.org/PymZoR/node-gpio)
# Node-gpio
A simple node.js platform-agnostic library to control GPIO pins, without running as root user.

# Installation
###### Install dependencies
The library depends on quick2wire-gpio-admin to ensure access to GPIO without running as root user.
```shell
$ git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
$ cd quick2wire-gpio-admin
$ make
$ sudo make install
$ sudo adduser $USER gpio
```
###### Get with npm
```shell
$ npm install node-gpio
```

# Platforms
Accessing the GPIO pins can be done with two types of ID.
- With the system ID (SoC), refering to the kernel ID of the GPIO pin.
- With a custom user ID, usually refering to the visual ID printed on the board.

A file composed by an associative "pinMap" of visual ID to SoC ID can be used for convenience purposes. Some of the most used plaforms, like Raspberry Pi or ODROID-C1 have their own files already written in the [platforms/](platforms) directory. To use it, you have two options:

#### Automatic loading
Just call `gpio.init()` to automatically detect your platform and load the associated file, if it exists.

#### Using a custom file
Use the [init([platformFile])](#init) function, with platformFile referring to the file path. It must respect the format of [platforms/platform.json.example](platforms/platform.json.example), without the "identifier" part.

#### Bonus: Pull-request a new platform file
If you have written a new platform file and want to share it with the community in addition to enjoy the automatic detection feel free to submit a pull request with the platform file respecting the format of [platforms/platform.json.example](platforms/platform.json.example).
- The "pinMap" part must map all the physical pin ID to their associated SoC ID
- The "identifier" part is used to auto-detect the platform on which the program is run. To find one, you have to run the following command:
```shell
$ cat /proc/cpuinfo
```
Then, search for an unique key/value pair which is platform specific. For example, on a ODROID-C1 the key/value pair used is the following:
```json
"identifier": {
"Hardware": "ODROIDC"
}
```

# API
## Functions
**_[setGpioAdminPath(newGpioAdminPath)](#setGpioAdminPath)_** → **`Promise`**
    Set gpio-admin executable path

**_[setGpioSysPath(newGpioSysPath)](#setGpioSysPath)_**
    Set the gpio system files path

**_[init([platformFile])](#init)_**
    Load GPIO map from platform file, or try to auto-detect it

**_[setWatchInterval(newWatchInterval)](#setWatchInterval)_**
    Set a new watchInterval. The less it is, the more the process will be CPU-heavy

**_[get(gpioID)](#get)_** → **`Object`**
    Gpio instance getter

**_[reset](#reset)_**
    Remove all stored Gpio instances. Used for testing purposes


### setGpioAdminPath(newGpioAdminPath) → **`Promise`**
Set gpio-admin executable path

| Param | Type | Description |
| --- | --- | --- |
| newGpioAdminPath | `String` | gpio-admin binary path |


### setGpioSysPath(newGpioSysPath)
Set the gpio system files path

| Param | Type | Description |
| --- | --- | --- |
| newGpioSysPath | `String` | gpio system-files path |


### init([platformFile])
Load GPIO map from platform file, or try to auto-detect it

| Param | Type | Description |
| --- | --- | --- |
| Load GPIO map from platform file, or try to auto-detect it | `String|null` | Platform file path if provided; try to auto-detect the platform otherwise |


### setWatchInterval(newWatchInterval)
Set a new watchInterval. The less it is, the more the process will be CPU-heavy.

| Param | Type | Description |
| --- | --- | --- |
| newWatchInterval | `Number` | In milliseconds |


### get(gpioID) → `Object`
Gpio instance getter

**Returns**: `Object` - Gpio instance

| Param | Type | Description |
| --- | --- | --- |
| gpioID | `Number` | specific platform accessID or systemID |


### reset()
Remove all stored Gpio instances. Used for testing purposes

## Gpio
Represent a physical GPIO.
One instance per systemID stored in the static pinMap object.

* [Gpio](#Gpio)
* [new Gpio(gpioID)](#new_Gpio_new)
* [.open()](#Gpio+open) → `Promise`
* [.close()](#Gpio+close) → `Promise`
* [.setMode(newMode)](#Gpio+setMode) → `Promise`
* [.getMode()](#Gpio+getMode) → `Promise`
* [.setValue(newValue)](#Gpio+setValue) → `Promise`
* [.getValue()](#Gpio+getValue) → `Promise`
* [.startWatch()](#Gpio+startWatch)
* [.stopWatch()](#Gpio+stopWatch)
* ["change"](#Gpio+event_change)
* ["raising"](#Gpio+event_raising)
* ["falling"](#Gpio+event_falling)
* ["error"](#Gpio+event_error)


### new Gpio(gpioID)
Private Constructor

| Param | Type | Description |
| --- | --- | --- |
| gpioID | `Number` | System GpioID |


### gpio.open() → `Promise`
Make the GPIO available to use


### gpio.close() → `Promise`
Close GPIO, making it unavailable to use


### gpio.setMode(newMode) → `Promise`
Set the GPIO mode

| Param | Type | Description |
| --- | --- | --- |
| newMode | `String` | Value must be either "input" or "output" |


### gpio.getMode() → `Promise`
Get the GPIO mode

**Returns**: `Promise` - Returned value is either "input" or "output"


### gpio.setValue(newValue) → `Promise`
Set the GPIO value. Used in "output" mode

| Param | Type | Description |
| --- | --- | --- |
| newValue | `String` | Value must be either "high" or "low" |


### gpio.getValue() → `Promise`
Get the GPIO value. Used in "input" mode

**Returns**: `Promise` - Returned value is either "high" or "low"


### gpio.startWatch()
Start listening to events


### gpio.stopWatch()
Stop listening to events


### "change"
Change event.
Fired on value change

**Kind**: event emitted by `[Gpio](#Gpio)`


### "raising"
Raising event.
Fired when values goes from "low" to "high"

**Kind**: event emitted by `[Gpio](#Gpio)`


### "falling"
Falling event.
Fired when values goes from "high" to "low"

**Kind**: event emitted by `[Gpio](#Gpio)`


### "error"
Error event.
Fired when an error occurs while reading a value

**Kind**: event emitted by `[Gpio](#Gpio)`

# License
This library is inspired by odroid-gpio

The MIT license.