diff --git a/examples/SurfaceDial/surface_dial.ino b/examples/SurfaceDial/surface_dial.ino new file mode 100644 index 0000000..5dcb1d7 --- /dev/null +++ b/examples/SurfaceDial/surface_dial.ino @@ -0,0 +1,67 @@ +#include "HID-Project.h" + +int pinA = 2; +int pinB = 3; + +int pinButton = 4; + +volatile bool previousButtonValue = false; + +volatile int previous = 0; +volatile int counter = 0; + +void setup() { + pinMode(pinA, INPUT_PULLUP); + pinMode(pinB, INPUT_PULLUP); + + pinMode(pinButton, INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(pinA), changed, CHANGE); + attachInterrupt(digitalPinToInterrupt(pinB), changed, CHANGE); + + SurfaceDial.begin(); +} + +void changed() { + int A = digitalRead(pinA); + int B = digitalRead(pinB); + + int current = (A << 1) | B; + int combined = (previous << 2) | current; + + if(combined == 0b0010 || + combined == 0b1011 || + combined == 0b1101 || + combined == 0b0100) { + counter++; + } + + if(combined == 0b0001 || + combined == 0b0111 || + combined == 0b1110 || + combined == 0b1000) { + counter--; + } + + previous = current; +} + +void loop(){ + bool buttonValue = digitalRead(pinButton); + if(buttonValue != previousButtonValue){ + if(buttonValue) { + SurfaceDial.press(); + } else { + SurfaceDial.release(); + } + previousButtonValue = buttonValue; + } + + if(counter >= 4) { + SurfaceDial.rotate(10); + counter -= 4; + } else if(counter <= -4) { + SurfaceDial.rotate(-10); + counter += 4; + } +}