summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/grovewfs/grovewfs.h
blob: bec30dcbb71da1e75bcd360ad9c73587d5a5a85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 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>
#include <stdint.h>
#include <sys/time.h>
#include <mraa/gpio.h>

namespace upm {

  /**
   * @brief Grove Water Flow Sensor library
   * @defgroup grovewfs libupm-grovewfs
   * @ingroup seeed gpio liquid eak
   */

  /**
   * @library grovewfs
   * @sensor grovewfs
   * @comname Grove Water Flow Sensor
   * @type liquid
   * @man seeed
   * @web http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
   * @con gpio
   * @kit eak

   * @brief API for the Grove Water Flow Sensor
   *
   * This sensor is used to measure water flow in liters per
   * minute (LPM). It incorporates a Hall Effect sensor. The UPM module
   * defines an interrupt routine to be triggered on each low pulse,
   * keeping count. This device requires a 10K pull-up resistor for
   * the signal line (yellow wire). There is a schematic diagram on
   * the SeeedStudio site (3/2015):
   * http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
   *
   * However, be careful when wiring this up - the schematic appears to
   * have a bug in it: the lower left connection of the signal line
   * (yellow) to Vcc (red) should not be there. The sensor can work
   * with this connection, but probably not for very long.
   *
   * @image html grovewfs.jpg
   * @snippet grovewfs.cxx Interesting
   */
  class GroveWFS {
  public:
    /**
     * Grove Water Flow sensor constructor
     *
     * @param pin Digital pin to use
     */
    GroveWFS(int pin);
    /**
     * GroveWFS destructor
     */
    ~GroveWFS();
    /**
     * Returns the number of milliseconds elapsed since initClock()
     * was last called.
     *
     * @return Elapsed milliseconds
     */
    uint32_t getMillis();

    /**
     * Resets the clock
     *
     */
    void initClock();

    /**
     * Resets the flow counter to 0. The flow counter should be
     * stopped via stopFlowCounter() prior to calling this function.
     *
     */
    void clearFlowCounter() { m_flowCounter = 0; };

    /**
     * Starts the flow counter
     *
     */
    void startFlowCounter();

    /**
     * Stops the flow counter
     *
     */
    void stopFlowCounter();

    /**
     * Gets the flow counter
     *
     * @return Flow counter
     */
    uint32_t flowCounter() { return m_flowCounter; };

    /**
     * Computes the flow rate in liters per minute (LPM)
     *
     * @return Computed flow rate
     */
    float flowRate();

  private:
    /**
     * Flow interrupt service routine (ISR)
     *
     */
    static void flowISR(void *ctx);

    volatile uint32_t m_flowCounter;
    struct timeval m_startTime;
    mraa_gpio_context m_gpio;
    bool m_isrInstalled;
  };
}