Browse Source

max5487: added new sensor with example (TESTED AND WORKING)

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
1747fbbe19

+ 3
- 0
examples/CMakeLists.txt View File

@@ -23,6 +23,7 @@ add_executable (mic-example mic-example.cxx)
23 23
 add_executable (mpu9150-example mpu9150-example.cxx)
24 24
 add_executable (maxds3231m-example maxds3231m.cxx)
25 25
 add_executable (max31723-example max31723.cxx)
26
+add_executable (max5487-example max5487.cxx)
26 27
 
27 28
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
28 29
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -44,6 +45,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mic)
44 45
 include_directories (${PROJECT_SOURCE_DIR}/src/mpu9150)
45 46
 include_directories (${PROJECT_SOURCE_DIR}/src/maxds3231m)
46 47
 include_directories (${PROJECT_SOURCE_DIR}/src/max31723)
48
+include_directories (${PROJECT_SOURCE_DIR}/src/max5487)
47 49
 
48 50
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
49 51
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -70,3 +72,4 @@ target_link_libraries (mic-example mic ${CMAKE_THREAD_LIBS_INIT})
70 72
 target_link_libraries (mpu9150-example mpu9150 ${CMAKE_THREAD_LIBS_INIT})
71 73
 target_link_libraries (maxds3231m-example maxds3231m ${CMAKE_THREAD_LIBS_INIT})
72 74
 target_link_libraries (max31723-example max31723 ${CMAKE_THREAD_LIBS_INIT})
75
+target_link_libraries (max5487-example max5487 ${CMAKE_THREAD_LIBS_INIT})

+ 66
- 0
examples/max5487.cxx View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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 "max5487.h"
28
+#include <signal.h>
29
+
30
+upm::MAX5487 *sensor = NULL;
31
+
32
+void
33
+sig_handler(int signo)
34
+{
35
+    printf("got signal\n");
36
+    if (signo == SIGINT) {
37
+        printf("exiting application\n");
38
+    }
39
+}
40
+
41
+int
42
+main(int argc, char **argv)
43
+{
44
+    //! [Interesting]
45
+    sensor = new upm::MAX5487(7);
46
+
47
+    // Power LED UP
48
+    for (int i = 0; i < 255; i++) {
49
+        sensor->setWiperA(i);
50
+        usleep (5000);
51
+    }
52
+
53
+    // Power LED DOWN
54
+    for (int i = 0; i < 255; i++) {
55
+        sensor->setWiperA(255 - i);
56
+        usleep (5000);
57
+    }
58
+
59
+    //! [Interesting]
60
+
61
+    std::cout << "exiting application" << std::endl;
62
+
63
+    delete sensor;
64
+
65
+    return 0;
66
+}

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

@@ -0,0 +1,5 @@
1
+set (libname "max5487")
2
+set (libdescription "Digital potentiometer")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/max5487/jsupm_max5487.i View File

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

+ 117
- 0
src/max5487/max5487.cxx View File

@@ -0,0 +1,117 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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
+#include <unistd.h>
27
+#include <stdlib.h>
28
+
29
+#include "max5487.h"
30
+
31
+using namespace upm;
32
+
33
+struct MAX5487Exception : public std::exception {
34
+    std::string message;
35
+    MAX5487Exception (std::string msg) : message (msg) { }
36
+    ~MAX5487Exception () throw () { }
37
+    const char* what() const throw () { return message.c_str(); }
38
+};
39
+
40
+MAX5487::MAX5487 (int csn) {
41
+    mraa_result_t error = MRAA_SUCCESS;
42
+    m_name = "MAX5487";
43
+
44
+    m_csnPinCtx = mraa_gpio_init (csn);
45
+    if (m_csnPinCtx == NULL) {
46
+        throw MAX5487Exception ("GPIO failed to initilize");
47
+    }
48
+
49
+    error = mraa_gpio_dir (m_csnPinCtx, MRAA_GPIO_OUT);
50
+    if (error != MRAA_SUCCESS) {
51
+        throw MAX5487Exception ("GPIO failed to initilize");
52
+    }
53
+
54
+    m_spi = mraa_spi_init (0);
55
+    if (m_spi == NULL) {
56
+        throw MAX5487Exception ("SPI failed to initilize");
57
+    }
58
+
59
+    CSOff ();
60
+}
61
+
62
+MAX5487::~MAX5487() {
63
+    mraa_result_t error = MRAA_SUCCESS;
64
+
65
+    error = mraa_spi_stop(m_spi);
66
+    if (error != MRAA_SUCCESS) {
67
+        mraa_result_print(error);
68
+    }
69
+    error = mraa_gpio_close (m_csnPinCtx);
70
+    if (error != MRAA_SUCCESS) {
71
+        mraa_result_print(error);
72
+    }
73
+}
74
+
75
+void
76
+MAX5487::setWiperA (uint8_t wiper) {
77
+    uint8_t data[2] = { 0x00, 0x00};
78
+
79
+    CSOn ();
80
+
81
+    data[0] = R_WR_WIPER_A;
82
+    data[1] = wiper;
83
+
84
+    uint8_t* retData = mraa_spi_write_buf(m_spi, data, 2);
85
+
86
+    CSOff ();
87
+}
88
+
89
+void
90
+MAX5487::setWiperB (uint8_t wiper) {
91
+    uint8_t data[2] = { 0x00, 0x00};
92
+
93
+    CSOn ();
94
+
95
+    data[0] = R_WR_WIPER_B;
96
+    data[1] = wiper;
97
+
98
+    uint8_t* retData = mraa_spi_write_buf(m_spi, data, 2);
99
+
100
+    CSOff ();
101
+}
102
+
103
+/*
104
+ * **************
105
+ *  private area
106
+ * **************
107
+ */
108
+
109
+mraa_result_t
110
+MAX5487::CSOn () {
111
+    return mraa_gpio_write (m_csnPinCtx, LOW);
112
+}
113
+
114
+mraa_result_t
115
+MAX5487::CSOff () {
116
+    return mraa_gpio_write (m_csnPinCtx, HIGH);
117
+}

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

@@ -0,0 +1,93 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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
+#pragma once
25
+
26
+#include <string>
27
+#include <mraa/aio.h>
28
+#include <mraa/gpio.h>
29
+#include <mraa/spi.h>
30
+
31
+#define HIGH                    1
32
+#define LOW                     0
33
+
34
+namespace upm {
35
+
36
+/**
37
+ * @brief C++ API for MAX5487 chip (Temperature sensor)
38
+ *
39
+ * This file defines the MAX5487 C++ interface for libmax5487
40
+ *
41
+ */
42
+class MAX5487 {
43
+    public:
44
+        static const uint8_t R_WR_WIPER_A   = 0x01;
45
+        static const uint8_t R_WR_WIPER_B   = 0x02;
46
+
47
+        /**
48
+         * Instanciates a MAX5487 object
49
+         *
50
+         * @param bus number of used bus
51
+         * @param devAddr addres of used i2c device
52
+         */
53
+        MAX5487 (int csn);
54
+
55
+        /**
56
+         * MAX5487 object destructor, basicaly it close SPI connection and GPIO.
57
+         */
58
+        ~MAX5487 ();
59
+
60
+        /**
61
+         * Set wiper for port A.
62
+         */
63
+        void setWiperA (uint8_t wiper);
64
+
65
+        /**
66
+         * Set wiper for port B.
67
+         */
68
+        void setWiperB (uint8_t wiper);
69
+
70
+        /**
71
+         * Return name of the component
72
+         */
73
+        std::string name()
74
+        {
75
+            return m_name;
76
+        }
77
+    private:
78
+        std::string m_name;
79
+        mraa_spi_context        m_spi;
80
+        mraa_gpio_context       m_csnPinCtx;
81
+
82
+        /**
83
+         * Set chip select pin LOW
84
+         */
85
+        mraa_result_t CSOn ();
86
+
87
+        /**
88
+         * Set chip select pin HIGH
89
+         */
90
+        mraa_result_t CSOff ();
91
+};
92
+
93
+}

+ 9
- 0
src/max5487/pyupm_max5487.i View File

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