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
25#include <iostream>
26#include <string>
27#include <stdexcept>
28
29#include "ta12200.h"
30
31using namespace upm;
32using namespace std;
33
34TA12200::TA12200(int pin)
35{
36  initClock();
37
38  if ( !(m_aio = mraa_aio_init(pin)) )
39    {
40      throw std::invalid_argument(std::string(__FUNCTION__) +
41                                  ": mraa_aio_init() failed, invalid pin?");
42      return;
43    }
44}
45
46TA12200::~TA12200()
47{
48  mraa_aio_close(m_aio);
49}
50
51void TA12200::initClock()
52{
53  gettimeofday(&m_startTime, NULL);
54}
55
56uint32_t TA12200::getMillis()
57{
58  struct timeval elapsed, now;
59  uint32_t elapse;
60
61  // get current time
62  gettimeofday(&now, NULL);
63
64  // compute the delta since m_startTime
65  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
66    {
67      elapsed.tv_usec += 1000000;
68      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
69    }
70  else
71    {
72      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
73    }
74
75  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
76
77  // never return 0
78  if (elapse == 0)
79    elapse = 1;
80
81  return elapse;
82}
83
84
85unsigned int TA12200::highestValue()
86{
87  unsigned int hiVal = 0;
88  unsigned int val;
89  uint32_t start = getMillis();
90
91  // 1 second
92  while (getMillis() < (start + 1000))
93    {
94      val = mraa_aio_read(m_aio);
95      if (val > hiVal)
96        hiVal = val;
97    }
98
99  return hiVal;
100}
101
102float TA12200::milliAmps(unsigned int val, int res)
103{
104  float ampCurrent;
105  float effectiveVal;
106
107  // From grove wiki page:
108  // minimum_current=1/1024*5/800*2000000/1.414=8.6(mA)
109  // Only for sinusoidal alternating current
110  //ampCurrent = float(val) / float(res) * 5.0 / 800.0 * 2000000.0;
111  ampCurrent = float(val) / float(res) * 12500.0;
112  effectiveVal = ampCurrent/1.414;
113  return (effectiveVal);
114}
115
116