-
Notifications
You must be signed in to change notification settings - Fork 0
/
NESTrument.ino
101 lines (83 loc) · 2.21 KB
/
NESTrument.ino
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
#include "Pitches.h"
#include "NESController.h"
// Arduino Pins
#define debugLED 13
#define clockLine 2
#define dataLine 4
#define latchLine 7
#define speakerLine 8
void controllerButtonPressed(int);
void controllerButtonReleased(int);
void playToneFromState(int*, int);
void playTonesFromReplay();
NESController controller(dataLine, latchLine, clockLine);
void setup()
{
pinMode(debugLED, OUTPUT);
pinMode(clockLine, OUTPUT);
pinMode(dataLine, INPUT);
pinMode(latchLine, OUTPUT);
controller.registerListeners(controllerButtonPressed,
controllerButtonReleased);
}
void loop()
{
controller.run();
if(controller.isRecording())
{
digitalWrite(debugLED, HIGH);
}
else
{
digitalWrite(debugLED, LOW);
}
}
void playToneFromState(int button)
{
if(button == BTN_A) tone(speakerLine, NOTE_B5);
else if(button == BTN_LEFT) tone(speakerLine, NOTE_C4);
else if(button == BTN_UP) tone(speakerLine, NOTE_D4);
else if(button == BTN_RIGHT) tone(speakerLine, NOTE_E4);
else if(button == BTN_DOWN) tone(speakerLine, NOTE_F4);
else if(button == BTN_B) tone(speakerLine, NOTE_A5);
else noTone(speakerLine);
}
void playTonesFromReplay()
{
int** replay_states = controller.getReplayStates();
/* Iterate over our stored replay state */
for(int i = 0; i < NESController::RECORD_BUFFER_MAX; i++)
{
/* End of recording, bail. */
if(replay_states[i] == 0)
return;
noTone(speakerLine);
/* Time between this note and last note, delay. */
if(i!=0) delay(replay_states[i][2]);
/* Every element in the recording state is both
when a key is released, and how long it has been pressed for.
When we see a note, play it for the time it was down for. */
int time = millis();
while(millis() - time < replay_states[i][1])
{
playToneFromState(replay_states[i][0]);
}
}
}
void controllerButtonReleased(int button)
{
playToneFromState(-1);
}
void controllerButtonPressed(int button)
{
if(button == BTN_START)
{
controller.toggleRecording();
}
if(button == BTN_SELECT)
{
if(!controller.isRecording())
playTonesFromReplay();
}
playToneFromState(button);
}