Permalink
Browse files

Wrap the shift library

This is untested, use with care.
  • Loading branch information...
1 parent 26af73d commit 17d1c71b30a9273d4ee9a5e0b29ac0d71107908e @klajo committed Aug 25, 2012
Showing with 71 additions and 2 deletions.
  1. +2 −1 README.md
  2. +35 −1 c_src/wpi.c
  3. +3 −0 include/wpi.hrl
  4. +31 −0 src/wpi.erl
View
@@ -38,11 +38,12 @@ the library may have another way of handling this.
wiringPiSetup: Unable to open /dev/mem: Permission denied
-Supported functionality
+Functionality
-----------------------
* read from, write to and control pins
* control and write to LCDs
+* shift in/out bits (untested)
Caveats
-------
View
@@ -21,6 +21,7 @@
#include "erl_nif.h"
#include <wiringPi.h>
#include <lcd.h>
+#include <wiringShift.h>
static int
load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
@@ -189,6 +190,36 @@ lcd_puts_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return enif_make_atom(env, "ok");
}
+static ERL_NIF_TERM
+shift_in_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int data_pin, clock_pin, order, value;
+ if (!enif_get_int(env, argv[0], &data_pin) ||
+ !enif_get_int(env, argv[1], &clock_pin) ||
+ !enif_get_int(env, argv[2], &order))
+ {
+ return enif_make_badarg(env);
+ }
+ value = shiftIn((uint8_t)data_pin, (uint8_t)clock_pin, (uint8_t)order);
+ return enif_make_int(env, value);
+}
+
+static ERL_NIF_TERM
+shift_out_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int data_pin, clock_pin, order, value;
+ if (!enif_get_int(env, argv[0], &data_pin) ||
+ !enif_get_int(env, argv[1], &clock_pin) ||
+ !enif_get_int(env, argv[2], &order) ||
+ !enif_get_int(env, argv[3], &value))
+ {
+ return enif_make_badarg(env);
+ }
+ shiftOut((uint8_t)data_pin, (uint8_t)clock_pin,
+ (uint8_t)order, (uint8_t)value);
+ return enif_make_atom(env, "ok");
+}
+
static ErlNifFunc nif_funcs[] =
{
// the basics: pins and stuff
@@ -203,7 +234,10 @@ static ErlNifFunc nif_funcs[] =
{"lcd_clear_nif", 1, lcd_clear_nif},
{"lcd_position_nif", 3, lcd_position_nif},
{"lcd_put_char_nif", 2, lcd_put_char_nif},
- {"lcd_puts_nif", 3, lcd_puts_nif}
+ {"lcd_puts_nif", 3, lcd_puts_nif},
+ // shift
+ {"shift_in_nif", 3, shift_in_nif},
+ {"shift_out_nif", 4, shift_out_nif}
};
ERL_NIF_INIT(wpi, nif_funcs, load, NULL, NULL, NULL)
View
@@ -30,4 +30,7 @@
-define(WPI_PUD_DOWN, 1).
-define(WPI_PUD_UP, 2).
+-define(WPI_LSB_FIRST, 0).
+-define(WPI_MSB_FIRST, 1).
+
-endif. % WPI_HRL
View
@@ -37,6 +37,10 @@
-export([lcd_printf/3]).
-export([lcd_format/3]).
+%% shift
+-export([shift_in/3]).
+-export([shift_out/4]).
+
-define(nif_stub,
erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE})).
@@ -50,6 +54,9 @@
-type wpi_pud_mode() :: 0..2 % WPI_PUD_OFF | WPI_PUD_DOWN | WPI_PUD_UP
| off | down | up.
-opaque wpi_lcd_handle() :: integer().
+-type wpi_bit_order() :: 0..1 % WPI_LSB_FIRST | WPI_MSB_FIRST
+ | lsb_first | msb_first.
+-type wpi_uint8() :: 0..255.
on_load() ->
ok = erlang:load_nif(filename:join(code:priv_dir(wpi), "./wpi_drv"), 0).
@@ -174,3 +181,27 @@ lcd_clear_nif(_Handle) -> ?nif_stub.
lcd_position_nif(_Handle, _X, _Y) -> ?nif_stub.
lcd_put_char_nif(_Handle, _Char) -> ?nif_stub.
lcd_puts_nif(_Handle, _StringLen, _String) -> ?nif_stub.
+
+-spec shift_in(wpi_pin_number(), wpi_pin_number(), wpi_bit_order()) ->
+ wpi_uint8().
+shift_in(DataPin, ClockPin, lsb_first) ->
+ shift_in(DataPin, ClockPin, ?WPI_LSB_FIRST);
+shift_in(DataPin, ClockPin, msb_first) ->
+ shift_in(DataPin, ClockPin, ?WPI_MSB_FIRST);
+shift_in(DataPin, ClockPin, Order)
+ when is_integer(DataPin), is_integer(ClockPin), is_integer(Order) ->
+ shift_in_nif(DataPin, ClockPin, Order).
+
+-spec shift_out(wpi_pin_number(), wpi_pin_number(), wpi_bit_order(),
+ wpi_uint8()) -> ok.
+shift_out(DataPin, ClockPin, lsb_first, Value) ->
+ shift_out(DataPin, ClockPin, ?WPI_LSB_FIRST, Value);
+shift_out(DataPin, ClockPin, msb_first, Value) ->
+ shift_out(DataPin, ClockPin, ?WPI_MSB_FIRST, Value);
+shift_out(DataPin, ClockPin, Order, Value)
+ when is_integer(DataPin), is_integer(ClockPin), is_integer(Order),
+ is_integer(Value) ->
+ shift_out_nif(DataPin, ClockPin, Order, Value).
+
+shift_in_nif(_DataPin, _ClockPin, _Order) -> ?nif_stub.
+shift_out_nif(_DataPin, _ClockPin, _Order, _Value) -> ?nif_stub.

0 comments on commit 17d1c71

Please sign in to comment.