63f2ecd447af8f049bcc546d09074c3e06de54d9
[contrib/upm.git] / src / grove / grove.cxx
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@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
25 #include <iostream>
26
27 #include "grove.h"
28 #include "math.h"
29
30 using namespace upm;
31
32 //// GroveLed ////
33
34 GroveLed::GroveLed(int pin)
35 {
36     maa_init();
37     m_gpio = maa_gpio_init(pin);
38     maa_gpio_dir(m_gpio, MAA_GPIO_OUT);
39     m_name = "LED Socket";
40 }
41
42 GroveLed::~GroveLed()
43 {
44     maa_gpio_close(m_gpio);
45 }
46
47 maa_result_t GroveLed::write(int value)
48 {
49     if (value >= 1) {
50         return maa_gpio_write(m_gpio, 1);
51     }
52     return maa_gpio_write(m_gpio, 0);
53 }
54
55 maa_result_t GroveLed::on()
56 {
57     return write(1);
58 }
59
60 maa_result_t GroveLed::off()
61 {
62     return write(0);
63 }
64
65 //// GroveTemp ////
66
67 GroveTemp::GroveTemp(unsigned int pin)
68 {
69     maa_init();
70     m_aio = maa_aio_init(pin);
71     m_name = "Temperature Sensor";
72 }
73
74 GroveTemp::~GroveTemp()
75 {
76     maa_aio_close(m_aio);
77 }
78
79 int GroveTemp::value ()
80 {
81     int a = maa_aio_read(m_aio);
82     float r = (float)(1023-a)*10000/a;
83     float t = 1/(logf(r/10000)/3975 + 1/298.15)-273.15;
84     return (int) t;
85 }
86
87 float GroveTemp::raw_value()
88 {
89     return (float) maa_aio_read(m_aio);
90 }
91
92 //// GroveLight ////
93
94 GroveLight::GroveLight(unsigned int pin)
95 {
96     maa_init();
97     m_aio = maa_aio_init(pin);
98     m_name = "Light Sensor";
99 }
100
101 GroveLight::~GroveLight()
102 {
103     maa_aio_close(m_aio);
104 }
105
106 int GroveLight::value ()
107 {
108     // rough conversion to Lux
109     int a = maa_aio_read(m_aio);
110     a = 10000/(((1023-a)*10/a)*15)^(4/3);
111     return a;
112 }
113
114 float GroveLight::raw_value()
115 {
116     return (float) maa_aio_read(m_aio);
117 }