preliminary I2C support

This commit is contained in:
Paolo Asperti 2018-02-18 18:57:33 +01:00
parent efa877eba4
commit 4da032952a
2 changed files with 31 additions and 5 deletions

View File

@ -7,7 +7,7 @@ pkgdesc="OpenPDU project - main binary"
url="https://github.com/openpdu/openpdu" url="https://github.com/openpdu/openpdu"
arch="noarch" arch="noarch"
license="GPL2" license="GPL2"
depends="python py-argh apk-cron" depends="python py-argh apk-cron i2c-tools"
makedepends="" makedepends=""
install="openpdu.post-install" install="openpdu.post-install"
subpackages="" subpackages=""

34
openpdu
View File

@ -4,7 +4,9 @@ import os
import sys import sys
import argh import argh
from argh import arg from argh import arg
from subprocess import call
import re import re
import glob
import ConfigParser import ConfigParser
import json as JSON import json as JSON
@ -30,6 +32,14 @@ def boards(json=False):
b += board.toSTR() b += board.toSTR()
return '\nBoards:\n' + b return '\nBoards:\n' + b
def initboards():
'initialize boards'
for board in _boards:
board.init()
return
@arg('-j', '--json', help="output in json") @arg('-j', '--json', help="output in json")
def outlets(json=False): def outlets(json=False):
'dump outlets configuration' 'dump outlets configuration'
@ -85,7 +95,7 @@ def getpower(outlet, json=False):
# MCP23008
class BoardI2COut(object): class BoardI2COut(object):
def __init__(self, boardnum, channels=None, address=None, bus=None): def __init__(self, boardnum, channels=None, address=None, bus=None):
self.boardnum = boardnum self.boardnum = boardnum
@ -111,10 +121,19 @@ class BoardI2COut(object):
return ' Board %s\n Type: i2c-out\n Channels: %s\n Address: %s\n\n' % (self.boardnum,self.channels,self.address) return ' Board %s\n Type: i2c-out\n Channels: %s\n Address: %s\n\n' % (self.boardnum,self.channels,self.address)
def setpower(self, channel, power): def setpower(self, channel, power):
return d = self.getpower(channel)
mask = 1 << channel
d &= ~mask
if power:
d |= mask
#i2cset -y 1 0x20 0x09 0xFF
return os.popen("/usr/sbin/i2cset -y %s %s 0x09 0x%s" % (self.bus, self.address, format(d, '02x'))).read()
def getpower(self, channel): def getpower(self, channel):
return False return os.popen("/usr/sbin/i2cget -y %s %s 0x09" % (self.bus, self.address)).read()
def init(self):
call(["/usr/sbin/i2cset", "-y", self.bus, self.address, "0x00", "0x00"])
class BoardGpioOut(object): class BoardGpioOut(object):
@ -151,6 +170,13 @@ class BoardGpioOut(object):
f = open(fn,'r') f = open(fn,'r')
return int(f.read()) == 0 return int(f.read()) == 0
def init(self):
for gpio in self.gpios:
open('/sys/class/gpio/export','w').write(str(gpio))
fn = '/sys/class/gpio/gpio%s/direction' % gpio
open(fn,'w').write('out')
return
class Outlet(object): class Outlet(object):
def __init__(self, outletnum, boardnum, channel): def __init__(self, outletnum, boardnum, channel):
@ -221,7 +247,7 @@ for s in configParser.sections():
_outlets.append(o) _outlets.append(o)
parser = argh.ArghParser() parser = argh.ArghParser()
parser.add_commands([setpower, getpower, outlets, boards, version]) parser.add_commands([setpower, getpower, outlets, boards, initboards, version])
# dispatching: # dispatching: