1 /*
2  * Author: Jon Trulson <jtrulson@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 
25 #include <iostream>
26 #include <string>
27 #include <stdexcept>
28 
29 #include "grovewfs.h"
30 
31 using namespace upm;
32 using namespace std;
33 
GroveWFS(int pin)34 GroveWFS::GroveWFS(int pin)
35 {
36   if ( !(m_gpio = mraa_gpio_init(pin)) )
37     {
38       throw std::invalid_argument(std::string(__FUNCTION__) +
39                                   ": mraa_gpio_init() failed, invalid pin?");
40       return;
41     }
42 
43   mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
44 
45   initClock();
46   m_flowCounter = 0;
47   m_isrInstalled = false;
48 }
49 
~GroveWFS()50 GroveWFS::~GroveWFS()
51 {
52   if (m_isrInstalled)
53     stopFlowCounter();
54 
55   mraa_gpio_close(m_gpio);
56 }
57 
initClock()58 void GroveWFS::initClock()
59 {
60   gettimeofday(&m_startTime, NULL);
61 }
62 
getMillis()63 uint32_t GroveWFS::getMillis()
64 {
65   struct timeval elapsed, now;
66   uint32_t elapse;
67 
68   // get current time
69   gettimeofday(&now, NULL);
70 
71   // compute the delta since m_startTime
72   if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
73     {
74       elapsed.tv_usec += 1000000;
75       elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
76     }
77   else
78     {
79       elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
80     }
81 
82   elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
83 
84   // never return 0
85   if (elapse == 0)
86     elapse = 1;
87 
88   return elapse;
89 }
90 
startFlowCounter()91 void GroveWFS::startFlowCounter()
92 {
93   initClock();
94   // install our interrupt handler
95   mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING,
96                 &flowISR, this);
97 
98   m_isrInstalled = true;
99 }
100 
stopFlowCounter()101 void GroveWFS::stopFlowCounter()
102 {
103   // remove the interrupt handler
104   mraa_gpio_isr_exit(m_gpio);
105 
106   m_isrInstalled = false;
107 }
108 
flowISR(void * ctx)109 void GroveWFS::flowISR(void *ctx)
110 {
111   upm::GroveWFS *This = (upm::GroveWFS *)ctx;
112   This->m_flowCounter++;
113 }
114 
flowRate()115 float GroveWFS::flowRate()
116 {
117   uint32_t millis = getMillis();
118   uint32_t flow = flowCounter();
119 
120   // 7.5 comes from the seeedstudio page, see the confusing datasheet :)
121   float flowRate = (float(flow) * 7.5) / ((float(millis) / 1000.0) * 60.0);
122 
123   return flowRate;
124 }
125