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 <string>
27 #include <stdexcept>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <functional>
31 #include <string.h>
32 #include "joystick12.h"
33 
34 using namespace upm;
35 
36 // FIXME AK: to make configurable if needed
37 const int Joystick12::X_left = 100;
38 const int Joystick12::X_center = 1610;
39 const int Joystick12::X_right= 4070;
40 
41 const int Joystick12::Y_left = 2;
42 const int Joystick12::Y_center = 1610;
43 const int Joystick12::Y_right= 4070;
44 
45 
Joystick12(int pinX,int pinY)46 Joystick12::Joystick12(int pinX, int pinY) {
47 
48     if ( !(m_joystickCtxX = mraa_aio_init(pinX)) )
49       {
50         throw std::invalid_argument(std::string(__FUNCTION__) +
51                                     ": mraa_aio_init(pinX) failed, invalid pin?");
52         return;
53       }
54 
55     if ( !(m_joystickCtxY = mraa_aio_init(pinY)) )
56       {
57         throw std::invalid_argument(std::string(__FUNCTION__) +
58                                     ": mraa_aio_init(pinY) failed, invalid pin?");
59         return;
60       }
61 }
62 
~Joystick12()63 Joystick12::~Joystick12() {
64     // close inputs
65     mraa_result_t error;
66     error = mraa_aio_close(m_joystickCtxX);
67     if (error != MRAA_SUCCESS) {
68         mraa_result_print(error);
69     }
70     error = mraa_aio_close(m_joystickCtxY);
71     if (error != MRAA_SUCCESS) {
72         mraa_result_print(error);
73     }
74 }
75 
getXInput()76 float Joystick12::getXInput() {
77     float in = mraa_aio_read (m_joystickCtxX);
78     if (in < X_left) return -1;
79     if (in < X_center) return -(X_center - in) / (X_center - X_left);
80     if (in == X_center) return 0;
81     if (in < X_right) return (in - X_center) / (X_right - X_center);
82     if (in >= X_right) return 1;
83     mraa_result_print(MRAA_ERROR_UNSPECIFIED);
84     return 0;
85 }
86 
getYInput()87 float Joystick12::getYInput() {
88     float  in = mraa_aio_read (m_joystickCtxY);
89     if (in < Y_left) return -1;
90     if (in < Y_center) return -(Y_center - in) / (Y_center - Y_left);
91     if (in == Y_center) return 0;
92     if (in < Y_right) return (in - Y_center) / (Y_right - Y_center);
93     if (in >= Y_right) return 1;
94     mraa_result_print(MRAA_ERROR_UNSPECIFIED);
95     return 0;
96 }
97 
98 
99