summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/hdxxvxta/hdxxvxta.cxx
blob: 0d734925f7c2a2211517f83d8c5ab8aa81eb5fe8 (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
/*
 * 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 "hdxxvxta.hpp"

using namespace upm;
using namespace std;

// conversion from celcius to fahrenheit

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


HDXXVXTA::HDXXVXTA(int hPin, int tPin, float aref) :
  m_aioHum(hPin), m_aioTemp(0)
{
  if (tPin >= 0)
    m_hasTemp = true;
  else
    m_hasTemp = false;

  m_temperature = 0.0;
  m_humidity = 0.0;

  if (m_hasTemp)
    {
      m_aioTemp = new mraa::Aio(tPin);
      m_aResTemp = (1 << m_aioTemp->getBit());
    }
  else
    m_aResTemp = 0;

  m_aResHum = (1 << m_aioHum.getBit());
  m_aref = aref;

  // set the default temperature range to the A1 series (-40C-50C),
  // regardless of whether temperature measuring is enabled
  setRange(RANGE_MINUS40_50);
}

HDXXVXTA::~HDXXVXTA()
{
  if (m_aioTemp)
    delete m_aioTemp;
}

void HDXXVXTA::update()
{
  // temperature
  int val;
  float volts;

  if (m_hasTemp)
    {
      val = m_aioTemp->read();
      volts = (float(val) * (m_aref / m_aResTemp));

      switch (m_range)
        {
        case RANGE_MINUS40_50:
          // valid range is 50 + abs(-40) = 90
          m_temperature = ((volts / m_aref) * 90.0) - 40.0;
          break;
          
        case RANGE_0_50:
          // valid range is 50
          m_temperature = ((volts / m_aref) * 50.0);
          break;
          
        default:
          // shouldn't happen, but...
          throw std::out_of_range(std::string(__FUNCTION__) +
                                  ": internal error, invalid range value");
          break;
        }
    }

  // humidity
  val = m_aioHum.read();
  volts = (float(val) * (m_aref / m_aResHum));
  m_humidity = ((volts / m_aref) * 100.0);
}

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

float HDXXVXTA::getHumidity()
{
  return m_humidity;
}