Commit 9992b4f6 authored by onny's avatar onny

first commit

parents
# Written by Jonas Heinrich <onny@project-insanity.org>
# Credits goes to armageddon421 <http://armageddon421.de/> for original research
# Using BusPirate v4 from DangerousPrototypes <http://dangerousprototypes.com/>
# Requires BusPirate Python 3 library by Steve Rodgers <https://github.com/hwstar/Buspirate-python3-i2c>
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment