From 400f9c0dcf153ba0e107260f5a4503e58f0e8c06 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 21 Mar 2016 18:21:12 +0100 Subject: [PATCH] Toggle LED behaviour using the select button --- examples/HID/SRWS1/SRWS1.ino | 52 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/examples/HID/SRWS1/SRWS1.ino b/examples/HID/SRWS1/SRWS1.ino index d2917fc9..c83936fd 100644 --- a/examples/HID/SRWS1/SRWS1.ino +++ b/examples/HID/SRWS1/SRWS1.ino @@ -28,33 +28,34 @@ void loop() { Usb.Task(); if (srw1.connected()) { -#if 0 // Set to 1 in order to show a crazy strobe light effect - static uint32_t timer; - if (millis() - timer > 12) { - timer = millis(); // Reset timer + if (printTilt) { // Show tilt angle using the LEDs + srw1.setLeds(1 << map(srw1.srws1Data.tilt, -1800, 1800, 0, 14)); // Turn on a LED according to tilt value + Serial.println(srw1.srws1Data.tilt); + } else { // Show strobe light effect + static uint32_t timer; + if (millis() - timer > 12) { + timer = millis(); // Reset timer - static uint16_t leds = 0; - //PrintHex (leds, 0x80); Serial.println(); - srw1.setLeds(leds); // Update LEDs + static uint16_t leds = 0; + //PrintHex (leds, 0x80); Serial.println(); + srw1.setLeds(leds); // Update LEDs - static bool dirUp = true; - if (dirUp) { - leds <<= 1; - if (leds == 0x8000) // All are actually turned off, as there is only 15 LEDs - dirUp = false; // If we have reached the end i.e. all LEDs are off, then change direction - else if (!(leds & 0x8000)) // If last bit is not set, then set the lowest bit - leds |= 1; // Set lowest bit - } else { - leds >>= 1; - if (leds == 0) // Check if all LEDs are off - dirUp = true; // If all LEDs are off, then repeat the sequence - else if (!(leds & 0x1)) // If last bit is not set, then set the top bit - leds |= 1 << 15; // Set top bit + static bool dirUp = true; + if (dirUp) { + leds <<= 1; + if (leds == 0x8000) // All are actually turned off, as there is only 15 LEDs + dirUp = false; // If we have reached the end i.e. all LEDs are off, then change direction + else if (!(leds & 0x8000)) // If last bit is not set, then set the lowest bit + leds |= 1; // Set lowest bit + } else { + leds >>= 1; + if (leds == 0) // Check if all LEDs are off + dirUp = true; // If all LEDs are off, then repeat the sequence + else if (!(leds & 0x1)) // If last bit is not set, then set the top bit + leds |= 1 << 15; // Set top bit + } } } -#else - srw1.setLeds(1 << map(srw1.srws1Data.tilt, -1800, 1800, 0, 14)); // Turn on a LED according to tilt value -#endif if (srw1.srws1Data.leftTrigger) { Serial.print(F("L2: ")); @@ -68,7 +69,7 @@ void loop() { if (srw1.buttonClickState.select) { srw1.buttonClickState.select = 0; // Clear event Serial.println(F("Select")); - printTilt = !printTilt; // Print tilt value + printTilt = !printTilt; // Print tilt value & show it using the LEDs as well } if (srw1.buttonClickState.back) { @@ -174,9 +175,6 @@ void loop() { Serial.println(); break; } - - if (printTilt) - Serial.println(srw1.srws1Data.tilt); } }