Skip to content

Instantly share code, notes, and snippets.

@graysonarts
Created July 21, 2025 22:07
Show Gist options
  • Save graysonarts/1970d2f8c69d6eb18bc09ed30749e3c5 to your computer and use it in GitHub Desktop.
Save graysonarts/1970d2f8c69d6eb18bc09ed30749e3c5 to your computer and use it in GitHub Desktop.
OMG I2C
#include <Wire.h>
#define SDA 4
#define SCL 5
#define I2CADDR 0x20
byte rxByte[2];
bool whichByte = false;
byte selectedChannel = 0;
byte selectedBank = 0;
void handleRx(int numBytes) {
for (uint8_t i = 0; i < max(numBytes, 2); i++) {
rxByte[i] = Wire.read();
}
if (numBytes > 2) {
// Eat everything after the first two bytes
while (Wire.available()) {
Wire.read();
}
}
// Decode
if (rxByte[0] != 0x13) {
// UNKNOWN COMMAND, IGNORING
return;
}
selectedChannel = rxByte[1] & 0x0F;
selectedBank = selectedChannel / 4;
}
setup() {
Wire.setSDA(SDA);
Wire.setSCL(SCL);
Wire.begin(I2CADDR);
Wire.onReceive(handleRx);
}
loop() {
// do stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment