Browse Source

lm35: Initial implementation

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
7935ef1622

+ 3
- 0
examples/c++/CMakeLists.txt View File

@@ -140,6 +140,7 @@ add_executable (mg811-example mg811.cxx)
140 140
 add_executable (wheelencoder-example wheelencoder.cxx)
141 141
 add_executable (sm130-example sm130.cxx)
142 142
 add_executable (grovegprs-example grovegprs.cxx)
143
+add_executable (lm35-example lm35.cxx)
143 144
 
144 145
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
145 146
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -251,6 +252,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mg811)
251 252
 include_directories (${PROJECT_SOURCE_DIR}/src/wheelencoder)
252 253
 include_directories (${PROJECT_SOURCE_DIR}/src/sm130)
253 254
 include_directories (${PROJECT_SOURCE_DIR}/src/grovegprs)
255
+include_directories (${PROJECT_SOURCE_DIR}/src/lm35)
254 256
 
255 257
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
256 258
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -392,3 +394,4 @@ target_link_libraries (mg811-example mg811 ${CMAKE_THREAD_LIBS_INIT})
392 394
 target_link_libraries (wheelencoder-example wheelencoder ${CMAKE_THREAD_LIBS_INIT})
393 395
 target_link_libraries (sm130-example sm130 ${CMAKE_THREAD_LIBS_INIT})
394 396
 target_link_libraries (grovegprs-example grovegprs ${CMAKE_THREAD_LIBS_INIT})
397
+target_link_libraries (lm35-example lm35 ${CMAKE_THREAD_LIBS_INIT})

+ 65
- 0
examples/c++/lm35.cxx View File

@@ -0,0 +1,65 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 <signal.h>
28
+#include "lm35.h"
29
+
30
+using namespace std;
31
+
32
+bool shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+int main()
41
+{
42
+  signal(SIGINT, sig_handler);
43
+
44
+//! [Interesting]
45
+
46
+  // Instantiate a LM35 on analog pin A0, with a default analog
47
+  // reference voltage of 5.0
48
+  upm::LM35 *sensor = new upm::LM35(0);
49
+  
50
+  // Every half second, sample the sensor and output the temperature
51
+
52
+  while (shouldRun)
53
+    {
54
+      cout << "Temperature: " << sensor->getTemperature() << " C" << endl;
55
+      
56
+      usleep(500000);
57
+    }
58
+
59
+//! [Interesting]
60
+
61
+  cout << "Exiting" << endl;
62
+
63
+  delete sensor;
64
+  return 0;
65
+}

+ 51
- 0
examples/javascript/lm35.js View File

@@ -0,0 +1,51 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@ics.com>
6
+ * Copyright (c) 2015 Intel Corporation.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+
29
+var sensorObj = require('jsupm_lm35');
30
+
31
+// Instantiate a LM35 on analog pin A0, with a default analog
32
+// reference voltage of 5.0
33
+var sensor = new sensorObj.LM35(0);
34
+
35
+// Every half second, sample the sensor and output the temperature
36
+
37
+setInterval(function()
38
+{
39
+    console.log("Temperature: " + sensor.getTemperature() + " C");
40
+}, 500);
41
+
42
+// exit on ^C
43
+process.on('SIGINT', function()
44
+{
45
+    sensor = null;
46
+    sensorObj.cleanUp();
47
+    sensorObj = null;
48
+    console.log("Exiting.");
49
+    process.exit(0);
50
+});
51
+

+ 49
- 0
examples/python/lm35.py View File

@@ -0,0 +1,49 @@
1
+#!/usr/bin/python
2
+# Author: Jon Trulson <jtrulson@ics.com>
3
+# Copyright (c) 2015 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
+import time, sys, signal, atexit
25
+import pyupm_lm35 as sensorObj
26
+
27
+# Instantiate a LM35 on analog pin A0, with a default analog
28
+# reference voltage of 5.0
29
+sensor = sensorObj.LM35(0)
30
+
31
+## Exit handlers ##
32
+# This function stops python from printing a stacktrace when you hit control-C
33
+def SIGINTHandler(signum, frame):
34
+	raise SystemExit
35
+
36
+# This function lets you run code on exit
37
+def exitHandler():
38
+	print "Exiting"
39
+	sys.exit(0)
40
+
41
+# Register exit handlers
42
+atexit.register(exitHandler)
43
+signal.signal(signal.SIGINT, SIGINTHandler)
44
+
45
+# Every half second, sample the sensor and output the temperature
46
+
47
+while (1):
48
+        print "Temperature:", sensor.getTemperature(), "C"
49
+	time.sleep(.5)

+ 5
- 0
src/lm35/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "lm35")
2
+set (libdescription "upm DFRobot LM35 temperature sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/lm35/javaupm_lm35.i View File

@@ -0,0 +1,8 @@
1
+%module javaupm_lm35
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "lm35.h"
6
+%}
7
+
8
+%include "lm35.h"

+ 8
- 0
src/lm35/jsupm_lm35.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_lm35
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "lm35.h"
6
+%}
7
+
8
+%include "lm35.h"

+ 51
- 0
src/lm35/lm35.cxx View File

@@ -0,0 +1,51 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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 "lm35.h"
27
+
28
+using namespace std;
29
+using namespace upm;
30
+
31
+LM35::LM35(int pin, float aref) :
32
+  m_aio(pin)
33
+{
34
+  m_aRes = m_aio.getBit();
35
+  m_aref = aref;
36
+}
37
+
38
+LM35::~LM35()
39
+{
40
+}
41
+
42
+float LM35::getTemperature()
43
+{
44
+  int val = m_aio.read();
45
+
46
+  // convert to mV
47
+  float temp = (float(val) * (m_aref / float(1 << m_aRes))) * 1000.0;
48
+
49
+  // 10mV/degree C
50
+  return(temp / 10.0);
51
+}

+ 93
- 0
src/lm35/lm35.h View File

@@ -0,0 +1,93 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2015 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
+#pragma once
25
+
26
+#include <iostream>
27
+#include <string>
28
+#include <mraa/aio.hpp>
29
+
30
+namespace upm {
31
+  /**
32
+   * @brief DFRobot LM35 Linear Temperature Sensor
33
+   * @defgroup lm35 libupm-lm35
34
+   * @ingroup dfrobot analog temp
35
+   */
36
+
37
+  /**
38
+   * @library lm35
39
+   * @sensor lm35
40
+   * @comname DFRobot LM35 Linear Temperature Sensor
41
+   * @altname LM35
42
+   * @type temp
43
+   * @man dfrobot
44
+   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=76
45
+   * @con analog
46
+   *
47
+   * @brief API for the DFRobot LM35 Linear Temperature Sensor
48
+   *
49
+   * This sensor returns an analog voltage proportional to the
50
+   * temperature of the ambient environment.
51
+   *
52
+   * It has a range of 2C to 150C.
53
+   *
54
+   * This driver was developed using the DFRobot LM35 Linear
55
+   * Temperature Sensor
56
+   *
57
+   * @snippet lm35.cxx Interesting
58
+   */
59
+
60
+  class LM35 {
61
+  public:
62
+
63
+    /**
64
+     * LM35 constructor
65
+     *
66
+     * @param pin Analog pin to use
67
+     * @param aref Analog reference voltage; default is 5.0 V
68
+     */
69
+    LM35(int pin, float aref=5.0);
70
+
71
+    /**
72
+     * LM35 destructor
73
+     */
74
+    ~LM35();
75
+
76
+    /**
77
+     * Returns the temperature in degrees Celcius
78
+     *
79
+     * @return The Temperature in degrees Celcius
80
+     */
81
+    float getTemperature();
82
+
83
+  protected:
84
+    mraa::Aio m_aio;
85
+
86
+  private:
87
+    float m_aref;
88
+    // ADC resolution
89
+    int m_aRes;
90
+  };
91
+}
92
+
93
+

+ 9
- 0
src/lm35/pyupm_lm35.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_lm35
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "lm35.h"
7
+%{
8
+    #include "lm35.h"
9
+%}