From 8d780a3c2c53ef0abaa3188426c917d6e4b6eca0 Mon Sep 17 00:00:00 2001 From: Rick Bullotta Date: Wed, 23 Oct 2019 13:35:23 -0400 Subject: [PATCH] Initial commit Initial commit of piface-node-12 --- LICENSE.txt | 24 ++++++++++++++++++++++ README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++ binding.gyp | 14 +++++++++++++ package.json | 29 +++++++++++++++++++++++++++ pfio.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+) create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 binding.gyp create mode 100644 package.json create mode 100644 pfio.cc diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..0ee9155 --- /dev/null +++ b/LICENSE.txt @@ -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. + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2650dd3 --- /dev/null +++ b/README.md @@ -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 +```` diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..313b956 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,14 @@ +{ + "targets": [ + { + "target_name": "pfio", + 'include_dirs': [ '/usr/local/include', "= 12.0.0" + }, + "dependencies": { + "nan": "^2.13.0", + "node-gyp": "^6.0.0" + }, + "scripts": { + "install": "node-gyp rebuild" + }, + "private": false +} diff --git a/pfio.cc b/pfio.cc new file mode 100644 index 0000000..1f1df70 --- /dev/null +++ b/pfio.cc @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace v8; + +void PfioInit(const v8::FunctionCallbackInfo& args) { + pfio_init(); +} + +void PfioDeinit(const v8::FunctionCallbackInfo& args) { + pfio_deinit(); +} + +void PfioDigitalRead(const v8::FunctionCallbackInfo& 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& 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& args) { + uint8_t val = pfio_read_input(); + args.GetReturnValue().Set(val); +} + +void PfioReadOutput(const v8::FunctionCallbackInfo& args) { + uint8_t val = pfio_read_output(); + args.GetReturnValue().Set(val); +} + +void PfioWriteOutput(const v8::FunctionCallbackInfo& 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 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);