1 /*
2  * Author: Jon Trulson <jtrulson@ics.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 <iostream>
28 #include <stdint.h>
29 #include <sys/time.h>
30 #include <mraa/aio.h>
31 
32 // default ADC resolution.
33 #define TA12200_ADC_RES 1024
34 
35 namespace upm {
36 /**
37  * @brief TA12-200 Current Transformer library
38  * @defgroup ta12200 libupm-ta12200
39  * @ingroup seeed analog electric
40  */
41 /**
42  * @library ta12200
43  * @sensor ta12200
44  * @comname TA12-200 Current Transformer
45  * @altname Grove Electricity Sensor
46  * @type electric
47  * @man seeed
48  * @web http://www.seeedstudio.com/wiki/Grove_-_Electricity_Sensor
49  * @con analog
50  *
51  * @brief API for the TA12-200 Current Transformer
52  *
53  *   UPM module for the TA12-200 current transformer found,
54  *   for instance, in the Grove Electricity Sensor.
55  *   This module can measure AC moving through a wire at up
56  *   to 5 A.
57  *
58  * @image html ta12200.jpg
59  * @snippet ta12200.cxx Interesting
60  */
61   class TA12200 {
62   public:
63     /**
64      * TA12200 constructor
65      *
66      * @param pin Analog pin to use
67      */
68     TA12200(int pin);
69 
70     /**
71      * TA12200 destructor
72      */
73     ~TA12200();
74 
75     /**
76      * Returns the number of milliseconds elapsed since initClock()
77      * was last called.
78      *
79      * @return Elapsed milliseconds
80      */
81     uint32_t getMillis();
82 
83     /**
84      * Resets the clock
85      *
86      */
87     void initClock();
88 
89     /**
90      * Gets the conversion value from the sensor
91      *
92      * @return Highest value obtained over 1 second of measuring
93      */
94     unsigned int highestValue();
95 
96     /**
97      * Computes the measured voltage
98      *
99      * @param val Value measured by highestValue()
100      * @param res ADC resolution
101      *
102      * @return Measured current in mA
103      */
104     float milliAmps(unsigned int val, int res=TA12200_ADC_RES);
105 
106   private:
107     struct timeval m_startTime;
108     mraa_aio_context m_aio;
109   };
110 }
111 
112 
113