From 428adc91c7804461661dd3bb5c8b7ec348e41fcb Mon Sep 17 00:00:00 2001
From: clacktronics <ben@clacktronics.co.uk>
Date: Wed, 10 Aug 2016 11:10:13 +0100
Subject: [PATCH] Initial prototype of module

---
 .gitignore |  1 +
 dmx.py     | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 dmx.py

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ca6ee4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.pyb
diff --git a/dmx.py b/dmx.py
new file mode 100644
index 0000000..22b8ca6
--- /dev/null
+++ b/dmx.py
@@ -0,0 +1,46 @@
+from pyb import UART, Pin, udelay
+from array import array
+
+tx_pins = [None, 'X9', 'X3', 'Y9', 'X1', None, 'Y1']
+
+class universe():
+    def __init__(self, port):
+        self.port = port
+
+        # To check if port is valid
+        dmx_uart = UART(port)
+        del(dmx_uart)
+
+        # First byte is always 0, 512 after that is the 512 channels
+        self.dmx_message = array('B', [0] * 513)
+
+    def set_channels(self, message):
+        """
+        a dict and writes them to the array
+        format {channel:value}
+        """
+
+        for ch in message:
+            self.dmx_message[ch] = message[ch]
+
+        # for i, ch in enumerate(channels):
+        #     self.dmx_message[ch] = values[i]
+
+    def write_frame(self):
+        """
+        Send a DMX frame
+        """
+        # DMX needs a 88us low to begin a frame,
+        # 77uS us used because of time it takes to init pin
+        dmx_uart = Pin(tx_pins[self.port], Pin.OUT_PP)
+        dmx_uart.value(0)
+        udelay(74)
+        dmx_uart.value(1)
+
+        # Now turn into a UART port and send DMX data
+        dmx_uart = UART(self.port)
+        dmx_uart.init(250000, bits=8, parity=None, stop=2)
+        #send bytes
+        dmx_uart.write(self.dmx_message)
+        #Delete as its going to change anyway
+        del(dmx_uart)