From de5a9d08cf0681bc87b71fe75243a8d14f279008 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 27 Aug 2020 04:15:09 +0000 Subject: [PATCH] add the wiringPi wrapper functions for FreeBSD.. --- wiringPi.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ wiringPi.h | 44 +++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 wiringPi.c create mode 100644 wiringPi.h diff --git a/wiringPi.c b/wiringPi.c new file mode 100644 index 0000000..47508d4 --- /dev/null +++ b/wiringPi.c @@ -0,0 +1,102 @@ +/*- + * Copyright 2020 John-Mark Gurney. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include + +#include + +long +micros(void) +{ + int64_t i; + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC_PRECISE, &ts); + + i = ts.tv_sec * 1000000000 + ts.tv_nsec; + i /= 1000; + + return i; +} + +static int opened; +static gpio_handle_t gpiohndl; + +int +wiringPiSetup(void) +{ + + if (opened) + return 1; + + gpiohndl = gpio_open(0); + opened = 1; + + return 1; +} + +void +pinMode(int pinnum, enum wp_dir direction) +{ + + if (direction == INPUT) + gpio_pin_input(gpiohndl, pinnum); + else if (direction == OUTPUT) + gpio_pin_output(gpiohndl, pinnum); +} + +enum wp_value +digitalRead(int pinnum) +{ + gpio_value_t v; + + v = gpio_pin_get(gpiohndl, pinnum); + if (v == GPIO_VALUE_LOW) + return LOW; + else if (v == GPIO_VALUE_HIGH) + return HIGH; + else + abort(); +} + +void +digitalWrite(int pinnum, enum wp_value value) +{ + gpio_value_t v; + + if (value == LOW) + v = GPIO_VALUE_LOW; + else if (value == HIGH) + v = GPIO_VALUE_HIGH; + else + abort(); + + gpio_pin_set(gpiohndl, pinnum, v); +} diff --git a/wiringPi.h b/wiringPi.h new file mode 100644 index 0000000..9ca37cc --- /dev/null +++ b/wiringPi.h @@ -0,0 +1,44 @@ +/*- + * Copyright 2020 John-Mark Gurney. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +enum wp_dir { + INPUT, + OUTPUT, +}; + +enum wp_value { + LOW, + HIGH, +}; + +/* Likely just compat*/ +long micros(void); + +int wiringPiSetup(void); + +void pinMode(int pinnum, enum wp_dir direction); +enum wp_value digitalRead(int pinnum); +void digitalWrite(int pinnum, enum wp_value value);