1
0
mirror of https://github.com/co-dan/ocaml-wiringpi/ synced 2025-12-15 13:33:51 +01:00

4 Commits

Author SHA1 Message Date
dan
0beb92d299 add an example 2019-02-09 23:43:08 +01:00
dan
5b9eb6b429 typed interface for the GPIO pins on RPI 3 2019-02-09 17:54:26 +01:00
Marek Kubica
fb03eeee58 Merge pull request #4 from co-dan/master
Fix the `ocamlwiring_pull_up_dn_control` stub
2019-01-27 14:06:18 +01:00
1e32b9112a Fix the ocamlwiring_pull_up_dn_control stub 2019-01-27 13:58:05 +01:00
11 changed files with 183 additions and 47 deletions

16
.gitignore vendored
View File

@@ -1,4 +1,12 @@
/_build/
/_opam/
.merlin
*.install
*.mllib
*.mldylib
*.clib
META
Makefile
configure
_tags
setup.ml
setup.data
setup.log
myocamlbuild.ml
_build/

View File

@@ -1,11 +0,0 @@
.PHONY: all
all:
jbuilder build --dev
.PHONY: clean
clean:
jbuilder clean
.PHONY: test
test:
jbuilder runtest --force

20
_oasis Normal file
View File

@@ -0,0 +1,20 @@
OASISFormat: 0.4
Name: WiringPi
Version: 0.0.1
Synopsis: Binding to the WiringPi library for hardware access
Authors: Tobias Bora, Marek Kubica
License: LGPL-3 with OCaml linking exception
Plugins: META (0.4), DevFiles (0.4)
Library "WiringPi"
Path: src/
BuildTools: ocamlbuild
Modules: WiringPi, Gpio3
CSources: WiringPi_stubs.c
CCLib: -lwiringPi
Executable "wpi-button-example"
Path: examples/
BuildTools: ocamlbuild
MainIs: button.ml
BuildDepends: WiringPi,unix

22
examples/button.ml Normal file
View File

@@ -0,0 +1,22 @@
open Gpio3
(* Set up the pins the following way: *)
(* pin 3 (pull-up) -> btn -> resistor -> gnd *)
let setup () =
Gpio3.setup ();
pin_mode GPIO3 IN;
pull_up_dn_control GPIO3 UP
let rec loop () =
(match digital_read GPIO3 with
| LOW -> print_endline "-- LOW"
| HIGH -> print_endline "++ HIGH");
Unix.sleepf 0.5;
loop ()
let _ =
setup ();
loop ();

View File

@@ -1,2 +0,0 @@
#use "topfind"
#require "topkg-jbuilder.auto"

95
src/Gpio3.ml Normal file
View File

@@ -0,0 +1,95 @@
(** Typed interface for WiritngPi.
Missing:
- modes PWM_OUTPUT and GPIO_GLOCK
- pwm_write
- digital_write_byte
- analog_read
- analog_write
- delay, delay_microseconds (?)
*)
open WiringPi
let setup () = ignore (setup_gpio ())
type pin =
(* 3V3 | 5V *)
GPIO2 | (* 5V *)
GPIO3 | (* GND *)
GPIO4 | GPIO14
(* GND *) | GPIO15 |
GPIO17 | GPIO18 |
GPIO27 | (* GND *)
GPIO22 | GPIO23
(* 3V3 *) | GPIO24 |
GPIO10 | (* GND *)
GPIO9 | GPIO25 |
GPIO11 | GPIO8
(* GND *) | GPIO7 |
GPIO0 | GPIO1 |
GPIO5 | (* GND *)
GPIO6 | GPIO12 |
GPIO13 | (* GND *)
GPIO19 | GPIO16 |
GPIO26 | GPIO20
let int_of_pin = function
| GPIO2 -> 2
| GPIO3 -> 3
| GPIO4 -> 4
| GPIO14 -> 14
| GPIO15 -> 15
| GPIO17 -> 17
| GPIO18 -> 18
| GPIO27 -> 27
| GPIO22 -> 22
| GPIO23 -> 23
| GPIO24 -> 24
| GPIO10 -> 10
| GPIO9 -> 9
| GPIO25 -> 25
| GPIO11 -> 11
| GPIO8 -> 8
| GPIO7 -> 7
| GPIO0 -> 0
| GPIO1 -> 1
| GPIO5 -> 5
| GPIO6 -> 6
| GPIO12 -> 12
| GPIO13 -> 13
| GPIO19 -> 19
| GPIO16 -> 16
| GPIO26 -> 26
| GPIO20 -> 20
(* What is not supported: PWM_OUTPUT and GPIO_GLOCK *)
type mode = IN | OUT
(** [pin_mode] sets the mode of the pin to either input or output
(other modes not supported at this moment). *)
let pin_mode (p : pin) (m : mode) =
match m with
| IN -> WiringPi.pin_mode (int_of_pin p) 0
| OUT -> WiringPi.pin_mode (int_of_pin p) 1
type pin_updn = UP | DOWN | OFF
(** [pull_up_dn_control] sets the pull-up or pull-down resistor mode on
the given pin, which should be set as an input. *)
let pull_up_dn_control (p : pin) = function
| UP -> WiringPi.pull_up_dn_control (int_of_pin p) 2
| DOWN -> WiringPi.pull_up_dn_control (int_of_pin p) 1
| OFF -> WiringPi.pull_up_dn_control (int_of_pin p) 0
type pin_value = LOW | HIGH
(** [digital_write] the value HIGH or LOW to the given pin which must
have been previously set as an output. *)
let digital_write (p : pin) (v : pin_value) =
match v with
| LOW -> WiringPi.digital_write (int_of_pin p) 0
| HIGH -> WiringPi.digital_write (int_of_pin p) 1
let digital_read (p : pin) : pin_value =
match WiringPi.digital_read (int_of_pin p) with
| 0 -> LOW
| _ -> HIGH

33
src/Gpio3.mli Normal file
View File

@@ -0,0 +1,33 @@
(** broadcom numbers *)
val setup : unit -> unit
type pin =
(* 3V3 | 5V *)
GPIO2 | (* 5V *)
GPIO3 | (* GND *)
GPIO4 | GPIO14
(* GND *) | GPIO15 |
GPIO17 | GPIO18 |
GPIO27 | (* GND *)
GPIO22 | GPIO23
(* 3V3 *) | GPIO24 |
GPIO10 | (* GND *)
GPIO9 | GPIO25 |
GPIO11 | GPIO8
(* GND *) | GPIO7 |
GPIO0 | GPIO1 |
GPIO5 | (* GND *)
GPIO6 | GPIO12 |
GPIO13 | (* GND *)
GPIO19 | GPIO16 |
GPIO26 | GPIO20
type pin_value = LOW | HIGH
type mode = IN | OUT
type pin_updn = UP | DOWN | OFF
val pin_mode : pin -> mode -> unit
val pull_up_dn_control : pin -> pin_updn -> unit
val digital_write : pin -> pin_value -> unit
val digital_read : pin -> pin_value

View File

@@ -45,7 +45,7 @@ value ocamlwiring_pin_mode(value pin, value mode)
value ocamlwiring_pull_up_dn_control(value pin, value pud)
{
CAMLparam2(pin, pud);
pullUpDnControl(Int_val(pin), Int_val(pin));
pullUpDnControl(Int_val(pin), Int_val(pud));
CAMLreturn(Val_unit);
}

View File

@@ -1,3 +0,0 @@
(library
((name wiringpi)
(public_name wiringpi)))

View File

@@ -1,10 +0,0 @@
WiringPi for OCaml, low level Raspberry Pi hardware access
WiringPi is a library that allows easy, Arduino-like access to hardware
functionality (GPIO pins mostly) of the Raspberry Pi minicomputer. That library
is written in C so what this package does is to provide a simple interface to
use the C library from OCaml. The API is unchanged (even the capitalization) to
provide an interface which is as similar as possible to the code that it is
based upon.
Please note that you need to install the WiringPi library first, as it is not
included in this package. Otherwise compilation will fail.

View File

@@ -1,16 +0,0 @@
opam-version: "1.2"
maintainer: "marek@xivilization.net"
homepage: "https://github.com/Leonidas-from-XIV/ocaml-wiringpi"
license: "LGPL-3 with OCaml linking exception"
build: ["jbuilder" "build" "-p" name "-j" jobs]
depends: [
"jbuilder" {build}
]
post-messages: [
"
This package requires WiringPi development files installed.
Tentative instructions : https://gist.githubusercontent.com/Leonidas-from-XIV/a1a7315ac01f7fbee3f0/raw
"
{failure}
]
available: os = "linux"