mockband/MockBand.ino

190 lines
5.4 KiB
Arduino
Raw Normal View History

2023-04-22 16:15:03 -06:00
#include <stdint.h>
#include <Arduino.h>
2023-12-31 16:19:10 -07:00
#include <PluggableUSB.h>
2023-04-22 16:15:03 -06:00
2023-12-31 20:33:29 -07:00
#include "hid.hh" // Modified HID library: doesn't prefix each packet with ID
#include "instrument.hh"
#include "standard.hh" // Standard pins
// If defined, we will check the wammy bar input
//#define WAMMY
2024-01-01 12:19:50 -07:00
// Maximum time between wammy bar updates.
2023-12-31 20:39:45 -07:00
#define UPDATE_INTERVAL_MS 20
2023-04-22 16:15:03 -06:00
2024-01-01 12:19:50 -07:00
// 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)
2024-01-01 12:42:07 -07:00
#if USB_VID != 0x1bad
2024-01-04 13:10:17 -07:00
#error USB_VID must be set to 0x1bad: see INSTALL.md
2024-01-01 12:42:07 -07:00
#endif
#if USB_PID == 0x0003
#define DRUM // XBox
#elif USB_PID == 0x0004
2024-01-01 12:42:07 -07:00
#define GUITAR
#elif USB_PID == 0x0005
2024-01-04 13:10:17 -07:00
#define DRUM // Wii RB1
#elif USB_PID == 0x3110
#define DRUM // Wii RB2
2024-01-01 12:42:07 -07:00
#else
2024-01-04 13:10:17 -07:00
#error USB_PID not recognized: see INSTALL.md
2024-01-01 12:42:07 -07:00
#endif
2024-01-01 12:19:50 -07:00
2024-01-04 13:10:17 -07:00
InstrumentButtonState buttonState = {0};
2023-04-22 16:15:03 -06:00
void setup() {
pinMode(STRUM_DOWN, INPUT_PULLUP);
pinMode(STRUM_UP, INPUT_PULLUP);
pinMode(TILT_SWITCH, INPUT_PULLUP);
pinMode(BUTTON_GREEN, INPUT_PULLUP);
pinMode(BUTTON_RED, INPUT_PULLUP);
pinMode(BUTTON_YELLOW, INPUT_PULLUP);
pinMode(BUTTON_BLUE, INPUT_PULLUP);
pinMode(BUTTON_ORANGE, INPUT_PULLUP);
2023-12-31 16:19:10 -07:00
pinMode(SOLO_GREEN, INPUT_PULLUP);
pinMode(SOLO_RED, INPUT_PULLUP);
pinMode(SOLO_YELLOW, INPUT_PULLUP);
pinMode(SOLO_BLUE, INPUT_PULLUP);
pinMode(SOLO_ORANGE, INPUT_PULLUP);
pinMode(BUTTON_PLUS, INPUT_PULLUP);
pinMode(BUTTON_MINUS, INPUT_PULLUP);
2024-01-04 13:10:17 -07:00
pinMode(ANALOG_WAMMY, INPUT);
pinMode(ANALOG_DPAD, INPUT);
2023-12-31 16:19:10 -07:00
// Initialize HID
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID().AppendDescriptor(&node);
2023-04-22 16:15:03 -06:00
buttonState.finalConstant = 0x0200020002000200;
}
2024-01-01 12:19:50 -07:00
// 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))
2023-12-31 17:04:49 -07:00
// The 3.3v Pro Micro is on the slow side.
// Our strategy is to poll button state as quickly as possible,
// and hope we don't miss anything while we're doing USB stuff.
2023-04-22 16:15:03 -06:00
void loop() {
2024-01-01 12:19:50 -07:00
uint16_t buttons = 0;
uint16_t samples = 0;
2023-12-31 20:33:29 -07:00
unsigned long next = 0;
2024-01-01 12:19:50 -07:00
uint16_t silence[npins] = {0};
2023-12-31 16:19:10 -07:00
2023-12-31 17:22:47 -07:00
while (1) {
2024-01-01 12:19:50 -07:00
uint16_t edge = 0;
2023-12-31 20:33:29 -07:00
2024-01-01 12:19:50 -07:00
samples++;
2023-12-31 16:44:08 -07:00
2024-01-01 12:19:50 -07:00
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;
}
2023-12-31 17:22:47 -07:00
}
2024-01-01 12:19:50 -07:00
buttons ^= edge;
2024-01-01 12:19:50 -07:00
// We've sampled everything. Is it time to do calculations and USB?
2024-01-04 13:10:17 -07:00
#ifdef WAMMY
unsigned long now = millis();
2024-01-01 12:19:50 -07:00
if (!edge && (next > now)) {
continue;
}
next = now + UPDATE_INTERVAL_MS;
2024-01-04 13:10:17 -07:00
buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4; // Wammy bar
#else
if (!edge) {
continue;
}
#endif
//
// Calculate and send an HID update
//
2024-01-07 15:55:26 -07:00
uint16_t vbuttons = buttons; // We're going to mess with the button state
2024-01-01 12:19:50 -07:00
2024-01-07 15:55:26 -07:00
buttonState.buttons = (vbuttons & 0b1100111111); // +-..!OYRGB
#ifdef GUITAR
2024-01-07 15:55:26 -07:00
buttonState.buttons |= (vbuttons >> 10) & 0b11111; // Solo keys
2024-01-07 21:37:18 -07:00
bitWrite(buttonState.buttons, 6, (vbuttons >> 10) & 0b11111); // Solo modifier
2024-01-07 15:55:26 -07:00
if (bitRead(vbuttons, 6)) {
buttonState.hatAndConstant = 0; // up
2024-01-07 15:55:26 -07:00
} else if bitRead(vbuttons, 7) {
buttonState.hatAndConstant = 4; // down
} else {
buttonState.hatAndConstant = 8; // nothing
}
#else // DRUMS
2024-01-07 15:55:26 -07:00
// Hi hat pedal (SOLO_RED) makes yellow cymbal strike a blue cymbal strike
if (bitRead(vbuttons, 12) && bitRead(vbuttons, 13)) {
bitClear(vbuttons, 13);
bitSet(vbuttons, 10);
}
2024-01-07 15:55:26 -07:00
buttonState.buttons |= (vbuttons >> 10) & 0b01011; // Cymbals
bitWrite(buttonState.buttons, 10, (vbuttons >> 0) & 0b01111); // Drum pad modifier
bitWrite(buttonState.buttons, 11, (vbuttons >> 10) & 0b01011); // Cymbals modifier
// rbdrum2midi wants these set: it ignores the button states.
2024-01-04 13:10:17 -07:00
buttonState.velocity[0] = bitRead(buttonState.buttons, 3)?127:0; // Y
buttonState.velocity[1] = bitRead(buttonState.buttons, 2)?127:0; // R
buttonState.velocity[2] = bitRead(buttonState.buttons, 1)?127:0; // G
buttonState.velocity[3] = bitRead(buttonState.buttons, 0)?127:0; // B
Squashed commit of the following: commit 13825855d3baeb456fa9577b224625d0c0e2f3b3 Author: Neale Pickett <neale@woozle.org> Date: Sat Jan 6 15:08:12 2024 -0700 twiddling things trying to get drums working on CH commit e54595e6302f793811fbf9819f8580f2a9cf761f Merge: 63bd067 703b070 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 18:18:18 2024 -0700 Merge branch 'main' into builder commit 63bd0672500631b8c47f24f041693e642ab32533 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 09:29:16 2024 -0700 More idiomatic (to me) Makefile commit 9f0c0711a85ca1ce9c2b5a285c779148a63acb55 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 09:10:28 2024 -0700 fancypants flash target commit efc67e9fe551475b4beb0757606dacb494cb92df Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 09:06:59 2024 -0700 Also publish zip file commit ce7d6107cf79553c1a7d325132f9a9ca30b2d478 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 09:04:51 2024 -0700 stop using removed `make publish` commit 48d245051454bc353f963cddbbb85359986ab3b3 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 09:02:53 2024 -0700 make dist commit 5f448321518a8a950330cab88277318455e5da32 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 08:50:10 2024 -0700 move to Makefile commit da45584955a4a5ae13e7aebcac4570c95a312243 Author: Neale Pickett <neale@woozle.org> Date: Fri Jan 5 08:37:03 2024 -0700 Makefile commit 50ce7c245ac89336ea340ccb3152239176ee9c13 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 23:16:08 2024 -0700 Fix quoting commit f767eb6e23f6153f0f09e02b9e3d984a79a9b949 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 23:15:05 2024 -0700 Build log formatting commit 7495bfd20fe5676c00becf80109792a0447fa664 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 23:12:51 2024 -0700 Try preserving package versions commit 5fa607d6b093cfafbf4b9b52e4686acd3e3625f6 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:59:14 2024 -0700 build and code cleanup commit 855ad3ba60c9a527dbf4fb40958f680a0993a5a6 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:48:20 2024 -0700 fix: change quoting? commit 25a917dc81313d1ff9f144d8029a5c70fc4132ae Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:42:11 2024 -0700 directly specify gcc flags commit a8d4918e05327049232a994cd7ba607cc778c2f2 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:23:03 2024 -0700 variables expand again commit 544ffa8ced48c6bee445bd6ec73e48662168e3e9 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:21:25 2024 -0700 quote stuff commit a3d8262e017c352e4bf1120fc7b30e05775260e1 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:19:58 2024 -0700 fix build on non-default branch commit 0ba9c0957a44cc1d01b51263607f366cc81fd323 Author: Neale Pickett <neale@woozle.org> Date: Thu Jan 4 22:16:36 2024 -0700 Clever vid/pid settings?
2024-01-06 15:13:19 -07:00
// Clone Hero 1.0.0.4080-final needs blue and yellow cymbals to send up and down on d-pad.
// This is what the mysterous CymExt1 and CymExt2 mappings mean.
// If these aren't set, all pads (except red) register as simultaneous drum and cymbal hits.
2024-01-07 15:55:26 -07:00
if (bitRead(vbuttons, 13)) {
buttonState.hatAndConstant = 0; // up
2024-01-07 15:55:26 -07:00
} else if (bitRead(vbuttons, 10)) {
buttonState.hatAndConstant = 4; // down
} else {
buttonState.hatAndConstant = 8; // nothing
}
#endif
2024-01-04 13:10:17 -07:00
#ifdef DPAD
#error DPAD isn't implemented yet
#endif
2024-01-07 21:30:19 -07:00
#ifdef DEBUGY0
// Log sample rate to the first Y axis
buttonState.axis[1] = samples & 0xff;
#endif
2024-01-01 12:19:50 -07:00
2023-12-31 17:22:47 -07:00
// Send an update
HID().SendReport(0, (uint8_t *)&buttonState, 27);
2023-12-31 20:33:29 -07:00
2024-01-01 12:19:50 -07:00
samples = 0;
2023-12-31 17:22:47 -07:00
}
2023-04-22 16:15:03 -06:00
}