/
keys.c
61 lines (51 loc) · 1.46 KB
/
keys.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
/* keys.c -
Routines to scan the piano key matrix
(c) 2016 Ken Rossato
*/
#include "main.h"
#include "keys.h"
#define KEY_SIZE 49
uint8_t key_octave_transpose = 15;
void key_octave_up() {
if (key_octave_transpose < 27)
key_octave_transpose += 12;
}
void key_octave_down() {
if (key_octave_transpose > 3)
key_octave_transpose -= 12;
}
void scan_keys() {
PORTB &= ~(_BV(PB0) | _BV(PB1));
PORTB |= _BV(PB0);
PORTB &= ~_BV(PB0);
PORTB |= _BV(PB1);
#ifdef SHIFT_74HC595
// I use the simpler 74HC164, but you can use a 595 by tying the two
// clock inputs together. You'll need this extra clock pulse to load the first bit.
PORTB |= _BV(PB0);
PORTB &= ~_BV(PB0);
#endif
uint8_t col;
for (col = 11; col > 3; --col) {
uint8_t rows = PIND | 0x01;
PORTB |= _BV(PB0);
PORTB &= ~_BV(PB0);
uint8_t note;
uint8_t col_start = (col & 7) + key_octave_transpose;
uint8_t col_end = KEY_SIZE + key_octave_transpose;
if (rows != 0xFF) {
for (note = col_start; note < col_end; note += 8) {
if (!(rows & 0x80))
start_note(BANK_KEYS, note);
else
stop_note(BANK_KEYS, note);
rows = rows << 1;
}
}
else {
for (note = col_start; note < col_end; note += 8) {
stop_note(BANK_KEYS, note);
}
}
}
}