Compare commits
No commits in common. "gpio3" and "0.0.1" have entirely different histories.
7
_oasis
7
_oasis
|
@ -9,12 +9,7 @@ Plugins: META (0.4), DevFiles (0.4)
|
||||||
Library "WiringPi"
|
Library "WiringPi"
|
||||||
Path: src/
|
Path: src/
|
||||||
BuildTools: ocamlbuild
|
BuildTools: ocamlbuild
|
||||||
Modules: WiringPi, Gpio3
|
Modules: WiringPi
|
||||||
CSources: WiringPi_stubs.c
|
CSources: WiringPi_stubs.c
|
||||||
CCLib: -lwiringPi
|
CCLib: -lwiringPi
|
||||||
|
|
||||||
Executable "wpi-button-example"
|
|
||||||
Path: examples/
|
|
||||||
BuildTools: ocamlbuild
|
|
||||||
MainIs: button.ml
|
|
||||||
BuildDepends: WiringPi,unix
|
|
|
@ -1,22 +0,0 @@
|
||||||
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 ();
|
|
||||||
|
|
95
src/Gpio3.ml
95
src/Gpio3.ml
|
@ -1,95 +0,0 @@
|
||||||
(** 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
|
|
|
@ -1,33 +0,0 @@
|
||||||
(** 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
|
|
|
@ -1,53 +1,53 @@
|
||||||
(** This module is useful to communicate with the GPIO ports of a Raspberry Pi.
|
(** This module is useful to communicate with the GPIO ports of a Raspberry Pi.
|
||||||
It uses the WiringPi library: http://wiringpi.com/ **)
|
It uses the WiringPi library: http://wiringpi.com/ **)
|
||||||
|
|
||||||
|
(* Test function *)
|
||||||
|
external test_hello_world : unit -> unit = "caml_hello"
|
||||||
|
|
||||||
(* Use it at the very beginning to choose the numeroting mode *)
|
(* Use it at the very beginning to choose the numeroting mode *)
|
||||||
external setup: unit -> int = "ocamlwiring_setup"
|
external setup : unit -> int = "caml_wiringPiSetup"
|
||||||
external setup_gpio: unit -> int = "ocamlwiring_setup_gpio"
|
external setupGio : unit -> int = "caml_wiringPiSetupGpio"
|
||||||
external setup_phys: unit -> int = "ocamlwiring_setup_phys"
|
external setupPhys : unit -> int = "caml_wiringPiSetupPhys"
|
||||||
external setup_sys: unit -> int = "ocamlwiring_setup_sys"
|
external setupSys : unit -> int = "caml_wiringPiSetupSys"
|
||||||
|
|
||||||
(* ########## Write on the device ########## *)
|
(* ########## Write on the device ########## *)
|
||||||
(* This sets the mode of a pin to either INPUT (= 0), OUTPUT (= 1),
|
(* This sets the mode of a pin to either INPUT (= 0), OUTPUT (= 1),
|
||||||
* PWM_OUTPUT (= 2) or GPIO_CLOCK (= 3).
|
* PWM_OUTPUT (= 2) or GPIO_CLOCK (= 3).
|
||||||
* Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only
|
* Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only
|
||||||
* wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output modes. *)
|
* wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output modes. *)
|
||||||
external pin_mode: int -> int -> unit = "ocamlwiring_pin_mode"
|
external pinMode : int -> int -> unit = "caml_pinMode"
|
||||||
|
|
||||||
(* This sets the pull-up or pull-down resistor mode on the given pin, which
|
(* This sets the pull-up or pull-down resistor mode on the given pin, which
|
||||||
* should be set as an input. *)
|
* should be set as an input. *)
|
||||||
external pull_up_dn_control: int -> int -> unit = "ocamlwiring_pull_up_dn_control"
|
external pullUpDnControl : int -> int -> unit = "caml_pullUpDnControl"
|
||||||
|
|
||||||
(* Writes the value HIGH or LOW (1 or 0) to the given pin which must have
|
(* Writes the value HIGH or LOW (1 or 0) to the given pin which must have
|
||||||
* been previously set as an output. *)
|
* been previously set as an output. *)
|
||||||
external digital_write: int -> int -> unit = "ocamlwiring_digital_write"
|
external digitalWrite : int -> int -> unit = "caml_digitalWrite"
|
||||||
|
|
||||||
(* Writes the value to the PWM register for the given pin.
|
(* Writes the value to the PWM register for the given pin.
|
||||||
* The Raspberry Pi has one on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and
|
* The Raspberry Pi has one on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and
|
||||||
* the range is 0-1024. Other PWM devices may have other PWM ranges *)
|
* the range is 0-1024. Other PWM devices may have other PWM ranges *)
|
||||||
external pwm_write: int -> int -> unit = "ocamlwiring_pwm_write"
|
external pwmWrite : int -> int -> unit = "caml_pwmWrite"
|
||||||
|
|
||||||
(* This function returns the value read at the given pin. It will be HIGH
|
(* This function returns the value read at the given pin. It will be HIGH
|
||||||
* or LOW (1 or 0) depending on the logic level at the pin. *)
|
* or LOW (1 or 0) depending on the logic level at the pin. *)
|
||||||
external digital_read: int -> int = "ocamlwiring_digital_read"
|
external digitalRead : int -> int = "caml_digitalRead"
|
||||||
external digital_write_byte: int -> unit = "ocamlwiring_digital_write_byte"
|
external digitalWriteByte : int -> unit = "caml_digitalWriteByte"
|
||||||
|
|
||||||
external analog_read: int -> int = "ocamlwiring_analog_read"
|
|
||||||
external analog_write: int -> int -> unit = "ocamlwiring_analog_write"
|
|
||||||
|
|
||||||
|
|
||||||
(* ########## Timing ########## *)
|
(* ########## Timing ########## *)
|
||||||
(* Use it to wait a few ms or µs. If you want to wait for several
|
(* Use it to wait a few ms or µs. If you want to wait for several
|
||||||
secondes, use Unix.sleep. *)
|
secondes, use Unix.sleep. *)
|
||||||
(* wait n ms *)
|
(* wait n ms *)
|
||||||
external delay: int -> unit = "ocamlwiring_delay"
|
external delay : int -> unit = "caml_delay"
|
||||||
(* wait n µs *)
|
(* wait n µs *)
|
||||||
external delay_microseconds: int -> unit = "ocamlwiring_delay_microseconds"
|
external delayMicroseconds : int -> unit = "caml_delayMicroseconds"
|
||||||
|
|
||||||
(* This returns a number representing the number if ms/µs since your program
|
(* This returns a number representing the number if ms/µs since your program
|
||||||
* called one of the wiringPiSetup functions. *)
|
* called one of the wiringPiSetup functions. *)
|
||||||
external millis : unit -> int = "ocamlwiring_millis"
|
external millis : unit -> int = "caml_millis"
|
||||||
external micros : unit -> int = "ocamlwiring_micros"
|
external micros : unit -> int = "caml_micros"
|
||||||
|
|
||||||
|
|
||||||
(* ################# Name of pins : ################# *)
|
(* ################# Name of pins : ################# *)
|
||||||
|
|
|
@ -9,25 +9,32 @@
|
||||||
#include <wiringPi.h>
|
#include <wiringPi.h>
|
||||||
#include <wiringShift.h>
|
#include <wiringShift.h>
|
||||||
|
|
||||||
value ocamlwiring_setup(value unit)
|
value caml_hello(value unit)
|
||||||
|
{
|
||||||
|
CAMLparam1(unit);
|
||||||
|
printf("Hello world!\n");
|
||||||
|
CAMLreturn(Val_unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
value caml_wiringPiSetup(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(wiringPiSetup()));
|
CAMLreturn(Val_int(wiringPiSetup()));
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_setup_gpio(value unit)
|
value caml_wiringPiSetupGpio(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(wiringPiSetupGpio()));
|
CAMLreturn(Val_int(wiringPiSetupGpio()));
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_setup_phys(value unit)
|
value caml_wiringPiSetupPhys(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(wiringPiSetupPhys()));
|
CAMLreturn(Val_int(wiringPiSetupPhys()));
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_setup_sys(value unit)
|
value caml_wiringPiSetupSys(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(wiringPiSetupSys()));
|
CAMLreturn(Val_int(wiringPiSetupSys()));
|
||||||
|
@ -35,56 +42,45 @@ value ocamlwiring_setup_sys(value unit)
|
||||||
|
|
||||||
// Core functions
|
// Core functions
|
||||||
|
|
||||||
value ocamlwiring_pin_mode(value pin, value mode)
|
value caml_pinMode(value pin, value mode)
|
||||||
{
|
{
|
||||||
CAMLparam2(pin, mode);
|
CAMLparam2(pin, mode);
|
||||||
pinMode(Int_val(pin), Int_val(mode));
|
pinMode(Int_val(pin), Int_val(mode));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_pull_up_dn_control(value pin, value pud)
|
value caml_pullUpDnControl(value pin, value pud)
|
||||||
{
|
{
|
||||||
CAMLparam2(pin, pud);
|
CAMLparam2(pin, pud);
|
||||||
pullUpDnControl(Int_val(pin), Int_val(pud));
|
pullUpDnControl(Int_val(pin), Int_val(pin));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_digital_write(value pin, value value_p)
|
value caml_digitalWrite(value pin, value value_p)
|
||||||
{
|
{
|
||||||
CAMLparam2(pin, value_p);
|
CAMLparam2(pin, value_p);
|
||||||
digitalWrite(Int_val(pin), Int_val(value_p));
|
digitalWrite(Int_val(pin), Int_val(value_p));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_pwm_write(value pin, value value_p)
|
value caml_pwmWrite(value pin, value value_p)
|
||||||
{
|
{
|
||||||
CAMLparam2(pin, value_p);
|
CAMLparam2(pin, value_p);
|
||||||
pwmWrite(Int_val(pin), Int_val(value_p));
|
pwmWrite(Int_val(pin), Int_val(value_p));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_digital_read(value pin)
|
value caml_digitalRead(value pin)
|
||||||
{
|
{
|
||||||
CAMLparam1(pin);
|
CAMLparam1(pin);
|
||||||
CAMLreturn(Val_int(digitalRead(Int_val(pin))));
|
CAMLreturn(Val_int(digitalRead(Int_val(pin))));
|
||||||
}
|
}
|
||||||
|
|
||||||
value ocamlwiring_analog_read(value pin)
|
// AnalogRead and AnalogWrite needs to be added (module must be added)
|
||||||
{
|
|
||||||
CAMLparam1(pin);
|
|
||||||
CAMLreturn(Val_int(analogRead(Int_val(pin))));
|
|
||||||
}
|
|
||||||
|
|
||||||
value ocamlwiring_analog_write(value pin, value value_p)
|
|
||||||
{
|
|
||||||
CAMLparam2(pin, value_p);
|
|
||||||
analogWrite(Int_val(pin), Int_val(value_p));
|
|
||||||
CAMLreturn(Val_unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Raspberry Pi Specifics
|
// Raspberry Pi Specifics
|
||||||
|
|
||||||
value ocamlwiring_digital_write_byte(value value_p)
|
value caml_digitalWriteByte(value value_p)
|
||||||
{
|
{
|
||||||
CAMLparam1(value_p);
|
CAMLparam1(value_p);
|
||||||
digitalWriteByte(Int_val(value_p));
|
digitalWriteByte(Int_val(value_p));
|
||||||
|
@ -99,7 +95,7 @@ value ocamlwiring_digital_write_byte(value value_p)
|
||||||
|
|
||||||
/* This returns a number representing the number if milliseconds since your
|
/* This returns a number representing the number if milliseconds since your
|
||||||
* program called one of the wiringPiSetup functions. */
|
* program called one of the wiringPiSetup functions. */
|
||||||
value ocamlwiring_millis(value unit)
|
value caml_millis(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(millis()));
|
CAMLreturn(Val_int(millis()));
|
||||||
|
@ -107,7 +103,7 @@ value ocamlwiring_millis(value unit)
|
||||||
|
|
||||||
/* This returns a number representing the number of microseconds since your
|
/* This returns a number representing the number of microseconds since your
|
||||||
* program called one of the wiringPiSetup functions. */
|
* program called one of the wiringPiSetup functions. */
|
||||||
value ocamlwiring_micros(value unit)
|
value caml_micros(value unit)
|
||||||
{
|
{
|
||||||
CAMLparam1(unit);
|
CAMLparam1(unit);
|
||||||
CAMLreturn(Val_int(micros()));
|
CAMLreturn(Val_int(micros()));
|
||||||
|
@ -115,10 +111,10 @@ value ocamlwiring_micros(value unit)
|
||||||
|
|
||||||
/* This causes program execution to pause for at least howLong milliseconds.
|
/* This causes program execution to pause for at least howLong milliseconds.
|
||||||
* Due to the multi-tasking nature of Linux it could be longer. */
|
* Due to the multi-tasking nature of Linux it could be longer. */
|
||||||
value ocamlwiring_delay(value how_long)
|
value caml_delay(value howLong)
|
||||||
{
|
{
|
||||||
CAMLparam1(how_long);
|
CAMLparam1(howLong);
|
||||||
delay(Int_val(how_long));
|
delay(Int_val(howLong));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,9 +125,9 @@ value ocamlwiring_delay(value how_long)
|
||||||
* nanosleep() function – You may need to consider the implications of very
|
* nanosleep() function – You may need to consider the implications of very
|
||||||
* short delays on the overall performance of the system, especially if using
|
* short delays on the overall performance of the system, especially if using
|
||||||
* threads. */
|
* threads. */
|
||||||
value ocamlwiring_delay_microseconds(value how_long)
|
value caml_delayMicroseconds(value howLong)
|
||||||
{
|
{
|
||||||
CAMLparam1(how_long);
|
CAMLparam1(howLong);
|
||||||
delayMicroseconds(Int_val(how_long));
|
delayMicroseconds(Int_val(howLong));
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue