-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoyStick.cpp
More file actions
93 lines (77 loc) · 1.58 KB
/
Copy pathJoyStick.cpp
File metadata and controls
93 lines (77 loc) · 1.58 KB
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
/**
* @Author: Claudius Laves <claudi>
* @Date: 02-06-2020 18:13:35
* @Email: claudiuslaves@gmx.de
* @Filename: JoyStick.cpp
* @Last modified by: claudi
* @Last modified time: 03-06-2020 14:05:49
*/
#include "JoyStick.h"
void JoyStick::setup()
{
pinMode(x_pin, INPUT);
pinMode(y_pin, INPUT);
pinMode(sw_pin, INPUT);
autoCalibrate();
}
void JoyStick::setOutput(float _min, float _max)
{
min_out = _min;
max_out = _max;
}
void JoyStick::update()
{
x_reading = analogRead(x_pin);
y_reading = analogRead(y_pin);
sw_reading = digitalRead(sw_pin);
x = map_float(x_reading, MIN_READ_VALUE, MAX_READ_VALUE, min_out, max_out, mean_x);
y = map_float(y_reading, MIN_READ_VALUE, MAX_READ_VALUE, min_out, max_out, mean_y);
}
void JoyStick::autoCalibrate()
{
mean_x = analogRead(x_pin);
mean_y = analogRead(y_pin);
}
float JoyStick::map_float(int val, int min_in, int max_in, float min_out, float max_out, int mean)
{
float output;
if(abs(val-mean) < mean_offset)
{
output = 0;
}
else if (val < (mean - mean_offset))
{
output = 1.0 - ((float)(val - min_in) / (float)((mean - mean_offset) - min_in));
output *= min_out;
}
else
{
output = (float)(val - mean + mean_offset) / (float)(max_in - mean + mean_offset);
output *= max_out;
}
return output;
}
float JoyStick::getX()
{
return x;
}
float JoyStick::getY()
{
return y;
}
bool JoyStick::isPressed()
{
return (sw_reading == 1);
}
int JoyStick::getRawX()
{
return x_reading;
}
int JoyStick::getRawY()
{
return y_reading;
}
void JoyStick::setOffset(int os)
{
mean_offset = os;
}