Repository: ManiacalLabs/BiblioPixel
Pull request #252 (2017-06-27 17:00:32)

Consolidated LEDTYPE for Serial and SPI
Change `type` to `ledtype`


Patch:
diff --git a/bibliopixel/drivers/SPI/__init__.py b/bibliopixel/drivers/SPI/__init__.py
index a3a50bf..a9b55f6 100644
--- a/bibliopixel/drivers/SPI/__init__.py
+++ b/bibliopixel/drivers/SPI/__init__.py
@@ -1,2 +1,2 @@
-from . driver import SPI, SPI_LEDTYPE
+from . driver import SPI, LEDTYPE
 from . interfaces import SPI_INTERFACES
diff --git a/bibliopixel/drivers/SPI/driver.py b/bibliopixel/drivers/SPI/driver.py
index 5bc0bc2..6df79eb 100644
--- a/bibliopixel/drivers/SPI/driver.py
+++ b/bibliopixel/drivers/SPI/driver.py
@@ -3,6 +3,7 @@ from .. driver_base import DriverBase
 from ... util.enum import resolve_enum
 from . import interfaces
 from enum import IntEnum
+from .. ledtype import LEDTYPE
 # SubDriver imports
 from . APA102 import APA102
 from . LPD8806 import LPD8806
@@ -10,30 +11,20 @@ from . WS281X import WS281X
 from . WS2801 import WS2801
 
 
-class SPI_LEDTYPE(IntEnum):
-    APA102 = 0
-    DOTSTAR = 0
-    SK9822 = 0
-    WS2801 = 1
-    WS281X = 2
-    WS2812 = 2
-    WS2812B = 2
-    NEOPIXEL = 2
-    LPD8806 = 3
+# This may look weird, but was done to keep the IDs the same
+# as Serial so that ledtype can always be resolved the same
+SPI_DRIVERS = {
+    1: LPD8806,
+    2: WS2801,
+    3: WS281X,
+    9: APA102
+}
 
 
-SPI_DRIVERS = [
-    APA102,
-    WS2801,
-    WS281X,
-    LPD8806
-]
-
-
-def SPI(type, num, **kwargs):
+def SPI(ledtype, num, **kwargs):
     """Wrapper function for using SPI device drivers on systems like the Raspberry Pi and BeagleBone"""
-    ledtype = resolve_enum(SPI_LEDTYPE, type)
-    if ledtype >= len(SPI_DRIVERS):
+    ledtype = resolve_enum(LEDTYPE, ledtype)
+    if ledtype not in SPI_DRIVERS.keys():
         raise ValueError('{} is not a valid LED type.'.format(type))
 
     return SPI_DRIVERS[ledtype](num, **kwargs)
diff --git a/bibliopixel/drivers/ledtype.py b/bibliopixel/drivers/ledtype.py
new file mode 100644
index 0000000..aa4d72f
--- /dev/null
+++ b/bibliopixel/drivers/ledtype.py
@@ -0,0 +1,25 @@
+from enum import IntEnum
+
+
+class LEDTYPE(IntEnum):
+    GENERIC = 0  # Use if the serial device only supports one chipset
+    LPD8806 = 1
+    WS2801 = 2
+    # These are all the same
+    WS2811 = 3
+    WS2812 = 3
+    WS2812B = 3
+    NEOPIXEL = 3
+    APA104 = 3
+    # 400khz variant of above
+    WS2811_400 = 4
+
+    TM1809 = 5
+    TM1804 = 5
+    TM1803 = 6
+    UCS1903 = 7
+    SM16716 = 8
+    APA102 = 9
+    SK9822 = 9
+    LPD1886 = 10
+    P9813 = 11
diff --git a/bibliopixel/drivers/serial/codes.py b/bibliopixel/drivers/serial/codes.py
index ef0ff27..9d3e69a 100644
--- a/bibliopixel/drivers/serial/codes.py
+++ b/bibliopixel/drivers/serial/codes.py
@@ -1,4 +1,5 @@
 from enum import IntEnum
+from .. ledtype import LEDTYPE
 
 
 class CMDTYPE(IntEnum):
@@ -11,29 +12,6 @@ class CMDTYPE(IntEnum):
     SYNC = 7
 
 
-class LEDTYPE(IntEnum):
-    GENERIC = 0  # Use if the serial device only supports one chipset
-    LPD8806 = 1
-    WS2801 = 2
-    # These are all the same
-    WS2811 = 3
-    WS2812 = 3
-    WS2812B = 3
-    NEOPIXEL = 3
-    APA104 = 3
-    # 400khz variant of above
-    WS2811_400 = 4
-
-    TM1809 = 5
-    TM1804 = 5
-    TM1803 = 6
-    UCS1903 = 7
-    SM16716 = 8
-    APA102 = 9
-    LPD1886 = 10
-    P9813 = 11
-
-
 SPIChipsets = [
     LEDTYPE.LPD8806,
     LEDTYPE.WS2801,
diff --git a/bibliopixel/drivers/serial/driver.py b/bibliopixel/drivers/serial/driver.py
index 57cc827..ec6c976 100644
--- a/bibliopixel/drivers/serial/driver.py
+++ b/bibliopixel/drivers/serial/driver.py
@@ -12,8 +12,8 @@ from ... return_codes import RETURN_CODES, print_error, BiblioSerialError
 class Serial(DriverBase):
     """Main driver for Serial based LED strips"""
 
-    def __init__(self, type, num, dev="",
-                 c_order=ChannelOrder.RGB, SPISpeed=2,
+    def __init__(self, ledtype, num, dev="",
+                 c_order=ChannelOrder.RGB, spi_speed=2,
                  gamma=None, restart_timeout=3,
                  device_id=None, hardwareID="1D50:60AB",
                  baudrate=921600, **kwds):
@@ -21,12 +21,12 @@ class Serial(DriverBase):
         self.devices = Devices(hardwareID, baudrate)
         self.serial = self.devices.serial
 
-        if SPISpeed < 1 or SPISpeed > 24 or not (type in SPIChipsets):
-            SPISpeed = 1
+        if spi_speed < 1 or spi_speed > 24 or not (type in SPIChipsets):
+            spi_speed = 1
 
-        self._SPISpeed = SPISpeed
+        self._spi_speed = spi_speed
         self._com = None
-        self._type = resolve_enum(LEDTYPE, type)
+        self._type = resolve_enum(LEDTYPE, ledtype)
         self._bufPad = 0
         self.dev = dev
         self.device_version = 0
@@ -51,7 +51,7 @@ class Serial(DriverBase):
             print_error(resp)
 
         if type in SPIChipsets:
-            log.info("Using SPI Speed: %sMHz", self._SPISpeed)
+            log.info("Using SPI Speed: %sMHz", self._spi_speed)
 
         self.set_device_brightness = self.set_brightness
 
@@ -89,7 +89,7 @@ class Serial(DriverBase):
 
             packet.append(byteCount & 0xFF)  # set 1st byte of byteCount
             packet.append(byteCount >> 8)  # set 2nd byte of byteCount
-            packet.append(self._SPISpeed)
+            packet.append(self._spi_speed)
             self._com.write(packet)
 
             resp = self._com.read(1)
diff --git a/bibliopixel/main/run.py b/bibliopixel/main/run.py
index e0e8e0b..b1cf0b5 100644
--- a/bibliopixel/main/run.py
+++ b/bibliopixel/main/run.py
@@ -36,6 +36,7 @@ def get_project_default_arguments(args):
 
 
 def project_to_animation(name, is_json, defaults):
+    log.info('Processing project file...')
     data = name if is_json else files.opener(name).read()
     try:
         desc = json.loads(data)
diff --git a/bibliopixel/project/types/defaults.py b/bibliopixel/project/types/defaults.py
index d297296..f0d695c 100644
--- a/bibliopixel/project/types/defaults.py
+++ b/bibliopixel/project/types/defaults.py
@@ -7,5 +7,5 @@ FIELD_TYPES = {
     'gamma': gamma,
     'ledtype': ledtype,
     'time': duration,
-    'type': ledtype,
+    'ledtype': ledtype,
 }
diff --git a/bibliopixel/project/types/ledtype.py b/bibliopixel/project/types/ledtype.py
index 6ffd1bd..0ef0071 100644
--- a/bibliopixel/project/types/ledtype.py
+++ b/bibliopixel/project/types/ledtype.py
@@ -1,9 +1,9 @@
 import functools
-from ...drivers.serial import codes
+from ...drivers.ledtype import LEDTYPE
 
 USAGE = """An LEDTYPE is represented by a string.
 
-Possible LEDTYPEs are """ + ', '.join(codes.LEDTYPE.__members__)
+Possible LEDTYPEs are """ + ', '.join(LEDTYPE.__members__)
 
 
 @functools.singledispatch
@@ -13,4 +13,4 @@ def make(c):
 
 @make.register(str)
 def _(c):
-    return codes.LEDTYPE[c]
+    return LEDTYPE[c]


Test patch:
diff --git a/test/driver_test.py b/test/driver_test.py
index 5d427b1..7ad2bdc 100644
--- a/test/driver_test.py
+++ b/test/driver_test.py
@@ -48,17 +48,17 @@ class DriverTest(unittest.TestCase):
         self.do_test(driver, expected)
 
     def test_apa102(self):
-        driver = SPI(type='APA102', num=4, **self.SPD)
+        driver = SPI(ledtype='APA102', num=4, **self.SPD)
         expected = [0, 0, 0, 0, 0, 8, 0, 0, 46, 0, 1, 125]
         self.do_test(driver, expected)
 
     def test_lpd8806(self):
-        driver = SPI(type='LPD8806', num=4, **self.SPD)
+        driver = SPI(ledtype='LPD8806', num=4, **self.SPD)
         expected = [
             128, 128, 128, 128, 128, 132, 128, 128, 151, 128, 128, 190, 0]
         self.do_test(driver, expected)
 
     def test_ws2801(self):
-        driver = SPI(type='WS2801', num=4, **self.SPD)
+        driver = SPI(ledtype='WS2801', num=4, **self.SPD)
         expected = [0, 0, 0, 0, 0, 8, 0, 0, 45, 0, 0, 125]
         self.do_test(driver, expected)
diff --git a/test/project_test.py b/test/project_test.py
index 7da0d25..26064cc 100644
--- a/test/project_test.py
+++ b/test/project_test.py
@@ -2,7 +2,7 @@ import json, unittest
 
 from bibliopixel.main import run
 from bibliopixel import gamma
-from bibliopixel.drivers.serial import codes
+from bibliopixel.drivers.ledtype import LEDTYPE
 from . mark_tests import SKIP_LONG_TESTS
 
 
@@ -31,9 +31,8 @@ PROJECT_TYPES = """
         "color": "green",
         "duration": "1 hour, 2 minutes",
         "gamma": "APA102",
-        "ledtype": "WS2812",
         "time": "35ks",
-        "type": "GENERIC"
+        "ledtype": "GENERIC"
     },
 
     "layout": "strip",
@@ -129,9 +128,8 @@ class ProjectTest(unittest.TestCase):
         self.assertEquals(kwds['color'], (0, 255, 0))
         self.assertEquals(kwds['duration'], 3720)
         self.assertEquals(kwds['gamma'], gamma.APA102)
-        self.assertEquals(kwds['ledtype'], codes.LEDTYPE.WS2812)
         self.assertEquals(kwds['time'], 35000)
-        self.assertEquals(kwds['type'], codes.LEDTYPE.GENERIC)
+        self.assertEquals(kwds['ledtype'], LEDTYPE.GENERIC)
 
     def test_file(self):
         start('test/project.json', False)