-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cpp
120 lines (113 loc) · 4.61 KB
/
InputManager.cpp
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
#include "InputManager.h"
InputManager::InputManager(uint8_t fps, EffectManager* effectManager) {
this->effectManager = effectManager;
this->frametime = 1000/fps;
pinMode(Params::JOY_SW_PIN, INPUT);
pinMode(A0, INPUT);
}
void InputManager::calibrate() {
long sum = 0;
for (int i = 0; i < 500; i++) {
sum += readX();
delay(1);
}
null_pt = sum/500;
}
void InputManager::frame() {
if (millis() - lastFrame > (unsigned long)frametime) {
uint16_t x = readX();
bool sw = readSW();
int16_t tmp, delta;
if (sw && is_button_free) {
selected_mode++;
if (selected_mode == 5) {
selected_mode = 0;
}
is_button_free = false;
} else if (!sw) {
is_button_free = true;
}
switch(selected_mode) {
case 0: // Brightness
if (x > null_pt + Params::JOY_DEAD_ZONE) {
delta = map(x - (null_pt + Params::JOY_DEAD_ZONE), 0, null_pt - Params::JOY_DEAD_ZONE, 0, 7);
tmp = max(effectManager->getMasterBrightness() - delta, 0);
effectManager->setMasterBrightness(tmp);
is_joystick_free = false;
} else if (x < null_pt - Params::JOY_DEAD_ZONE) {
delta = map(x, 0, null_pt - Params::JOY_DEAD_ZONE, 7, 0);
tmp = min(effectManager->getMasterBrightness() + delta, 255);
effectManager->setMasterBrightness(tmp);
is_joystick_free = false;
} else if (x > (null_pt - Params::JOY_DEAD_ZONE) && x < (null_pt + Params::JOY_DEAD_ZONE) && !is_joystick_free) {
is_joystick_free = true;
}
break;
case 1: // Effect speed
if (x > null_pt + Params::JOY_DEAD_ZONE) {
delta = map(x - (null_pt + Params::JOY_DEAD_ZONE), 0, null_pt - Params::JOY_DEAD_ZONE, 0, 7);
tmp = max(effectManager->getEffectSpeed() - delta, 0);
effectManager->setEffectSpeed(tmp);
is_joystick_free = false;
} else if (x < null_pt - Params::JOY_DEAD_ZONE) {
delta = map(x, 0, null_pt - Params::JOY_DEAD_ZONE, 7, 0);
tmp = min(effectManager->getEffectSpeed() + delta, 255);
effectManager->setEffectSpeed(tmp);
is_joystick_free = false;
} else if (x > (null_pt - Params::JOY_DEAD_ZONE) && x < (null_pt + Params::JOY_DEAD_ZONE) && !is_joystick_free) {
is_joystick_free = true;
}
break;
case 2: // Effect intensity
if (x > null_pt + Params::JOY_DEAD_ZONE) {
delta = map(x - (null_pt + Params::JOY_DEAD_ZONE), 0, null_pt - Params::JOY_DEAD_ZONE, 0, 7);
tmp = max(effectManager->getEffectIntensity() - delta, 0);
effectManager->setEffectIntensity(tmp);
is_joystick_free = false;
} else if (x < null_pt - Params::JOY_DEAD_ZONE) {
delta = map(x, 0, null_pt - Params::JOY_DEAD_ZONE, 7, 0);
tmp = min(effectManager->getEffectIntensity() + delta, 255);
effectManager->setEffectIntensity(tmp);
is_joystick_free = false;
} else if (x > (null_pt - Params::JOY_DEAD_ZONE) && x < (null_pt + Params::JOY_DEAD_ZONE) && !is_joystick_free) {
is_joystick_free = true;
}
break;
case 3: // Effects
if (x > 4096 - Params::JOY_DEAD_ZONE && is_joystick_free) {
tmp = effectManager->getEffectId() - 1;
if (tmp < 0) tmp += Params::NB_EFFECTS;
effectManager->selectEffect(tmp);
is_joystick_free = false;
} else if (x < Params::JOY_DEAD_ZONE && is_joystick_free) {
tmp = (effectManager->getEffectId() + 1) % Params::NB_EFFECTS;
effectManager->selectEffect(tmp);
is_joystick_free = false;
} else if (x > (null_pt - Params::JOY_DEAD_ZONE) && x < (null_pt + Params::JOY_DEAD_ZONE) && !is_joystick_free) {
is_joystick_free = true;
}
break;
case 4: // Palettes
if (x > 4096 - Params::JOY_DEAD_ZONE && is_joystick_free) {
tmp = effectManager->getPaletteId() - 1;
if (tmp < 0) tmp += Params::NB_PALETTES;
effectManager->selectPalette(tmp);
is_joystick_free = false;
} else if (x < Params::JOY_DEAD_ZONE && is_joystick_free) {
tmp = (effectManager->getPaletteId() + 1) % Params::NB_PALETTES;
effectManager->selectPalette(tmp);
is_joystick_free = false;
} else if (x > (null_pt - Params::JOY_DEAD_ZONE) && x < (null_pt + Params::JOY_DEAD_ZONE) && !is_joystick_free) {
is_joystick_free = true;
}
break;
}
lastFrame = millis();
}
}
uint16_t InputManager::readX() {
return analogRead(A0);
}
bool InputManager::readSW() {
return digitalRead(Params::JOY_SW_PIN) == LOW;
}