-
-
Save k3a/c56c19cd231a92dc8fffe7e99db5dc6e to your computer and use it in GitHub Desktop.
Serial to I2C bridge for Arduino Uno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// based on https://gist.github.com/maly/3668e069c78b94843812c3140d9fa960 | |
// Read and write I2C protocol (without Bus pirate or other complex tool) | |
// Ideal for Arduino Pro Micro or Leonardo with USB serial | |
// Usage: | |
// 50! : set default address to 0x50 | |
// .1213 : write 0x12 0x13 bytes to default address | |
// 60.a1 : write 0xa1 to address 0x60 | |
// ?03 : read three bytes from default address | |
// .10?03 : write 0x10 byte to default address and read three bytes | |
// Notes: | |
// Addresses are 7-bit ones (without read/write bit) | |
// Lengths and addresses are octets (always two hex digits) | |
#include <Wire.h> | |
char inputString[200]; | |
uint8_t ipos = 0; | |
boolean stringComplete = false; | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(); | |
ipos = 0; | |
} | |
int parseHex(char *g){ | |
uint8_t a,b; | |
a = g[0]; | |
b = g[1]; | |
if (a>'f') return -1; | |
if (a<'0') return -1; | |
if (a>'9' && a<'A') return -1; | |
if (a>'F') a-=32; //abcdef -> ABCDEF | |
a = a-'0'; | |
if (a>9) a-=7; | |
if (a>15) return -1; | |
if (a<0 || a>15) return -1; | |
if (b>'f') return -1; | |
if (b<'0') return -1; | |
if (b>'9' && b<'A') return -1; | |
if (b>'F') b-=32; //abcdef -> ABCDEF | |
b = b-'0'; | |
if (b>9) b-=7; | |
if (b>15) return -1; | |
if (b<0 || b>15) return -1; | |
return a*16+b; | |
} | |
char output[3]; | |
char *toHex(int i){ | |
output[0] = i/16; | |
output[1] = i%16; | |
output[0] += '0'; | |
output[1] += '0'; | |
if (output[0]>'9') output[0] += 7; | |
if (output[1]>'9') output[1] += 7; | |
output[2] = 0; | |
return output; | |
} | |
int defaddr = 0; | |
void loop() { | |
int addr; | |
int val; | |
int xpos; | |
int len; | |
if (stringComplete) { | |
//Serial.print("Go: "); | |
xpos = 2; | |
addr = parseHex(inputString); | |
//Serial.println(toHex(addr)); | |
if (addr == -1) { | |
xpos = 0; | |
addr = defaddr; | |
} | |
switch (inputString[xpos++]){ | |
//commands | |
case '!': | |
//set addr | |
defaddr = addr; | |
Serial.print("Default address set to "); | |
Serial.println(toHex(addr)); | |
break; | |
case '.': | |
Serial.print("Write to "); | |
Serial.print(toHex(addr)); | |
Serial.print(":"); | |
Wire.beginTransmission(addr); | |
while(1) { | |
val = parseHex(inputString + xpos); | |
xpos += 2; | |
if (val==-1) { | |
if (inputString[xpos-2]=='?') { | |
Serial.print(" ? "); | |
xpos--; | |
len = parseHex(inputString + xpos); | |
if (len==-1) { | |
Wire.endTransmission(); | |
newString();return; | |
} | |
Wire.requestFrom(addr,len); | |
while(len--) { | |
val = Wire.read(); | |
Serial.print(toHex(val)); | |
Serial.print(" "); | |
} | |
} | |
Serial.println("*"); | |
Wire.endTransmission(); | |
newString();return; | |
} | |
Wire.write(val); | |
Serial.print(toHex(val)); | |
} | |
break; | |
case '?': | |
len = parseHex(inputString + xpos); | |
if (len==-1) { | |
newString();return; | |
} | |
Serial.print("Read "); | |
Serial.print(len); | |
Serial.print(" bytes from "); | |
Serial.print(toHex(addr)); | |
Serial.print(":"); | |
Wire.requestFrom(addr,len); | |
while(Wire.available()) { | |
val = Wire.read(); | |
Serial.print(" "); | |
Serial.print(toHex(val)); | |
} | |
Serial.println(); | |
newString();return; | |
} | |
// clear the string: | |
newString(); | |
} | |
serialEvent(); | |
} | |
void newString(void) { | |
stringComplete = false; | |
ipos = 0; | |
} | |
//--------------- | |
void serialEvent() { | |
while (Serial.available()) { | |
char inChar = (char)Serial.read(); | |
Serial.write(inChar); | |
if (inChar == '\n') continue; | |
if (ipos==199) return; | |
inputString[ipos++] = inChar; | |
inputString[ipos] = 0; | |
if (inChar == '\r') { | |
Serial.println(); | |
stringComplete = true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment