104 lines
2.0 KiB
C
104 lines
2.0 KiB
C
|
// License MIT
|
||
|
// Compatibility layer done by Kamil Trzcinski <ayufan@ayufan.eu>
|
||
|
|
||
|
#include "wiringPi.h"
|
||
|
|
||
|
static uint64_t epochMilli, epochMicro;
|
||
|
|
||
|
int wiringPiSetup()
|
||
|
{
|
||
|
initialiseEpoch();
|
||
|
return setup();
|
||
|
}
|
||
|
|
||
|
int digitalRead(int gpio)
|
||
|
{
|
||
|
return input_gpio(gpio);
|
||
|
}
|
||
|
|
||
|
void digitalWrite(int gpio, int value)
|
||
|
{
|
||
|
output_gpio(gpio, value);
|
||
|
}
|
||
|
|
||
|
void pinMode(int gpio, int direction)
|
||
|
{
|
||
|
setup_gpio(gpio, direction, PUD_UP);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// The functions belowe are copied from wiringPi
|
||
|
//
|
||
|
|
||
|
void initialiseEpoch (void)
|
||
|
{
|
||
|
struct timeval tv ;
|
||
|
|
||
|
gettimeofday (&tv, NULL) ;
|
||
|
epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
|
||
|
epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
|
||
|
}
|
||
|
|
||
|
void delayMicrosecondsHard (unsigned int howLong)
|
||
|
{
|
||
|
struct timeval tNow, tLong, tEnd ;
|
||
|
|
||
|
gettimeofday (&tNow, NULL) ;
|
||
|
tLong.tv_sec = howLong / 1000000 ;
|
||
|
tLong.tv_usec = howLong % 1000000 ;
|
||
|
timeradd (&tNow, &tLong, &tEnd) ;
|
||
|
|
||
|
while (timercmp (&tNow, &tEnd, <))
|
||
|
gettimeofday (&tNow, NULL) ;
|
||
|
}
|
||
|
|
||
|
void delayMicroseconds (unsigned int howLong)
|
||
|
{
|
||
|
struct timespec sleeper ;
|
||
|
unsigned int uSecs = howLong % 1000000 ;
|
||
|
unsigned int wSecs = howLong / 1000000 ;
|
||
|
|
||
|
/**/ if (howLong == 0)
|
||
|
return ;
|
||
|
else if (howLong < 100)
|
||
|
delayMicrosecondsHard (howLong) ;
|
||
|
else
|
||
|
{
|
||
|
sleeper.tv_sec = wSecs ;
|
||
|
sleeper.tv_nsec = (long)(uSecs * 1000L) ;
|
||
|
nanosleep (&sleeper, NULL) ;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void delay (unsigned int howLong)
|
||
|
{
|
||
|
struct timespec sleeper, dummy ;
|
||
|
|
||
|
sleeper.tv_sec = (time_t)(howLong / 1000) ;
|
||
|
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
|
||
|
|
||
|
nanosleep (&sleeper, &dummy) ;
|
||
|
}
|
||
|
|
||
|
unsigned int millis (void)
|
||
|
{
|
||
|
struct timeval tv ;
|
||
|
uint64_t now ;
|
||
|
|
||
|
gettimeofday (&tv, NULL) ;
|
||
|
now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
|
||
|
|
||
|
return (uint32_t)(now - epochMilli) ;
|
||
|
}
|
||
|
|
||
|
unsigned int micros (void)
|
||
|
{
|
||
|
struct timeval tv ;
|
||
|
uint64_t now ;
|
||
|
|
||
|
gettimeofday (&tv, NULL) ;
|
||
|
now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
|
||
|
|
||
|
return (uint32_t)(now - epochMicro) ;
|
||
|
}
|