Skip to content

Instantly share code, notes, and snippets.

@onny
Created July 14, 2015 12:28
Show Gist options
  • Save onny/c059c84917362e132baa to your computer and use it in GitHub Desktop.
Save onny/c059c84917362e132baa to your computer and use it in GitHub Desktop.
I2C Buspirate Python3
from I2C import *
import time
timestring = time.strftime("%Y-%m-%d %H:%M:%S")
i2caddr = 0x0B # Slave address of battery controller
def get_word(i2c, i2caddr, addr):
""" Reads two byte value (big-endian) from address addr """
i2c.send_start_bit();
stat = i2c.bulk_trans(2, [i2caddr<<1, addr]);
i2c.send_start_bit();
stat += i2c.bulk_trans(1, [i2caddr<<1 | 1]);
rh = i2c.read_byte();
i2c.send_ack();
rl = i2c.read_byte();
i2c.send_nack();
i2c.send_stop_bit();
#if stat.find(chr(0x01)) != -1:
#raise IOError, "I2C command on address 0x%02x not acknowledged!"%(i2caddr);
return ord(rl)*256+ord(rh);
i2c = I2C("/dev/buspirate", 115200)
if i2c.BBmode():
pass
else:
fatalError("Can't set binmode on Buspirate!")
if i2c.enter_I2C():
pass
else:
fatalError("Can't set raw mode on Buspirate!")
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS):
fatalError("Failed to set I2C peripherals on BusPirate!")
if not i2c.set_speed(I2CSpeed._50KHZ):
fatalError("Can't set I2C speed on Buspirate!")
temp = float(get_word(i2c, i2caddr, 0x08) * 0.1 -273.2)
desVolt = float(get_word(i2c, i2caddr, 0x19) * 0.001)
volt = float(get_word(i2c, i2caddr, 0x09) * 0.001)
current = float(get_word(i2c, i2caddr, 0x0a) * 0.001)
avgCurrent = float(get_word(i2c, i2caddr, 0x0b) * 0.001)
maxError = int(get_word(i2c, i2caddr, 0x0c))
relChg = int(get_word(i2c, i2caddr, 0x0d))
absChg = int(get_word(i2c, i2caddr, 0x0e))
remCap = float(get_word(i2c, i2caddr, 0x0f) *0.001)
fullCap = float(get_word(i2c, i2caddr, 0x10) *0.001)
desCap = float(get_word(i2c, i2caddr, 0x18) *0.001)
cycles = int(get_word(i2c, i2caddr, 0x17))
serNr = int(get_word(i2c, i2caddr, 0x1c))
#date calc
date = get_word(i2c, i2caddr, 0x1b)
year = int(((date & 0b1111111000000000) >> 9) + 1980)
month = int(((date & 0b0000000111100000) >> 5))
day = int(((date & 0b0000000000011111) >> 0))
print ("Current Time: ", timestring)
print ("Temperature: ", temp, " Celsius")
print ("")
print ("DesignVoltage: ", desVolt, " V")
print ("Voltage: ", volt, " V")
print ("")
print ("Current: ", current, " A")
print ("AverageCurrent: ", avgCurrent, " A")
print ("")
print ("MaxError: ", maxError, " %")
print ("RelCharge: ", relChg, " %")
print ("AbsCharge: ", absChg, " %")
print ("")
print ("RemainingCap: ", remCap, " Ah")
print ("FullChargeCap: ", fullCap, " Ah")
print ("DesignCap: ", desCap, " Ah")
print ("CycleCount: ", cycles, " ")
print ("")
print ("SerialNR: ", serNr, " ")
print ("ProdDate: ", year, month, day)
@onny
Copy link
Author

onny commented Jul 14, 2015

Thats the output:

onny@onny ~/projects/Buspirate-python3-i2c (git)-[master] % python test.py
Current Time:    2015-07-14 12:23:37
Temperature:     26.30000000000001  Celsius

DesignVoltage:   11.1  V
Voltage:         11.259  V

Current:         65.402  A
AverageCurrent:  65.402  A

MaxError:        26  %
RelCharge:       60  %
AbsCharge:       48  %

RemainingCap:    3.4210000000000003  Ah
FullChargeCap:   5.727  Ah
DesignCap:       7.2  Ah
CycleCount:      60  

SerialNR:        1168  
ProdDate:        2010 1 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment