summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2012-09-11 15:26:04 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2012-09-11 15:26:04 +0200
commita52a1d65a95606c254e384b558de49371002252c (patch)
tree58b292cf679e5d086fa824fc7b83c5d4810c0525
parent2421be8140898e5306d9622c7856f71adc8f4518 (diff)
downloaddigitale-debutanten-a52a1d65a95606c254e384b558de49371002252c.tar.gz
include symbolic control name in GenEvent, so port creation can be defined in GenEvent (by default)
* simplified and cleaned up NanoEvent and RumbleEvent code
-rw-r--r--lib/GenEvent.ck34
-rw-r--r--lib/NanoEvent.ck74
-rw-r--r--lib/RumbleEvent.ck85
3 files changed, 71 insertions, 122 deletions
diff --git a/lib/GenEvent.ck b/lib/GenEvent.ck
index 63328ea..ba73445 100644
--- a/lib/GenEvent.ck
+++ b/lib/GenEvent.ck
@@ -2,9 +2,41 @@
* Base class for controller events
*/
public class GenEvent extends Event {
- /* normalized value between [0, 1] */
+ class Port extends Step {
+ fun void
+ update(GenEvent @ev, string control)
+ {
+ while (ev => now)
+ if (control => ev.isControl)
+ ev.getFloat(-1, 1) => next;
+ /* never reached */
+ }
+ }
+
+ /* symbolic control name */
+ string control;
+ /* normalized control value between [0, 1] */
float value;
+ fun int
+ isControl(string c)
+ {
+ return control == c;
+ }
+
+ /*
+ * Create and return UGen that generates a control's value,
+ * normalized between [-1, 1]
+ */
+ fun Step @
+ getPort(string control)
+ {
+ Port p;
+ spork ~ p.update(this, control);
+
+ return p;
+ }
+
/*
* Getter functions to scale `value'
*/
diff --git a/lib/NanoEvent.ck b/lib/NanoEvent.ck
index 1ae9c59..2bf6378 100644
--- a/lib/NanoEvent.ck
+++ b/lib/NanoEvent.ck
@@ -7,38 +7,15 @@ public class NanoEvent extends GenEvent {
/* map scene name and control id (0-255) to control name */
static string @__controlToName[][]; /* pseudo-private */
- class Port extends Step {
- fun void
- fetch(int device, string scene, string control)
- {
- MidiIn min;
-
- if (!min.open(device)) {
- cherr <= "Cannot open MIDI device " <= device
- <= IO.newline();
- me.exit();
- }
- chout <= "Opened MIDI device " <= device <= " (" <= min.name() <= ")"
- <= IO.newline();
-
- while (min => now) {
- while (MidiMsg msg => min.recv) {
- if (NanoEvent.__channelToScene[msg.data1 & 0x0F] == scene &&
- NanoEvent.__controlToName[scene] != null &&
- NanoEvent.__controlToName[scene][msg.data2] == control)
- msg.data3*2/127.0 - 1 => next;
- }
- }
- /* not reached */
- }
- }
-
- /* by default open MIDI Through port, actual connection is done by Jack */
+ /*
+ * Should be "MIDI Through Port-0" (actual connection is done by Jack)
+ * Can be changed by constructor since __midi_loop() starts only
+ * when constructing shred passes time
+ */
0 => int device;
string wantScene;
string scene;
- string control;
int CCId;
fun int
@@ -46,22 +23,6 @@ public class NanoEvent extends GenEvent {
{
return scene == s;
}
- fun int
- isControl(string c)
- {
- return control == c;
- }
-
- fun Step @
- getPort(string control)
- {
- if (wantScene == "")
- return null;
-
- Port p;
- spork ~ p.fetch(device, wantScene, control);
- return p;
- }
fun void
__midi_loop() /* pseudo-private */
@@ -73,32 +34,33 @@ public class NanoEvent extends GenEvent {
<= IO.newline();
me.exit();
}
- chout <= "Opened MIDI device " <= device <= " (" <= min.name() <= ")"
+ chout <= "Opened MIDI device " <= min.num() <= " (" <= min.name() <= ")"
<= IO.newline();
while (min => now) {
while (MidiMsg msg => min.recv) {
- __channelToScene[msg.data1 & 0x0F] @=> scene;
+ __channelToScene[msg.data1 & 0x0F] => scene;
if (scene == "") {
- cherr <= "Unknown channel "
+ cherr <= "Unknown MIDI channel "
<= (msg.data1 & 0x0F)
<= IO.newline();
- msg.data1 & 0x0F => Std.itoa @=> scene;
+ msg.data1 & 0x0F => Std.itoa => scene;
}
msg.data1 & 0xF0 => int cmd;
msg.data2 => CCId;
if (__controlToName[scene] != null)
- __controlToName[scene][CCId] @=> control;
+ __controlToName[scene][CCId] => control;
else
- "" @=> control;
+ "" => control;
if (control == "") {
- cherr <= "Unknown controller " <= CCId
+ cherr <= "Unknown MIDI controller " <= CCId
<= IO.newline();
- CCId => Std.itoa @=> control;
+ CCId => Std.itoa => control;
}
+ /* normalize value [0, 127] to [0, 1] */
msg.data3/127.0 => value;
if (cmd == 0xB0 &&
@@ -116,7 +78,7 @@ public class NanoEvent extends GenEvent {
NanoEvent obj;
device => obj.device;
- scene @=> obj.wantScene;
+ scene => obj.wantScene;
return obj;
}
@@ -125,7 +87,7 @@ public class NanoEvent extends GenEvent {
{
NanoEvent obj;
- scene @=> obj.wantScene;
+ scene => obj.wantScene;
return obj;
}
@@ -140,7 +102,7 @@ public class NanoEvent extends GenEvent {
cherr <= "Warning: Already registered scene name \""
<= name <= "\"" <= IO.newline();
- name @=> __channelToScene[channel];
+ name => __channelToScene[channel];
new string[0x100] @=> __controlToName[name];
}
@@ -152,7 +114,7 @@ public class NanoEvent extends GenEvent {
<= " on scene \"" <= sceneName <= "\""
<= IO.newline();
- controlName @=> __controlToName[sceneName][id];
+ controlName => __controlToName[sceneName][id];
}
}
/* static initialization */
diff --git a/lib/RumbleEvent.ck b/lib/RumbleEvent.ck
index 0992bc0..91aed80 100644
--- a/lib/RumbleEvent.ck
+++ b/lib/RumbleEvent.ck
@@ -5,63 +5,13 @@ public class RumbleEvent extends GenEvent {
static string @__axisToName[]; /* pseudo-private */
static string @__buttonToName[]; /* pseudo-private */
- class Port extends Step {
- fun void
- fetch(int device, string wantControl)
- {
- Hid hid;
-
- if (!hid.openJoystick(device)) {
- cherr <= "Cannot open joystick device " <= device
- <= IO.newline();
- me.exit();
- }
- chout <= "Opened joystick device " <= device <= " (" <= hid.name() <= ")"
- <= IO.newline();
-
- while (hid => now) {
- while (HidMsg msg => hid.recv) {
- string control;
- float value;
-
- if (msg.isAxisMotion()) {
- RumbleEvent.__axisToName[msg.which] => control;
- msg.axisPosition => value;
- } else if (msg.isButtonDown()) {
- RumbleEvent.__buttonToName[msg.which] => control;
- 1 => value;
- } else if (msg.isButtonUp()) {
- RumbleEvent.__buttonToName[msg.which] => control;
- -1 => value;
- }
-
- if (control == wantControl)
- value => next;
- }
- }
- /* never reached */
- }
- }
-
- /* should be "Generic X-Box pad" */
+ /*
+ * Should be "Generic X-Box pad"
+ * Can be changed by constructor since __hid_loop() starts only
+ * when constructing shred passes time
+ */
0 => int device;
- string control;
-
- fun int
- isControl(string c)
- {
- return control == c;
- }
-
- fun Step @
- getPort(string control)
- {
- Port p;
- spork ~ p.fetch(device, control);
- return p;
- }
-
fun void
__hid_loop() /* pseudo-private */
{
@@ -72,24 +22,27 @@ public class RumbleEvent extends GenEvent {
<= IO.newline();
me.exit();
}
- chout <= "Opened joystick device " <= device <= " (" <= hid.name() <= ")"
- <= IO.newline();
+ chout <= "Opened joystick device " <= hid.num()
+ <= " (" <= hid.name() <= ")" <= IO.newline();
while (hid => now) {
while (HidMsg msg => hid.recv) {
if (msg.isAxisMotion()) {
__axisToName[msg.which] => control;
+ /* normalize value [-1, 1] to [0, 1] */
(msg.axisPosition+1)/2 => value;
- } else if (msg.isButtonDown()) {
- __buttonToName[msg.which] => control;
- true => value;
- } else if (msg.isButtonUp()) {
+ } else if (msg.isButtonDown() || msg.isButtonUp()) {
__buttonToName[msg.which] => control;
- false => value;
+ msg.isButtonDown() => value;
}
- if (control != "")
+ if (control == "") {
+ cherr <= "Unknown joystick controller " <= msg.which
+ <= " (isAxisMotion=" <= msg.isAxisMotion() <= ")"
+ <= IO.newline();
+ } else {
broadcast();
+ }
}
}
/* never reached */
@@ -100,6 +53,8 @@ public class RumbleEvent extends GenEvent {
["leftJoystickX", "leftJoystickY", "leftButton",
"rightJoystickX", "rightJoystickY", "rightButton",
"cursorX", "cursorY"] @=> RumbleEvent.__axisToName;
+
["buttonA", "buttonB", "buttonX", "buttonY",
- "buttonLB", "buttonRB",
- "buttonStart", "buttonBack"] @=> RumbleEvent.__buttonToName;
+ "buttonLB", "buttonRB", "buttonStart",
+ "", "", "", /* 7 to 9 are unused */
+ "buttonBack"] @=> RumbleEvent.__buttonToName;