-
Notifications
You must be signed in to change notification settings - Fork 1
/
kbd.c
89 lines (73 loc) · 1.99 KB
/
kbd.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
#include "audio.h"
struct {
int* banks;
int nbanks;
int* inputs;
int ninputs;
unsigned int keystates[4]; // space for 128 midi keys
kbd_onevent_t onevent;
} data;
void kbd_init(int* banks, int nbanks, int* inputs, int ninputs, kbd_onevent_t onevent)
{
data.banks = banks;
data.nbanks = nbanks;
data.inputs = inputs;
data.ninputs = ninputs;
data.onevent = onevent;
int b, i;
for (b = 0; b < nbanks; b++) {
gpio_config(banks[b], GPIO_OUT);
gpio_set(banks[b]);
}
for (i = 0; i < ninputs; i++) {
gpio_config(inputs[i], GPIO_IN);
}
gpio_pullups(inputs, ninputs);
}
void kbd_scan() {
int b, i;
for (b = 0; b < data.nbanks; b++) {
int bank = data.banks[b];
gpio_clear(bank);
delay(100);
for (i = 0; i < data.ninputs; i++) {
int input = data.inputs[i];
int key = b * data.ninputs + i;
int byte = key / 32;
int bit = key % 32;
unsigned int state = data.keystates[byte] & (1 << bit);
if (gpio_level(input) == 0) {
if (!state) {
data.onevent(KEY_DOWN, key);
data.keystates[byte] |= (1 << bit);
}
} else {
if (state) {
data.onevent(KEY_UP, key);
data.keystates[byte] &= ~(1 << bit);
}
}
}
gpio_set(bank);
}
}
void value_fillblock(mod_handle_t handle, float* block, void* d)
{
float value = *((float*) d);
int i;
for (i = 0; i < BLOCK_FRAMES; i++) {
block[i * 2] = block[i * 2 + 1] = value;
}
}
void value_ontrigger(mod_handle_t handle, void* d, float value)
{
float* data = (float*) d;
*data = value;
}
mod_handle_t value_create(float value)
{
mod_handle_t handle = mod_create(value_fillblock, value_ontrigger, sizeof(float));
float* data = mod_data(handle);
*data = value;
return handle;
}