DMX512 library test http://mbed.org/users/okini3939/notebook/dmx512

Dependencies:   DMX mbed

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Fri Jun 03 13:20:01 2011 +0000
Parent:
2:eeb225671cac
Child:
4:4a566dc74bfb
Commit message:

Changed in this revision

DMX.lib Show diff for this revision Revisions of this file
DMX/DMX.cpp Show annotated file Show diff for this revision Revisions of this file
DMX/DMX.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DMX.lib	Sun May 08 14:50:41 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/okini3939/code/DMX/#2156e39c3363
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DMX/DMX.cpp	Fri Jun 03 13:20:01 2011 +0000
@@ -0,0 +1,136 @@
+/*
+ * DMX512 send/recv library
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+/** @file DMX.cpp
+ * @brief DMX512 send/recv
+ */
+
+#include "mbed.h"
+#include "DMX.h"
+
+DMX::DMX (PinName p_tx, PinName p_rx) : dmx(p_tx, p_rx) {
+    int i;
+
+    for (i = 0; i < DMX_SIZE; i ++) {
+        data_tx[i] = 0;
+        data_rx[i] = 0;
+    }
+    mode_tx = DMX_MODE_BEGIN;
+    mode_rx = DMX_MODE_BEGIN;
+
+    if (p_tx == p9) {
+      uart_lcr = &LPC_UART3->LCR;
+    } else
+    if (p_tx == p13) {
+      uart_lcr = &LPC_UART1->LCR;
+    } else
+    if (p_tx == p28) {
+      uart_lcr = &LPC_UART2->LCR;
+    }
+    if (p_rx == p10) {
+      uart_lsr = &LPC_UART3->LSR;
+    } else
+    if (p_rx == p14) {
+      uart_lsr = &LPC_UART1->LSR;
+    } else
+    if (p_rx == p27) {
+      uart_lsr = &LPC_UART2->LSR;
+    }
+
+    dmx.baud(250000);
+    dmx.format(8, Serial::None, 2);
+    dmx.attach(this, &DMX::int_rx, Serial::RxIrq);
+
+    timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
+}
+
+void DMX::put (int ch, int data) {
+    data_tx[ch] = data;
+}
+
+int DMX::get (int ch) {
+    return data_rx[ch];
+}
+
+void DMX::int_timer () {
+
+    switch (mode_tx) {
+    case DMX_MODE_BEGIN:
+        // Break Time
+        timeout01.detach();
+        *uart_lcr |= (1 << 6);
+        mode_tx = DMX_MODE_BREAK;
+        timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK);
+        break;
+
+    case DMX_MODE_BREAK:
+        // Mark After Break
+        timeout01.detach();
+        *uart_lcr &= ~(1 << 6);
+        mode_tx = DMX_MODE_MAB;
+        timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB);
+        break;
+
+    case DMX_MODE_MAB:
+        // Start code
+        timeout01.detach();
+        addr_tx = 0;
+        mode_tx = DMX_MODE_DATA;
+        dmx.attach(this, &DMX::int_tx, Serial::TxIrq);
+        dmx.putc(0);
+        break;
+    }
+}
+
+void DMX::int_tx () {
+    // Data
+    if (mode_tx == DMX_MODE_DATA) {
+        dmx.putc(data_tx[addr_tx]);
+        addr_tx ++;
+        
+        if (addr_tx >= DMX_SIZE) {
+            dmx.attach(0, Serial::TxIrq);
+            mode_tx = DMX_MODE_BEGIN;
+            timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
+        }
+    }
+}
+
+void DMX::int_rx () {
+    int flg, dat;
+
+    flg = *uart_lsr;
+    dat = dmx.getc();
+
+    if (flg & (1 << 3) || flg & (1 << 4)) {
+        // Break Time
+        mode_rx = DMX_MODE_BREAK;
+        return;
+    }
+
+    if (mode_rx == DMX_MODE_BREAK) {
+
+        // Start Code
+        if (dat == 0) {
+            addr_rx = 0;
+            mode_rx = DMX_MODE_DATA;
+        } else {
+            mode_rx = DMX_MODE_ERROR;
+        }
+
+    } else
+    if (mode_rx == DMX_MODE_DATA) {
+
+        // Data
+        data_rx[addr_rx] = dat;
+        addr_rx ++;
+        
+        if (addr_rx >= DMX_SIZE) {
+            mode_rx = DMX_MODE_BEGIN;
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DMX/DMX.h	Fri Jun 03 13:20:01 2011 +0000
@@ -0,0 +1,71 @@
+/*
+ * DMX512 send/recv library
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+/** @file DMX.h
+ * @brief DMX512 send/recv
+ */
+ 
+#ifndef DMX_H
+#define DMX_H
+
+#include "mbed.h"
+
+#define DMX_SIZE 512
+#define DMX_TIME_BREAK 100 // 100us
+#define DMX_TIME_MAB 10 // 10us
+#define DMX_TIME_BETWEEN 10 // 10us
+
+enum DMX_MODE {
+    DMX_MODE_BEGIN,
+    DMX_MODE_START,
+    DMX_MODE_BREAK,
+    DMX_MODE_MAB,
+    DMX_MODE_DATA,
+    DMX_MODE_ERROR,
+};
+
+/** DMX512 class (sender/client)
+ */
+class DMX {
+public:
+    /** init DMX class
+     * @param p_tx TX serial port (p9, p13, p28)
+     * @param p_rx RX serial port (p10, p14, p27)
+     */
+    DMX (PinName p_tx, PinName p_rx); 
+
+    /** Send the message
+     * @param ch DMX data address (0-511)
+     * @param data DMX data (0-255)
+     */
+    void put (int ch, int data);
+
+    /** Send the message
+     * @param ch DMX data address (0-511)
+     * @return DMX data (0-255)
+     */
+    int get (int ch);
+
+protected:
+
+    void int_timer ();
+    void int_tx ();
+    void int_rx ();
+
+    Serial dmx;
+    Timeout timeout01;
+    __IO uint8_t *uart_lcr;
+    __I  uint8_t *uart_lsr;
+    volatile DMX_MODE mode_tx, mode_rx;
+    volatile int addr_tx, addr_rx;
+    unsigned char data_tx[512];
+    unsigned char data_rx[512];
+
+private:
+
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Sun May 08 14:50:41 2011 +0000
+++ b/main.cpp	Fri Jun 03 13:20:01 2011 +0000
@@ -1,32 +1,40 @@
 #include "mbed.h"
 #include "DMX.h"
 
-DMX dmx(p9, p10);
-//DMX dmx(p13, p14);
-//DMX dmx(p28, p27);
+//DMX dmx0(p9, p10);
+DMX dmx1(p13, p14);
+DMX dmx2(p28, p27);
 
 AnalogIn adc(p15);
-PwmOut led[] = {(LED1), (LED2), (LED3), (LED4)};
 Serial pc(USBTX, USBRX);
+PwmOut led_r(p22), led_y(p23);
 
 int main() {
     int i, n;
 
+    // OUTPUT1: fade in / OUTPUT2: fade out
     while(1) {
+        for (n = 0; n < 256; n ++) {
+            dmx1.put(0, n);
+            dmx2.put(0, 255 - n);
+            led_r = (float)n / 255;
+            led_y = (float)(255 - n) / 255;
+            wait(0.02);
+        }
+    }
+                
+/*        
+    // INPUT1 -> OUTPUT2 / INPUT2 -> OUTPUT1
+    while (1) {
+        for (i = 0; i < 512; i ++) {
+            n = dmx1.get(i);
+            dmx2.put(i, n);
+            n = dmx2.get(i);
+            dmx1.put(i, n);
+        }
+        led_r = (float)dmx1.get(0) / 255;
+        led_y = (float)dmx2.get(0) / 255;
         wait(0.1);
-
-        n = adc.read_u16() >> 8;
-        dmx.put(0, n);
-
-        pc.printf("tx: %d / ", n);
-        
-        pc.printf("rx: ", dmx.get(0));
-        for (i = 0; i < 4; i ++) {
-            n = dmx.get(i);
-            led[i] = (float)n / 255.0;
-            pc.printf("%d ", n);
-        }
-
-        pc.printf("\r\n");
     }
+*/
 }