forked from NattyBumppo/Simon-Clone-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cpp
87 lines (64 loc) · 1.41 KB
/
Input.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
/*
Functions to handle button input on the Arduino.
*/
#include "Arduino.h"
#include "Input.h"
#include "SNESpad.h"
#include "pins.h"
#include "gameplay.h"
#include "Modes.h"
Input* Input::spInstance = NULL;
// RED, GREEN, BLUE, YELLOW
const int colorButtons[] = {SNES_A, SNES_Y, SNES_X, SNES_B};
Input::Input()
:
curPad(0),
prevPad(0)
{
pinMode(DIFF_SWITCH_PIN, INPUT);
padInput = new SNESpad(SNESPAD_STROBE_PIN, SNESPAD_CLOCK_PIN, SNESPAD_DATA_PIN);
// Use unconnected analog pin 1 as a random seed
randomSeed(analogRead(A1));
spInstance = this;
}
Input& Input::Get()
{
if(NULL == spInstance)
spInstance = new Input();
return *spInstance;
}
void Input::Update()
{
prevPad = curPad;
curPad = padInput->buttons();
}
// Buttons that are pressed this frame.
int Input::Buttons()
{
return curPad;
}
// Buttons that were pressed this frame, but not last
int Input::Pressed()
{
return curPad & ~prevPad;
}
// Buttons that were not pressed this frame, but were last
int Input::Released()
{
return ~curPad & prevPad;
}
// Buttons that were pressed both this and last frame
int Input::Held()
{
return curPad & prevPad;
}
// The Button Flag for the specified color
int Input::SNESCode(Color color)
{
return colorButtons[color];
}
// State of the Difficulty Switch
Difficulty Input::GetDifficulty()
{
return digitalRead(DIFF_SWITCH_PIN) == HIGH ? HARD : EASY;
}