Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First version of a WiringPi NIF

  • Loading branch information...
commit f26bc78a607bc5426a388f732506abc6bd885d13 0 parents
@klajo authored
3  .gitignore
@@ -0,0 +1,3 @@
+priv/
+ebin/
+*.o
46 README.md
@@ -0,0 +1,46 @@
+wpi - an Erlang NIF for the WiringPi library for the Raspberry Pi
+=================================================================
+
+This application is an Erlang wrapper around the [WiringPi][1] library.
+
+Getting started
+---------------
+
+Start by [downloading and installing the WiringPi][2] library.
+
+Then it should be possible to build wpi using [rebar][3].
+
+ rebar compile
+
+Before starting, you need to include a hrl file which defines some
+useful constants (matches those of WiringPi):
+
+ -include_lib("wpi/include/wpi.hrl").
+
+Then you can start setting up the pins and use them:
+
+ Pin = 4,
+ wpi:gpio_mode(?WPI_MODE_GPIO),
+ wpi:pin_mode(Pin, ?WPI_OUTPUT),
+ wpi:digital_write(Pin, ?WPI_HIGH),
+
+Caveats
+-------
+
+This NIF is entirely experimental - use at your own risk. It has been
+used to both write to (LED) and read from (button) pins successfully,
+while some functionality may be untested.
+
+Contributions
+-------------
+
+Contributions are more than welcome.
+
+Credits
+-------
+
+Credits go to Gordon Henderson for the WiringPi library.
+
+[1]: https://projects.drogon.net/raspberry-pi/wiringpi/
+[2]: https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/
+[3]: https://github.com/basho/rebar/
124 c_src/wpi.c
@@ -0,0 +1,124 @@
+// An Erlang NIF which wraps the WiringPi library (by Gordon Henderson).
+// Copyright (C) 2012 Klas Johansson
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include "erl_nif.h"
+#include <wiringPi.h>
+
+static int
+load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
+{
+ return wiringPiSetup(); // returns -1 in case of error ==> loading fails
+}
+
+static ERL_NIF_TERM
+gpio_mode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int mode;
+ if (!enif_get_int(env, argv[0], &mode))
+ {
+ return enif_make_badarg(env);
+ }
+ wiringPiGpioMode(mode);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM
+pin_mode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int pin, mode;
+ if (!enif_get_int(env, argv[0], &pin))
+ {
+ return enif_make_badarg(env);
+ }
+ if (!enif_get_int(env, argv[1], &mode))
+ {
+ return enif_make_badarg(env);
+ }
+ pinMode(pin, mode);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM
+digital_write(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int pin, value;
+ if (!enif_get_int(env, argv[0], &pin))
+ {
+ return enif_make_badarg(env);
+ }
+ if (!enif_get_int(env, argv[1], &value))
+ {
+ return enif_make_badarg(env);
+ }
+ digitalWrite(pin, value);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM
+pwm_write(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int pin, value;
+ if (!enif_get_int(env, argv[0], &pin))
+ {
+ return enif_make_badarg(env);
+ }
+ if (!enif_get_int(env, argv[1], &value))
+ {
+ return enif_make_badarg(env);
+ }
+ pwmWrite(pin, value);
+ return enif_make_atom(env, "ok");
+}
+
+static ERL_NIF_TERM
+digital_read(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int pin, value;
+ if (!enif_get_int(env, argv[0], &pin))
+ {
+ return enif_make_badarg(env);
+ }
+ value = digitalRead(pin);
+ return enif_make_int(env, value);
+}
+
+static ERL_NIF_TERM
+pull_up_dn_control(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int pin, mode;
+ if (!enif_get_int(env, argv[0], &pin))
+ {
+ return enif_make_badarg(env);
+ }
+ if (!enif_get_int(env, argv[1], &mode))
+ {
+ return enif_make_badarg(env);
+ }
+ pullUpDnControl(pin, mode);
+ return enif_make_atom(env, "ok");
+}
+
+static ErlNifFunc nif_funcs[] =
+ {
+ {"gpio_mode", 1, gpio_mode},
+ {"pin_mode", 2, pin_mode},
+ {"digital_write", 2, digital_write},
+ {"pwm_write", 2, pwm_write},
+ {"digital_read", 1, digital_read},
+ {"pull_up_dn_control", 2, pull_up_dn_control}
+ };
+
+ERL_NIF_INIT(wpi, nif_funcs, load, NULL, NULL, NULL)
34 include/wpi.hrl
@@ -0,0 +1,34 @@
+%%% An Erlang NIF which wraps the WiringPi library (by Gordon Henderson).
+%%% Copyright (C) 2012 Klas Johansson
+%%%
+%%% This program is free software: you can redistribute it and/or modify
+%%% it under the terms of the GNU General Public License as published by
+%%% the Free Software Foundation, either version 3 of the License, or
+%%% (at your option) any later version.
+%%%
+%%% This program is distributed in the hope that it will be useful,
+%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
+%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+%%% GNU General Public License for more details.
+%%%
+%%% You should have received a copy of the GNU General Public License
+%%% along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+-ifndef(WPI_HRL).
+-define(WPI_HRL, true).
+
+-define(WPI_MODE_PINS, 0).
+-define(WPI_MODE_GPIO, 1).
+
+-define(WPI_INPUT, 0).
+-define(WPI_OUTPUT, 1).
+-define(WPI_PWM_OUTPUT, 2).
+
+-define(WPI_LOW, 0).
+-define(WPI_HIGH, 1).
+
+-define(WPI_PUD_OFF, 0).
+-define(WPI_PUD_DOWN, 1).
+-define(WPI_PUD_UP, 2).
+
+-endif. % WPI_HRL
1  rebar.config
@@ -0,0 +1 @@
+{port_env, [{"LDFLAGS", "$LDFLAGS -lwiringPi"}]}.
9 src/wpi.app.src
@@ -0,0 +1,9 @@
+%% -*- erlang -*-
+{application, wpi,
+ [{description, "NIF which wraps the wiringPi library for raspberry pi"},
+ {vsn, "0.1"},
+ {registered, []},
+ {applications, [kernel,
+ stdlib]},
+ {env, []}
+ ]}.
65 src/wpi.erl
@@ -0,0 +1,65 @@
+%%% An Erlang NIF which wraps the WiringPi library (by Gordon Henderson).
+%%% Copyright (C) 2012 Klas Johansson
+%%%
+%%% This program is free software: you can redistribute it and/or modify
+%%% it under the terms of the GNU General Public License as published by
+%%% the Free Software Foundation, either version 3 of the License, or
+%%% (at your option) any later version.
+%%%
+%%% This program is distributed in the hope that it will be useful,
+%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
+%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+%%% GNU General Public License for more details.
+%%%
+%%% You should have received a copy of the GNU General Public License
+%%% along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+-module(wpi).
+
+-include_lib("wpi/include/wpi.hrl").
+
+-export([gpio_mode/1]).
+-export([pin_mode/2]).
+-export([digital_write/2]).
+-export([pwm_write/2]).
+-export([digital_read/1]).
+-export([pull_up_dn_control/2]).
+
+-define(nif_stub,
+ erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE})).
+
+-on_load(on_load/0).
+
+-type wpi_gpio_mode() :: 0..1. % WPI_MODE_PINS|WPI_MODE_GPIO
+-type wpi_pin_mode() :: 0..2. % WPI_INPUT|WPI_OUTPUT|WPI_PWM_OUTPUT
+-type wpi_pin_number() :: integer().
+-type wpi_digital_value() :: 0..1. % WPI_LOW|WPI_HIGH
+-type wpi_pwm_value() :: 0..1024.
+-type wpi_pud_mode() :: 0..2. % WPI_PUD_OFF|WPI_PUD_DOWN|WPI_PUD_DOWN
+
+on_load() ->
+ ok = erlang:load_nif(filename:join(code:priv_dir(wpi), "./wpi_drv"), 0).
+
+-spec gpio_mode(wpi_gpio_mode()) -> ok.
+gpio_mode(_Mode) ->
+ ?nif_stub.
+
+-spec pin_mode(wpi_pin_number(), wpi_pin_mode()) -> ok.
+pin_mode(_Pin, _Mode) ->
+ ?nif_stub.
+
+-spec digital_write(wpi_pin_number(), wpi_digital_value()) -> ok.
+digital_write(_Pin, _Value) ->
+ ?nif_stub.
+
+-spec pwm_write(wpi_pin_number(), wpi_pwm_value()) -> ok.
+pwm_write(_Pin, _Value) ->
+ ?nif_stub.
+
+-spec digital_read(wpi_pin_number()) -> wpi_digital_value().
+digital_read(_Pin) ->
+ ?nif_stub.
+
+-spec pull_up_dn_control(wpi_pin_number(), wpi_pud_mode()) -> ok.
+pull_up_dn_control(_Pin, _Mode) ->
+ ?nif_stub.
Please sign in to comment.
Something went wrong with that request. Please try again.