mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
wfs: C port; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
0a91eb0b46
commit
849711c47d
@ -33,46 +33,47 @@ int shouldRun = true;
|
|||||||
|
|
||||||
void sig_handler(int signo)
|
void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
if (signo == SIGINT)
|
if (signo == SIGINT)
|
||||||
shouldRun = false;
|
shouldRun = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
signal(SIGINT, sig_handler);
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
// Instantiate a Water Flow Sensor on digital pin D2
|
// Instantiate a Water Flow Sensor on digital pin D2. This must
|
||||||
upm::WFS* flow = new upm::WFS(2);
|
// be an interrupt capable pin.
|
||||||
|
upm::WFS* flow = new upm::WFS(2);
|
||||||
// set the flow counter to 0 and start counting
|
|
||||||
flow->clearFlowCounter();
|
|
||||||
flow->startFlowCounter();
|
|
||||||
|
|
||||||
while (shouldRun)
|
// set the flow counter to 0 and start counting
|
||||||
|
flow->clearFlowCounter();
|
||||||
|
flow->startFlowCounter();
|
||||||
|
|
||||||
|
while (shouldRun)
|
||||||
{
|
{
|
||||||
// we grab these (,illis and flowCount) just for display
|
// we grab these (millis and flowCount) just for display
|
||||||
// purposes in this example
|
// purposes in this example
|
||||||
uint32_t millis = flow->getMillis();
|
uint32_t millis = flow->getMillis();
|
||||||
uint32_t flowCount = flow->flowCounter();
|
uint32_t flowCount = flow->flowCounter();
|
||||||
|
|
||||||
float fr = flow->flowRate();
|
float fr = flow->flowRate();
|
||||||
|
|
||||||
// output milliseconds passed, flow count, and computed flow rate
|
// output milliseconds passed, flow count, and computed flow rate
|
||||||
cout << "Millis: " << millis << " Flow Count: " << flowCount;
|
cout << "Millis: " << millis << " Flow Count: " << flowCount;
|
||||||
cout << " Flow Rate: " << fr << " LPM" << endl;
|
cout << " Flow Rate: " << fr << " LPM" << endl;
|
||||||
|
|
||||||
// best to gather data for at least one second for reasonable
|
// best to gather data for at least one second for reasonable
|
||||||
// results.
|
// results.
|
||||||
sleep(2);
|
sleep(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
flow->stopFlowCounter();
|
flow->stopFlowCounter();
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
|
|
||||||
cout << "Exiting..." << endl;
|
cout << "Exiting..." << endl;
|
||||||
|
|
||||||
delete flow;
|
delete flow;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -146,6 +146,7 @@ add_example (max30100)
|
|||||||
add_example (speaker)
|
add_example (speaker)
|
||||||
add_example (cjq4435)
|
add_example (cjq4435)
|
||||||
add_example (hmc5883l)
|
add_example (hmc5883l)
|
||||||
|
add_example (wfs)
|
||||||
|
|
||||||
# Custom examples
|
# Custom examples
|
||||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||||
|
91
examples/c/wfs.c
Normal file
91
examples/c/wfs.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2014-02017 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <upm_utilities.h>
|
||||||
|
#include "wfs.h"
|
||||||
|
|
||||||
|
int shouldRun = true;
|
||||||
|
|
||||||
|
void sig_handler(int signo)
|
||||||
|
{
|
||||||
|
if (signo == SIGINT)
|
||||||
|
shouldRun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
|
//! [Interesting]
|
||||||
|
// Instantiate a Water Flow Sensor on digital pin D2. This must
|
||||||
|
// be an interrupt capable pin.
|
||||||
|
|
||||||
|
wfs_context sensor = wfs_init(2);
|
||||||
|
if (!sensor)
|
||||||
|
{
|
||||||
|
printf("%s: wfs_init() failed\n", __FUNCTION__);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the flow counter to 0 and start counting
|
||||||
|
wfs_clear_flow_counter(sensor);
|
||||||
|
if (wfs_start_flow_counter(sensor))
|
||||||
|
{
|
||||||
|
printf("%s: wfs_start_flow_counter() failed\n", __FUNCTION__);
|
||||||
|
wfs_close(sensor);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (shouldRun)
|
||||||
|
{
|
||||||
|
// we grab these (millis and flowCount) just for display
|
||||||
|
// purposes in this example
|
||||||
|
uint32_t millis = wfs_get_millis(sensor);
|
||||||
|
uint32_t flowCount = wfs_flow_counter(sensor);
|
||||||
|
|
||||||
|
float fr = wfs_flow_rate(sensor);
|
||||||
|
|
||||||
|
// output milliseconds passed, flow count, and computed flow rate
|
||||||
|
printf("Millis: %8d Flow Count: %5d Flow Rate: %f LPM\n",
|
||||||
|
millis, flowCount, fr);
|
||||||
|
|
||||||
|
// best to gather data for at least one second for reasonable
|
||||||
|
// results.
|
||||||
|
upm_delay(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
wfs_stop_flow_counter(sensor);
|
||||||
|
|
||||||
|
printf("Exiting...\n");
|
||||||
|
|
||||||
|
wfs_close(sensor);
|
||||||
|
|
||||||
|
//! [Interesting]
|
||||||
|
return 0;
|
||||||
|
}
|
@ -25,20 +25,23 @@
|
|||||||
//NOT TESTED!!!
|
//NOT TESTED!!!
|
||||||
public class WFSSample {
|
public class WFSSample {
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
// Instantiate a Water Flow Sensor on digital pin D2
|
|
||||||
upm_wfs.WFS flow = new upm_wfs.WFS(2);
|
|
||||||
|
|
||||||
flow.clearFlowCounter();
|
// Instantiate a Water Flow Sensor on
|
||||||
flow.startFlowCounter();
|
// digital pin D2. This must be an
|
||||||
|
// interrupt capable pin.
|
||||||
|
upm_wfs.WFS flow = new upm_wfs.WFS(2);
|
||||||
|
|
||||||
while (true) {
|
flow.clearFlowCounter();
|
||||||
System.out.print("Millis: " + flow.getMillis() + " FlowCount: " + flow.flowCounter());
|
flow.startFlowCounter();
|
||||||
System.out.println(" Flow Rate: " + flow.flowRate() + " LPM");
|
|
||||||
|
|
||||||
Thread.sleep(2000);
|
while (true) {
|
||||||
}
|
System.out.print("Millis: " + flow.getMillis() + " FlowCount: " + flow.flowCounter());
|
||||||
// ! [Interesting]
|
System.out.println(" Flow Rate: " + flow.flowRate() + " LPM");
|
||||||
}
|
|
||||||
|
Thread.sleep(2000);
|
||||||
|
}
|
||||||
|
// ! [Interesting]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,8 @@
|
|||||||
|
|
||||||
var waterFlow_lib = require('jsupm_wfs');
|
var waterFlow_lib = require('jsupm_wfs');
|
||||||
|
|
||||||
// Instantiate a Water Flow Sensor on digital pin D2
|
// Instantiate a Water Flow Sensor on digital pin D2. This must be an
|
||||||
|
// interrupt capable pin.
|
||||||
var myWaterFlow_obj = new waterFlow_lib.WFS(2);
|
var myWaterFlow_obj = new waterFlow_lib.WFS(2);
|
||||||
|
|
||||||
// set the flow counter to 0 and start counting
|
// set the flow counter to 0 and start counting
|
||||||
|
@ -26,7 +26,8 @@ import time, sys, signal, atexit
|
|||||||
from upm import pyupm_wfs as upmwfs
|
from upm import pyupm_wfs as upmwfs
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Instantiate a Water Flow Sensor on digital pin D2
|
# Instantiate a Water Flow Sensor on digital pin D2. This must be
|
||||||
|
# an interrupt capable GPIO.
|
||||||
myWaterFlow = upmwfs.WFS(2)
|
myWaterFlow = upmwfs.WFS(2)
|
||||||
|
|
||||||
## Exit handlers ##
|
## Exit handlers ##
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
upm_mixed_module_init (NAME wfs
|
upm_mixed_module_init (NAME wfs
|
||||||
DESCRIPTION "Water flow sensor (WFS)"
|
DESCRIPTION "WFS (Water Flow Sensor)"
|
||||||
|
C_HDR wfs.h
|
||||||
|
C_SRC wfs.c
|
||||||
CPP_HDR wfs.hpp
|
CPP_HDR wfs.hpp
|
||||||
CPP_SRC wfs.cxx
|
CPP_SRC wfs.cxx
|
||||||
|
CPP_WRAPS_C
|
||||||
REQUIRES mraa)
|
REQUIRES mraa)
|
||||||
|
152
src/wfs/wfs.c
Normal file
152
src/wfs/wfs.c
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2017 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "wfs.h"
|
||||||
|
|
||||||
|
// interrupt handler
|
||||||
|
static void wfs_flow_isr(void *ctx)
|
||||||
|
{
|
||||||
|
assert(ctx != NULL);
|
||||||
|
|
||||||
|
wfs_context dev = (wfs_context)ctx;
|
||||||
|
|
||||||
|
dev->flowCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
wfs_context wfs_init(int pin)
|
||||||
|
{
|
||||||
|
// make sure MRAA is initialized
|
||||||
|
int mraa_rv;
|
||||||
|
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
|
||||||
|
{
|
||||||
|
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wfs_context dev =
|
||||||
|
(wfs_context)malloc(sizeof(struct _wfs_context));
|
||||||
|
|
||||||
|
if (!dev)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// zero out context
|
||||||
|
memset((void *)dev, 0, sizeof(struct _wfs_context));
|
||||||
|
|
||||||
|
if ( !(dev->gpio = mraa_gpio_init(pin)) )
|
||||||
|
{
|
||||||
|
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
|
||||||
|
wfs_close(dev);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN);
|
||||||
|
|
||||||
|
wfs_init_clock(dev);
|
||||||
|
|
||||||
|
return dev;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wfs_close(wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
if (dev->isrInstalled)
|
||||||
|
wfs_stop_flow_counter(dev);
|
||||||
|
|
||||||
|
if (dev->gpio)
|
||||||
|
mraa_gpio_close(dev->gpio);
|
||||||
|
|
||||||
|
free(dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wfs_init_clock(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
upm_clock_init(&dev->clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t wfs_get_millis(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
return upm_elapsed_ms(&dev->clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wfs_clear_flow_counter(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
dev->flowCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
upm_result_t wfs_start_flow_counter(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
wfs_init_clock(dev);
|
||||||
|
|
||||||
|
// install our interrupt handler
|
||||||
|
if (mraa_gpio_isr(dev->gpio, MRAA_GPIO_EDGE_RISING,
|
||||||
|
&wfs_flow_isr, dev))
|
||||||
|
{
|
||||||
|
printf("%s: mraa_gpio_isr() failed.\n", __FUNCTION__);
|
||||||
|
return UPM_ERROR_OPERATION_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
dev->isrInstalled = true;
|
||||||
|
return UPM_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wfs_stop_flow_counter(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
// remove the interrupt handler
|
||||||
|
mraa_gpio_isr_exit(dev->gpio);
|
||||||
|
|
||||||
|
dev->isrInstalled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t wfs_flow_counter(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
return dev->flowCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
float wfs_flow_rate(const wfs_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
uint32_t millis = wfs_get_millis(dev);
|
||||||
|
uint32_t flow = wfs_flow_counter(dev);
|
||||||
|
|
||||||
|
// 7.5 comes from the seeedstudio page, see the confusing datasheet :)
|
||||||
|
return ((float)flow * 7.5) / ( ((float)millis / 1000.0) * 60.0);
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
* Copyright (c) 2015 Intel Corporation.
|
* Copyright (c) 2015-2017 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -31,94 +31,42 @@
|
|||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
WFS::WFS(int pin)
|
WFS::WFS(int pin) :
|
||||||
|
m_wfs(wfs_init(pin))
|
||||||
{
|
{
|
||||||
if ( !(m_gpio = mraa_gpio_init(pin)) )
|
if (!m_wfs)
|
||||||
{
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
": wfs_init() failed");
|
||||||
": mraa_gpio_init() failed, invalid pin?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
|
|
||||||
|
|
||||||
initClock();
|
|
||||||
m_flowCounter = 0;
|
|
||||||
m_isrInstalled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WFS::~WFS()
|
WFS::~WFS()
|
||||||
{
|
{
|
||||||
if (m_isrInstalled)
|
wfs_close(m_wfs);
|
||||||
stopFlowCounter();
|
|
||||||
|
|
||||||
mraa_gpio_close(m_gpio);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFS::initClock()
|
void WFS::initClock()
|
||||||
{
|
{
|
||||||
gettimeofday(&m_startTime, NULL);
|
wfs_init_clock(m_wfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t WFS::getMillis()
|
uint32_t WFS::getMillis()
|
||||||
{
|
{
|
||||||
struct timeval elapsed, now;
|
return wfs_get_millis(m_wfs);
|
||||||
uint32_t elapse;
|
|
||||||
|
|
||||||
// get current time
|
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
|
|
||||||
// compute the delta since m_startTime
|
|
||||||
if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
|
|
||||||
{
|
|
||||||
elapsed.tv_usec += 1000000;
|
|
||||||
elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
|
|
||||||
}
|
|
||||||
|
|
||||||
elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
|
|
||||||
|
|
||||||
// never return 0
|
|
||||||
if (elapse == 0)
|
|
||||||
elapse = 1;
|
|
||||||
|
|
||||||
return elapse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFS::startFlowCounter()
|
void WFS::startFlowCounter()
|
||||||
{
|
{
|
||||||
initClock();
|
if (wfs_start_flow_counter(m_wfs))
|
||||||
// install our interrupt handler
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING,
|
": wfs_start_flow_counter() failed");
|
||||||
&flowISR, this);
|
|
||||||
|
|
||||||
m_isrInstalled = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WFS::stopFlowCounter()
|
void WFS::stopFlowCounter()
|
||||||
{
|
{
|
||||||
// remove the interrupt handler
|
wfs_stop_flow_counter(m_wfs);
|
||||||
mraa_gpio_isr_exit(m_gpio);
|
|
||||||
|
|
||||||
m_isrInstalled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WFS::flowISR(void *ctx)
|
|
||||||
{
|
|
||||||
upm::WFS *This = (upm::WFS *)ctx;
|
|
||||||
This->m_flowCounter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float WFS::flowRate()
|
float WFS::flowRate()
|
||||||
{
|
{
|
||||||
uint32_t millis = getMillis();
|
return wfs_flow_rate(m_wfs);
|
||||||
uint32_t flow = flowCounter();
|
|
||||||
|
|
||||||
// 7.5 comes from the seeedstudio page, see the confusing datasheet :)
|
|
||||||
float flowRate = (float(flow) * 7.5) / ((float(millis) / 1000.0) * 60.0);
|
|
||||||
|
|
||||||
return flowRate;
|
|
||||||
}
|
}
|
||||||
|
134
src/wfs/wfs.h
Normal file
134
src/wfs/wfs.h
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2017 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 <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <upm.h>
|
||||||
|
#include <upm_utilities.h>
|
||||||
|
#include <mraa/gpio.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file wfs.h
|
||||||
|
* @library wfs
|
||||||
|
* @brief WFS (Water Flow Sensor)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Device context
|
||||||
|
*/
|
||||||
|
typedef struct _wfs_context {
|
||||||
|
mraa_gpio_context gpio;
|
||||||
|
|
||||||
|
volatile uint32_t flowCounter;
|
||||||
|
upm_clock_t clock;
|
||||||
|
bool isrInstalled;
|
||||||
|
} *wfs_context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a WFS device
|
||||||
|
*
|
||||||
|
* @param pin GPIO pin to use. This must be an interrupt capable
|
||||||
|
* pin.
|
||||||
|
* @return Device context, or NULL on error
|
||||||
|
*/
|
||||||
|
wfs_context wfs_init(int pin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WFS close function
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
*/
|
||||||
|
void wfs_close(wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of milliseconds elapsed since wfs_init_clock()
|
||||||
|
* was last called.
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
* @return Elapsed milliseconds
|
||||||
|
*/
|
||||||
|
uint32_t wfs_get_millis(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the clock
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
*/
|
||||||
|
void wfs_init_clock(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the flow counter to 0. The flow counter should be
|
||||||
|
* stopped via stopFlowCounter() prior to calling this function.
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
*/
|
||||||
|
void wfs_clear_flow_counter(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the flow counter
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
* @return UPM result
|
||||||
|
*/
|
||||||
|
upm_result_t wfs_start_flow_counter(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the flow counter
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
*/
|
||||||
|
void wfs_stop_flow_counter(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the flow counter
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
* @return Flow counter
|
||||||
|
*/
|
||||||
|
uint32_t wfs_flow_counter(const wfs_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the flow rate in liters per minute (LPM). Note, this
|
||||||
|
* is for the Grove WFS. If you are using some other WFS, you
|
||||||
|
* should compute the flow rate on your own based on the data for
|
||||||
|
* your sensor.
|
||||||
|
*
|
||||||
|
* @param dev Device context
|
||||||
|
* @return Computed flow rate in liters per minute
|
||||||
|
*/
|
||||||
|
float wfs_flow_rate(const wfs_context dev);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
196
src/wfs/wfs.hpp
196
src/wfs/wfs.hpp
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
* Copyright (c) 2015 Intel Corporation.
|
* Copyright (c) 2015-2017 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -23,119 +23,113 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include "wfs.h"
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <mraa/gpio.h>
|
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Grove Water Flow Sensor library
|
|
||||||
* @defgroup wfs libupm-wfs
|
|
||||||
* @ingroup seeed gpio liquid eak
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @library wfs
|
|
||||||
* @sensor wfs
|
|
||||||
* @comname Water Flow Sensor
|
|
||||||
* @altname 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 wfs.jpg
|
|
||||||
* @snippet wfs.cxx Interesting
|
|
||||||
*/
|
|
||||||
class WFS {
|
|
||||||
public:
|
|
||||||
/**
|
/**
|
||||||
* Grove Water Flow sensor constructor
|
* @brief Grove Water Flow Sensor library
|
||||||
|
* @defgroup wfs libupm-wfs
|
||||||
|
* @ingroup seeed gpio liquid eak
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @library wfs
|
||||||
|
* @sensor wfs
|
||||||
|
* @comname Water Flow Sensor
|
||||||
|
* @altname 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
|
||||||
*
|
*
|
||||||
* @param pin Digital pin to use
|
* This sensor is used to measure water flow in liters per
|
||||||
*/
|
* minute (LPM). It incorporates a Hall Effect sensor. The UPM module
|
||||||
WFS(int pin);
|
* defines an interrupt routine to be triggered on each low pulse,
|
||||||
/**
|
* keeping count. This device requires a 10K pull-up resistor for
|
||||||
* WFS destructor
|
* the signal line (yellow wire). There is a schematic diagram on
|
||||||
*/
|
* the SeeedStudio site (3/2015):
|
||||||
~WFS();
|
* http://www.seeedstudio.com/wiki/index.php?title=G1/2_Water_Flow_sensor
|
||||||
/**
|
|
||||||
* Returns the number of milliseconds elapsed since initClock()
|
|
||||||
* was last called.
|
|
||||||
*
|
*
|
||||||
* @return Elapsed milliseconds
|
* However, be careful when wiring this up - the schematic appears to
|
||||||
*/
|
* have a bug in it: the lower left connection of the signal line
|
||||||
uint32_t getMillis();
|
* (yellow) to Vcc (red) should not be there. The sensor can work
|
||||||
|
* with this connection, but probably not for very long.
|
||||||
/**
|
|
||||||
* Resets the clock
|
|
||||||
*
|
*
|
||||||
|
* @image html wfs.jpg
|
||||||
|
* @snippet wfs.cxx Interesting
|
||||||
*/
|
*/
|
||||||
void initClock();
|
class WFS {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Grove Water Flow sensor constructor
|
||||||
|
*
|
||||||
|
* @param pin GPIO pin to use. This must be an interrupt
|
||||||
|
* capable pin.
|
||||||
|
*/
|
||||||
|
WFS(int pin);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets the flow counter to 0. The flow counter should be
|
* WFS destructor
|
||||||
* stopped via stopFlowCounter() prior to calling this function.
|
*/
|
||||||
*
|
~WFS();
|
||||||
*/
|
|
||||||
void clearFlowCounter() { m_flowCounter = 0; };
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the flow counter
|
* Returns the number of milliseconds elapsed since initClock()
|
||||||
*
|
* was last called.
|
||||||
*/
|
*
|
||||||
void startFlowCounter();
|
* @return Elapsed milliseconds
|
||||||
|
*/
|
||||||
|
uint32_t getMillis();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the flow counter
|
* Resets the clock
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void stopFlowCounter();
|
void initClock();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the flow counter
|
* Resets the flow counter to 0. The flow counter should be
|
||||||
*
|
* stopped via stopFlowCounter() prior to calling this function.
|
||||||
* @return Flow counter
|
*
|
||||||
*/
|
*/
|
||||||
uint32_t flowCounter() { return m_flowCounter; };
|
void clearFlowCounter() { wfs_clear_flow_counter(m_wfs); };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the flow rate in liters per minute (LPM)
|
* Starts the flow counter
|
||||||
*
|
*
|
||||||
* @return Computed flow rate
|
*/
|
||||||
*/
|
void startFlowCounter();
|
||||||
float flowRate();
|
|
||||||
|
|
||||||
private:
|
/**
|
||||||
/**
|
* Stops the flow counter
|
||||||
* Flow interrupt service routine (ISR)
|
*
|
||||||
*
|
*/
|
||||||
*/
|
void stopFlowCounter();
|
||||||
static void flowISR(void *ctx);
|
|
||||||
|
|
||||||
volatile uint32_t m_flowCounter;
|
/**
|
||||||
struct timeval m_startTime;
|
* Gets the flow counter
|
||||||
mraa_gpio_context m_gpio;
|
*
|
||||||
bool m_isrInstalled;
|
* @return Flow counter
|
||||||
};
|
*/
|
||||||
|
uint32_t flowCounter() { return wfs_flow_counter(m_wfs); };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the flow rate in liters per minute (LPM). Note, this
|
||||||
|
* is for the Grove WFS. If you are using some other WFS, you
|
||||||
|
* should compute the flow rate on your own based on the data for
|
||||||
|
* your sensor.
|
||||||
|
*
|
||||||
|
* @return Computed flow rate
|
||||||
|
*/
|
||||||
|
float flowRate();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
wfs_context m_wfs;
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user