Home | History | Annotate | Download | only in hcsr04
      1 /*
      2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha (at) intel.com>
      3  * Author: Rafael Neri <rafael.neri (at) gmail.com>
      4  * Copyright (c) 2014-2015 Intel Corporation.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining
      7  * a copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sublicense, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice shall be
     15  * included in all copies or substantial portions of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     24  */
     25 #pragma once
     26 
     27 #include <string>
     28 #include <mraa/aio.h>
     29 #include <mraa/gpio.h>
     30 #include <mraa/pwm.h>
     31 #include <sys/time.h>
     32 
     33 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
     34 #include "../IsrCallback.h"
     35 #endif
     36 
     37 #define CM 1
     38 #define INC 0
     39 
     40 namespace upm {
     41 /**
     42  * @brief HC-SR04 Ultrasonic Sensor library
     43  * @defgroup hcsr04 libupm-hcsr04
     44  * @ingroup generic gpio sound
     45  */
     46 
     47 /**
     48  * @library hcsr04
     49  * @sensor hcsr04
     50  * @comname HC-SR04 Ultrasonic Sensor
     51  * @type sound
     52  * @man generic
     53  * @con gpio
     54  *
     55  * @brief API for the HC-SR04 Ultrasonic Sensor
     56  *
     57  * This module defines the HC-SR04 interface for libhcsr04
     58  *
     59  * @snippet hcsr04.cxx Interesting
     60  */
     61 class HCSR04 {
     62     public:
     63         /**
     64          * Instantiates an HCSR04 object
     65          *
     66          * @param triggerPin Pin to trigger the sensor for distance
     67          * @param echoPin Pulse response to triggering
     68          * @param fptr Function pointer to handle rising-edge and
     69          * falling-edge interrupts
     70          */
     71 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
     72         HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
     73 #else
     74         HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
     75 #endif
     76         /**
     77          * HCSR04 object destructor
     78          */
     79         ~HCSR04 ();
     80 
     81         /**
     82          * Gets the distance from the sensor
     83          */
     84         double getDistance (int sys);
     85 
     86         /**
     87          * On each interrupt, this function detects if the interrupt
     88          * was falling-edge or rising-edge.
     89          * Should be called from the interrupt handler.
     90          */
     91         void ackEdgeDetected ();
     92 
     93         uint8_t m_doWork; /**< Flag to control blocking function while waiting for a falling-edge interrupt */
     94 
     95         /**
     96          * Returns the name of the sensor
     97          */
     98         std::string name()
     99         {
    100             return m_name;
    101         }
    102 
    103     private:
    104 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
    105         HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
    106 #endif
    107         double timing();
    108         mraa_gpio_context   m_triggerPinCtx;
    109         mraa_gpio_context   m_echoPinCtx;
    110 
    111         long    m_RisingTimeStamp;
    112         long    m_FallingTimeStamp;
    113         uint8_t m_InterruptCounter;
    114 
    115         std::string         m_name;
    116 };
    117 
    118 }
    119