power sensor class and test added
This commit is contained in:
parent
25f163b4cc
commit
241281ef99
38
power_sensor.py
Executable file
38
power_sensor.py
Executable file
@ -0,0 +1,38 @@
|
|||||||
|
import smbus
|
||||||
|
|
||||||
|
class power_sensor:
|
||||||
|
def __init__(self, bus = 1, addr = 0x40):
|
||||||
|
self.bus = smbus.SMBus(bus)
|
||||||
|
self.addr = addr
|
||||||
|
|
||||||
|
bytes = [(0x1000 >> 8) & 0xFF, 0x1000 & 0xFF]
|
||||||
|
self.bus.write_i2c_block_data(self.addr, 0x05, bytes)
|
||||||
|
config = 0x2000 | 0x1800 | 0x0400 | 0x0018 | 0x0007
|
||||||
|
bytes = [(config >> 8) & 0xFF, config & 0xFF]
|
||||||
|
self.bus.write_i2c_block_data(self.addr, 0x00, bytes)
|
||||||
|
|
||||||
|
def shunt_voltage_mv(self):
|
||||||
|
data = self.bus.read_i2c_block_data(self.addr, 0x01)
|
||||||
|
voltage = data[0] * 256 + data[1]
|
||||||
|
return voltage * 0.01
|
||||||
|
|
||||||
|
def current_ma(self):
|
||||||
|
data = self.bus.read_i2c_block_data(self.addr, 0x04)
|
||||||
|
if(data[0] >> 7 == 1):
|
||||||
|
current = data[0] * 256 + data[1]
|
||||||
|
if(current & (1 << 15)):
|
||||||
|
current = current - (1 << 16)
|
||||||
|
else:
|
||||||
|
current = (data[0] << 8) | (data[1])
|
||||||
|
return current / 10
|
||||||
|
|
||||||
|
def power_mw(self):
|
||||||
|
data = self.bus.read_i2c_block_data(self.addr, 0x03)
|
||||||
|
if(data[0] >> 7 == 1):
|
||||||
|
power = data[0] * 256 + data[1]
|
||||||
|
if(power & (1 << 15)):
|
||||||
|
power = power - (1 << 16)
|
||||||
|
else:
|
||||||
|
power = (data[0] << 8) | (data[1])
|
||||||
|
return power / 2
|
||||||
|
|
14
power_sensor_test.py
Executable file
14
power_sensor_test.py
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/python2
|
||||||
|
from power_sensor import power_sensor
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
ps = power_sensor(1, 0x40)
|
||||||
|
while True:
|
||||||
|
voltage = ps.shunt_voltage_mv()
|
||||||
|
print str(voltage) + " mV"
|
||||||
|
current = ps.current_ma()
|
||||||
|
print str(current) + " mA"
|
||||||
|
power = ps.power_mw()
|
||||||
|
print str(power) + " mW"
|
||||||
|
sleep(1)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user