-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.c
119 lines (110 loc) · 3.04 KB
/
play.c
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
#include "play.h"
#include <avr/io.h>
#include "interface.h"
#include "midi.h"
#define DEBOUNCE 50
// Starting parameters
uint8_t mode = 0;
uint8_t octave = 3;
uint8_t noteVelocity = 100;
uint8_t noteButtonState;
uint8_t controlButtonState;
uint8_t lastNoteSent;
void play (key_t *keys, controlButton_t *controlButtons) {
while(1) {
switch(mode) {
case(0):
normalMode(keys);
break;
case(1):
holdMode(keys);
break;
}
control(controlButtons);
}
}
void control(controlButton_t * controlButtons) {
for(uint8_t i = 0; i < numctrl; ++i) {
controlButtonState = bitIsSet(*(controlButtons[i].pinReg), controlButtons[i].pin);
// Control debounce has reached 0
if(!controlButtons[i].debounce) {
// New control input detected
if(!controlButtonState) {
if(i == 0 || octave < 10) {
octave += controlButtons[i].value;
}
else if(i == 1 || octave > 1) {
octave += controlButtons[i].value;
}
controlButtons[i].debounce = DEBOUNCE;
}
}
// Control debounce still counting down
else {
// Control has been released
if (controlButtonState) {
--controlButtons[i].debounce;
}
// Control is held or has been repressed
else {
controlButtons[i].debounce = DEBOUNCE;
}
}
}
}
void normalMode(key_t * keys) {
for(int i = 0; i < numKeys; ++i) {
noteButtonState = bitIsSet(*(keys[i].pinReg),keys[i].pin);
// Key debounce has reached 0
if (!keys[i].debounce) {
// New key input detected
if (!noteButtonState) {
noteOn(keys[i].midiNote + 12*octave, noteVelocity);
keys[i].noteSent = keys[i].midiNote + 12*octave;
keys[i].debounce = DEBOUNCE;
setBit(PORTD,PIND0);
}
}
// Key debounce still counting down
else {
// Key has been released
if (noteButtonState) {
// Decrementing debounce gets it to zero
if (--keys[i].debounce == 0) {
noteOff(keys[i].noteSent);
clearBit(PORTD,PIND0);
}
}
// Key is held or has been repressed
else {
keys[i].debounce = DEBOUNCE;
}
}
}
}
// Not implemented in current design
void holdMode(key_t * keys) {
/*while(modeButtonState == LOW) {
for(int i = 0; i < numKeys; ++i) {
noteButtonState = bitIsSet(*(keys[i].pinReg),keys[i].pin);
if(keys[i].debounce == 0) {
if(noteButtonState == LOW) {
if(keys[i]->midiNote == lastNoteSent) {
noteOff(lastNoteSent);
}
else {
noteOff(lastNoteSent);
noteOn(keys[i].midiNote + 12*octave;);
lastNoteSent = keys[i].midiNote + 12*octave;;
keys[i].debounce = DEBOUNCE;
}
}
}
else {
--keys[i].debounce;
}
}
control();
}
noteOff(lastNoteSent);*/
}