Properly raise exceptions
authorDan White <dan@whiteaudio.com>
Fri, 2 Nov 2012 23:39:09 +0000 (18:39 -0500)
committerDan White <dan@whiteaudio.com>
Fri, 2 Nov 2012 23:39:09 +0000 (18:39 -0500)
python-lib/usbio.py

index e088fce9fcecf14b7d93dd4f4794b3713ad78b11..3b8d28be87289982a9171b46159bd7be2eb2fbc1 100644 (file)
@@ -83,6 +83,17 @@ def int2str(value, width):
 def str2hex(x):
     return ''.join(map(lambda h: '%02x' % ord(h), x))
 
+
+class FTDI_Error(Exception):
+    pass
+
+class I2C_Error(Exception):
+    pass
+
+class SPI_Error(Exception):
+    pass
+
+
 class I2C(object):
     def __init__(self, interface=ftdi.INTERFACE_A, scl=0, sda=1,
                     vid=0x0403, pid=0x6011, delay=0.001, timeout=100,
@@ -118,7 +129,7 @@ class I2C(object):
 
         e = self.ftdi.usb_open(self.context, vid, pid)
         if e != 0:
-            raise Exception, 'Could not open usb device, error: %i' % e
+            raise FTDI_Error('Could not open usb device, error: %i' % e)
 
         self.ftdi.set_baudrate(self.context, 1000)
         self.ftdi.usb_purge_buffers(self.context)
@@ -167,7 +178,7 @@ class I2C(object):
             sleep(self.DELAY)
             i += 1
             if i > self.TIMEOUT:
-                raise Exception, "Timeout waiting for SCL to go high."
+                raise I2C_Error("Timeout waiting for SCL to go high.")
 
     def _start(self):
         if self.started:
@@ -176,7 +187,7 @@ class I2C(object):
             self._clock_stretch()
             sleep(self.DELAY)
         if self._read_sda() == 0:
-            raise Exception, "Lost bus arbitration."
+            raise I2C_Error("Lost bus arbitration.")
         self._clear_sda()
         sleep(self.DELAY)
         self._clear_scl()
@@ -190,7 +201,7 @@ class I2C(object):
 
         sleep(self.DELAY)
         if self._read_sda() == 0:
-            raise Exception, "Lost bus arbitration."
+            raise I2C_Error("Lost bus arbitration.")
         sleep(self.DELAY)
         self.started = False
 
@@ -202,7 +213,7 @@ class I2C(object):
         sleep(self.DELAY)
         self._clock_stretch()
         if bit and (self._read_sda() == 0):
-            raise Exception, "Lost bus arbitration."
+            raise I2C_Error("Lost bus arbitration.")
         sleep(self.DELAY)
         self._clear_scl()
 
@@ -378,7 +389,7 @@ class SPI(object):
         # source
         e = self.ftdi.usb_open(self.context, vid, pid)
         if e != 0:
-            raise Exception, self.ftdi.get_error_string(self.context)
+            raise FTDI_Error(self.ftdi.get_error_string(self.context))
 
         self.ftdi.set_latency_timer(self.context, max(1, min(255, latency)))
         self.ftdi.set_bitmode(self.context, int(pindir), self.ftdi.BITMODE_RESET)
@@ -402,7 +413,7 @@ class SPI(object):
             data = ''.join(map(chr, data))
         ret = self.ftdi.write_data(self.context, data, len(data))
         if ret < 0:
-            raise Exception, self.ftdi.get_error_string(self.context)
+            raise FTDI_Error(self.ftdi.get_error_string(self.context))
 
     def _raw_read(self, n):
         """Read n bytes from buffer."""
@@ -411,7 +422,7 @@ class SPI(object):
         while len(r) < n:
             ret, s = self.ftdi.read_data(self.context, n)
             if ret < 0:
-                raise Exception, self.ftdi.get_error_string(self.context)
+                raise FTDI_Error(self.ftdi.get_error_string(self.context))
             r += s
         #NOTE: including ftdi.SEND_IMMEDIATE commands make this redundant
         self.ftdi.usb_purge_rx_buffer(self.context)
@@ -1380,6 +1391,9 @@ class DAC8568(object):
         self._word += ((value & 0xf) << 0)#intbv(value, max=2**4)
         
 
+class ADS8201_Error(Exception):
+    pass
+
 class ADS8201(object):
     """Configures and operates an ADS8201 over the given SPI bus."""
     # register addresses
@@ -1447,7 +1461,7 @@ class ADS8201(object):
         if g in (1, 2, 4, 8):
             g = int(log2(g))
         else:
-            raise Exception, 'Gain is only (1, 2, 4, 8), you tried %i' % g
+            raise ADS8201_Error('Gain is only (1, 2, 4, 8), you tried %i' % g)
 
         if (channel % 2) == 0:
             self._ch[channel][2:0] = g
@@ -1463,10 +1477,10 @@ class ADS8201(object):
     def channelMode(self, channel, mode):
         """Setup channel (pair) to SE or differential even/odd modes."""
         if mode not in (0, 1, 3):
-            raise Exception, 'Invalid mode: %i' % mode
+            raise ADS8201_Error('Invalid mode: %i' % mode)
 
         if ((channel % 2) == 1):
-            raise Exception, 'Cannot set mode on odd channel: %i' % channel
+            raise ADS8201_Error('Cannot set mode on odd channel: %i' % channel)
 
         self._ch[channel][4:2] = mode
         self.setRegister(int(channel/2), self._ch[channel])
@@ -1483,7 +1497,7 @@ class ADS8201(object):
     #
     def average(self, results, accurate=True):
         if results not in (0, 1, 4, 8, 16):
-            raise Exception, 'Invalid average number: %i, must be 0, 4, 8, 16' % results
+            raise ADS8201_Error('Invalid average number: %i, must be 0, 4, 8, 16' % results)
         v = 0
         if accurate:
             v += 0b100
@@ -1529,7 +1543,7 @@ class ADS8201(object):
     INT_ADC_DATA_READY = (1 << 0)
     def interruptMode(self, mode):
         if mode not in range(16):
-            raise Exception, 'Invalid interrupt mode: %i' % mode
+            raise ADS8201_Error('Invalid interrupt mode: %i' % mode)
         self._INT_SCR[4:0] = mode
         self.setRegister(self.INT_SCR, self._INT_SCR)
 
@@ -1547,7 +1561,7 @@ class ADS8201(object):
         """Set trigger operating mode.  NB: 0 (idle) is required for setting
         most config registers."""
         if mode in (1, 7):
-            raise Exception, 'Invalid trigger mode: %i' % mode
+            raise ADS8201_Error('Invalid trigger mode: %i' % mode)
         self._TRIGGER_SCR = intbv(mode, max=2**8)
         self.setRegister(self.TRIGGER_SCR, self._TRIGGER_SCR)
 
@@ -1591,6 +1605,9 @@ class ADS8201(object):
         return r
 
 
+class M25PExx_Error(Exception):
+    pass
+
 class M25PExx(object):
     def __init__(self, spibus, csname):
         self.bus = spibus
@@ -1643,7 +1660,7 @@ class M25PExx(object):
 
     def writePage(self, addr, data):
         if ((addr & 0xff) + len(data)) > 256:
-            raise Exception, 'Too much data: %i' % len(data)
+            raise M25PExx_Error('Too much data: %i' % len(data))
         a = int2str(addr, 3*8)
         while not self.isReady():
             pass
@@ -1651,7 +1668,7 @@ class M25PExx(object):
 
     def programPage(self, addr, data):
         if ((addr & 0xff) + len(data)) > 256:
-            raise Exception, 'Too much data: %i' % len(data)
+            raise M25PExx_Error('Too much data: %i' % len(data))
         a = int2str(addr, 3*8)
         while not self.isReady():
             pass
@@ -1659,7 +1676,7 @@ class M25PExx(object):
 
     def erasePage(self, addr):
         if (addr & 0xff) != 0:
-            raise Exception, 'Address must be a page boundary: %i' % addr
+            raise M25PExx_Error('Address must be a page boundary: %i' % addr)
         while not self.isReady():
             pass
         self._write('\xdb' + a)
@@ -1680,7 +1697,7 @@ class M25PExx(object):
 
     def write(self, addr, data):
         if addr & 0xff != 0:
-            raise Exception, 'Address must start on a page boundary (0x....00).'
+            raise M25PExx_Error('Address must start on a page boundary (0x....00).')
 
         n = len(data)
         nPages = (n // 256) + 1
@@ -1698,7 +1715,7 @@ class M25PExx(object):
 
     def program(self, addr, data):
         if addr & 0xff != 0:
-            raise Exception, 'Address must start on a page boundary (0x....00).'
+            raise M25PExx_Error('Address must start on a page boundary (0x....00).')
 
         n = len(data)
         nPages = (n // 256) + 1