|
| 1 | +/****************************************************************************** |
| 2 | + Example_3c_Qwiic_HID_Mouse_Keyboard_Combined.ino |
| 3 | + Written by: Ho Yun "Bobby" Chan |
| 4 | + Date: February 6, 2020 |
| 5 | + Development Environment Specifics: |
| 6 | + Arduino IDE 1.8.9 |
| 7 | +
|
| 8 | + Description: |
| 9 | + Based on the following examples: |
| 10 | + - Jim's Pro Micro "HID Mouse/Keyboard" |
| 11 | + - Wes' Qwiic Joystick "basic reading" |
| 12 | + - Pete's Qwiic Keypad "read button" |
| 13 | +
|
| 14 | + This example controls the mouse with the Qwiic Joystick |
| 15 | + and keyboard presses associated with the Qwiic Keypad. |
| 16 | + The serial output is turned off so that Qwiic Pro Micro |
| 17 | + can respond faster. |
| 18 | +
|
| 19 | + Libraries: |
| 20 | + Mouse.h (included with Arduino IDE) |
| 21 | + Keyboard.h (included with Arduino IDE) |
| 22 | + Wire.h (included with Arduino IDE) |
| 23 | + SparkFun_Qwiic_Joystick_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_joystick |
| 24 | + SparkFun_Qwiic_Keypad_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_keypad |
| 25 | +
|
| 26 | + License: |
| 27 | + This code is released under the MIT License (http://opensource.org/licenses/MIT) |
| 28 | +
|
| 29 | +******************************************************************************/ |
| 30 | + |
| 31 | +#include <Mouse.h> |
| 32 | +#include <Keyboard.h> |
| 33 | +#include <Wire.h> |
| 34 | + |
| 35 | +#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick |
| 36 | +JOYSTICK joystick; //Create instance of this object |
| 37 | + |
| 38 | +#include "SparkFun_Qwiic_Keypad_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_keypad |
| 39 | +KEYPAD keypad1; //Create instance of this object |
| 40 | + |
| 41 | +int vertZero, horzZero; // Stores the initial value of each axis, usually around 512 |
| 42 | +int vertValue, horzValue; // Stores current analog output of each axis |
| 43 | +const int sensitivity = 150; // Higher sensitivity value = slower mouse, should be <= about 500 |
| 44 | +int mouseClickFlag = 0; |
| 45 | + |
| 46 | +//int invertMouse = 1; //Invert joystick based on orientation |
| 47 | +int invertMouse = -1; //Noninverted joystick based on orientation |
| 48 | + |
| 49 | +//Debug mode, comment one of these lines out using a syntax |
| 50 | +//for a single line comment ("//"): |
| 51 | +#define DEBUG 0 //0 = HID only |
| 52 | +//#define DEBUG 1 //1 = HID with serial output |
| 53 | + |
| 54 | +void setup() { |
| 55 | +#if DEBUG |
| 56 | + Serial.begin(9600); |
| 57 | + Serial.println("Qwiic KeyPad Example"); |
| 58 | +#endif |
| 59 | + |
| 60 | + if (joystick.begin() == false) { |
| 61 | +#if DEBUG |
| 62 | + Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing..."); |
| 63 | +#endif |
| 64 | + while (1); |
| 65 | + } |
| 66 | + if (keypad1.begin() == false) { // Note, using begin() like this will use default I2C address, 0x4B. |
| 67 | + // You can pass begin() a different address like so: keypad1.begin(Wire, 0x4A). |
| 68 | +#if DEBUG |
| 69 | + Serial.println("Keypad does not appear to be connected. Please check wiring. Freezing..."); |
| 70 | +#endif |
| 71 | + while (1); |
| 72 | + } |
| 73 | + |
| 74 | + delay(1000); // short delay to let outputs settle |
| 75 | + vertZero = joystick.getVertical(); // get the initial values |
| 76 | + horzZero = joystick.getHorizontal(); // Joystick should be in neutral position when reading these |
| 77 | + |
| 78 | +#if DEBUG |
| 79 | + Serial.print("Initialized. Firmware Version: "); |
| 80 | + Serial.println(keypad1.getVersion()); |
| 81 | + Serial.println("Press a button: * to do a space. # to go to next line."); |
| 82 | +#endif |
| 83 | + |
| 84 | + Mouse.begin(); //Init mouse emulation |
| 85 | + Keyboard.begin(); //Init keyboard emulation |
| 86 | + |
| 87 | +}//end setup |
| 88 | + |
| 89 | +void loop() { |
| 90 | + keypad1.updateFIFO(); // necessary for keypad to pull button from stack to readable register |
| 91 | + char button = keypad1.getButton(); |
| 92 | + |
| 93 | +#if DEBUG |
| 94 | + Serial.print("X: "); |
| 95 | + Serial.print(joystick.getHorizontal()); |
| 96 | + |
| 97 | + Serial.print(" Y: "); |
| 98 | + Serial.print(joystick.getVertical()); |
| 99 | + |
| 100 | + Serial.print(" Button: "); |
| 101 | + Serial.println(joystick.getButton()); |
| 102 | +#endif |
| 103 | + |
| 104 | + vertValue = joystick.getVertical() - vertZero; // read vertical offset |
| 105 | + horzValue = joystick.getHorizontal() - horzZero; // read horizontal offset |
| 106 | + |
| 107 | + if (vertValue != 0) |
| 108 | + Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis |
| 109 | + if (horzValue != 0) |
| 110 | + Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis |
| 111 | + |
| 112 | + if ((joystick.getButton() == 0) && (!mouseClickFlag)) // if the joystick button is pressed |
| 113 | + { |
| 114 | + mouseClickFlag = 1; |
| 115 | + Mouse.press(MOUSE_LEFT); // click the left button down |
| 116 | + } |
| 117 | + else if ((joystick.getButton()) && (mouseClickFlag)) // if the joystick button is not pressed |
| 118 | + { |
| 119 | + mouseClickFlag = 0; |
| 120 | + Mouse.release(MOUSE_LEFT); // release the left button |
| 121 | + } |
| 122 | + |
| 123 | + if (button == -1) { |
| 124 | +#if DEBUG |
| 125 | + Serial.println("No keypad detected"); |
| 126 | +#endif |
| 127 | + delay(1000); |
| 128 | + } |
| 129 | + |
| 130 | + else if (button != 0) { |
| 131 | +#if DEBUG |
| 132 | + Serial.print("Qwiic Keypad Button: "); |
| 133 | + Serial.println(button); |
| 134 | +#endif |
| 135 | + if (button == '0') {//note that this is a keypad '0' not the button press itself |
| 136 | + Keyboard.write('0'); |
| 137 | + } |
| 138 | + else if (button == '1') { |
| 139 | + Keyboard.write('1'); |
| 140 | + } |
| 141 | + else if (button == '2') { |
| 142 | + Keyboard.write('2'); |
| 143 | + } |
| 144 | + else if (button == '3') { |
| 145 | + Keyboard.write('3'); |
| 146 | + } |
| 147 | + else if (button == '4') { |
| 148 | + Keyboard.write('4'); |
| 149 | + } |
| 150 | + else if (button == '5') { |
| 151 | + Keyboard.write('5'); |
| 152 | + } |
| 153 | + else if (button == '6') { |
| 154 | + Keyboard.write('6'); |
| 155 | + } |
| 156 | + else if (button == '7') { |
| 157 | + Keyboard.write('7'); |
| 158 | + } |
| 159 | + else if (button == '8') { |
| 160 | + Keyboard.write('8'); |
| 161 | + } |
| 162 | + else if (button == '9') { |
| 163 | + Keyboard.write('9'); |
| 164 | + } |
| 165 | + else if (button == '#') { |
| 166 | + Keyboard.write('#'); |
| 167 | + } |
| 168 | + else if (button == '*') { |
| 169 | + Keyboard.write('*'); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + //Do something else. Don't call your Keypad a ton otherwise you'll tie up the I2C bus |
| 174 | + //Uncomment this if necessary but since we check the Qwiic Joystick it does not |
| 175 | + // appear to be an issue |
| 176 | + //delay(25); //25 is good, more is better |
| 177 | + |
| 178 | +}//end loop |
0 commit comments