Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
bus.py
Go to the documentation of this file.
1 # Copyright 2012-2013 Eric Ptak - trouch.com
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import os
16 import time
17 import subprocess
18 
19 from webiopi.utils.logger import debug, info
20 
21 BUSLIST = {
22  "I2C": {"enabled": False, "gpio": {0:"SDA", 1:"SCL", 2:"SDA", 3:"SCL"}, "modules": ["i2c-bcm2708", "i2c-dev"]},
23  "SPI": {"enabled": False, "gpio": {7:"CE1", 8:"CE0", 9:"MISO", 10:"MOSI", 11:"SCLK"}, "modules": ["spi-bcm2708", "spidev"]},
24  "UART": {"enabled": False, "gpio": {14:"TX", 15:"RX"}},
25  "ONEWIRE": {"enabled": False, "gpio": {4:"DATA"}, "modules": ["w1-gpio"], "wait": 2}
26 }
27 
28 def loadModule(module):
29  debug("Loading module : %s" % module)
30  subprocess.call(["modprobe", module])
31 
32 def unloadModule(module):
33  subprocess.call(["modprobe", "-r", module])
34 
35 def loadModules(bus):
36  if BUSLIST[bus]["enabled"] == False and not modulesLoaded(bus):
37  info("Loading %s modules" % bus)
38  for module in BUSLIST[bus]["modules"]:
39  loadModule(module)
40  if "wait" in BUSLIST[bus]:
41  info("Sleeping %ds to let %s modules load" % (BUSLIST[bus]["wait"], bus))
42  time.sleep(BUSLIST[bus]["wait"])
43 
44  BUSLIST[bus]["enabled"] = True
45 
46 def unloadModules(bus):
47  info("Unloading %s modules" % bus)
48  for module in BUSLIST[bus]["modules"]:
49  unloadModule(module)
50  BUSLIST[bus]["enabled"] = False
51 
52 def __modulesLoaded__(modules, lines):
53  if len(modules) == 0:
54  return True
55  for line in lines:
56  if modules[0].replace("-", "_") == line.split(" ")[0]:
57  return __modulesLoaded__(modules[1:], lines)
58  return False
59 
60 def modulesLoaded(bus):
61  if not bus in BUSLIST or not "modules" in BUSLIST[bus]:
62  return True
63 
64  try:
65  with open("/proc/modules") as f:
66  c = f.read()
67  f.close()
68  lines = c.split("\n")
69  return __modulesLoaded__(BUSLIST[bus]["modules"], lines)
70  except:
71  return False
72 
74  for bus in BUSLIST:
75  if modulesLoaded(bus):
76  BUSLIST[bus]["enabled"] = True
77 
78 class Bus():
79  def __init__(self, busName, device, flag=os.O_RDWR):
80  loadModules(busName)
81  self.busName = busName
82  self.device = device
83  self.flag = flag
84  self.fd = 0
85  self.open()
86 
87  def open(self):
88  self.fd = os.open(self.device, self.flag)
89  if self.fd < 0:
90  raise Exception("Cannot open %s" % self.device)
91 
92  def close(self):
93  if self.fd > 0:
94  os.close(self.fd)
95 
96  def read(self, size=1):
97  if self.fd > 0:
98  return os.read(self.fd, size)
99  raise Exception("Device %s not open" % self.device)
100 
101  def readBytes(self, size=1):
102  return bytearray(self.read(size))
103 
104  def readByte(self):
105  return self.readBytes()[0]
106 
107  def write(self, string):
108  if self.fd > 0:
109  return os.write(self.fd, string)
110  raise Exception("Device %s not open" % self.device)
111 
112  def writeBytes(self, data):
113  return self.write(bytearray(data))
114 
115  def writeByte(self, value):
116  self.writeBytes([value])
117 
def unloadModule
Definition: bus.py:32
def modulesLoaded
Definition: bus.py:60
def unloadModules
Definition: bus.py:46
def loadModules
Definition: bus.py:35
def checkAllBus
Definition: bus.py:73
def loadModule
Definition: bus.py:28
def __modulesLoaded__
Definition: bus.py:52