Przeglądaj źródła

joystick12: Add Elecfreaks Joystic 1.2/1.4 support

This should also support similar analog joysticks

Signed-off-by: izard <alexander.komarov@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
izard 10 lat temu
rodzic
commit
d44eb0b751

+ 3
- 0
examples/CMakeLists.txt Wyświetl plik

@@ -36,6 +36,7 @@ add_executable (mq9-example mq9.cxx)
36 36
 add_executable (tcs3414cs-example tcs3414cs.cxx)
37 37
 add_executable (th02-example th02.cxx)
38 38
 add_executable (lsm303-example lsm303.cxx)
39
+add_executable (joystick12-example joystick12-example.cxx)
39 40
 
40 41
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
41 42
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -66,6 +67,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/gas)
66 67
 include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs)
67 68
 include_directories (${PROJECT_SOURCE_DIR}/src/th02)
68 69
 include_directories (${PROJECT_SOURCE_DIR}/src/lsm303)
70
+include_directories (${PROJECT_SOURCE_DIR}/src/joystick12)
69 71
 
70 72
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
71 73
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -105,3 +107,4 @@ target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
105 107
 target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})
106 108
 target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT})
107 109
 target_link_libraries (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT})
110
+target_link_libraries (joystick12-example joystick12 ${CMAKE_THREAD_LIBS_INIT})

+ 64
- 0
examples/joystick12-example.cxx Wyświetl plik

@@ -0,0 +1,64 @@
1
+/*
2
+ * Author: Alexander Komarov <alexander.komarov@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 <signal.h>
28
+#include "joystick12.h"
29
+#include <stdlib.h>
30
+#include <sys/time.h>
31
+
32
+int is_running = 0;
33
+upm::Joystick12 *sensor = NULL;
34
+
35
+void
36
+sig_handler(int signo)
37
+{
38
+    printf("got signal\n");
39
+    if (signo == SIGINT) {
40
+        is_running = 1;
41
+    }
42
+}
43
+
44
+//! [Interesting]
45
+int
46
+main(int argc, char **argv)
47
+{
48
+    sensor = new upm::Joystick12(0,1);
49
+    signal(SIGINT, sig_handler);
50
+
51
+    while (!is_running) {
52
+        float x = sensor->getXInput();
53
+        float y = sensor->getYInput();
54
+        std::cout << "Driving X:" << x << ": and Y:" << y << std::endl;
55
+        sleep(1);
56
+    }
57
+
58
+    std::cout << "exiting application" << std::endl;
59
+
60
+    delete sensor;
61
+
62
+    return 0;
63
+}
64
+//! [Interesting]

+ 5
- 0
src/joystick12/CMakeLists.txt Wyświetl plik

@@ -0,0 +1,5 @@
1
+set (libname "joystick12")
2
+set (libdescription "ElecFreaks Joystick 1.2")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 85
- 0
src/joystick12/joystick12.cxx Wyświetl plik

@@ -0,0 +1,85 @@
1
+/*
2
+ * Author: Alexander Komarov <alexander.komarov@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
+#include <functional>
29
+#include <string.h>
30
+#include "joystick12.h"
31
+
32
+using namespace upm;
33
+
34
+// FIXME AK: to make configurable if needed
35
+const int Joystick12::X_left = 100;
36
+const int Joystick12::X_center = 1610;
37
+const int Joystick12::X_right= 4070;
38
+
39
+const int Joystick12::Y_left = 2;
40
+const int Joystick12::Y_center = 1610;
41
+const int Joystick12::Y_right= 4070;
42
+
43
+
44
+Joystick12::Joystick12(int pinX, int pinY) {
45
+    mraa_result_t error;
46
+    m_joystickCtxX = mraa_aio_init(pinX);
47
+    m_joystickCtxY = mraa_aio_init(pinY);
48
+}
49
+
50
+Joystick12::~Joystick12() {
51
+    // close inputs
52
+    mraa_result_t error;
53
+    error = mraa_aio_close(m_joystickCtxX);
54
+    if (error != MRAA_SUCCESS) {
55
+        mraa_result_print(error);
56
+    }
57
+    error = mraa_aio_close(m_joystickCtxY);
58
+    if (error != MRAA_SUCCESS) {
59
+        mraa_result_print(error);
60
+    }
61
+}
62
+
63
+float Joystick12::getXInput() {
64
+    float in = mraa_aio_read (m_joystickCtxX);
65
+    if (in < X_left) return -1;
66
+    if (in < X_center) return -(X_center - in) / (X_center - X_left);
67
+    if (in == X_center) return 0;
68
+    if (in < X_right) return (in - X_center) / (X_right - X_center);
69
+    if (in >= X_right) return 1;
70
+    mraa_result_print(MRAA_ERROR_UNSPECIFIED);
71
+    return 0;
72
+}
73
+
74
+float Joystick12::getYInput() {
75
+    float  in = mraa_aio_read (m_joystickCtxY);
76
+    if (in < Y_left) return -1;
77
+    if (in < Y_center) return -(Y_center - in) / (Y_center - Y_left);
78
+    if (in == Y_center) return 0;
79
+    if (in < Y_right) return (in - Y_center) / (Y_right - Y_center);
80
+    if (in >= Y_right) return 1;
81
+    mraa_result_print(MRAA_ERROR_UNSPECIFIED);
82
+    return 0;
83
+}
84
+
85
+

+ 82
- 0
src/joystick12/joystick12.h Wyświetl plik

@@ -0,0 +1,82 @@
1
+/*
2
+ * Author: Alexander Komarov <alexander.komarov@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/gpio.h>
28
+#include <mraa/aio.h>
29
+
30
+namespace upm {
31
+
32
+/**
33
+ * @brief C++ API for Elecfreaks Joystick v 1.2-1.4 breakout
34
+ *
35
+ * This file defines the Joystick API and implemntation for X, Y
36
+ * button could be treated as normal GPIO, this enables easier
37
+ * interrupt support
38
+ *
39
+ * @snippet joystick12-example.cxx Interesting
40
+ *
41
+ */
42
+class Joystick12 {
43
+    public:
44
+        /**
45
+         * Instantiates a Joystick object
46
+         *
47
+         * @param pinX analog pin where X input is connected
48
+         * @param pinY analog pin where Y input is connected
49
+         */
50
+        Joystick12(int pinX, int pinY);
51
+
52
+        /**
53
+         * Joystick object destructor
54
+         */
55
+        ~Joystick12();
56
+
57
+        /**
58
+         * Get X input
59
+         * @return float X value, range from -1 to 1. 0 is mid
60
+         */
61
+        float getXInput();
62
+
63
+        /**
64
+         * Get Y input
65
+         *
66
+         * @return float Y value, range from -1 to 1. 0 is mid
67
+         */
68
+        float getYInput();
69
+
70
+    private:
71
+        mraa_aio_context    m_joystickCtxX;
72
+        mraa_aio_context    m_joystickCtxY;
73
+
74
+        static const int X_left;
75
+        static const int X_center;
76
+        static const int X_right;
77
+        static const int Y_left;
78
+        static const int Y_center;
79
+        static const int Y_right;
80
+};
81
+};
82
+

+ 8
- 0
src/joystick12/jsupm_joystick12.i Wyświetl plik

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

+ 11
- 0
src/joystick12/pyupm_joystick12.i Wyświetl plik

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