-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjoystick_ppm.h
126 lines (100 loc) · 2.91 KB
/
joystick_ppm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef JOYSTICK_PPM_H
#define JOYSTICK_PPM_H
#include "joystick_state.h"
#include "ppm_stream.h"
class ppm_mixer
{
public:
ppm_mixer(ppm_target& target);
public:
void set_mixing_value(unsigned char ppm_channel_id,
float value,
int mixing_id = 0,
bool no_write = false);
private:
ppm_target& m_target;
std::map<unsigned char, std::map<int, float>> m_map;
};
class joystick_ppm : public joystick_state
{
public:
struct axis_mapping_target
{
inline axis_mapping_target(unsigned char ppm_channel_id,
float factor,
float expo = 1.0f,
float offset = 0.0f,
float trim = 0.0f,
int mixing_id = 0)
: ppm_channel_id(ppm_channel_id),
factor(factor),
expo(expo),
offset(offset),
mixing_id(mixing_id > 0 ? mixing_id : --s_mixingid_counter)
{}
unsigned char ppm_channel_id;
float factor;
float expo;
float offset;
float user_trim = 0.0f;
int mixing_id;
static int s_mixingid_counter;
};
enum class ButtonActionType
{
SetValue = 0, // axis_or_channel_id is a PPM channel ID
AddTrim = 1, // axis_or_channel_id is an axis id
AddSubTrim = 2 // axis_or_channel_id is an axis id
};
struct button_action
{
inline button_action(ButtonActionType type,
unsigned char axis_or_channel_id,
float value)
: type(type),
axis_or_channel_id(axis_or_channel_id),
value(value)
{}
ButtonActionType type;
// depending on type, this is the axis number or the PPM channel ID
unsigned char axis_or_channel_id;
float value;
};
struct buttonize_axis
{
inline buttonize_axis(int low_button_id, int high_button_id)
: low_button_id(low_button_id), high_button_id(high_button_id)
{}
unsigned char low_button_id;
unsigned char high_button_id;
};
struct configuration
{
std::map<unsigned char, axis_mapping_target> AxisMappings;
std::map<unsigned char, button_action> ButtonPressActions;
std::map<unsigned char, button_action> ButtonReleaseActions;
std::map<unsigned char, buttonize_axis> Buttonizations;
};
public:
joystick_ppm(const configuration& config, ppm_target& target);
protected:
void loadUserTrim(unsigned char axis_number, float trim);
void loadUserSubtrim(unsigned char ppm_channel_id, float subtrim);
private:
void writePPM(unsigned char channel);
void updateVirtualButton(unsigned char number, bool value);
private:
void onAxisUpdated(unsigned char number) override;
void onButtonUpdated(unsigned char number) override;
private:
// should save trim & subtrim value to the configuration
virtual void saveTrimValue(unsigned char axis_number, float trim);
virtual void saveSubtrimValue(unsigned char ppm_channel_id, float subtrim);
protected:
configuration m_config;
std::map<unsigned char, float> m_mapSubtrims;
std::map<unsigned char, bool> m_mapVirtualButtons;
private:
ppm_mixer m_mixer;
};
#endif