1
1
/* *****************************************************************************
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!
3
4
4
5
Fischer Moseley @ SparkFun Electronics
5
6
Original Creation Date: July 30, 2019
6
7
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
8
9
local, and you've found our code helpful, please buy us a round!
9
10
10
11
Hardware Connections:
@@ -16,60 +17,111 @@ Distributed as-is; no warranty is given.
16
17
******************************************************************************/
17
18
18
19
#include < SparkFun_Qwiic_Button.h>
19
- QwiicButton button ;
20
+ QwiicButton device ;
20
21
21
22
void setup (){
22
23
Serial.begin (115200 );
23
24
Wire.begin (); // Join I2C bus
24
- Wire.setClock (400000 );
25
- button.begin (0x46 );
25
+ Wire.setClock (400000 ); // Set I2C clock speed to 400kHz
26
26
27
27
// check if button will acknowledge over I2C
28
- if (button .isConnected ()){
28
+ if (device .isConnected ()){
29
29
Serial.println (" Device will acknowledge!" );
30
30
}
31
31
32
32
else {
33
33
Serial.println (" Device did not acknowledge! Freezing." );
34
34
while (1 );
35
35
}
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." );
36
42
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'" );
40
45
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
43
68
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);
45
81
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 );
63
101
}
64
102
65
103
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. " );
67
105
}
68
106
}
69
107
70
108
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;
72
124
}
73
-
74
125
}
126
+ return -1 ;
75
127
}
0 commit comments