Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save initialed85/478a5ef78fd476d85d1620a2a268518c to your computer and use it in GitHub Desktop.
Save initialed85/478a5ef78fd476d85d1620a2a268518c to your computer and use it in GitHub Desktop.
SMBus Read Byte protocol w/ Synapse Wireless Snappy
"""
Synapse Wireless RF200 devices use some terribly hobbled version of Python2 coupled with a terribly limited library.
They are in general terrible devices.
Anyway- to read SMBus (a protocol layer on top of i2c- see Page 41 of 85 of http://smbus.org/specs/SMBus_3_0_20141220.pdf) the process is as follows:
- write 2 bytes to i2c
- byte 0 = slave address shifted left by 1 bit, write bit of 0
- byte 1 = command
- send restart bit
- write 1 byte to i2c
- byte 0 = slave address shifted left by 1 bit, read bit of 1
- read n bytes from i2c (in our case, 2)
"""
#
# mocked out functions so you can test this on a desktop Python implementation
#
def i2cWrite(byteStr, retries, ignoreFirstAck, endWithRestart=False):
return len(byteStr)
def i2cRead(byteStr, numToRead, retries, ignoreFirstAck):
return chr(0x00) * numToRead
#
# stuff to actually paste into your Portal sketch
#
def get_address_byte(address, read):
return chr((address << 1) + (0x01 if read else 0x00))
def smbus_read_word(address, command, length):
write_address = get_address_byte(address, False) + chr(command)
read_address = get_address_byte(address, True)
# critical that last argument (endWithRestart) is True
_ = i2cWrite(write_address, 1, True, True)
return i2cRead(read_address, length, 1, True)
#
# example usage
#
data = smbus_read_word(0x68, 0x3B, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment