Working, updating occasionally

This commit is contained in:
Neale Pickett 2023-12-31 16:44:08 -07:00
parent b0a2a94923
commit deec112aee
1 changed files with 44 additions and 23 deletions

View File

@ -9,6 +9,7 @@
#include "standard.h" // Standard pins
InstrumentButtonState buttonState;
InstrumentButtonState lastState;
void setup() {
pinMode(STRUM_DOWN, INPUT_PULLUP);
@ -47,39 +48,59 @@ void setup() {
}
void loop() {
static unsigned long next = 0;
unsigned long now = millis();
static uint16_t buttons = 0;
static uint16_t last_buttons = 0;
// 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
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
);
if (buttons == last_buttons) {
return;
}
last_buttons = buttons;
// 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);
// open switch will read 1, so invert everything
buttons = ~buttons;
buttonState.buttons = (buttons & 0b1100111111); // All directly-mappable bits
buttonState.buttons |= ((buttons >> 10) & 0b11111); // Solo keys
bitWrite(buttonState.buttons, 6, (buttons >> 10) & 0b11111); // Solo bit
// Poll direction
// I don't understand why any rational engineer would have done this.
if (!digitalRead(STRUM_UP)) {
if (bitRead(buttons, 6)) {
buttonState.hatAndConstant = 0x00; // up
} else if (false) {
buttonState.hatAndConstant = 0x02; // right
} else if (!digitalRead(STRUM_DOWN)) {
} else if (bitRead(buttons, 7)) {
buttonState.hatAndConstant = 0x04; // down
} else if (false) {
buttonState.hatAndConstant = 0x06; // left
} else {
buttonState.hatAndConstant = 0x08; // nothing
}
// I'm not implementing this for the time being
//buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4;
// Send an update
HID().SendReport(0, (uint8_t *)&buttonState, 27);
// don't report more frequently than 1kHz
// this doubles as debouncing logic
delay(10);
}