summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/teams/teams.cxx
blob: eab59c19e59a3d8ac3570026bf99b89bfea41b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 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 <iostream>

#include "teams.hpp"

using namespace upm;
using namespace std;

// for current loop reads that seems a little noisy, we average over
// several aio reads.
static const int maxSamples = 50;

// conversion from celcius to fahrenheit

static float c2f(float c)
{
  return (c * (9.0 / 5.0) + 32.0);
}


TEAMS::TEAMS(int tPin, float rResistor, float aref) :
  m_aioTemp(tPin)
{
  if (rResistor < 0.0)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": rResistor must be >= 0.0");
    }

  m_aResTemp = (1 << m_aioTemp.getBit());

  m_temperature = 0.0;

  m_aref = aref;
  m_rResistor = rResistor;
  m_connected = false;

  // this will only be non-zero when using a direct 4-20ma interface
  // like libelium
  m_rawMilliamps = 0.0;

  m_offset = 0.0;
}

TEAMS::~TEAMS()
{
}

void TEAMS::update()
{
  float milliamps = 0.0;

  int val = average(maxSamples);
  float volts = (float(val) * (m_aref / m_aResTemp));

  // valid temp range is 25.0 (35C - 10C), current loop range is 16ma
  // (20ma - 4ma)
  if (m_rResistor)
    {
      // direct 4-20 current loop interface
      milliamps = (volts / m_rResistor * 1000.0) + m_offset;
      m_rawMilliamps = milliamps;

      // subtract 0 (4ma) value
      milliamps -= 4.0;
      if (milliamps < 0.0) // not connected
        {
          milliamps = 0.0;
          m_connected = false;
        }
      else
        m_connected = true;

      m_temperature = (milliamps * (25.0 / 16.0)) + 10.0;
    }
  else
    {
      // normal analog read, already scaled to 0-5v, always connected
      m_temperature = ((volts / m_aref) * 25.0) + 10.0;
      m_connected = true;
    }
}

float TEAMS::getTemperature(bool fahrenheit)
{
  if (fahrenheit)
    return c2f(m_temperature);
  else
    return m_temperature;
}

int TEAMS::average(int samples)
{
  if (samples <= 0)
    samples = 1;

  int avg = 0;
  for (int i=0; i<samples; i++)
    avg += m_aioTemp.read();

  return (avg / samples);
}