39 lines
1.0 KiB
Python
Executable File
39 lines
1.0 KiB
Python
Executable File
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
|
|
|