Browse Source

grove: add grove temperature sensor support and example

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Brendan Le Foll 10 years ago
parent
commit
11ac453218
4 changed files with 87 additions and 1 deletions
  1. 2
    0
      examples/CMakeLists.txt
  2. 41
    0
      examples/grovetemp.cxx
  3. 32
    0
      src/grove/grove.cxx
  4. 12
    1
      src/grove/grove.h

+ 2
- 0
examples/CMakeLists.txt View File

@@ -1,8 +1,10 @@
1 1
 add_executable (compass compass.cxx)
2 2
 add_executable (groveled groveled.cxx)
3
+add_executable (grovetemp grovetemp.cxx)
3 4
 
4 5
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
5 6
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
6 7
 
7 8
 target_link_libraries (compass hmc5883l)
8 9
 target_link_libraries (groveled grove)
10
+target_link_libraries (grovetemp grove)

+ 41
- 0
examples/grovetemp.cxx View File

@@ -0,0 +1,41 @@
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 <unistd.h>
26
+#include <iostream>
27
+#include "grove.h"
28
+
29
+int
30
+main(int argc, char **argv)
31
+{
32
+    // Use i2c device 0 all the time
33
+    upm::GroveTemp* s = new upm::GroveTemp(0);
34
+    std::cout << s->name() << std::endl;
35
+    for (int i=0; i < 10; i++) {
36
+        std::cout << s->value() << std::endl;
37
+        sleep(1);
38
+    }
39
+
40
+    return 0;
41
+}

+ 32
- 0
src/grove/grove.cxx View File

@@ -22,10 +22,15 @@
22 22
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
  */
24 24
 
25
+#include <iostream>
26
+
25 27
 #include "grove.h"
28
+#include "math.h"
26 29
 
27 30
 using namespace upm;
28 31
 
32
+//// GroveLed ////
33
+
29 34
 GroveLed::GroveLed(int pin)
30 35
 {
31 36
     maa_init();
@@ -56,3 +61,30 @@ maa_result_t GroveLed::off()
56 61
 {
57 62
     return write(0);
58 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_u16(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_u16(m_aio);
90
+}

+ 12
- 1
src/grove/grove.h View File

@@ -24,6 +24,7 @@
24 24
 #pragma once
25 25
 
26 26
 #include <string>
27
+#include <maa/aio.h>
27 28
 #include <maa/gpio.h>
28 29
 
29 30
 namespace upm {
@@ -47,7 +48,17 @@ class GroveLed: public Grove {
47 48
         maa_result_t off();
48 49
         maa_result_t on();
49 50
     private:
50
-        maa_gpio_context * m_gpio;
51
+        maa_gpio_context* m_gpio;
52
+};
53
+
54
+class GroveTemp: public Grove {
55
+    public:
56
+        GroveTemp(unsigned int pin);
57
+        ~GroveTemp();
58
+        float raw_value();
59
+        int value();
60
+    private:
61
+        maa_aio_context* m_aio;
51 62
 };
52 63
 
53 64
 }