Skip to content

Commit

Permalink
Wrap the soft PWM library
Browse files Browse the repository at this point in the history
  • Loading branch information
klajo committed Aug 25, 2012
1 parent 5a82d6d commit 354a251
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Functionality
* read from, write to and control pins
* control and write to LCDs
* shift in/out bits (untested)
* soft PWM

Caveats
-------
Expand Down
50 changes: 49 additions & 1 deletion c_src/wpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
#include <wiringPi.h>
#include <lcd.h>
#include <wiringShift.h>
#include <softPwm.h>

static ERL_NIF_TERM atom_ok;
static ERL_NIF_TERM atom_error;

static int
load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{
atom_ok = enif_make_atom(env, "ok");
atom_error = enif_make_atom(env, "error");
return wiringPiSetup(); // returns -1 in case of error ==> loading fails
}

Expand Down Expand Up @@ -221,6 +226,46 @@ shift_out_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return atom_ok;
}

// soft PWM
static ERL_NIF_TERM
soft_pwm_create_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int pin, init_value, range, result;
ERL_NIF_TERM atom_fail, err_code;
if (!enif_get_int(env, argv[0], &pin) ||
!enif_get_int(env, argv[1], &init_value) ||
!enif_get_int(env, argv[2], &range))
{
return enif_make_badarg(env);
}
result = softPwmCreate(pin, init_value, range);
if (result)
{
atom_fail = enif_make_atom(env, "failed_to_init_pwm_pin");
err_code = enif_make_int(env, result);
return enif_make_tuple2(env,
atom_error,
enif_make_tuple2(env, atom_fail, err_code));
}
else
{
return atom_ok;
}
}

static ERL_NIF_TERM
soft_pwm_write_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int pin, value;
if (!enif_get_int(env, argv[0], &pin) ||
!enif_get_int(env, argv[1], &value))
{
return enif_make_badarg(env);
}
softPwmWrite(pin, value);
return atom_ok;
}

static ErlNifFunc nif_funcs[] =
{
// the basics: pins and stuff
Expand All @@ -238,7 +283,10 @@ static ErlNifFunc nif_funcs[] =
{"lcd_puts_nif", 3, lcd_puts_nif},
// shift
{"shift_in_nif", 3, shift_in_nif},
{"shift_out_nif", 4, shift_out_nif}
{"shift_out_nif", 4, shift_out_nif},
// soft pwm
{"soft_pwm_create_nif", 3, soft_pwm_create_nif},
{"soft_pwm_write_nif", 2, soft_pwm_write_nif}
};

ERL_NIF_INIT(wpi, nif_funcs, load, NULL, NULL, NULL)
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{port_env, [{"LDFLAGS", "$LDFLAGS -lwiringPi"}]}.
{port_env, [{"LDFLAGS", "$LDFLAGS -lwiringPi -lpthread"}]}.
17 changes: 17 additions & 0 deletions src/wpi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
-export([shift_in/3]).
-export([shift_out/4]).

%% soft PWM
-export([soft_pwm_create/3]).
-export([soft_pwm_write/2]).

-define(nif_stub,
erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE})).

Expand Down Expand Up @@ -205,3 +209,16 @@ shift_out(DataPin, ClockPin, Order, Value)

shift_in_nif(_DataPin, _ClockPin, _Order) -> ?nif_stub.
shift_out_nif(_DataPin, _ClockPin, _Order, _Value) -> ?nif_stub.

-spec soft_pwm_create(wpi_pin_number(), integer(), integer()) -> ok.
soft_pwm_create(Pin, InitValue, Range) when is_integer(Pin),
is_integer(InitValue),
is_integer(Range) ->
soft_pwm_create_nif(Pin, InitValue, Range).

-spec soft_pwm_write(wpi_pin_number(), integer()) -> ok.
soft_pwm_write(Pin, Value) when is_integer(Pin), is_integer(Value) ->
soft_pwm_write_nif(Pin, Value).

soft_pwm_create_nif(_Pin, _InitValue, _Range) -> ?nif_stub.
soft_pwm_write_nif(_Pin, _Value) -> ?nif_stub.

0 comments on commit 354a251

Please sign in to comment.