Skip to content

Commit 0e75789

Browse files
Fischer Moseleyfischermoseley
andcommitted
Added ability to change I2C address
Co-Authored-By: Fischer Moseley <[email protected]>
1 parent f6773e1 commit 0e75789

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/******************************************************************************
2+
Checks whether the button is pressed, and then prints its status!
3+
4+
Fischer Moseley @ SparkFun Electronics
5+
Original Creation Date: July 30, 2019
6+
7+
This code is beerware; if you see me (or any other SparkFun employee) at the
8+
local, and you've found our code helpful, please buy us a round!
9+
10+
Hardware Connections:
11+
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
12+
Plug the button into the shield
13+
Print it to the serial monitor at 115200 baud.
14+
15+
Distributed as-is; no warranty is given.
16+
******************************************************************************/
17+
18+
#include <SparkFun_Qwiic_Button.h>
19+
QwiicButton button;
20+
21+
void setup(){
22+
Serial.begin(115200);
23+
Wire.begin(); //Join I2C bus
24+
Wire.setClock(400000);
25+
button.begin(0x46);
26+
27+
//check if button will acknowledge over I2C
28+
if(button.isConnected()){
29+
Serial.println("Device will acknowledge!");
30+
}
31+
32+
else {
33+
Serial.println("Device did not acknowledge! Freezing.");
34+
while(1);
35+
}
36+
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.");
40+
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.");
43+
Serial.println();
44+
}
45+
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());
63+
}
64+
65+
else {
66+
Serial.println("Address out of range! Try an adress between 0x08 and 0x77");
67+
}
68+
}
69+
70+
else {
71+
Serial.print("Invalid Text! Try again.");
72+
}
73+
74+
}
75+
}

src/SparkFun_Qwiic_Button.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool QwiicButton::checkDeviceID() {
5656
uint8_t QwiicButton::getDeviceType() {
5757
if(isConnected()){ //only try to get the device ID if the device will acknowledge
5858
uint8_t id = deviceID();
59-
if(id == DEV_ID_BTN) return 1; //
59+
if(id == DEV_ID_BTN) return 1;
6060
if(id == DEV_ID_SW) return 2;
6161
}
6262
return 0;
@@ -68,6 +68,21 @@ uint16_t QwiicButton::getFirmwareVersion() {
6868
return version;
6969
}
7070

71+
bool QwiicButton::setI2Caddress(uint8_t address) {
72+
if(address < 0x08 || address > 0x77) return 1; //error immediately if the address is out of legal range
73+
74+
bool success = writeSingleRegister(I2C_ADDRESS, address);
75+
76+
if (success) {
77+
_deviceAddress = address;
78+
return 0; //return 0 if the write was successful, and after the struct's _deviceAddress variable was changed
79+
}
80+
81+
else {
82+
return 1; //otherwise just return error
83+
}
84+
}
85+
7186

7287
/*------------------------------ Button Status ---------------------- */
7388
bool QwiicButton::isPressed() {

src/SparkFun_Qwiic_Button.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Distributed as-is; no warranty is given.
2626
#include <Arduino.h>
2727
#include "registers.h"
2828

29-
#define DEV_ADDR 0x5F //default device address of the QwiicButton
29+
#define DEV_ADDR 0x60 //default device address of the QwiicButton
3030
#define DEV_ID_BTN 0x5D //device ID of the Qwiic Button
3131
#define DEV_ID_SW 0x5E //device ID of the Qwiic Switch
3232

@@ -43,6 +43,7 @@ class QwiicButton {
4343
bool checkDeviceID(); //Returns true if the device ID matches that of either the button or the switch
4444
uint8_t getDeviceType(); //Returns 1 if a button is attached, 2 if a switch is attached. Returns 0 if there is no device attached.
4545
uint16_t getFirmwareVersion(); //Returns the firmware version of the attached device as a 16-bit integer. The leftmost (high) byte is the major revision number, and the rightmost (low) byte is the minor revision number.
46+
bool setI2Caddress(uint8_t address); //Configures the attached device to attach to the I2C bus using the specified address
4647

4748
//Button status/config
4849
bool isPressed(); //Returns 1 if the button/switch is pressed, and 0 otherwise

0 commit comments

Comments
 (0)