6 short calibration_correction(int64_t v
, int64_t low
, int64_t high
)
11 double _pos
= 65535 * (_v
- _low
) / (_high
- _low
) - 32768;
17 return static_cast<short>(_pos
);
20 short angle_to_bitmask(int pov
)
23 if((pov
>= 0 && pov
<= 6000) || (pov
>= 30000 && pov
<= 36000))
25 if(pov
>= 3000 && pov
<= 15000)
27 if(pov
>= 12000 && pov
<= 24000)
29 if(pov
>= 21000 && pov
<= 33000)
34 joystick_model::change_info::change_info()
36 has_been_read
= false;
41 void joystick_model::change_info::update(short newv
)
46 bool joystick_model::change_info::read(short& val
)
48 bool r
= (last_known
!= last_read
|| !has_been_read
);
50 val
= last_read
= last_known
;
54 bool joystick_model::read_common(std::vector
<change_info
>& i
, unsigned id
, short& res
)
58 return i
[id
].read(res
);
61 unsigned joystick_model::size_common(std::vector
<change_info
>& i
)
66 void joystick_model::report_button(uint64_t id
, bool value
)
68 if(button_map
.count(id
))
69 _buttons
[button_map
[id
]].update(value
? 1 : 0);
72 void joystick_model::report_pov(uint64_t id
, int angle
)
75 _hats
[pov_map
[id
]].update(angle_to_bitmask(angle
));
78 void joystick_model::report_axis(uint64_t id
, int64_t value
)
81 if(axis_map
.count(id
))
82 _axes
[axis_map
[id
]].update(calibration_correction(value
, aranges
[id
].first
, aranges
[id
].second
));
83 //It isn't known axis. See if it is connected with axis pair for hat.
84 else if(hataxis_map
.count(id
)) {
85 struct hat_axis_info
& ai
= hataxis_map
[id
];
86 struct hat_info
& hi
= hatinfos
[ai
.hnum
];
88 if(value
<= -ai
.mindev
) v
= -1;
89 if(value
>= ai
.mindev
) v
= 1;
90 if(ai
.yflag
) hi
.ystate
= v
;
93 if(hi
.ystate
< 0) v
|= 1;
94 if(hi
.xstate
> 0) v
|= 2;
95 if(hi
.ystate
> 0) v
|= 4;
96 if(hi
.xstate
< 0) v
|= 8;
97 _hats
[ai
.hnum
].update(v
);
101 unsigned joystick_model::new_axis(uint64_t id
, int64_t minv
, int64_t maxv
, const std::string
& xname
)
104 aranges
[id
] = std::make_pair(minv
, maxv
);
107 axisnames
[n
] = xname
;
111 unsigned joystick_model::new_button(uint64_t id
, const std::string
& xname
)
113 unsigned n
= buttons();
114 _buttons
.resize(n
+ 1);
116 buttonnames
[n
] = xname
;
120 unsigned joystick_model::new_hat(uint64_t id_x
, uint64_t id_y
, int64_t min_dev
, const std::string
& xname_x
,
121 const std::string
& xname_y
)
124 hatinfos
.resize(n
+ 1);
125 hatinfos
[n
].xstate
= 0;
126 hatinfos
[n
].ystate
= 0;
128 hataxis_map
[id_x
].yflag
= false;
129 hataxis_map
[id_x
].hnum
= n
;
130 hataxis_map
[id_x
].mindev
= min_dev
;
131 hataxis_map
[id_y
].yflag
= true;
132 hataxis_map
[id_y
].hnum
= n
;
133 hataxis_map
[id_y
].mindev
= min_dev
;
134 hatnames
[n
] = "<X: " + xname_x
+ " Y: " + xname_y
+ ">";
138 unsigned joystick_model::new_hat(uint64_t id
, const std::string
& xname
)
147 std::pair
<int64_t, int64_t> joystick_model::axiscalibration(unsigned id
)
149 for(auto i
: axis_map
)
151 return aranges
[i
.first
];
152 return std::make_pair(0, 0);
155 std::string
joystick_model::axisname(unsigned id
)
157 if(axisnames
.count(id
)) return axisnames
[id
];
161 std::string
joystick_model::buttonname(unsigned id
)
163 if(buttonnames
.count(id
)) return buttonnames
[id
];
167 std::string
joystick_model::hatname(unsigned id
)
169 if(hatnames
.count(id
)) return hatnames
[id
];
173 void joystick_model::name(const std::string
& newn
)
178 const std::string
& joystick_model::name()
183 std::string
joystick_model::compose_report(unsigned jnum
)
185 std::ostringstream out
;
186 out
<< "Joystick #" << jnum
<< ": " << joyname
<< std::endl
;
187 for(size_t i
= 0; i
< _axes
.size(); i
++) {
188 auto c
= axiscalibration(i
);
189 out
<< " Axis #" << i
<< ": " << axisnames
[i
] << "(" << c
.first
<< " - " << c
.second
<< ")"
192 for(size_t i
= 0; i
< _buttons
.size(); i
++)
193 out
<< " Button #" << i
<< ": " << buttonnames
[i
] << std::endl
;
194 for(size_t i
= 0; i
< _hats
.size(); i
++)
195 out
<< " Hat #" << i
<< ": " << hatnames
[i
] << std::endl
;