Skip to content

Commit 80358b4

Browse files
unknownfischermoseley
authored and
unknown
committed
Made files more user-friendly
Co-Authored-By: Fischer Moseley <[email protected]>
1 parent 47dfb7d commit 80358b4

File tree

9 files changed

+206
-105
lines changed

9 files changed

+206
-105
lines changed

examples/Example1_PrintButtonStatus/Example1_PrintButtonStatus.ino

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/******************************************************************************
2-
Checks whether the button is pressed, and then prints its status!
2+
Checks whether the button is pressed, and then prints its status over serial!
33
44
Fischer Moseley @ SparkFun Electronics
55
Original Creation Date: June 28, 2019
66
7-
This code is beerware; if you see me (or any other SparkFun employee) at the
7+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
88
local, and you've found our code helpful, please buy us a round!
99
1010
Hardware Connections:
@@ -21,8 +21,9 @@ QwiicButton button;
2121
void setup(){
2222
Serial.begin(115200);
2323
Wire.begin(); //Join I2C bus
24-
Wire.setClock(400000);
25-
button.begin();
24+
Wire.setClock(400000); //Set I2C clock speed to 400kHz
25+
button.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
26+
// switch, or whatever the I2C address of your device is
2627

2728
//check if button will acknowledge over I2C
2829
if(button.isConnected()){
@@ -45,5 +46,5 @@ void loop(){
4546
Serial.println("The button is not pressed.");
4647
}
4748

48-
delay(20); //let's not hammer too hard on the I2C bus
49+
delay(20); //Don't hammer too hard on the I2C bus
4950
}

examples/Example2_LightWhenPressed/Example2_LightWhenPressed.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/******************************************************************************
2-
Checks whether the button is pressed, and light it up if it is! Also prints
3-
status to the serial monitor.
2+
Turns on the Button's built in LED when pressed, and prints status over Serial!
43
54
Fischer Moseley @ SparkFun Electronics
65
Original Creation Date: July 24, 2019
76
8-
This code is beerware; if you see me (or any other SparkFun employee) at the
7+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
98
local, and you've found our code helpful, please buy us a round!
109
1110
Hardware Connections:
@@ -24,8 +23,9 @@ uint8_t brightness = 100; //The brightness to set the LED to when the button i
2423
void setup(){
2524
Serial.begin(115200);
2625
Wire.begin(); //Join I2C bus
27-
Wire.setClock(400000);
28-
button.begin();
26+
Wire.setClock(400000); //Set the I2C clock speed to 400kHz
27+
button.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
28+
// switch, or whatever the I2C address of your device is
2929

3030
//check if button will acknowledge over I2C
3131
if(button.isConnected()){
@@ -51,5 +51,5 @@ void loop(){
5151
Serial.println("The button is not pressed.");
5252
button.LEDoff();
5353
}
54-
delay(20); //let's not hammer too hard on the I2C bus
54+
delay(20); //Don't hammer too hard on the I2C bus
5555
}

examples/Example3_PulseWhenPressed/Example3_PulseWhenPressed.ino

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ status to the serial monitor.
55
Fischer Moseley @ SparkFun Electronics
66
Original Creation Date: July 24, 2019
77
8-
This code is beerware; if you see me (or any other SparkFun employee) at the
8+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
99
local, and you've found our code helpful, please buy us a round!
1010
1111
Hardware Connections:
@@ -26,8 +26,9 @@ uint16_t offTime = 500; //The total time to stay off between pulses. Set to
2626
void setup(){
2727
Serial.begin(115200);
2828
Wire.begin(); //Join I2C bus
29-
Wire.setClock(400000);
30-
button.begin();
29+
Wire.setClock(400000); //Set I2C clock to 400kHz
30+
button.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
31+
// switch, or whatever the I2C address of your device is
3132

3233
//check if button will acknowledge over I2C
3334
if(button.isConnected()){

examples/Example4_InterruptSetup/Example4_InterruptSetup.ino

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ serial. Also allows us to enable/disable/reset the interrupt too!
55
Fischer Moseley @ SparkFun Electronics
66
Original Creation Date: July 29, 2019
77
8-
This code is beerware; if you see me (or any other SparkFun employee) at the
8+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
99
local, and you've found our code helpful, please buy us a round!
1010
1111
Hardware Connections:
@@ -22,8 +22,9 @@ QwiicButton button;
2222
void setup(){
2323
Serial.begin(115200);
2424
Wire.begin(); //Join I2C bus
25-
Wire.setClock(400000);
26-
button.begin();
25+
Wire.setClock(400000); //Set I2C address to 400kHz
26+
button.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
27+
// switch, or whatever the I2C address of your device is
2728

2829
//check if button will acknowledge over I2C
2930
if(button.isConnected()){
@@ -35,8 +36,9 @@ void setup(){
3536
while(1);
3637
}
3738

38-
button.resetInterruptConfig(); //reset all the interrupt configuration stuff to defaults, so we have a clean slate to work with!
39-
button.enablePressedInterrupt(); //configure the interrupt to trigger when we press the button
39+
button.resetInterruptConfig(); //reset all the interrupt configuration stuff to defaults, so we have a clean slate to work with!
40+
button.enablePressedInterrupt(); //configure the interrupt to trigger when we press the button. Change to enableClickedInterrupt()
41+
//to interrupt when the button is clicked!
4042
}
4143

4244
void loop(){
@@ -50,16 +52,16 @@ void loop(){
5052

5153
if(Serial.available()){
5254
uint8_t recieved = Serial.read();
53-
if(recieved == 'r' || recieved == 'R') { //if the user has send either r or R over serial, then reset the interrupt!
55+
if(recieved == 'r' || recieved == 'R') { //if the user has sent either r or R over serial, then reset the interrupt!
5456
Serial.println("Resetting Interrupt...");
5557
button.clearInterrupt();
5658
}
57-
if(recieved == 'd' || recieved == 'D') {
59+
if(recieved == 'd' || recieved == 'D') { //if the user has sent either d or D over serial, then disable the interrupt!
5860
Serial.println("Disabling Interrupt...");
5961
button.disablePressedInterrupt();
6062
}
6163

62-
if(recieved == 'e' || recieved == 'E') {
64+
if(recieved == 'e' || recieved == 'E') { //if the user has sent either e or E over serial, then enable the interrupt!
6365
Serial.println("Enabling Interrupt...");
6466
button.enablePressedInterrupt();
6567
}

examples/Example5_QueueUsage/Example5_QueueUsage.ino

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/******************************************************************************
22
Configures the button to raise an interrupt when pressed, and notifies us over
3-
serial. Also allows us to enable/disable/reset the interrupt too!
3+
serial. Allows us to enable/disable/reset the interrupt too!
44
55
Fischer Moseley @ SparkFun Electronics
66
Original Creation Date: July 29, 2019
77
8-
This code is beerware; if you see me (or any other SparkFun employee) at the
8+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
99
local, and you've found our code helpful, please buy us a round!
1010
1111
Hardware Connections:
@@ -22,8 +22,9 @@ QwiicButton button;
2222
void setup(){
2323
Serial.begin(115200);
2424
Wire.begin(); //Join I2C bus
25-
Wire.setClock(400000);
26-
button.begin();
25+
Wire.setClock(400000); //Set I2C clock speed to 400kHz
26+
button.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
27+
// switch, or whatever the I2C address of your device is
2728

2829
//check if button will acknowledge over I2C
2930
if(button.isConnected()){
@@ -37,34 +38,42 @@ void setup(){
3738
}
3839

3940
void loop(){
40-
if(button.isPressedQueueEmpty() == false) {
41+
if(button.isPressedQueueEmpty() == false) { //if the queue of pressed events is not empty
42+
//then print the time since the last and first button press
4143
Serial.print(button.timeSinceLastPress()/1000.0);
4244
Serial.print("s since the button was last pressed ");
4345
Serial.print(button.timeSinceFirstPress()/1000.0);
4446
Serial.print("s since the button was first pressed ");
4547
}
4648

47-
if(button.isPressedQueueEmpty() == true) Serial.print("ButtonPressed Queue is empty! ");
49+
//if the queue of pressed events is empty, just print that the queue is empty!
50+
if(button.isPressedQueueEmpty() == true) {
51+
Serial.print("ButtonPressed Queue is empty! ");
52+
}
4853

49-
if(button.isClickedQueueEmpty() == false){
54+
if(button.isClickedQueueEmpty() == false) { //if the queue of clicked events is not empty
55+
//then print the time since the last and first button click
5056
Serial.print(button.timeSinceLastClick()/1000.0);
5157
Serial.print("s since the button was last clicked ");
5258
Serial.print(button.timeSinceFirstClick()/1000.0);
5359
Serial.print("s since the button was first clicked");
5460
}
61+
//if the queue of clicked events is empty, just print that the queue is empty!
62+
if(button.isPressedQueueEmpty() == true) {
63+
Serial.print(" ButtonClicked Queue is empty!");
64+
}
5565

56-
if(button.isPressedQueueEmpty() == true) Serial.print(" ButtonClicked Queue is empty!");
57-
Serial.println();
66+
Serial.println(); //print a new line to not clutter up the serial monitor
5867

59-
if(Serial.available()) {
68+
if(Serial.available()) { //if the user sent a character
6069

6170
uint8_t data = Serial.read();
62-
if(data == 'p' || data == 'P') {
71+
if(data == 'p' || data == 'P') { //if the character is p or P, then pop a value off of the pressed Queue
6372
button.popPressedQueue();
6473
Serial.println("Popped PressedQueue!");
6574
}
6675

67-
if(data == 'c' || data == 'C') {
76+
if(data == 'c' || data == 'C') { //if the character is c or C, then pop a value off of the pressed Queue
6877
button.popClickedQueue();
6978
Serial.println("Popped ClickedQueue!");
7079
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/******************************************************************************
2-
Checks whether the button is pressed, and then prints its status!
2+
A configurator for changing the I2C address on the Qwiic Button/Switch that walks
3+
the user through finding the address of their button, and then changing it!
34
45
Fischer Moseley @ SparkFun Electronics
56
Original Creation Date: July 30, 2019
67
7-
This code is beerware; if you see me (or any other SparkFun employee) at the
8+
This code is Lemonadeware; if you see me (or any other SparkFun employee) at the
89
local, and you've found our code helpful, please buy us a round!
910
1011
Hardware Connections:
@@ -16,60 +17,111 @@ Distributed as-is; no warranty is given.
1617
******************************************************************************/
1718

1819
#include <SparkFun_Qwiic_Button.h>
19-
QwiicButton button;
20+
QwiicButton device;
2021

2122
void setup(){
2223
Serial.begin(115200);
2324
Wire.begin(); //Join I2C bus
24-
Wire.setClock(400000);
25-
button.begin(0x46);
25+
Wire.setClock(400000); //Set I2C clock speed to 400kHz
2626

2727
//check if button will acknowledge over I2C
28-
if(button.isConnected()){
28+
if(device.isConnected()){
2929
Serial.println("Device will acknowledge!");
3030
}
3131

3232
else {
3333
Serial.println("Device did not acknowledge! Freezing.");
3434
while(1);
3535
}
36+
}
37+
38+
void loop() {
39+
//print an introduction
40+
Serial.println("Howdy stranger! This configurator will help you change");
41+
Serial.println("The I2C address of your attached Qwiic Button/Switch.");
3642
Serial.println();
37-
Serial.println("Enter a new I2C address for the Qwiic Button to use!");
38-
Serial.println("Don't use the 0x prefix. For instance, if you wanted to");
39-
Serial.println("change the address to 0x5B, you would enter 5B and press enter.");
43+
Serial.println("Also, make sure that your Line Ending in the Serial Monitor");
44+
Serial.println("is set to 'Both NL & CR'");
4045
Serial.println();
41-
Serial.println("One more thing! Make sure your line ending is set to 'Both NL & CR'");
42-
Serial.println("in the Serial Monitor.");
46+
Serial.println("To begin, let's scan for a device. Disconnect all other Qwiic or");
47+
Serial.println("I2C devices from your microcontroller, and then send any character");
48+
Serial.println("to begin the scan.");
49+
50+
while(!Serial.available()); //wait for the user to send a character
51+
while(Serial.available()) Serial.read(); //flush the readbuffer
52+
53+
Serial.println("Beginning scan...");
54+
uint8_t address = scanForDevices();
55+
56+
if(address == -1) { //if the function returned with error, freeze
57+
Serial.println("No devices found! Freezing.");
58+
while(1);
59+
}
60+
61+
//if we got to here, it means that we haven't frozen, so we print that we found a device
62+
Serial.print("Device found at address: 0x");
63+
Serial.println(address, HEX);
64+
65+
device.begin(address);
66+
67+
//Inform the user that they'll have to pick out a new address
4368
Serial.println();
44-
}
69+
Serial.println("Enter a new I2C address for the Qwiic Button to use (in hex)!");
70+
Serial.println("Don't use the 0x prefix. For instance, if you wanted to");
71+
Serial.println("change the address to 0x5B, you would enter 5B and press enter.");
72+
73+
while(!Serial.available()); //wait until the user sends some characters
74+
75+
//Read the buffer and parse it for a valid hex address
76+
String stringBuffer = Serial.readStringUntil('\r');
77+
uint8_t charBuffer[10];
78+
stringBuffer.toCharArray(charBuffer, 10);
79+
uint8_t newAddress = 0;
80+
uint8_t success = sscanf(charBuffer, "%x", &newAddress);
4581

46-
void loop(){
47-
//check if button is pressed, and tell us if it is!
48-
if(Serial.available()) {
49-
uint8_t newAddress = 0;
50-
String stringBuffer = Serial.readStringUntil('\r');
51-
uint8_t charBuffer[10];
52-
stringBuffer.toCharArray(charBuffer, 10);
53-
uint8_t success = sscanf(charBuffer, "%x", &newAddress);
54-
55-
if(success == 1) {
56-
if(newAddress > 0x08 && newAddress < 0x77) {
57-
Serial.println("Character recieved, and device address is valid!");
58-
Serial.println("Attempting to set device address");
59-
Serial.println(newAddress, HEX);
60-
Serial.println(button.setI2Caddress(newAddress));
61-
delay(100);
62-
Serial.println(button.isConnected());
82+
//if precisely 1 valid hex number was found in the string, begin setting address
83+
if(success == 1) {
84+
//check that the address is valid
85+
if(newAddress > 0x08 && newAddress < 0x77) {
86+
Serial.println("Character recieved, and device address is valid!");
87+
Serial.print("Attempting to set device address to: ");
88+
Serial.println(newAddress, HEX);
89+
90+
device.setI2Caddress(newAddress);
91+
delay(10); //give the button/switch some time to restart its I2C hardware
92+
93+
if(device.isConnected()) {
94+
//Job is done, print that we're finished
95+
Serial.print("Address successfully changed! Device will respond at: ");
96+
Serial.println(device.getI2Caddress(), HEX);
97+
98+
Serial.println();
99+
Serial.println("Job done, freezing.");
100+
while(1);
63101
}
64102

65103
else {
66-
Serial.println("Address out of range! Try an adress between 0x08 and 0x77");
104+
Serial.print("Address change was not successfull! Reset and try again.");
67105
}
68106
}
69107

70108
else {
71-
Serial.print("Invalid Text! Try again.");
109+
Serial.println("Address out of range! Try an adress between 0x08 and 0x77");
110+
}
111+
}
112+
113+
else {
114+
Serial.print("Invalid Text! Try again.");
115+
}
116+
}
117+
118+
//returns the I2C address of the connected device, or -1 if there's no device found
119+
int16_t scanForDevices() {
120+
for(uint8_t addr = 0; addr < 127; addr++) {
121+
Wire.beginTransmission(addr);
122+
if(Wire.endTransmission() == 0) {
123+
return addr;
72124
}
73-
74125
}
126+
return -1;
75127
}

0 commit comments

Comments
 (0)