Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Firmware/pong/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
NodeJS Wrapper for SparkFun Edison OLED Block
=============================================

Developing a node app for your Edison? Need to use the OLED display from SparkFun, GPIO pins or SPI? You're in luck!

Features

* OLED
* GPIO
* SPI

NOTES

* This has only been tested with the SparkFun Edison OLED Block, but GPIO and SPI should (may possibly) work with other hardware.
* SPI - need to use a CS pin on Edison other than the default pin 23 (GP110), the MRAA library won't work. This library works using a pin of your choosing (tested with pin 9 (GP111)).
* GPIO pin mraa /edison reference mapping may be useful - https://github.com/intel-iot-devkit/mraa/blob/master/docs/edison.md#intelr-breakout-board
* The files in the Tests folder are meant to be run on an Intel Edison as they interact with the hardware.

Installation
============
This module is only designed to work on the Intel Edison.

```
$ npm install edison-oled
```

You may need to use the --unsafe-perm option if you get the warning "cannot run in wd".

```
$ npm install edison-oled --unsafe-perm
```
API
============
Example using the OLED screen

```javascript
var edison = require('edison-oled');

//create Oled
var oled = new edison.Oled();

//setup input buttons
var btnUp = new edison.Gpio(47, edison.INPUT);
var btnDown = new edison.Gpio(44, edison.INPUT);
var btnLeft = new edison.Gpio(165, edison.INPUT);
var btnRight = new edison.Gpio(45, edison.INPUT);
var btnSelect = new edison.Gpio(48, edison.INPUT);
var btnA = new edison.Gpio(49, edison.INPUT);
var btnB = new edison.Gpio(46, edison.INPUT);

//setup screen
oled.begin();
oled.clear(0);
oled.display();
oled.setFontType(0);

//draw on screen
oled.clear();
oled.setCursor(14, 5);
oled.print("Press A");
oled.setCursor(2, 13);
oled.print("for single");
oled.setCursor(14, 30);
oled.print("Press B");
oled.setCursor(6, 38);
oled.print("for multi");
oled.display();

//wait for user to make a choice using Button A or Button B
while (btnA.pinRead() == edison.HIGH && btnB.pinRead() == edison.HIGH) {}

```

Example uses SPI and GPIO to communicate with OLED hardware (not a complete example, see test-spi.js for full code)

```javascript
var edison = require('edison-oled');

//setup pins needed for SPI
var cspin = new edison.Gpio(111, edison.OUTPUT, edison.HIGH); //chip select --> edison_oled.c code uses 111 mraa uses 9
var dcpin = new edison.Gpio(14, edison.OUTPUT, edison.HIGH); //data/command --> edison_oled.c code uses 14 mraa uses 36
var rstpin = new edison.Gpio(15, edison.OUTPUT, edison.HIGH); //reset --> edison_oled.c code uses 15 mraa uses 48
var sclkpin = new edison.Gpio(109, edison.SPI, edison.HIGH); //sclk --> edison_oled.c code uses 109 mraa uses 10
var mosipin = new edison.Gpio(115, edison.SPI, edison.HIGH); //mosi --> edison_oled.c code uses 115 mraa uses 11
var misopin = new edison.Gpio(114, edison.SPI, edison.HIGH); //miso --> edison_oled.c code uses 114 mraa uses 24

//create spi
var spi = new edison.Spi(edison.SPI_MODE_0, 10000000, false, cspin);

//reset screen
rstpin.pinWrite(edison.HIGH);
//should add a wait time here for a few milliseconds
rstpin.pinWrite(edison.LOW);
//should add a wait time here for a few milliseconds
rstpin.pinWrite(edison.HIGH);

//send commands to OLED
dcpin.pinWrite(edison.LOW); // DC pin LOW
spi.transferData(edison.DISPLAYOFF);
spi.transferData(edison.SETDISPLAYCLOCKDIV);
spi.transferData(0x80);

//send data to OLED
dcpin.pinWrite(edison.HIGH); //DC pin HIGH
spi.transferData(0xFF);

```

SparkFun Edison OLED Block
===========================

![SparkFun Edison OLED Block](https://cdn.sparkfun.com//assets/parts/1/0/0/3/6/13035-01.jpg)

[*SparkFun Edison OLED Block(DEV-13035)*](https://www.sparkfun.com/products/13035)

Equip your Edison with a graphic display using the Edison OLED board! This board features a 0.66", 64x48 pixel monochrome OLED.

To add some control over your Edison and the OLED, this board also includes a small joystick and a pair of push-buttons. Use them to create a game, file navigator, or more!

Repository Contents
-------------------
* **/Firmware** - Example Pong sketch to demonstrate OLED Functionality.
* **/Hardware** - All Eagle design files (.brd, .sch)
* **/Production** - Test bed files and production panel files

Documentation
--------------
* **[Hookup Guide](https://learn.sparkfun.com/tutorials/sparkfun-blocks-for-intel-edison---oled-block)** - Basic hookup guide for the OLED Block.
* **[SparkFun Fritzing repo](https://github.com/sparkfun/Fritzing_Parts)** - Fritzing diagrams for SparkFun products.
* **[SparkFun 3D Model repo](https://github.com/sparkfun/3D_Models)** - 3D models of SparkFun products.
* **[SparkFun Graphical Datasheets](https://github.com/sparkfun/Graphical_Datasheets)** -Graphical Datasheets for various SparkFun products.


License Information
-------------------

This product is _**open source**_!

Please review the LICENSE.md file for license information.

If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com.

Distributed as-is; no warranty is given.

- Your friends at SparkFun.
13 changes: 13 additions & 0 deletions Firmware/pong/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
'targets': [
{
'target_name': 'edisonnodeaddon',
'sources': [ 'oled/edison_oled_wrap.cpp', 'spi/edison_spi_wrap.cpp', 'gpio/edison_gpio_wrap.cpp', 'gpio/gpio_edison.cpp', 'spi/spi_port_edison.cpp', 'spi/spi_device_edison.cpp', 'oled/Edison_OLED.cpp', 'edison_node_addon.cpp' ],
'include_dirs': [
"<!(node -e \"require('nan')\")"
],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
}
]
}
7 changes: 7 additions & 0 deletions Firmware/pong/edison_node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef EDISON_NODE_H
#define EDISON_NODE_H

#define ERROR_WRONG_NUMBER_OF_ARGUMENTS "Wrong number of arguments"
#define ERROR_ARGUMENTS_WRONG_TYPE "Arguments are of the wrong data type"
#define ERROR_CREATE_INSTANCE_WITH_NEW "Use new to create a new instance."
#endif
37 changes: 37 additions & 0 deletions Firmware/pong/edison_node_addon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/******************************************************************************
MIT License

Copyright (c) 2017 Brian Feldman

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.
******************************************************************************/
#include <nan.h>
#include "oled/edison_oled_wrap.hpp"
#include "gpio/edison_gpio_wrap.hpp"
#include "spi/edison_spi_wrap.hpp"

using namespace v8;

void InitAll (v8::Local<v8::Object> exports) {
EdisonOledWrap::Init(exports);
EdisonGpioWrap::Init(exports);
EdisonSpiWrap::Init(exports);
}

NODE_MODULE(edisonoledaddon, InitAll)
146 changes: 146 additions & 0 deletions Firmware/pong/gpio/edison_gpio_wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/******************************************************************************
MIT License

Copyright (c) 2017 Brian Feldman

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.
******************************************************************************/
#include "../edison_node.hpp"
#include "edison_gpio_wrap.hpp"
#include "gpio.h"
#include "gpio_edison.h"
#include <node.h>

using namespace v8;

Nan::Persistent<v8::Function> EdisonGpioWrap::constructor;

EdisonGpioWrap::EdisonGpioWrap() : m_gpio() {
}

EdisonGpioWrap::~EdisonGpioWrap() {
}

void EdisonGpioWrap::Init(v8::Local<v8::Object> exports) {
Nan::HandleScope scope;

// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Gpio").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

// Prototype
Nan::SetPrototypeMethod(tpl, "pinMode", pinMode);
Nan::SetPrototypeMethod(tpl, "pinWrite", pinWrite);
Nan::SetPrototypeMethod(tpl, "pinRead", pinRead);

/* constants */
/* PIN_MODE*/
NODE_DEFINE_CONSTANT(exports, INPUT);
NODE_DEFINE_CONSTANT(exports, OUTPUT);
NODE_DEFINE_CONSTANT(exports, INPUT_PU);
NODE_DEFINE_CONSTANT(exports, INPUT_PD);
NODE_DEFINE_CONSTANT(exports, SPI);
NODE_DEFINE_CONSTANT(exports, I2C);
NODE_DEFINE_CONSTANT(exports, PWM);
NODE_DEFINE_CONSTANT(exports, ADC);
NODE_DEFINE_CONSTANT(exports, UART);

/* PIN_VALUE */
NODE_DEFINE_CONSTANT(exports, LOW);
NODE_DEFINE_CONSTANT(exports, HIGH);
NODE_DEFINE_CONSTANT(exports, NONE);

constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("Gpio").ToLocalChecked(), tpl->GetFunction());
}

void EdisonGpioWrap::New(const Nan::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 2 || args.Length() > 3) {
Nan::ThrowTypeError(ERROR_WRONG_NUMBER_OF_ARGUMENTS);
return;
}
int pinID = args[0]->Uint32Value();
PIN_MODE mode = static_cast<PIN_MODE>(args[1]->Uint32Value());
PIN_VALUE value = LOW;
if (args.Length() == 3) {
value = static_cast<PIN_VALUE>(args[2]->Uint32Value());
}
if (args.IsConstructCall()) {
// Invoked as constructor: `new EdisonGpioWrap(...)`
EdisonGpioWrap* obj = new EdisonGpioWrap();
//gpio *gpioObj = new gpio(args[0]->Uint32Value(), static_cast<PIN_MODE>(args[1]->Uint32Value()), static_cast<PIN_VALUE>(args[2]->Uint32Value()));
gpio *gpioObj = new gpio(pinID, mode, value);
obj->m_gpio = gpioObj;
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
// Invoked as plain function `EdisonGpioWrap(...)`, turn into construct call.
//v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
//args.GetReturnValue().Set(cons->NewInstance());
Nan::ThrowTypeError(ERROR_CREATE_INSTANCE_WITH_NEW);
}
}

gpio* EdisonGpioWrap::getGpio() {
// this is needed by SPI wrapper
return m_gpio;
}

void EdisonGpioWrap::setGpio(gpio *pin) {
// this is needed by SPI wrapper
m_gpio = pin;
}

void EdisonGpioWrap::pinMode(const Nan::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
Nan::ThrowTypeError(ERROR_WRONG_NUMBER_OF_ARGUMENTS);
return;
}
if (!args[0]->IsNumber()) {
Nan::ThrowTypeError(ERROR_ARGUMENTS_WRONG_TYPE);
return;
}
EdisonGpioWrap* wrap = ObjectWrap::Unwrap<EdisonGpioWrap>(args.Holder());
gpio* gpioObj = wrap->m_gpio;
gpioObj->pinMode(static_cast<PIN_MODE>(args[0]->Uint32Value()));
args.GetReturnValue().SetUndefined();
}

void EdisonGpioWrap::pinWrite(const Nan::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
Nan::ThrowTypeError(ERROR_WRONG_NUMBER_OF_ARGUMENTS);
return;
}
if (!args[0]->IsNumber()) {
Nan::ThrowTypeError(ERROR_ARGUMENTS_WRONG_TYPE);
return;
}
EdisonGpioWrap* wrap = ObjectWrap::Unwrap<EdisonGpioWrap>(args.Holder());
gpio* gpioObj = wrap->m_gpio;
gpioObj->pinWrite(static_cast<PIN_VALUE >(args[0]->Uint32Value()));
args.GetReturnValue().SetUndefined();
}

void EdisonGpioWrap::pinRead(const Nan::FunctionCallbackInfo<v8::Value>& args) {
EdisonGpioWrap* wrap = ObjectWrap::Unwrap<EdisonGpioWrap>(args.Holder());
gpio* gpioObj = wrap->m_gpio;
int out = gpioObj->pinRead();
args.GetReturnValue().Set(Nan::New<Integer>(out));
}
30 changes: 30 additions & 0 deletions Firmware/pong/gpio/edison_gpio_wrap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef EDISON_GPIO_WRAP_H
#define EDISON_GPIO_WRAP_H

#include <nan.h>

#include "../edison_node.hpp"
#include "gpio.h"
#include "gpio_edison.h"

class EdisonGpioWrap : public Nan::ObjectWrap {
public:
static void Init(v8::Local<v8::Object> exports);
static void New(const Nan::FunctionCallbackInfo<v8::Value>& args);

gpio* getGpio();
void setGpio(gpio *pin);

EdisonGpioWrap();
private:
~EdisonGpioWrap();

static void pinMode(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void pinWrite(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void pinRead(const Nan::FunctionCallbackInfo<v8::Value>& args);

gpio* m_gpio;
static Nan::Persistent<v8::Function> constructor;
};

#endif
1 change: 1 addition & 0 deletions Firmware/pong/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/Release/edisonnodeaddon');
Loading