wfs: C port; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson 2017-01-26 15:06:05 -07:00
parent 0a91eb0b46
commit 849711c47d
11 changed files with 537 additions and 208 deletions

View File

@ -33,46 +33,47 @@ int shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a Water Flow Sensor on digital pin D2
upm::WFS* flow = new upm::WFS(2);
// set the flow counter to 0 and start counting
flow->clearFlowCounter();
flow->startFlowCounter();
// Instantiate a Water Flow Sensor on digital pin D2. This must
// be an interrupt capable pin.
upm::WFS* flow = new upm::WFS(2);
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
// purposes in this example
uint32_t millis = flow->getMillis();
uint32_t flowCount = flow->flowCounter();
// we grab these (millis and flowCount) just for display
// purposes in this example
uint32_t millis = flow->getMillis();
uint32_t flowCount = flow->flowCounter();
float fr = flow->flowRate();
float fr = flow->flowRate();
// output milliseconds passed, flow count, and computed flow rate
cout << "Millis: " << millis << " Flow Count: " << flowCount;
cout << " Flow Rate: " << fr << " LPM" << endl;
// output milliseconds passed, flow count, and computed flow rate
cout << "Millis: " << millis << " Flow Count: " << flowCount;
cout << " Flow Rate: " << fr << " LPM" << endl;
// best to gather data for at least one second for reasonable
// results.
sleep(2);
// best to gather data for at least one second for reasonable
// results.
sleep(2);
}
flow->stopFlowCounter();
flow->stopFlowCounter();
//! [Interesting]
cout << "Exiting..." << endl;
cout << "Exiting..." << endl;
delete flow;
return 0;
delete flow;
return 0;
}

View File

@ -146,6 +146,7 @@ add_example (max30100)
add_example (speaker)
add_example (cjq4435)
add_example (hmc5883l)
add_example (wfs)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

91
examples/c/wfs.c Normal file
View 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;
}

View File

@ -25,20 +25,23 @@
//NOT TESTED!!!
public class WFSSample {
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
// Instantiate a Water Flow Sensor on digital pin D2
upm_wfs.WFS flow = new upm_wfs.WFS(2);
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
flow.clearFlowCounter();
flow.startFlowCounter();
// Instantiate a Water Flow Sensor on
// digital pin D2. This must be an
// interrupt capable pin.
upm_wfs.WFS flow = new upm_wfs.WFS(2);
while (true) {
System.out.print("Millis: " + flow.getMillis() + " FlowCount: " + flow.flowCounter());
System.out.println(" Flow Rate: " + flow.flowRate() + " LPM");
flow.clearFlowCounter();
flow.startFlowCounter();
Thread.sleep(2000);
}
// ! [Interesting]
}
while (true) {
System.out.print("Millis: " + flow.getMillis() + " FlowCount: " + flow.flowCounter());
System.out.println(" Flow Rate: " + flow.flowRate() + " LPM");
Thread.sleep(2000);
}
// ! [Interesting]
}
}

View File

@ -24,7 +24,8 @@
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);
// set the flow counter to 0 and start counting

View File

@ -26,7 +26,8 @@ import time, sys, signal, atexit
from upm import pyupm_wfs as upmwfs
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)
## Exit handlers ##

View File

@ -1,5 +1,8 @@
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_SRC wfs.cxx
CPP_WRAPS_C
REQUIRES mraa)

152
src/wfs/wfs.c Normal file
View 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);
}

View File

@ -1,6 +1,6 @@
/*
* 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
* a copy of this software and associated documentation files (the
@ -31,94 +31,42 @@
using namespace upm;
using namespace std;
WFS::WFS(int pin)
WFS::WFS(int pin) :
m_wfs(wfs_init(pin))
{
if ( !(m_gpio = mraa_gpio_init(pin)) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
return;
}
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
initClock();
m_flowCounter = 0;
m_isrInstalled = false;
if (!m_wfs)
throw std::runtime_error(std::string(__FUNCTION__) +
": wfs_init() failed");
}
WFS::~WFS()
{
if (m_isrInstalled)
stopFlowCounter();
mraa_gpio_close(m_gpio);
wfs_close(m_wfs);
}
void WFS::initClock()
{
gettimeofday(&m_startTime, NULL);
wfs_init_clock(m_wfs);
}
uint32_t WFS::getMillis()
{
struct timeval elapsed, now;
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;
return wfs_get_millis(m_wfs);
}
void WFS::startFlowCounter()
{
initClock();
// install our interrupt handler
mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING,
&flowISR, this);
m_isrInstalled = true;
if (wfs_start_flow_counter(m_wfs))
throw std::runtime_error(std::string(__FUNCTION__) +
": wfs_start_flow_counter() failed");
}
void WFS::stopFlowCounter()
{
// remove the interrupt handler
mraa_gpio_isr_exit(m_gpio);
m_isrInstalled = false;
}
void WFS::flowISR(void *ctx)
{
upm::WFS *This = (upm::WFS *)ctx;
This->m_flowCounter++;
wfs_stop_flow_counter(m_wfs);
}
float WFS::flowRate()
{
uint32_t millis = getMillis();
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;
return wfs_flow_rate(m_wfs);
}

134
src/wfs/wfs.h Normal file
View 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

View File

@ -1,6 +1,6 @@
/*
* 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
* a copy of this software and associated documentation files (the
@ -23,119 +23,113 @@
*/
#pragma once
#include <string>
#include <stdint.h>
#include <sys/time.h>
#include <mraa/gpio.h>
#include "wfs.h"
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
*/
WFS(int pin);
/**
* WFS destructor
*/
~WFS();
/**
* Returns the number of milliseconds elapsed since initClock()
* was last called.
* 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
*
* @return Elapsed milliseconds
*/
uint32_t getMillis();
/**
* Resets the clock
* 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
*/
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
* stopped via stopFlowCounter() prior to calling this function.
*
*/
void clearFlowCounter() { m_flowCounter = 0; };
/**
* WFS destructor
*/
~WFS();
/**
* Starts the flow counter
*
*/
void startFlowCounter();
/**
* Returns the number of milliseconds elapsed since initClock()
* was last called.
*
* @return Elapsed milliseconds
*/
uint32_t getMillis();
/**
* Stops the flow counter
*
*/
void stopFlowCounter();
/**
* Resets the clock
*
*/
void initClock();
/**
* Gets the flow counter
*
* @return Flow counter
*/
uint32_t flowCounter() { return m_flowCounter; };
/**
* Resets the flow counter to 0. The flow counter should be
* stopped via stopFlowCounter() prior to calling this function.
*
*/
void clearFlowCounter() { wfs_clear_flow_counter(m_wfs); };
/**
* Computes the flow rate in liters per minute (LPM)
*
* @return Computed flow rate
*/
float flowRate();
/**
* Starts the flow counter
*
*/
void startFlowCounter();
private:
/**
* Flow interrupt service routine (ISR)
*
*/
static void flowISR(void *ctx);
/**
* Stops the flow counter
*
*/
void stopFlowCounter();
volatile uint32_t m_flowCounter;
struct timeval m_startTime;
mraa_gpio_context m_gpio;
bool m_isrInstalled;
};
/**
* Gets the flow counter
*
* @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:
};
}