Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit of piface-node-12
  • Loading branch information
RickBullotta authored Oct 23, 2019
0 parents commit 8d780a3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

The MIT License (MIT)

Copyright (c) 2013 Darryl Hodgins

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
About
=====
This is an interface to the Raspberry Pi Piface Digital 2 board using Node.js version 12. It was adapted from the original piface-node module created by Darryl Hodgins and updated to support Node 12.X. Darryl's original code is at:

https://github.com/darrylhodgins/piface-node

...and the updated version is at:

https://github.com/RickBullotta/piface-node-12


This addon shouldn't require any sudo or root privileges to run, as long as your user is in the "spi" group on your Pi (handled by the spidev-setup script).

Installation
============
Assuming a fresh Raspbian Stretch install, there are two steps to getting a project off the ground with piface-node.
- Get Node.js
- Get the Piface C libraries (if not already installed on your Raspian distribution)
- Get the piface-node-12 module with NPM

Using piface-node-12
====================
Darryl (original author) intended this to be used with the full awesome power of Node's EventEmitter. You can easily wire up the physical I/O on the Piface with pretty much anything.

Here's a basic example of the usage, in lieu of actual documentation. There are also a few examples in the examples folder.
```js
var pfio = require('piface-node-12');
pfio.init();
pfio.digital_write(0,1); // (pin, state)
var foo = pfio.digital_read(0); // (pin; returns state)
pfio.deinit();
```

```js
var pfio = require('piface-node-12');
pfio.init();
var foo = pfio.read_input(); // bit-mapped
pfio.write_output(255); // that's binary 11111111, so it'll turn all outputs on.
pfio.deinit();
```

The Examples Folder
-------------------
Everything in the example application is modular and decoupled: most of the components don't even know that the other ones exist. They only communicate through the EventBus. All that example.js does is start up those modules. The example application watches for changes on the input pins, and echoes them to the output pins. Try it out by pressing the tactile switches.

```bash
$ cd node_modules/piface-node-12/examples
$ node example.js
````
14 changes: 14 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"targets": [
{
"target_name": "pfio",
'include_dirs': [ '/usr/local/include', "<!(node -e \"require('nan')\")" ],
"sources": [ "pfio.cc" ],
'link_settings': {
'libraries': [
'-lpiface-1.0'
]
}
}
]
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "piface-node-12",
"description": "Raspberry Pi Piface Addon For Node 12.X",
"version": "1.0",
"keywords": [
"piface",
"raspberry pi"
],
"repository": {
"type": "git",
"url": "https://github.com/RickBullotta/piface-node-12.git"
},
"author": {
"name": "Rick Bullotta",
"email": "[email protected]"
},
"main": "./build/Release/pfio",
"engines": {
"node": ">= 12.0.0"
},
"dependencies": {
"nan": "^2.13.0",
"node-gyp": "^6.0.0"
},
"scripts": {
"install": "node-gyp rebuild"
},
"private": false
}
56 changes: 56 additions & 0 deletions pfio.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <node.h>
#include <v8.h>
#include <nan.h>
#include <libpiface-1.0/pfio.h>

using namespace v8;

void PfioInit(const v8::FunctionCallbackInfo<v8::Value>& args) {
pfio_init();
}

void PfioDeinit(const v8::FunctionCallbackInfo<v8::Value>& args) {
pfio_deinit();
}

void PfioDigitalRead(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t pin = Integer::New(isolate, args[0]->IntegerValue(Nan::GetCurrentContext()).FromJust())->Value();
uint8_t val = pfio_digital_read(pin);
args.GetReturnValue().Set(val);
}

void PfioDigitalWrite(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t pin = Integer::New(isolate, args[0]->IntegerValue(Nan::GetCurrentContext()).FromJust())->Value();
uint8_t val = Integer::New(isolate, args[1]->IntegerValue(Nan::GetCurrentContext()).FromJust())->Value();
pfio_digital_write(pin, val);
}

void PfioReadInput(const v8::FunctionCallbackInfo<v8::Value>& args) {
uint8_t val = pfio_read_input();
args.GetReturnValue().Set(val);
}

void PfioReadOutput(const v8::FunctionCallbackInfo<v8::Value>& args) {
uint8_t val = pfio_read_output();
args.GetReturnValue().Set(val);
}

void PfioWriteOutput(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t val = Integer::New(isolate, args[0]->IntegerValue(Nan::GetCurrentContext()).FromJust())->Value();
pfio_write_output(val);
}

void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "init", PfioInit);
NODE_SET_METHOD(exports, "deinit", PfioDeinit);
NODE_SET_METHOD(exports, "digital_read", PfioDigitalRead);
NODE_SET_METHOD(exports, "digital_write", PfioDigitalWrite);
NODE_SET_METHOD(exports, "read_input", PfioReadInput);
NODE_SET_METHOD(exports, "read_output", PfioReadOutput);
NODE_SET_METHOD(exports, "write_output", PfioWriteOutput);
}

NODE_MODULE(pfio, init);

0 comments on commit 8d780a3

Please sign in to comment.