Created
July 14, 2015 12:28
-
-
Save onny/c059c84917362e132baa to your computer and use it in GitHub Desktop.
I2C Buspirate Python3
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thats the output: