RPi-HCSR04
view release on metacpan or search on metacpan
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <wiringPi.h>
int _setup(int trig, int echo){
int setup_mode = -1;
if (getenv("RPI_PIN_MODE"))
setup_mode = atoi(getenv("RPI_PIN_MODE"));
if (setup_mode == -1){
if (wiringPiSetupGpio() == -1)
exit(1);
}
else {
/* setenv() copies its args; putenv() with a stack buffer left
environ pointing at a dead frame once _setup() returned */
char mode_value[12];
sprintf(mode_value, "%d", setup_mode);
setenv("RPI_PIN_MODE", mode_value, 1);
}
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(trig, LOW);
delay(30);
return 1;
}
long _fetch(int trig, int echo) {
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);
// wait for echo
while(digitalRead(echo) == LOW);
// wait for echo end
long start_time = micros();
while(digitalRead(echo) == HIGH);
long travel_time = micros() - start_time;
return travel_time;
}
float _inch (int trig, int echo){
long raw = _fetch(trig, echo);
float inch = ((float)raw / 2) / 74;
return inch;
}
float _cm (int trig, int echo){
long raw = _fetch(trig, echo);
float cm = (((float)raw / 2) / 74) * 2.54;
return cm;
}
long _raw (int trig, int echo){
return _fetch(trig, echo);
}
MODULE = RPi::HCSR04 PACKAGE = RPi::HCSR04
PROTOTYPES: DISABLE
int
_setup(trig, echo)
int trig
int echo
float
_inch(trig, echo)
int trig
( run in 0.508 second using v1.01-cache-2.11-cpan-6aa56a78535 )