-
Notifications
You must be signed in to change notification settings - Fork 7
/
input.h
90 lines (80 loc) · 2 KB
/
input.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
#pragma once
#include <Arduino.h>
namespace pixl {
/* An Input is an interface for Visualizations to use. getInput() returns a
float between 0 and 1. */
class Input {
public:
Input(){};
virtual void update()=0;
virtual float getInput(int index = 0)=0;
};
class NullInput : public Input {
public:
NullInput(){};
void update() {};
float getInput(int index = 0) { return 0.0; }
};
class FullInput : public Input {
public:
FullInput(){};
void update() {};
float getInput(int index = 0) { return 1.0; }
};
class ConstantInput : public Input {
public:
ConstantInput(float value) : value_(value) {};
void update() {};
float getInput(int index = 0) { return value_; }
private:
float value_;
};
class WaveInput : public Input {
public:
WaveInput(float seconds = 1.0) : millis_(seconds * 1000.0) {};
void update() {};
float getInput(int index = 0) {
return
(sin(2.0 * 3.14 * (millis() % millis_) / (float)millis_) + 1.0)
/ 2.0;
}
private:
int millis_;
};
class RandomInput : public Input {
public:
RandomInput(){};
void update() {};
float getInput(int index = 0) { return (float) rand() / (float) RAND_MAX; }
};
class SwitchInput : public Input {
public:
SwitchInput(int pin) : pin_(pin) { pinMode(pin_, INPUT); };
void update() {};
float getInput(int index = 0) { return digitalRead(pin_) == HIGH; }
private:
int pin_;
bool released_;
};
// This will return 1.0 only once per press by keeping track of when the button
// is released
class ButtonInput : public Input {
public:
ButtonInput(int pin) : pin_(pin), released_(true) {};
void update() {};
float getInput(int index = 0) {
if (digitalRead(pin_) == HIGH) {
if (released_ == true) {
released_ = false;
return 1.0;
}
} else if (released_ == false) {
released_ = true;
}
return 0.0;
}
private:
int pin_;
bool released_;
};
} // end namespace pixl