From 5b9eb6b42995374880d7e3fc8a9bb34783919645 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 9 Feb 2019 17:54:26 +0100 Subject: [PATCH] typed interface for the GPIO pins on RPI 3 --- _oasis | 4 +-- src/Gpio3.ml | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/Gpio3.mli | 33 ++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/Gpio3.ml create mode 100644 src/Gpio3.mli diff --git a/_oasis b/_oasis index 2e9b837..cfbc9ef 100644 --- a/_oasis +++ b/_oasis @@ -9,7 +9,7 @@ Plugins: META (0.4), DevFiles (0.4) Library "WiringPi" Path: src/ BuildTools: ocamlbuild - Modules: WiringPi + Modules: WiringPi, Gpio3 CSources: WiringPi_stubs.c CCLib: -lwiringPi - + diff --git a/src/Gpio3.ml b/src/Gpio3.ml new file mode 100644 index 0000000..ce4d13c --- /dev/null +++ b/src/Gpio3.ml @@ -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 diff --git a/src/Gpio3.mli b/src/Gpio3.mli new file mode 100644 index 0000000..81e2b47 --- /dev/null +++ b/src/Gpio3.mli @@ -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