@@ -22,106 +22,71 @@ QwiicButton device;
22
22
void setup (){
23
23
Serial.begin (115200 );
24
24
Wire.begin (); // Join I2C bus
25
- Wire.setClock (400000 ); // Set I2C clock speed to 400kHz
25
+ Wire.setClock (400000 );
26
+ device.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
26
28
27
29
// check if button will acknowledge over I2C
28
- if (device.isConnected ()){
30
+ if (device.isConnected ()) {
29
31
Serial.println (" Device will acknowledge!" );
30
32
}
31
33
32
34
else {
33
35
Serial.println (" Device did not acknowledge! Freezing." );
34
36
while (1 );
35
37
}
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." );
42
- Serial.println ();
43
- Serial.println (" Also, make sure that your Line Ending in the Serial Monitor" );
44
- Serial.println (" is set to 'Both NL & CR'" );
45
38
Serial.println ();
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
68
- Serial.println ();
69
- Serial.println (" Enter a new I2C address for the Qwiic Button to use (in hex)!" );
39
+ Serial.println (" Enter a new I2C address for the Qwiic Button/Switch to use!" );
70
40
Serial.println (" Don't use the 0x prefix. For instance, if you wanted to" );
71
41
Serial.println (" change the address to 0x5B, you would enter 5B and press enter." );
42
+ Serial.println ();
43
+ Serial.println (" One more thing! Make sure your line ending is set to 'Both NL & CR'" );
44
+ Serial.println (" in the Serial Monitor." );
45
+ Serial.println ();
46
+ }
72
47
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
- char charBuffer[10 ];
78
- stringBuffer.toCharArray (charBuffer, 10 );
79
- uint8_t newAddress = 0 ;
80
- uint8_t success = sscanf (charBuffer, " %x" , &newAddress);
81
-
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 );
48
+ void loop (){
49
+ // check if button is pressed, and tell us if it is!
50
+ if (Serial.available ()) {
51
+ uint8_t newAddress = 0 ;
52
+ String stringBuffer = Serial.readStringUntil (' \r ' );
53
+ char charBuffer[10 ];
54
+ stringBuffer.toCharArray (charBuffer, 10 );
55
+ uint8_t success = sscanf (charBuffer, " %x" , &newAddress);
56
+
57
+ if (success) {
58
+ if (newAddress > 0x08 && newAddress < 0x77 ) {
59
+ Serial.println (" Character recieved, and device address is valid!" );
60
+ Serial.print (" Attempting to set device address to 0x" );
61
+ Serial.println (newAddress, HEX);
62
+
63
+ if (device.setI2Caddress (newAddress) == 0 ) {
64
+ Serial.println (" Device address set succeeded!" );
65
+ }
66
+
67
+ else {
68
+ Serial.println (" Device address set failed!" );
69
+ }
70
+
71
+ delay (100 ); // give the hardware time to do whatever configuration it needs to do\
72
+
73
+ if (device.isConnected ()) {
74
+ Serial.println (" Device will acknowledge on new I2C address!" );
75
+ }
76
+
77
+ else {
78
+ Serial.println (" Device will not acknowledge on new I2C address." );
79
+ }
101
80
}
102
81
103
82
else {
104
- Serial.print (" Address change was not successfull! Reset and try again. " );
83
+ Serial.println (" Address out of range! Try an adress between 0x08 and 0x77 " );
105
84
}
106
85
}
107
86
108
87
else {
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;
88
+ Serial.print (" Invalid Text! Try again." );
124
89
}
90
+
125
91
}
126
- return -1 ;
127
92
}
0 commit comments