-
Notifications
You must be signed in to change notification settings - Fork 1
/
padkontrol.c
407 lines (318 loc) · 10.7 KB
/
padkontrol.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/****************************************************************************
2012 Jonathan Sieber: modified for mpd and light control automation
Korg padKontrol native mode reference code and demo.
(C)2009 Stephen Mosher
Contact: smosher_@hotmail.com
To build:
Make sure the portmidi development library (eg. portmidi-dev) is
installed, link against it:
gcc native_demo.c -o native_demo -lportmidi
This code was written and tested on Linux, but it should be portable.
Running:
Run without args for usage, and to list devices. First arg is input
device, second is output. Ex:
...
Device: # 8 (output) 'padKONTROL MIDI 1'
Device: # 9 (input ) 'padKONTROL MIDI 1'
Device: #10 (output) 'padKONTROL MIDI 2'
Device: #11 (input ) 'padKONTROL MIDI 2'
Device: #12 (input ) 'padKONTROL MIDI 3'
I need to use "padKONTROL MIDI 2", so I invoke like so:
./native_demo 11 10
*****************************************************************************
Demo functions:
Full report from the device on all pads/buttons/knobs/etc.
Face buttons toggle flashing on/off
Pads flash when triggered (1~8 shortest flash, 9~16 longest)
Simple 7seg messages on knobs and xy pad.
TODO:
Add a function for manipulating individual 7seg LEDs, including the
decimals.
Related materials:
Page related to this code:
http://comichunter.net/nowhere/pK_native/
Korg pK SysEx manual (elsewhere referred to as 'the PDF'):
http://www.thecovertoperators.org/uploads/PadKONTROL%20imp.pdf
(via) http://www.thecovertoperators.org/
Hap's guide (very handy companion):
http://www.mediafire.com/file/lkmwfajtjzg/padkontrol-native-mode.pdf
(via) http://www.korgforums.com/forum/phpBB2/viewtopic.php?t=28030
Interesting functions:
- The start_native() function has notes about entering native mode.
- The native_buttonstate() and native_ledmsg() are all that's needed to
update the lights.
- The print_...msg() functions call all of the light updates in addition
to printing received messages.
- The poll_native() function classifies all received messages.
A note on the code:
This code is meant to be a handy reference and as a result good coding
style and good practices have occasionally been sacrificed in favour of
brevity and information locality, and in the interest of getting it
finished quickly. You are free to reuse any code found in this source
file.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <portmidi.h>
#include <porttime.h>
#include <string.h>
#include <unistd.h>
// define this to see (most) raw messages (sent and received):
#undef BUGZ
// sleep time in usec, dramatically reduces CPU usage
#define POLLING_USLEEP 1000
#include "padkontrol.h"
const char *FaceButtonStrings[] = {
"Scene Button",
"Message Button",
"Setting Button",
"Note/CC Button",
"MIDI CH Button",
"SW Type Button",
"Rel. Val. Button",
"Velocity Button",
"Port Button",
"Fixed Velocity Button",
"Prog. Change Button",
"X Button",
"Y Button",
"Knob 1 Assign Button",
"Knob 2 Assign Button",
"Pedal Button",
"Roll Button",
"Flam Button",
"Hold Button",
"UNKNOWN BUTTON",
"XY Pad"
};
void sysex_print(char *buf) {
int i;
printf("Sending SysEx message:");
for(i=0;;i++) {
if(!(i%4)) printf("\n\t");
printf("%02hhx ", buf[i]);
if(buf[i] == (char)0xf7) break;
}
printf("\n");
return;
}
void pk_buttonstate(PadKontrol* pad, ButtonLightSpec button, ButtonState state)
{
char buffer[] = {0xf0, 0x42, 0x40, 0x6e, 0x08, 0x01, 0x00, 0x00, 0xf7};
buffer[6] = button;
buffer[7] = state;
#ifdef BUGZ
sysex_print(buffer);
#endif
Pm_WriteSysEx(pad->midi_out, 0, buffer);
}
void pk_ledmsg(PadKontrol* pad, char abc[3], char flash) {
char led[] = {
0xf0, 0x42, 0x40, 0x6e,
0x08, 0x22, 0x04, 0x00,
0x29, 0x29, 0x29, 0xf7
};
// this accepts ascii codes from 0x20~0x7f according to the pdf.
// note: any character in this range that it cannot print will be converted to space
led[7] = flash & 0x1;
led[8] = abc[0];
led[9] = abc[1];
led[10] = abc[2];
#ifdef BUGZ
sysex_print(led);
#endif
Pm_WriteSysEx(pad->midi_out, 0, led);
}
// note: msg must be pre-allocated
// (this function is needlessly complicated for personal reasons)
PmError read_native(PmStream *midi, char *msg) {
PmError c;
PmEvent buf;
int header=0, body=0, complete=0;
while(c=Pm_Read(midi, &buf, 1) >0) {
if(c<0) return c;
#ifdef BUGZ
printf("buffer: 0x%08lx\n", buf.message);
#endif
if(buf.message == 0x6e4042f0) header=1; // device header
if((buf.message == 0xf7) && body) {
complete = 1;
break;
}
if(buf.message & 0x08) { // this is actually part of the device header
body=1;
msg[0] = (char)(buf.message>>8);
msg[1] = (char)(buf.message>>16);
msg[2] = (char)(buf.message>>24);
}
}
return complete;
}
void print_nativestatemsg(char *msg) {
printf("Native mode is now %s\n", msg[2]==0x03?"enabled":"disabled");
return;
}
void print_datadumpmsg(char *msg) {
if(msg[1] == 0x00 || msg[1] == 0x01) {
// packet comm
printf("Packet %hhd received, %s\n", msg[1], msg[2]?"Err":"OK");
} else {
// data dump: not used here
printf("Data dump with params: 0x%02hhx 0x%02hhx\n", msg[1], msg[2]);
}
return;
}
void print_unknownmsg(char *msg) {
printf("Unknown command: 0x%02hhx(0x%02hhx, 0x%02hhx)\n", msg[0], msg[1], msg[2]);
return;
}
enum {
NativeMessage_NativeState = 0x40,
NativeMessage_DataDump = 0x5f,
NativeMessage_Pad = 0x45,
NativeMessage_Face = 0x48,
NativeMessage_Knob = 0x49,
NativeMessage_Encoder = 0x43,
NativeMessage_XY = 0x4b
} NativeMessage;
void poll_native(PadKontrol* pad) {
char msg[3];
PmError c;
int lights[0x13];
memset(&lights, 0, sizeof(lights));
for(;;) {
c=read_native(pad->midi_in, msg);
if(!c) {
usleep(POLLING_USLEEP);
continue;
}
if(c<0) return; // error condition
switch(msg[0]&0xff) {
case NativeMessage_NativeState:
print_nativestatemsg(msg);
break;
case NativeMessage_DataDump:
print_datadumpmsg(msg);
break;
case NativeMessage_Pad:
pad->padmsg(pad, msg[1] & 0x3f, msg[1] & 0x40, msg[2]);
break;
case NativeMessage_Face:
pad->facemsg(pad, msg[1], msg[2]);
break;
case NativeMessage_Knob:
pad->knobmsg(pad, msg[1], msg[2]);
break;
case NativeMessage_Encoder:
// note: 0x01=inc, 0x7f=dec. rshift converts these to false, true
pad->encodermsg(pad, msg[2]>>1);
break;
case NativeMessage_XY:
pad->xymsg(pad, msg[1], msg[2]);
break;
default:
print_unknownmsg(msg);
break;
}
}
return;
}
void start_native(PmStream *out) {
/**************************************************************************
The only hairy part of setting up native mode is the packet communications
messages. These messages init native mode according to tables at the end
of the PDF. I've documented them a little here to help people understand
what's going on. The sections marked data are from the tables at the end.
Packet comm. #1 specifies the global midi channel, which real-time events
to transmit to the host, and the midi channels and controller numbers for
those events (which I gather are still sent, but on a different port,
while in native mode.)
Packet comm. #2 specifies the initial states for all lights, with the
exception of the decimals in the 7seg display.
**************************************************************************/
// native mode in request (section 3-1, fig 15, pg. 6)
char native_start[] = {
0xf0, 0x42, 0x40, 0x6e, 0x08,
0x00, 0x00, 0x01,
0xf7};
// packet comm. #1 (section 3-1, fig 17, pg. 6)
// see table 3 for the packet format
// this is needed to init the pK. for communicating
char native_comms[] = {
0xf0, 0x42, 0x40, 0x6e, 0x08,
0x3f, 0x2a, 0x00, // 0x3f = packet comm, 0x2a = msg len (41 bytes) +1, 0x00 = comm #1
0x00, 0x05, 0x05, 0x05, 0x7f, 0x7e, 0x7f, 0x7f, 0x03, 0x0a, 0x0a, 0x0a, 0x0a, // data ( 13 bytes)
0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x01, 0x02, // data (+ 14 bytes)
0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, // data (+ 14 bytes = 41 bytes)
0xf7}; // EOX
// packet comm. #2 (section 3-1, fig 19, pg. 7)
// see table 4 for the packet format
// turns off all lights.
char native_lightsoff[] = {
0xf0, 0x42, 0x40, 0x6e, 0x08,
0x3f, 0x0a, 0x01, // 0x3f = packet comm, 0x0a = msg len (9 bytes) +1, 0x01 = comm #2
0x00, 0x00, 0x00, 0x00, // data ( 4 bytes)
0x00, 0x00, 0x29, 0x29, 0x29, // data (+ 5 bytes = 9 bytes)
0xf7}; // EOX
Pm_WriteSysEx(out, 0, native_start);
Pm_WriteSysEx(out, 0, native_comms);
Pm_WriteSysEx(out, 0, native_lightsoff);
return;
}
void pk_process(PadKontrol* pad) {
if (pad->midi_in == 0) {
Pm_OpenInput(&pad->midi_in, pad->devi, NULL, 512, NULL, NULL);
}
if (pad->midi_out == 0) {
Pm_OpenOutput(&pad->midi_out, pad->devo, NULL, 512, NULL, NULL, 0);
start_native(pad->midi_out);
}
poll_native(pad);
return;
}
void pk_printdevicelist(int argc, char** argv)
{
int dev_count, i;
PmDeviceID devi, devo;
const PmDeviceInfo *in, *out;
Pm_Initialize();
dev_count = Pm_CountDevices();
printf("\nUsage: %s <input device id> <output device id>\n\n", argv[0]);
printf("Listing %d devices:\n", dev_count);
for(i=0;i<dev_count;i++) {
devi = i;
in = Pm_GetDeviceInfo(devi);
if(in==NULL) {
printf("\tDevice id (%2d) out of range\n", devi);
break;
}
printf("\tDevice: #%2d (%s%s) '%s'\n",
devi, in->input?"input ":"", in->output?"output":"",
in->name);
}
}
int pk_init(PadKontrol* pad, int devi, int devo) {
const PmDeviceInfo *in, *out;
Pm_Initialize();
pad->midi_in = 0;
pad->midi_out = 0;
pad->devi= devi;
pad->devo = devo;
Pt_Start(1,0,0);
in=Pm_GetDeviceInfo(devi);
out=Pm_GetDeviceInfo(devo);
printf("Using device #%2d, '%s' for input\n", devi, in->name);
printf("Using device #%2d, '%s' for output\n", devo, out->name);
if(!out->output) {
printf("Selected device is unsuitable for output, exiting.\n");
return -1;
}
if(!in->input) {
printf("Selected device is unsuitable for input, exiting.\n");
return -1;
}
}
void pk_deinit(PadKontrol* pad)
{
Pm_Terminate();
}