1/*
2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 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 <math.h>
28#include <mraa/aio.h>
29#include <mraa/gpio.h>
30
31namespace upm {
32
33#define NUMBER_OF_SAMPLES  500
34#define ADC_RESOLUTION     1024
35#define SUPPLYVOLTAGE      5100
36#define CURRENT_RATIO      2000.0
37
38#define HIGH               1
39#define LOW                0
40
41#define TRUE               HIGH
42#define FALSE              LOW
43
44  /**
45   * @brief ECS1030 Electricity Sensor library
46   * @defgroup ecs1030 libupm-ecs1030
47   * @ingroup sparkfun analog electric
48   */
49
50  /**
51   * @library ecs1030
52   * @sensor ecs1030
53   * @comname ECS1030 Non-Invasive Current Sensor
54   * @type electric
55   * @man sparkfun
56   * @web https://www.sparkfun.com/products/11005
57   * @con analog
58   *
59   * @brief API for the ECS1030 Non-Invasive Current/Electricity Sensor
60   *
61   * This non-invasive current sensor can be clamped around the supply line of
62   * an electrical load to tell you how much current is passing through it. It
63   * does this by acting as an inductor and responding to the magnetic field
64   * around a current-carrying conductor. This particular current sensor
65   * measures a load up to 30 A, which makes it great for building your own
66   * energy monitors.
67   *
68   * @image html ecs1030.jpg
69   * <br><em>ECS1030 Sensor image provided by SparkFun* under
70   * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
71   * CC BY-NC-SA-3.0</a>.</em>
72   *
73   * @snippet ecs1030.cxx Interesting
74   */
75class ECS1030 {
76    public:
77        static const uint8_t DELAY_MS  = 20000 / NUMBER_OF_SAMPLES; /* 1/50Hz is 20ms period */
78        static const uint8_t VOLT_M    = 5.1 / 1023;
79        static const uint8_t R_LOAD    = 2000.0 / CURRENT_RATIO;
80
81        /**
82         * Instantiates an ECS1030 object
83         *
84         * @param pinNumber Number of the data pin
85         */
86        ECS1030 (uint8_t pinNumber);
87
88        /**
89         * ECS1030 object destructor; basically, it closes the GPIO.
90         */
91        ~ECS1030 ();
92
93        /**
94         * Returns electric current data for a sampled period
95         */
96        double getCurrency_A ();
97
98        /**
99         * Returns power data for a sampled period
100         */
101        double getPower_A ();
102
103        /**
104         * Returns electric current data for a sampled period
105         */
106        double getCurrency_B ();
107
108        /**
109         * Returns power data for a sampled period
110         */
111        double getPower_B ();
112
113        /**
114         * Returns the name of the component
115         */
116        std::string name() {
117            return m_name;
118        }
119    private:
120        std::string         m_name;
121        mraa_aio_context    m_dataPinCtx;
122
123        double              m_calibration;
124        int                 m_lastSample;
125        double              m_lastFilter;
126        int                 m_sample;
127        double              m_filteredSample;
128};
129}
130