|
@@ -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
|
+
|