Added simple example
This commit is contained in:
parent
2169d34f2c
commit
813eb71440
1 changed files with 67 additions and 0 deletions
67
examples/SurfaceDial/surface_dial.ino
Normal file
67
examples/SurfaceDial/surface_dial.ino
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue