mockband/WiiGuitarController.ino

86 lines
3.1 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 16:19:10 -07:00
#include "hid.h" // Modified HID library: doesn't prefix each packet with ID
#include "instrument.h"
2023-04-22 16:15:03 -06:00
2023-12-31 16:19:10 -07:00
//#include "blue.h" // Ginnie's blue guitar
#include "standard.h" // Standard pins
2023-04-22 16:15:03 -06:00
InstrumentButtonState buttonState;
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(ANALOG_WAMMY, INPUT);
pinMode(BUTTON_PLUS, INPUT_PULLUP);
pinMode(BUTTON_MINUS, INPUT_PULLUP);
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.buttons = 0x0000;
buttonState.hatAndConstant = 0x08;
buttonState.axis[0] = 0;
buttonState.axis[1] = 0;
buttonState.axis[2] = 0;
buttonState.axis[3] = 0;
2023-12-31 16:19:10 -07:00
for (int i = 0; i < 12; i++) {
2023-04-22 16:15:03 -06:00
buttonState.reserved1[i] = 0x0;
}
buttonState.finalConstant = 0x0200020002000200;
2023-12-31 16:19:10 -07:00
buttonState.buttons = 0;
buttonState.hatAndConstant = 0x08; // This apparently means "nothing pressed"
2023-04-22 16:15:03 -06:00
}
void loop() {
2023-12-31 16:19:10 -07:00
static unsigned long next = 0;
unsigned long now = millis();
// Send a state update right away;
if (now >= next) {
buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4;
HID().SendReport(0, (uint8_t *)&buttonState, 27);
buttonState.buttons = 0; // reset
buttonState.hatAndConstant = 0x08; // reset
next = now + 10; // wait 10ms before sending another status update
2023-04-22 16:15:03 -06:00
}
2023-12-31 16:19:10 -07:00
// Poll buttons
if (!digitalRead(BUTTON_BLUE) || !digitalRead(SOLO_BLUE)) bitSet(buttonState.buttons, 0);
if (!digitalRead(BUTTON_GREEN) || !digitalRead(SOLO_GREEN)) bitSet(buttonState.buttons, 1);
if (!digitalRead(BUTTON_RED) || !digitalRead(SOLO_RED)) bitSet(buttonState.buttons, 2);
if (!digitalRead(BUTTON_YELLOW) || !digitalRead(SOLO_YELLOW)) bitSet(buttonState.buttons, 3);
if (!digitalRead(BUTTON_ORANGE) || !digitalRead(SOLO_ORANGE)) bitSet(buttonState.buttons, 4);
if (!digitalRead(TILT_SWITCH)) bitSet(buttonState.buttons, 5);
if (!digitalRead(SOLO_BLUE) || !digitalRead(SOLO_GREEN) || !digitalRead(SOLO_RED) || !digitalRead(SOLO_YELLOW) || !digitalRead(SOLO_ORANGE)) bitSet(buttonState.buttons, 6);
if (!digitalRead(BUTTON_MINUS)) bitSet(buttonState.buttons, 8);
if (!digitalRead(BUTTON_PLUS)) bitSet(buttonState.buttons, 9);
// Poll direction
// I don't understand why any rational engineer would have done this.
if (!digitalRead(STRUM_UP)) {
buttonState.hatAndConstant = 0x00; // up
} else if (false) {
buttonState.hatAndConstant = 0x02; // right
} else if (!digitalRead(STRUM_DOWN)) {
buttonState.hatAndConstant = 0x04; // down
} else if (false) {
buttonState.hatAndConstant = 0x06; // left
}
2023-04-22 16:15:03 -06:00
}