2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
13 * processing in the value of the joystick-event in Tizen IVI 2.0
15 * event-type is used JS_EVENT_BUTTON and JS_EVENT_AXIS
16 * axis/button number value is determine in configuration file
19 * Steering value: -32767(Right) <-> 32767(Left)
20 * Accel value:0(full throttle) <-> 32767(free throttle)
21 * Brake value:0(full throttle) <-> 32767(free throttle)
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
34 #include <linux/input.h>
35 #include "CJoyStick.h"
36 #include "CJoyStickEV.h"
39 #include "ico-util/ico_log.h"
44 CJoyStickEV::CJoyStickEV()
46 // TODO Auto-generated constructor stub
47 memset(m_absInf, 0, sizeof(m_absInf));
48 m_devName = std::string(D_DEV_NAME_G27);
51 CJoyStickEV::~CJoyStickEV()
53 // TODO Auto-generated destructor stub
57 * @brief joystick device(event) open
58 * @retval number of file descriptor / error
60 int CJoyStickEV::Open()
62 string dirpath(D_DEV_DIR_PATH);
63 string nameparts(D_DEV_NAME_PARTS_EV);
68 printf("Open Device Name is <%s>\n", m_devName.c_str());
69 int rfd = deviceOpen(dirpath, nameparts, m_devName, devPath);
71 printf("Can't open device.\n");
76 fds.fd = m_nJoyStickID;
78 if (pthread_create(&m_threadid, NULL, CJoyStickEV::loop,
79 (void *) this) != 0) {
80 cerr << "Failed to create thread." << endl;
88 * @brief joystick device file close
89 * @return 0:close success
91 int CJoyStickEV::Close()
93 if (0 > m_nJoyStickID) {
96 return CJoyStick::Close();
100 * @brief joystick event read loop entrance
103 void *CJoyStickEV::loop(void *arg)
105 CJoyStickEV *src = reinterpret_cast < CJoyStickEV * >(arg);
106 return (void *) src->recvEV();
110 * @brief joystick event read loop
113 bool CJoyStickEV::recvEV()
117 if (poll(&fds, 1, -1) < 0) {
118 ICO_ERR("poll error");
121 if (0 != (fds.revents & POLLIN)) {
125 struct input_event ie;
127 while ((rc = read(m_nJoyStickID, &ie, sizeof(ie))) > 0) {
129 if (sizeof(ie) != rc) {
130 ICO_DBG("data not enough [%d/%d]", rc, sizeof(ie));
133 if (0 > rc && errno !=11) { /* read error(errno:11=EAGAIN) */
134 ICO_ERR("read error[ret=%d, errno=%d]", rc, errno);
142 * @brief joystick event read
143 * @param nubmer joystick event data(convert input_event.code)
144 * @param value joystick event data(convert input_event.value)
145 * @return convert input_event.type
146 * not -1:joystick event data -1:read data nothing
148 int CJoyStickEV::Read(int *num, int *val)
153 struct input_event ie;
168 r = getJS_EVENT_BUTTON(*num, *val, ie);
173 r = getJS_EVENT_AXIS(*num, *val, ie);
198 int CJoyStickEV::ReadData()
200 struct input_event ie;
202 int r = read(m_nJoyStickID, &ie, sizeof(ie));
208 * @brief change input_event value to js_event value
210 int CJoyStickEV::getJS_EVENT_BUTTON(int& num, int& val,
211 const struct input_event& s)
214 if ((BTN_JOYSTICK <= s.code) && (s.code <= BTN_GEAR_UP)) {
215 num = s.code - BTN_JOYSTICK;
217 return JS_EVENT_BUTTON;
223 return JS_EVENT_BUTTON;
227 * @brief change input_event value to js_event value
229 int CJoyStickEV::getJS_EVENT_AXIS(int& num, int& val,
230 const struct input_event& s)
235 // Convert value Steering
236 // 0 to 16353 -> -32766 to 32767
239 val = calc1pm32767((int)s.value, m_absInf[E_ABSX]);
244 val = calc1pm32767((int)s.value, m_absInf[E_ABSY]);
249 val = calc3p32767Reverse((int)s.value, m_absInf[E_ABSZ]);
254 val = calc3p32767Reverse((int)s.value, m_absInf[E_ABSRZ]);
272 * @brief calc value case 1
273 * change to -32767 <-> 32767
275 int CJoyStickEV::calc1pm32767(int val, const struct input_absinfo& ai)
277 int a = ai.maximum - ai.minimum;
278 double b = (double)val / (double)a;
279 int c = ((int) (b * 65534)) - 32767;
284 * @brief calc value case 2
285 * change to 0 <-> 65534
287 int CJoyStickEV::calc2p65535(int val, const struct input_absinfo& ai)
289 int a = ai.maximum - ai.minimum;
290 double b = (double)val / (double)a;
291 int c = (int) (b * 65534);
296 * @brief calc value case 3
297 * change to 0 <-> 32767
299 int CJoyStickEV::calc3p32767(int val, const struct input_absinfo& ai)
301 int a = ai.maximum - ai.minimum;
302 double b = (double)val / (double)a;
303 int c = (int) (b * 32767);
308 * @brief calc value case 3
309 * change to 32767 <-> 0
311 int CJoyStickEV::calc3p32767Reverse(int val, const struct input_absinfo& ai)
313 int a = ai.maximum - ai.minimum;
314 double b = (double)val / (double)a;
315 int c = abs(((int) (b * 32767)) - 32767);
322 bool CJoyStickEV::getDeviceName(int fd, char* devNM, size_t sz)
327 if (0 > ioctl(fd, EVIOCGNAME(sz), devNM)) {
334 * End of File.(CJoyStickEV.cpp)