Finally happy with drums!
This commit is contained in:
parent
6b1b26fc0b
commit
94287ddcbd
82
MockBand.ino
82
MockBand.ino
|
@ -8,12 +8,20 @@
|
|||
//#include "blue.hh" // Ginnie's blue guitar
|
||||
#include "standard.hh" // Standard pins
|
||||
|
||||
// How often do you want to send an update?
|
||||
// Smaller number: lower latency, but more bounces
|
||||
// Bigger number: higher latency
|
||||
// 20ms feels pretty good to me
|
||||
// Maximum time between wammy bar updates.
|
||||
#define UPDATE_INTERVAL_MS 20
|
||||
|
||||
// After an edge on a pin, stop listening for this long, to debounce it
|
||||
#define SILENCE_INTERVAL_MS 40
|
||||
|
||||
// Your measured samples per frame, more or less
|
||||
#define SAMPLES_PER_FRAME 127
|
||||
|
||||
// Some arithmetic for the compiler, to make the code fast
|
||||
#define SAMPLES_PER_MS (SAMPLES_PER_FRAME / UPDATE_INTERVAL_MS)
|
||||
#define SILENCE_SAMPLES (SAMPLES_PER_MS * SILENCE_INTERVAL_MS)
|
||||
|
||||
|
||||
InstrumentButtonState buttonState;
|
||||
|
||||
void setup() {
|
||||
|
@ -48,40 +56,57 @@ void setup() {
|
|||
buttonState.finalConstant = 0x0200020002000200;
|
||||
}
|
||||
|
||||
// Order of pins in sample
|
||||
uint8_t pins[] = {
|
||||
BUTTON_BLUE,
|
||||
BUTTON_GREEN,
|
||||
BUTTON_RED,
|
||||
BUTTON_YELLOW,
|
||||
BUTTON_ORANGE,
|
||||
TILT_SWITCH,
|
||||
STRUM_UP, // Not in USB packet
|
||||
STRUM_DOWN, // Not in USB packet
|
||||
BUTTON_MINUS,
|
||||
BUTTON_PLUS,
|
||||
SOLO_BLUE, // Not in USB packet
|
||||
SOLO_GREEN, // Not in USB packet
|
||||
SOLO_RED, // Not in USB packet
|
||||
SOLO_YELLOW, // Not in USB packet
|
||||
SOLO_ORANGE, // Not in USB packet
|
||||
};
|
||||
#define npins (sizeof(pins) / sizeof(*pins))
|
||||
|
||||
// The 3.3v Pro Micro is on the slow side.
|
||||
// Our strategy is to poll button state as quickly as possible,
|
||||
// send an update every 10ms,
|
||||
// and hope we don't miss anything while we're doing USB stuff.
|
||||
void loop() {
|
||||
register uint16_t buttons = 0;
|
||||
uint16_t buttons = 0;
|
||||
uint16_t samples = 0;
|
||||
unsigned long next = 0;
|
||||
uint16_t silence[npins] = {0};
|
||||
|
||||
while (1) {
|
||||
unsigned long now = millis();
|
||||
uint16_t edge = 0;
|
||||
|
||||
buttons |= ~(0
|
||||
| (digitalRead(BUTTON_BLUE) << 0)
|
||||
| (digitalRead(BUTTON_GREEN) << 1)
|
||||
| (digitalRead(BUTTON_RED) << 2)
|
||||
| (digitalRead(BUTTON_YELLOW) << 3)
|
||||
| (digitalRead(BUTTON_ORANGE) << 4)
|
||||
| (digitalRead(TILT_SWITCH) << 5)
|
||||
| (digitalRead(STRUM_UP) << 6) // Not in USB packet
|
||||
| (digitalRead(STRUM_DOWN) << 7) // Not in USB packet
|
||||
| (digitalRead(BUTTON_MINUS) << 8)
|
||||
| (digitalRead(BUTTON_PLUS) << 9)
|
||||
| (digitalRead(SOLO_BLUE) << 10) // Not in USB packet
|
||||
| (digitalRead(SOLO_GREEN) << 11) // Not in USB packet
|
||||
| (digitalRead(SOLO_RED) << 12) // Not in USB packet
|
||||
| (digitalRead(SOLO_YELLOW) << 13) // Not in USB packet
|
||||
| (digitalRead(SOLO_ORANGE) << 14) // Not in USB packet
|
||||
);
|
||||
samples++;
|
||||
|
||||
if (now < next) {
|
||||
for (uint8_t i = 0; i < npins; i++) {
|
||||
if (silence[i]) {
|
||||
silence[i]--;
|
||||
} else if (bitRead(buttons, i) != !digitalRead(pins[i])) {
|
||||
edge |= bit(i);
|
||||
silence[i] = SILENCE_SAMPLES;
|
||||
}
|
||||
}
|
||||
buttons ^= edge;
|
||||
|
||||
// We've sampled everything. Is it time to do calculations and USB?
|
||||
if (!edge && (next > now)) {
|
||||
continue;
|
||||
}
|
||||
next = now + 20;
|
||||
|
||||
next = now + UPDATE_INTERVAL_MS;
|
||||
|
||||
buttonState.buttons = (buttons & 0b1100111111); // All directly-mappable bits
|
||||
buttonState.buttons |= ((buttons >> 10) & 0b11111); // Solo keys
|
||||
bitWrite(buttonState.buttons, 6, (buttons >> 10) & 0b11111); // Solo bit
|
||||
|
@ -96,9 +121,12 @@ void loop() {
|
|||
|
||||
buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4;
|
||||
|
||||
// The second Y axis doesn't appear to be used, so I'm logging sample rate with it
|
||||
buttonState.axis[3] = samples & 0xff; // most things map this to [-1,1]
|
||||
|
||||
// Send an update
|
||||
HID().SendReport(0, (uint8_t *)&buttonState, 27);
|
||||
|
||||
buttons = 0;
|
||||
samples = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
I did some performance testing.
|
||||
|
||||
The Pro Micro runs an Atmel 32u4 at 8MHz.
|
||||
If the code does nothing else,
|
||||
I was able to poll all pins around 13 times every 1ms,
|
||||
or 13kHz.
|
||||
|
||||
|
||||
Sample Rate
|
||||
-----------
|
||||
|
||||
The number of samples taken since the last HID report
|
||||
is sent as the second Y axis.
|
||||
If you use the
|
||||
[included gamepad tester](gamepad.html),
|
||||
it will show the approximate number of samples as an integer,
|
||||
for Y axis 2.
|
||||
|
||||
This is approximate,
|
||||
because the browser encodes the value as a real number between -1 and 1.
|
||||
We convert it back, but may lose a little precision.
|
||||
It's close enough for me,
|
||||
hopefully it's close enough for you.
|
||||
|
||||
|
||||
Debouncing
|
||||
----------
|
||||
|
||||
Using `millis()` time to debounce the switch
|
||||
roughly halved my sample frequency.
|
||||
So instead, I do some preprocessor arithmetic
|
||||
to calculate how many samples to take after an edge,
|
||||
in order to debounce switches.
|
||||
|
||||
The drum controller was a partcular pain:
|
||||
in addition to the switch bouncing,
|
||||
the stick was bouncing on the rubber pad.
|
||||
I settled on a 40ms silence window as feeling pretty good.
|
||||
You can adjust this if you want to.
|
|
@ -0,0 +1,264 @@
|
|||
<!--
|
||||
/*
|
||||
* Copyright 2019 Gregg Tavares
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
-->
|
||||
<style>
|
||||
body {
|
||||
background: #444;
|
||||
color: white;
|
||||
font-family: monospace;
|
||||
}
|
||||
#gamepads>* {
|
||||
background: #333;
|
||||
padding: 1em;
|
||||
margin: 10px 5px 0 0;
|
||||
}
|
||||
#gamepads pre {
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.head {
|
||||
display: flex;
|
||||
}
|
||||
.head .id {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
.head .index,
|
||||
.head .id {
|
||||
display: inline-block;
|
||||
background: #222;
|
||||
padding: 0.5em;
|
||||
}
|
||||
.head .index {
|
||||
}
|
||||
|
||||
.info .label {
|
||||
width: 7em;
|
||||
display: inline-block;
|
||||
}
|
||||
.info>div {
|
||||
padding: 0.25em;
|
||||
background: #222;
|
||||
margin: 0.25em 0.25em 0 0;
|
||||
}
|
||||
|
||||
.inputs {
|
||||
display: flex;
|
||||
}
|
||||
.axes {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.svg text {
|
||||
color: #CCC;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.axes svg text {
|
||||
font-size: 0.6px;
|
||||
}
|
||||
.buttons svg text {
|
||||
font-size: 1.2px;
|
||||
}
|
||||
.axes>div, .buttons>div {
|
||||
display: inline-block;
|
||||
background: #222;
|
||||
}
|
||||
.axes>div {
|
||||
margin: 2px 5px 0 0;
|
||||
}
|
||||
.buttons>div {
|
||||
margin: 2px 2px 0 0;
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
|
||||
<h1>HTML5 Gamepad Test</h1>
|
||||
<div>running: <span id="running"></span></div>
|
||||
<div id="gamepads"></div>
|
||||
</body>
|
||||
<script>
|
||||
|
||||
const fudgeFactor = 2; // because of bug in Chrome related to svg text alignment font sizes can not be < 1
|
||||
const runningElem = document.querySelector('#running');
|
||||
const gamepadsElem = document.querySelector('#gamepads');
|
||||
const gamepadsByIndex = {};
|
||||
|
||||
const controllerTemplate = `
|
||||
<div>
|
||||
<div class="head"><div class="index"></div><div class="id"></div></div>
|
||||
<div class="info"><div class="label">connected:</div><span class="connected"></span></div>
|
||||
<div class="info"><div class="label">mapping:</div><span class="mapping"></span></div>
|
||||
<div class="inputs">
|
||||
<div class="axes"></div>
|
||||
<div class="buttons"></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
const axisTemplate = `
|
||||
<svg viewBox="-2.2 -2.2 4.4 4.4" width="128" height="128">
|
||||
<circle cx="0" cy="0" r="2" fill="none" stroke="#888" stroke-width="0.04" />
|
||||
<path d="M0,-2L0,2M-2,0L2,0" stroke="#888" stroke-width="0.04" />
|
||||
<circle cx="0" cy="0" r="0.22" fill="red" class="axis" />
|
||||
<text text-anchor="middle" fill="#CCC" x="0" y="2">0</text>
|
||||
</svg>
|
||||
`
|
||||
|
||||
const buttonTemplate = `
|
||||
<svg viewBox="-2.2 -2.2 4.4 4.4" width="64" height="64">
|
||||
<circle cx="0" cy="0" r="2" fill="none" stroke="#888" stroke-width="0.1" />
|
||||
<circle cx="0" cy="0" r="0" fill="none" fill="red" class="button" />
|
||||
<text class="value" dominant-baseline="middle" text-anchor="middle" fill="#CCC" x="0" y="0">0.00</text>
|
||||
<text class="index" alignment-baseline="hanging" dominant-baseline="hanging" text-anchor="start" fill="#CCC" x="-2" y="-2">0</text>
|
||||
</svg>
|
||||
`;
|
||||
|
||||
function addGamepad(gamepad) {
|
||||
console.log('add:', gamepad.index);
|
||||
const elem = document.createElement('div');
|
||||
elem.innerHTML = controllerTemplate;
|
||||
|
||||
const axesElem = elem.querySelector('.axes');
|
||||
const buttonsElem = elem.querySelector('.buttons');
|
||||
|
||||
const axes = [];
|
||||
for (let ndx = 0; ndx < gamepad.axes.length; ndx += 2) {
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = axisTemplate;
|
||||
axesElem.appendChild(div);
|
||||
axes.push({
|
||||
axis: div.querySelector('.axis'),
|
||||
value: div.querySelector('text'),
|
||||
});
|
||||
}
|
||||
|
||||
const buttons = [];
|
||||
for (let ndx = 0; ndx < gamepad.buttons.length; ++ndx) {
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = buttonTemplate;
|
||||
buttonsElem.appendChild(div);
|
||||
div.querySelector('.index').textContent = ndx;
|
||||
buttons.push({
|
||||
circle: div.querySelector('.button'),
|
||||
value: div.querySelector('.value'),
|
||||
});
|
||||
}
|
||||
|
||||
gamepadsByIndex[gamepad.index] = {
|
||||
gamepad,
|
||||
elem,
|
||||
axes,
|
||||
buttons,
|
||||
index: elem.querySelector('.index'),
|
||||
id: elem.querySelector('.id'),
|
||||
mapping: elem.querySelector('.mapping'),
|
||||
connected: elem.querySelector('.connected'),
|
||||
};
|
||||
gamepadsElem.appendChild(elem);
|
||||
}
|
||||
|
||||
function removeGamepad(gamepad) {
|
||||
const info = gamepadsByIndex[gamepad.index];
|
||||
if (info) {
|
||||
delete gamepadsByIndex[gamepad.index];
|
||||
info.elem.parentElement.removeChild(info.elem);
|
||||
}
|
||||
}
|
||||
|
||||
function addGamepadIfNew(gamepad) {
|
||||
const info = gamepadsByIndex[gamepad.index];
|
||||
if (!info) {
|
||||
addGamepad(gamepad);
|
||||
} else {
|
||||
// This broke sometime in the past. It used to be
|
||||
// the same gamepad object was returned forever.
|
||||
// Then Chrome only changed to a new gamepad object
|
||||
// is returned every frame.
|
||||
info.gamepad = gamepad;
|
||||
}
|
||||
}
|
||||
|
||||
function handleConnect(e) {
|
||||
console.log('connect');
|
||||
addGamepadIfNew(e.gamepad);
|
||||
}
|
||||
|
||||
function handleDisconnect(e) {
|
||||
console.log('disconnect');
|
||||
removeGamepad(e.gamepad);
|
||||
}
|
||||
|
||||
const t = String.fromCharCode(0x26AA);
|
||||
const f = String.fromCharCode(0x26AB);
|
||||
function onOff(v) {
|
||||
return v ? t : f;
|
||||
}
|
||||
|
||||
const keys = ['index', 'id', 'connected', 'mapping', /*'timestamp'*/];
|
||||
function processController(info) {
|
||||
const {elem, gamepad, axes, buttons} = info;
|
||||
const lines = [`gamepad : ${gamepad.index}`];
|
||||
for (const key of keys) {
|
||||
info[key].textContent = gamepad[key];
|
||||
}
|
||||
axes.forEach(({axis, value}, ndx) => {
|
||||
const off = ndx * 2;
|
||||
axis.setAttributeNS(null, 'cx', gamepad.axes[off ] * fudgeFactor);
|
||||
axis.setAttributeNS(null, 'cy', gamepad.axes[off + 1] * fudgeFactor);
|
||||
value.textContent = `${Math.floor(127*gamepad.axes[off]+127)},${Math.floor(127*gamepad.axes[off+1]+127)}`;
|
||||
});
|
||||
buttons.forEach(({circle, value}, ndx) => {
|
||||
const button = gamepad.buttons[ndx];
|
||||
circle.setAttributeNS(null, 'r', button.value * fudgeFactor);
|
||||
circle.setAttributeNS(null, 'fill', button.pressed ? 'red' : 'gray');
|
||||
value.textContent = `${button.value.toFixed(2)}`;
|
||||
});
|
||||
|
||||
// lines.push(`axes : [${gamepad.axes.map((v, ndx) => `${ndx}: ${v.toFixed(2).padStart(5)}`).join(', ')} ]`);
|
||||
// lines.push(`buttons : [${gamepad.buttons.map((v, ndx) => `${ndx}: ${onOff(v.pressed)} ${v.value.toFixed(2)}`).join(', ')} ]`);
|
||||
// elem.textContent = lines.join('\n');
|
||||
}
|
||||
|
||||
function addNewPads() {
|
||||
const gamepads = navigator.getGamepads();
|
||||
for (let i = 0; i < gamepads.length; i++) {
|
||||
const gamepad = gamepads[i]
|
||||
if (gamepad) {
|
||||
addGamepadIfNew(gamepad);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("gamepadconnected", handleConnect);
|
||||
window.addEventListener("gamepaddisconnected", handleDisconnect);
|
||||
|
||||
function process() {
|
||||
runningElem.textContent = ((performance.now() * 0.001 * 60 | 0) % 100).toString().padStart(2, '0');
|
||||
addNewPads(); // some browsers add by polling, others by event
|
||||
|
||||
Object.values(gamepadsByIndex).forEach(processController);
|
||||
requestAnimationFrame(process);
|
||||
}
|
||||
requestAnimationFrame(process);
|
||||
|
||||
</script>
|
Loading…
Reference in New Issue