Browse Source

add the wiringPi wrapper functions for FreeBSD..

master
John-Mark Gurney 4 years ago
parent
commit
de5a9d08cf
2 changed files with 146 additions and 0 deletions
  1. +102
    -0
      wiringPi.c
  2. +44
    -0
      wiringPi.h

+ 102
- 0
wiringPi.c View File

@@ -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 <sys/types.h>
#include <libgpio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>

#include <wiringPi.h>

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);
}

+ 44
- 0
wiringPi.h View File

@@ -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);

Loading…
Cancel
Save