From 0960b0e2f73f4d66981af9a4d9e21874c1260303 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 31 Dec 2023 17:04:49 -0700 Subject: [PATCH] Another attempt --- WiiGuitarController.ino | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/WiiGuitarController.ino b/WiiGuitarController.ino index e7c6a39..5870d70 100644 --- a/WiiGuitarController.ino +++ b/WiiGuitarController.ino @@ -47,11 +47,16 @@ void setup() { buttonState.hatAndConstant = 0x08; // This apparently means "nothing pressed" } +// 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() { static uint16_t buttons = 0; - static uint16_t last_buttons = 0; + static unsigned long next = 0; + unsigned long now = millis(); - buttons = (0 + buttons |= (0 | (digitalRead(BUTTON_BLUE) << 0) | (digitalRead(BUTTON_GREEN) << 1) | (digitalRead(BUTTON_RED) << 2) @@ -69,12 +74,11 @@ void loop() { | (digitalRead(SOLO_ORANGE) << 14) // Not in USB packet ); - if (buttons == last_buttons) { + if (next > now) { return; } - last_buttons = buttons; - // open switch will read 1, so invert everything + // Open pullup reads 1, so invert everything we've read buttons = ~buttons; buttonState.buttons = (buttons & 0b1100111111); // All directly-mappable bits @@ -94,13 +98,11 @@ void loop() { buttonState.hatAndConstant = 0x08; // nothing } - // I'm not implementing this for the time being - //buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4; + buttonState.axis[2] = analogRead(ANALOG_WAMMY) / 4; // Send an update HID().SendReport(0, (uint8_t *)&buttonState, 27); - - // don't report more frequently than 100Hz - // this doubles as debouncing logic - delay(10); + + next = now + 10; + buttons = 0; }