1 /*
2  *  Copyright (C) 2016-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 namespace PERIPHERALS
12 {
13 class CPeripheral;
14 }
15 
16 namespace KODI
17 {
18 namespace JOYSTICK
19 {
20 class IButtonMap;
21 
22 /*!
23  * \ingroup joystick
24  * \brief Analog axis deadzone filtering
25  *
26  * Axis is scaled appropriately, so position is continuous
27  * from -1.0 to 1.0:
28  *
29  *            |    / 1.0
30  *            |   /
31  *          __|__/
32  *         /  |
33  *        /   |--| Deadzone
34  *  -1.0 /    |
35  *
36  * After deadzone filtering, the value will be:
37  *
38  *   - Negative in the interval [-1.0, -deadzone)
39  *   - Zero in the interval [-deadzone, deadzone]
40  *   - Positive in the interval (deadzone, 1.0]
41  */
42 class CDeadzoneFilter
43 {
44 public:
45   CDeadzoneFilter(IButtonMap* buttonMap, PERIPHERALS::CPeripheral* peripheral);
46 
47   /*!
48    * \brief Apply deadzone filtering to an axis
49    * \param axisIndex The axis index
50    * \param axisValue The axis value
51    * \return The value after applying deadzone filtering
52    */
53   float FilterAxis(unsigned int axisIndex, float axisValue);
54 
55 private:
56   /*!
57    * \brief Get the deadzone value from the peripheral's settings
58    * \param axisIndex The axis index
59    * \param[out] result The deadzone value
60    * \param featureName The feature that axisIndex is mapped to
61    * \param settingName The setting corresponding to the given feature
62    * \return True if the feature is an analog stick and the peripheral has the setting
63    */
64   bool GetDeadzone(unsigned int axisIndex,
65                    float& result,
66                    const char* featureName,
67                    const char* settingName);
68 
69   /*!
70    * \brief Utility function to calculate the deadzone
71    * \param value The value
72    * \param deadzone The deadzone
73    * \return The scaled deadzone
74    */
75   static float ApplyDeadzone(float value, float deadzone);
76 
77   // Construction parameters
78   IButtonMap* const m_buttonMap;
79   PERIPHERALS::CPeripheral* const m_peripheral;
80 };
81 } // namespace JOYSTICK
82 } // namespace KODI
83