Skip to content

Instantly share code, notes, and snippets.

@tancredi
Created December 25, 2021 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tancredi/4b2aa3eb9113af742ea879e8d84f8f3c to your computer and use it in GitHub Desktop.
Save tancredi/4b2aa3eb9113af742ea879e8d84f8f3c to your computer and use it in GitHub Desktop.
Custom joystick as sampler controls - Mixxx HID controller mappings
const GC = {
stopOnRelease: false,
};
const MAP = {
TOP_L: [6, 1], // 0, 1
TOP_R: [6, 2], // 1, 2
TRIGGER_L: [6, 4], // 2, 4
TRIGGER_R: [6, 8], // 3, 8
ARR_RIGHT: [2, 1], // 0, 1
ARR_LEFT: [2, 128], // 7, 128
ARR_DOWN: [3, 1], // 0, 1
ARR_UP: [3, 128], // 7, 128
STICK_R_UP: [5, 16], // 4, 16
STICK_R_RIGHT: [5, 32], // 5, 32
STICK_R_DOWN: [5, 64], // 6, 64
STICK_R_LEFT: [5, 128], // 7, 128
SELECT: [6, 16], // 4, 16
START: [6, 32], //5, 32
STICK_L_PRESS: [6, 64], // 6, 64
STICK_R_PRESS: [6, 128], // 7, 128
STICK_L_UP: [7, 1], // 0, 1
STICK_L_RIGHT: [7, 2], // 1, 2
STICK_L_DOWN: [7, 4], // 2, 4
STICK_L_LEFT: [7, 8], // 3, 8
};
GC.init = function (id) {
GC.controller = new HIDController();
GC.id = id;
GC.registerInputPackets();
GC.registerOutputPackets();
GC.registerCallbacks();
};
GC.shutdown = function () {
GC.controller.close();
};
GC.registerOutputPackets = function () {
const packet = new HIDPacket("out");
GC.controller.registerOutputPacket(packet);
};
GC.registerInputPackets = function () {
const packet = new HIDPacket("control");
for (var key in MAP) {
packet.addControl("hid", key, MAP[key][0], "B", MAP[key][1]);
}
GC.controller.registerInputPacket(packet);
};
GC.incomingData = function (data, length) {
GC.controller.parsePacket(data, length);
};
GC.registerCallbacks = function (id) {
const controller = GC.controller;
for (var key in MAP) {
if (GC.handlers[key]) {
controller.setCallback("control", "hid", key, GC.handlers[key]);
}
}
};
GC.getSamplerGroup = function (i) {
return "[Sampler" + i + "]";
};
GC.resetSampler = function (i) {
const group = GC.getSamplerGroup(i);
engine.setValue(group, "play", false);
engine.setValue(group, "playposition", 0);
};
GC.toggleSamplerPFL = function (i, status) {
const group = GC.getSamplerGroup(i);
engine.setValue(group, "pfl", status);
engine.setValue(group, "volume", status ? 0 : 1.4);
};
GC.toggleSamplersPFL = function (status) {
GC.forEachSampler(function (i) {
GC.toggleSamplerPFL(i, status);
});
};
GC.forEachSampler = function (fn) {
const count = engine.getValue("[Master]", "num_samplers");
for (var i = 0; i < count; i++) {
fn(i + 1);
}
};
GC.samplerCallback = function (i, invert) {
const group = GC.getSamplerGroup(i);
return function (e) {
const pressed = invert ? !e.value : e.value;
print(pressed ? "Press" : "Released");
if (pressed) {
engine.setValue(group, "playposition", 0);
engine.setValue(group, "play", true);
} else if (GC.stopOnRelease) {
engine.setValue(group, "play", false);
} else {
}
};
};
GC.handlers = {
TRIGGER_L: GC.samplerCallback(1),
TRIGGER_R: GC.samplerCallback(2),
TOP_L: GC.samplerCallback(3),
TOP_R: GC.samplerCallback(4),
ARR_UP: GC.samplerCallback(5, true),
STICK_R_UP: GC.samplerCallback(6),
ARR_LEFT: GC.samplerCallback(7, true),
ARR_RIGHT: GC.samplerCallback(8, true),
STICK_R_LEFT: GC.samplerCallback(9),
STICK_R_RIGHT: GC.samplerCallback(10),
ARR_DOWN: GC.samplerCallback(11, true),
STICK_R_DOWN: GC.samplerCallback(12),
SELECT: function (e) {
// Pressed
if (e.value) {
GC.stopOnRelease = !GC.stopOnRelease;
}
},
START: function (e) {
// Pressed
if (e.value) {
GC.forEachSampler(GC.resetSampler);
}
},
STICK_L_PRESS: function (e) {
if (e.value) {
// Pressed
GC.toggleSamplersPFL(false);
}
},
STICK_R_PRESS: function (e) {
if (e.value) {
// Pressed
GC.toggleSamplersPFL(true);
}
},
};
<?xml version='1.0' encoding='utf-8'?>
<MixxxControllerPreset schemaVersion="1" mixxxVersion="1.11+">
<info>
<name>Game controller (Samplers control)</name>
<author>Tancredi Trugenberger</author>
<description>Game controller HID mapping for samplers control</description>
</info>
<controller id="Griffin">
<scriptfiles>
<file filename="common-hid-packet-parser.js" functionprefix=""/>
<file filename="game-controller-sampler.hid.js" functionprefix="GC"/>
</scriptfiles>
</controller>
</MixxxControllerPreset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment