added 'inverted' in boards config

This commit is contained in:
Paolo Asperti 2018-02-25 18:03:14 +01:00
parent 73121c0ad1
commit 1093b2283d

32
openpdu
View File

@ -12,8 +12,8 @@ import ConfigParser
import json as JSON import json as JSON
VERSION = '0.1.1' VERSION = '0.1.1'
boardsDefaults = {} boardsDefaults = {'inverted':False}
outletsDefaults = {'description': 'Generic outlet','startpower':0} outletsDefaults = {'description': 'Generic outlet','startpower':False}
_boards = [] _boards = []
_outlets = [] _outlets = []
@ -108,7 +108,7 @@ class BoardI2COut(object):
next_refresh = 0 next_refresh = 0
lifetime_sec = 2 lifetime_sec = 2
def __init__(self, boardnum, channels=None, address=None, bus=None): def __init__(self, boardnum, channels=None, address=None, bus=None, inverted=False):
self.boardnum = boardnum self.boardnum = boardnum
if channels is None: if channels is None:
self.channels = 0 self.channels = 0
@ -122,6 +122,7 @@ class BoardI2COut(object):
self.bus = 1 self.bus = 1
else: else:
self.bus = bus self.bus = bus
self.inverted = inverted
if not glob.glob('/dev/i2c*'): if not glob.glob('/dev/i2c*'):
raise OSError('Cannot access I2C. Please ensure I2C is enabled') raise OSError('Cannot access I2C. Please ensure I2C is enabled')
@ -135,6 +136,8 @@ class BoardI2COut(object):
old_data = data = self.getdata() old_data = data = self.getdata()
mask = 1 << channel mask = 1 << channel
data &= ~mask data &= ~mask
if self.inverted:
power = not power
if power: if power:
data |= mask data |= mask
if old_data != data: if old_data != data:
@ -144,7 +147,8 @@ class BoardI2COut(object):
def getpower(self, channel): def getpower(self, channel):
data = self.getdata() data = self.getdata()
d = ( data >> channel ) & 1 d = ( data >> channel ) & 1
return d == 1 c = 0 if self.inverted else 1
return d == c
def getdata(self): def getdata(self):
now = time.time() now = time.time()
@ -160,12 +164,13 @@ class BoardI2COut(object):
class BoardGpioOut(object): class BoardGpioOut(object):
gpios = [] gpios = []
def __init__(self, boardnum, channels=None, gpios=None): def __init__(self, boardnum, channels=None, gpios=None, inverted=False):
self.boardnum = boardnum self.boardnum = boardnum
if channels is None: if channels is None:
self.channels = 0 self.channels = 0
else: else:
self.channels = int(channels) self.channels = int(channels)
self.inverted = inverted
if not isinstance(gpios, list): if not isinstance(gpios, list):
print 'No gpios specified for gpio board %s' % self.boardnum print 'No gpios specified for gpio board %s' % self.boardnum
if len(gpios) != self.channels: if len(gpios) != self.channels:
@ -182,14 +187,20 @@ class BoardGpioOut(object):
io = self.gpios[channel] io = self.gpios[channel]
fn = '/sys/class/gpio/gpio%s/value' % io fn = '/sys/class/gpio/gpio%s/value' % io
f = open(fn,'w') f = open(fn,'w')
out = '0' if power else '1' if self.inverted:
power = not power
out = '1' if power else '0'
return f.write(out) return f.write(out)
def getpower(self, channel): def getpower(self, channel):
io = self.gpios[channel] io = self.gpios[channel]
fn = '/sys/class/gpio/gpio%s/value' % io fn = '/sys/class/gpio/gpio%s/value' % io
f = open(fn,'r') f = open(fn,'r')
return int(f.read()) == 0 e = f.read()
power = int('0'+e) == 1
if self.inverted:
power = not power
return power
def init(self): def init(self):
for gpio in self.gpios: for gpio in self.gpios:
@ -250,18 +261,19 @@ for s in boardsConfigParser.sections():
if re.match('^board.*',s): if re.match('^board.*',s):
bType = boardsConfigParser.get(s, 'type') bType = boardsConfigParser.get(s, 'type')
num = int(re.sub(r'^board','',s)) num = int(re.sub(r'^board','',s))
inverted = int('0' + boardsConfigParser.get(s, 'inverted')) == 1
if bType=='gpio-out': if bType=='gpio-out':
channels = int(boardsConfigParser.get(s, 'channels')) channels = int(boardsConfigParser.get(s, 'channels'))
gpios = [] gpios = []
for g in range(0,channels): for g in range(0,channels):
gpios.append(int(boardsConfigParser.get(s, 'channel%s' % g))) gpios.append(int(boardsConfigParser.get(s, 'channel%s' % g)))
b = BoardGpioOut(boardnum=num, channels=channels, gpios=gpios) b = BoardGpioOut(boardnum=num, channels=channels, gpios=gpios, inverted=inverted)
_boards.append(b) _boards.append(b)
elif bType=='i2c-out': elif bType=='i2c-out':
channels = int(boardsConfigParser.get(s, 'channels')) channels = int(boardsConfigParser.get(s, 'channels'))
address = boardsConfigParser.get(s, 'address') address = boardsConfigParser.get(s, 'address')
bus = boardsConfigParser.get(s, 'bus') bus = boardsConfigParser.get(s, 'bus')
b = BoardI2COut(boardnum=num, channels=channels, address=address, bus=bus) b = BoardI2COut(boardnum=num, channels=channels, address=address, bus=bus, inverted=inverted)
_boards.append(b) _boards.append(b)
outletsConfigParser = ConfigParser.SafeConfigParser(defaults=outletsDefaults) outletsConfigParser = ConfigParser.SafeConfigParser(defaults=outletsDefaults)
@ -274,7 +286,7 @@ for s in outletsConfigParser.sections():
description = outletsConfigParser.get(s, 'description') description = outletsConfigParser.get(s, 'description')
boardnum = outletsConfigParser.get(s, 'board') boardnum = outletsConfigParser.get(s, 'board')
channel = outletsConfigParser.get(s, 'channel') channel = outletsConfigParser.get(s, 'channel')
startpower = outletsConfigParser.get(s, 'startpower') == 1 startpower = int('0' + outletsConfigParser.get(s, 'startpower')) == 1
o = Outlet(outletnum=num, boardnum=boardnum, channel=channel, startpower=startpower) o = Outlet(outletnum=num, boardnum=boardnum, channel=channel, startpower=startpower)
o.description = description o.description = description
_outlets.append(o) _outlets.append(o)