Last active
March 19, 2017 02:12
-
-
Save deton/f657022a3cb6c6b336f604d61a1b25a5 to your computer and use it in GitHub Desktop.
Use "SparkFun Blackberry Trackballer Breakout" as trackball for ATmega32U4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SparkFun Blackberry Trackballer Breakout | |
| // https://www.switch-science.com/catalog/2276/ | |
| #define DEBUG 0 | |
| #define Lft 3 | |
| #define Rht 2 | |
| #define Up 0 | |
| #define Dwn 1 | |
| #define Btn1 7 | |
| #define Btn2 6 | |
| const int MINCNT = 10; | |
| volatile int left = 0; | |
| volatile int right = 0; | |
| volatile int up = 0; | |
| volatile int down = 0; | |
| void onRight() { | |
| right++; | |
| } | |
| void onLeft() { | |
| left++; | |
| } | |
| void onUp() { | |
| up++; | |
| } | |
| void onDown() { | |
| down++; | |
| } | |
| void setup() { | |
| pinMode(Btn1, INPUT_PULLUP); | |
| pinMode(Btn2, INPUT_PULLUP); | |
| pinMode(Lft, INPUT); | |
| pinMode(Rht, INPUT); | |
| pinMode(Up, INPUT); | |
| pinMode(Dwn, INPUT); | |
| Mouse.begin(); | |
| #if DEBUG | |
| while (!Serial) { | |
| // wait for Leonardo | |
| } | |
| Serial.begin(9600); | |
| #endif | |
| attachInterrupt(0, onLeft, CHANGE); // leonardo pin2 | |
| attachInterrupt(1, onRight, CHANGE); // leonardo pin3 | |
| attachInterrupt(2, onUp, CHANGE); // leonardo pin0 | |
| attachInterrupt(3, onDown, CHANGE); // leonardo pin1 | |
| } | |
| void loop() { | |
| int dx = 0; | |
| int dy = 0; | |
| if (left > 0) { | |
| dx -= left; | |
| left = 0; | |
| } | |
| if (right > 0) { | |
| dx += right; | |
| right = 0; | |
| } | |
| if (up > 0) { | |
| dy += up; | |
| up = 0; | |
| } | |
| if (down > 0) { | |
| dy -= down; | |
| down = 0; | |
| } | |
| if (dx != 0 || dy != 0) { | |
| Mouse.move(dx * MINCNT, dy * MINCNT, 0); | |
| #if DEBUG | |
| Serial.print("dx,dy="); | |
| Serial.print(dx); | |
| Serial.print(","); | |
| Serial.print(dy); | |
| Serial.println(); | |
| #endif | |
| } | |
| //Check for button click | |
| if (digitalRead(Btn1) == LOW) { | |
| if (!Mouse.isPressed(MOUSE_LEFT)) { | |
| #if DEBUG | |
| Serial.println("click"); | |
| #endif | |
| Mouse.press(MOUSE_LEFT); | |
| } | |
| } else { | |
| if (Mouse.isPressed(MOUSE_LEFT)) { | |
| Mouse.release(MOUSE_LEFT); | |
| } | |
| } | |
| if (digitalRead(Btn2) == LOW) { | |
| if (!Mouse.isPressed(MOUSE_RIGHT)) { | |
| #if DEBUG | |
| Serial.println("click2"); | |
| #endif | |
| Mouse.press(MOUSE_RIGHT); | |
| } | |
| } else { | |
| if (Mouse.isPressed(MOUSE_RIGHT)) { | |
| Mouse.release(MOUSE_RIGHT); | |
| } | |
| } | |
| delay(10); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment