mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
hcsr04: Made ISR be used internally and not be exposed to the user. Changed access modifier to private. Removed passing ISR to constructor.
Signed-off-by: Stefan Andritoiu <stefan.andritoiu@intel.com> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
parent
e54f5e21c3
commit
1accafa145
@ -42,15 +42,10 @@ sig_handler(int signo)
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
void
|
||||
interrupt (void * args) {
|
||||
sonar->ackEdgeDetected ();
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
sonar = new upm::HCSR04(5, 6, &interrupt);
|
||||
sonar = new upm::HCSR04(5, 6);
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
sleep(1);
|
||||
|
@ -22,8 +22,6 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import upm_hcsr04.IsrCallback;
|
||||
|
||||
//NOT TESTED!!!
|
||||
public class HCSR04Sample {
|
||||
|
||||
@ -38,10 +36,8 @@ public class HCSR04Sample {
|
||||
|
||||
// ! [Interesting]
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
HCSR04ISR callback = new HCSR04ISR();
|
||||
upm_hcsr04.HCSR04 sonar = new upm_hcsr04.HCSR04((short) 5, (short) 6);
|
||||
|
||||
upm_hcsr04.HCSR04 sonar = new upm_hcsr04.HCSR04((short) 5, (short) 6, callback);
|
||||
callback.setSonar(sonar);
|
||||
Thread.sleep(1000);
|
||||
|
||||
while (true) {
|
||||
@ -53,24 +49,4 @@ public class HCSR04Sample {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class HCSR04ISR extends IsrCallback {
|
||||
|
||||
private upm_hcsr04.HCSR04 sonar = null;
|
||||
|
||||
public HCSR04ISR() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setSonar(upm_hcsr04.HCSR04 sonar) {
|
||||
this.sonar = sonar;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (sonar != null)
|
||||
sonar.ackEdgeDetected();
|
||||
else
|
||||
System.out.println("No HCSR04ISR instance given to callback");
|
||||
}
|
||||
}
|
||||
// ! [Interesting]
|
@ -34,14 +34,7 @@
|
||||
|
||||
using namespace upm;
|
||||
|
||||
#ifdef JAVACALLBACK
|
||||
HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb)
|
||||
{
|
||||
HCSR04 (triggerPin, echoPin, generic_callback_isr);
|
||||
}
|
||||
#endif
|
||||
|
||||
HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
|
||||
HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin) {
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
m_name = "HCSR04";
|
||||
|
||||
@ -63,7 +56,7 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
|
||||
}
|
||||
|
||||
mraa_gpio_dir(m_echoPinCtx, MRAA_GPIO_IN);
|
||||
mraa_gpio_isr(m_echoPinCtx, MRAA_GPIO_EDGE_BOTH, fptr, (void*)this);
|
||||
mraa_gpio_isr(m_echoPinCtx, MRAA_GPIO_EDGE_BOTH, &ackEdgeDetected, (void*)this);
|
||||
}
|
||||
|
||||
HCSR04::~HCSR04 () {
|
||||
@ -96,16 +89,17 @@ HCSR04::timing() {
|
||||
}
|
||||
|
||||
void
|
||||
HCSR04::ackEdgeDetected () {
|
||||
HCSR04::ackEdgeDetected (void *ctx) {
|
||||
upm::HCSR04 *This = (upm::HCSR04 *)ctx;
|
||||
struct timeval timer;
|
||||
gettimeofday(&timer, NULL);
|
||||
|
||||
++m_InterruptCounter;
|
||||
if (!(m_InterruptCounter % 2)) {
|
||||
m_FallingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
m_doWork = 1;
|
||||
This->m_InterruptCounter++;
|
||||
if (!(This->m_InterruptCounter % 2)) {
|
||||
This->m_FallingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
This->m_doWork = 1;
|
||||
} else {
|
||||
m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
This->m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,11 +68,7 @@ class HCSR04 {
|
||||
* @param fptr Function pointer to handle rising-edge and
|
||||
* falling-edge interrupts
|
||||
*/
|
||||
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||
HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
|
||||
#else
|
||||
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
|
||||
#endif
|
||||
HCSR04 (uint8_t triggerPin, uint8_t echoPin);
|
||||
/**
|
||||
* HCSR04 object destructor
|
||||
*/
|
||||
@ -83,12 +79,6 @@ class HCSR04 {
|
||||
*/
|
||||
double getDistance (int sys);
|
||||
|
||||
/**
|
||||
* On each interrupt, this function detects if the interrupt
|
||||
* was falling-edge or rising-edge.
|
||||
* Should be called from the interrupt handler.
|
||||
*/
|
||||
void ackEdgeDetected ();
|
||||
|
||||
uint8_t m_doWork; /**< Flag to control blocking function while waiting for a falling-edge interrupt */
|
||||
|
||||
@ -101,9 +91,12 @@ class HCSR04 {
|
||||
}
|
||||
|
||||
private:
|
||||
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
|
||||
#endif
|
||||
/**
|
||||
* On each interrupt, this function detects if the interrupt
|
||||
* was falling-edge or rising-edge.
|
||||
*/
|
||||
static void ackEdgeDetected (void *ctx);
|
||||
|
||||
double timing();
|
||||
mraa_gpio_context m_triggerPinCtx;
|
||||
mraa_gpio_context m_echoPinCtx;
|
||||
|
@ -1,11 +1,6 @@
|
||||
%module(directors="1") javaupm_hcsr04
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("director") IsrCallback;
|
||||
|
||||
%ignore generic_callback_isr;
|
||||
%include "../IsrCallback.h"
|
||||
|
||||
%{
|
||||
#include "hcsr04.h"
|
||||
%}
|
||||
@ -21,4 +16,5 @@
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
%}
|
||||
%}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user