Skip to content

Commit 2bdf474

Browse files
committed
Adding new firmware examples
1 parent dab07c9 commit 2bdf474

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/******************************************************************************
2+
Example_3a_Qwiic_Joystick_HID_Mouse.ino
3+
Written by: Ho Yun "Bobby" Chan
4+
Date: January 13, 2020
5+
Development Environment Specifics:
6+
Arduino IDE 1.8.9
7+
8+
Description:
9+
Based on the Jim's Pro Micro "HID Mouse" and Wes' Qwiic Joystick "basic reading"
10+
examples, this example moves your computer's mouse based on the joystick's
11+
position. Pressing down on the joystick's will enable a mouse's left click.
12+
The left click will relese as soon as you stop pressing down on the joystick.
13+
14+
Libraries:
15+
Mouse.h (included with Arduino IDE)
16+
Wire.h (included with Arduino IDE)
17+
SparkFun_Qwiic_Joystick_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_joystick
18+
19+
License:
20+
This code is released under the MIT License (http://opensource.org/licenses/MIT)
21+
22+
******************************************************************************/
23+
24+
#include <Mouse.h>
25+
#include <Wire.h>
26+
27+
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick
28+
JOYSTICK joystick; //Create instance of this object
29+
30+
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
31+
int vertValue, horzValue; // Stores current analog output of each axis
32+
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
33+
int mouseClickFlag = 0;
34+
35+
//int invertMouse = 1; //Invert joystick based on orientation
36+
int invertMouse = -1; //Noninverted joystick based on orientation
37+
38+
//Debug mode, comment one of these lines out using a syntax
39+
//for a single line comment ("//"):
40+
#define DEBUG 0 //0 = HID only
41+
//#define DEBUG 1 //1 = HID with serial output
42+
43+
void setup() {
44+
#if DEBUG
45+
Serial.begin(9600);
46+
Serial.println("Example 3: HID Mouse w/ Qwiic Joystick");
47+
#endif
48+
49+
if (joystick.begin() == false)
50+
{
51+
#if DEBUG
52+
Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
53+
#endif
54+
while (1);
55+
}
56+
57+
delay(1000); // short delay to let outputs settle
58+
vertZero = joystick.getVertical(); // get the initial values
59+
horzZero = joystick.getHorizontal(); // Joystick should be in neutral position when reading these
60+
61+
Mouse.begin(); //Init mouse emulation
62+
}
63+
64+
void loop() {
65+
#if DEBUG
66+
Serial.print("X: ");
67+
Serial.print(joystick.getHorizontal());
68+
69+
Serial.print(" Y: ");
70+
Serial.print(joystick.getVertical());
71+
72+
Serial.print(" Button: ");
73+
Serial.println(joystick.getButton());
74+
#endif
75+
76+
vertValue = joystick.getVertical() - vertZero; // read vertical offset
77+
horzValue = joystick.getHorizontal() - horzZero; // read horizontal offset
78+
79+
if (vertValue != 0)
80+
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis
81+
if (horzValue != 0)
82+
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
83+
84+
if ((joystick.getButton() == 0) && (!mouseClickFlag)) // if the joystick button is pressed
85+
{
86+
mouseClickFlag = 1;
87+
Mouse.press(MOUSE_LEFT); // click the left button down
88+
}
89+
else if ((joystick.getButton()) && (mouseClickFlag)) // if the joystick button is not pressed
90+
{
91+
mouseClickFlag = 0;
92+
Mouse.release(MOUSE_LEFT); // release the left button
93+
}
94+
95+
//delay(200); //remove "//" on this line if you need a small delay
96+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/******************************************************************************
2+
Example_3b_Qwiic_Keypad_HID_Keyboard.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 Jim's Pro Micro "HID Mouse" and Pete' Qwiic Keypad "read button"
10+
examples, this example outputs keyboard presses associated with the keypad.
11+
12+
Libraries:
13+
Keyboard.h (included with Arduino IDE)
14+
Wire.h (included with Arduino IDE)
15+
SparkFun_Qwiic_Keypad_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_keypad
16+
17+
License:
18+
This code is released under the MIT License (http://opensource.org/licenses/MIT)
19+
20+
******************************************************************************/
21+
22+
#include <Keyboard.h>
23+
#include <Wire.h>
24+
#include "SparkFun_Qwiic_Keypad_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_keypad
25+
KEYPAD keypad1; //Create instance of this object
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
Serial.println("Qwiic KeyPad Example");
30+
31+
if (keypad1.begin() == false) // Note, using begin() like this will use default I2C address, 0x4B.
32+
// You can pass begin() a different address like so: keypad1.begin(Wire, 0x4A).
33+
{
34+
Serial.println("Keypad does not appear to be connected. Please check wiring. Freezing...");
35+
while (1);
36+
}
37+
Serial.print("Initialized. Firmware Version: ");
38+
Serial.println(keypad1.getVersion());
39+
Serial.println("Press a button: * to do a space. # to go to next line.");
40+
41+
Keyboard.begin(); //Init keyboard emulation
42+
}
43+
44+
void loop() {
45+
keypad1.updateFIFO(); // necessary for keypad to pull button from stack to readable register
46+
char button = keypad1.getButton();
47+
48+
if (button == -1)
49+
{
50+
Serial.println("No keypad detected");
51+
delay(1000);
52+
}
53+
else if (button != 0)
54+
{
55+
if (button == '0') {//note that this is a keypad '0' not the button press itself
56+
Keyboard.write('0');
57+
}
58+
else if (button == '1') {
59+
Keyboard.write('1');
60+
}
61+
else if (button == '2') {
62+
Keyboard.write('2');
63+
}
64+
else if (button == '3') {
65+
Keyboard.write('3');
66+
}
67+
else if (button == '4') {
68+
Keyboard.write('4');
69+
}
70+
else if (button == '5') {
71+
Keyboard.write('5');
72+
}
73+
else if (button == '6') {
74+
Keyboard.write('6');
75+
}
76+
else if (button == '7') {
77+
Keyboard.write('7');
78+
}
79+
else if (button == '8') {
80+
Keyboard.write('8');
81+
}
82+
else if (button == '9') {
83+
Keyboard.write('9');
84+
}
85+
else if (button == '#') {
86+
Keyboard.write('#');
87+
}
88+
else if (button == '*') {
89+
Keyboard.write('*');
90+
}
91+
}
92+
93+
//Do something else. Don't call your Keypad a ton otherwise you'll tie up the I2C bus
94+
delay(25); //25 is good, more is better
95+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

Comments
 (0)