Home | History | Annotate | Download | only in grovewfs
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) ics.com>
      3  * Copyright (c) 2015 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 #pragma once
     25 
     26 #include <string>
     27 #include <stdint.h>
     28 #include <sys/time.h>
     29 #include <mraa/gpio.h>
     30 
     31 namespace upm {
     32 
     33   /**
     34    * @brief Grove Water Flow Sensor library
     35    * @defgroup grovewfs libupm-grovewfs
     36    * @ingroup seeed gpio liquid eak
     37    */
     38 
     39   /**
     40    * @library grovewfs
     41    * @sensor grovewfs
     42    * @comname Grove Water Flow Sensor
     43    * @type liquid
     44    * @man seeed
     45    * @web http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
     46    * @con gpio
     47    * @kit eak
     48 
     49    * @brief API for the Grove Water Flow Sensor
     50    *
     51    * This sensor is used to measure water flow in liters per
     52    * minute (LPM). It incorporates a Hall Effect sensor. The UPM module
     53    * defines an interrupt routine to be triggered on each low pulse,
     54    * keeping count. This device requires a 10K pull-up resistor for
     55    * the signal line (yellow wire). There is a schematic diagram on
     56    * the SeeedStudio site (3/2015):
     57    * http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
     58    *
     59    * However, be careful when wiring this up - the schematic appears to
     60    * have a bug in it: the lower left connection of the signal line
     61    * (yellow) to Vcc (red) should not be there. The sensor can work
     62    * with this connection, but probably not for very long.
     63    *
     64    * @image html grovewfs.jpg
     65    * @snippet grovewfs.cxx Interesting
     66    */
     67   class GroveWFS {
     68   public:
     69     /**
     70      * Grove Water Flow sensor constructor
     71      *
     72      * @param pin Digital pin to use
     73      */
     74     GroveWFS(int pin);
     75     /**
     76      * GroveWFS destructor
     77      */
     78     ~GroveWFS();
     79     /**
     80      * Returns the number of milliseconds elapsed since initClock()
     81      * was last called.
     82      *
     83      * @return Elapsed milliseconds
     84      */
     85     uint32_t getMillis();
     86 
     87     /**
     88      * Resets the clock
     89      *
     90      */
     91     void initClock();
     92 
     93     /**
     94      * Resets the flow counter to 0. The flow counter should be
     95      * stopped via stopFlowCounter() prior to calling this function.
     96      *
     97      */
     98     void clearFlowCounter() { m_flowCounter = 0; };
     99 
    100     /**
    101      * Starts the flow counter
    102      *
    103      */
    104     void startFlowCounter();
    105 
    106     /**
    107      * Stops the flow counter
    108      *
    109      */
    110     void stopFlowCounter();
    111 
    112     /**
    113      * Gets the flow counter
    114      *
    115      * @return Flow counter
    116      */
    117     uint32_t flowCounter() { return m_flowCounter; };
    118 
    119     /**
    120      * Computes the flow rate in liters per minute (LPM)
    121      *
    122      * @return Computed flow rate
    123      */
    124     float flowRate();
    125 
    126   private:
    127     /**
    128      * Flow interrupt service routine (ISR)
    129      *
    130      */
    131     static void flowISR(void *ctx);
    132 
    133     volatile uint32_t m_flowCounter;
    134     struct timeval m_startTime;
    135     mraa_gpio_context m_gpio;
    136     bool m_isrInstalled;
    137   };
    138 }
    139 
    140 
    141