2014-09-22 16:37:36 +01:00
|
|
|
/*
|
2014-06-04 14:57:19 +00:00
|
|
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2014-06-25 10:05:27 +01:00
|
|
|
#include <mraa/aio.h>
|
|
|
|
#include <mraa/gpio.h>
|
|
|
|
#include <mraa/pwm.h>
|
2014-06-04 14:57:19 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2015-09-14 18:10:12 +03:00
|
|
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
2015-08-19 19:27:16 +03:00
|
|
|
#include "../IsrCallback.h"
|
|
|
|
#endif
|
|
|
|
|
2014-06-04 14:57:19 +00:00
|
|
|
#define HIGH 1
|
|
|
|
#define LOW 0
|
|
|
|
|
|
|
|
#define MAX_PERIOD 7968
|
|
|
|
#define TRIGGER_PULSE 10
|
|
|
|
|
|
|
|
namespace upm {
|
2014-09-22 16:37:36 +01:00
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* @brief HC-SR04 Ultrasonic Sensor library
|
2014-09-22 16:37:36 +01:00
|
|
|
* @defgroup hcsr04 libupm-hcsr04
|
2015-04-13 11:39:10 -07:00
|
|
|
* @ingroup generic gpio sound
|
2014-09-22 16:37:36 +01:00
|
|
|
*/
|
|
|
|
|
2014-06-10 07:24:56 +00:00
|
|
|
/**
|
2015-04-13 11:39:10 -07:00
|
|
|
* @library hcsr04
|
|
|
|
* @sensor hcsr04
|
2015-08-09 22:51:25 +03:00
|
|
|
* @comname HC-SR04 Ultrasonic Sensor
|
2015-04-13 11:39:10 -07:00
|
|
|
* @type sound
|
|
|
|
* @man generic
|
|
|
|
* @con gpio
|
|
|
|
*
|
2015-08-09 22:51:25 +03:00
|
|
|
* @brief API for the HC-SR04 Ultrasonic Sensor
|
2014-06-10 07:24:56 +00:00
|
|
|
*
|
2015-09-08 11:03:36 -07:00
|
|
|
* This module defines the HC-SR04 interface for libhcsr04
|
2014-06-10 07:24:56 +00:00
|
|
|
*
|
|
|
|
* @snippet hcsr04.cxx Interesting
|
|
|
|
*/
|
2014-06-04 14:57:19 +00:00
|
|
|
class HCSR04 {
|
|
|
|
public:
|
2015-09-14 18:10:12 +03:00
|
|
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
|
|
|
HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
|
2015-08-19 19:27:16 +03:00
|
|
|
#else
|
2014-06-10 07:24:56 +00:00
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* Instantiates an HCSR04 object
|
2014-06-10 07:24:56 +00:00
|
|
|
*
|
2015-08-09 22:51:25 +03:00
|
|
|
* @param triggerPin Pin to trigger the sensor for distance
|
|
|
|
* @param echoPin Pulse response to triggering
|
|
|
|
* @param fptr Function pointer to handle rising-edge and
|
|
|
|
* falling-edge interrupts
|
2014-06-10 07:24:56 +00:00
|
|
|
*/
|
2014-06-20 15:22:55 +00:00
|
|
|
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
|
2015-08-19 19:27:16 +03:00
|
|
|
#endif
|
2014-06-10 07:24:56 +00:00
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* HCSR04 object destructor
|
2014-06-10 07:24:56 +00:00
|
|
|
*/
|
2014-06-04 14:57:19 +00:00
|
|
|
~HCSR04 ();
|
2014-06-10 07:24:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* Gets the distance from the sensor
|
2014-06-10 07:24:56 +00:00
|
|
|
*/
|
2014-06-04 14:57:19 +00:00
|
|
|
int getDistance ();
|
2014-06-10 07:24:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* On each interrupt, this function detects if the interrupt
|
|
|
|
* was falling-edge or rising-edge.
|
2014-06-10 07:24:56 +00:00
|
|
|
* Should be called from the interrupt handler.
|
|
|
|
*/
|
2014-06-04 14:57:19 +00:00
|
|
|
void ackEdgeDetected ();
|
|
|
|
|
2015-08-09 22:51:25 +03:00
|
|
|
uint8_t m_doWork; /**< Flag to control blocking function while waiting for a falling-edge interrupt */
|
2014-06-04 14:57:19 +00:00
|
|
|
|
2014-06-10 07:24:56 +00:00
|
|
|
/**
|
2015-08-09 22:51:25 +03:00
|
|
|
* Returns the name of the sensor
|
2014-06-10 07:24:56 +00:00
|
|
|
*/
|
2014-06-04 14:57:19 +00:00
|
|
|
std::string name()
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-14 18:10:12 +03:00
|
|
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
2015-08-19 19:27:16 +03:00
|
|
|
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
|
|
|
|
#endif
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_pwm_context m_pwmTriggerCtx;
|
|
|
|
mraa_gpio_context m_echoPinCtx;
|
2014-06-04 14:57:19 +00:00
|
|
|
|
|
|
|
uint8_t m_waitEcho;
|
|
|
|
long m_RisingTimeStamp;
|
|
|
|
long m_FallingTimeStamp;
|
|
|
|
uint8_t m_InterruptCounter;
|
|
|
|
|
|
|
|
std::string m_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|