DMX library
Fork of DMX by
DMX.cpp@4:dd0544c80096, 2012-11-21 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 21 01:44:21 2012 +0000
- Revision:
- 4:dd0544c80096
- Parent:
- 3:2eb66b4d99bd
- Child:
- 5:72039cd4c874
add start(), stop()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:cbff6bf41542 | 1 | /* |
okini3939 | 0:cbff6bf41542 | 2 | * DMX512 send/recv library |
okini3939 | 0:cbff6bf41542 | 3 | * Copyright (c) 2011 Hiroshi Suga |
okini3939 | 0:cbff6bf41542 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 0:cbff6bf41542 | 5 | */ |
okini3939 | 0:cbff6bf41542 | 6 | |
okini3939 | 0:cbff6bf41542 | 7 | /** @file DMX.cpp |
okini3939 | 0:cbff6bf41542 | 8 | * @brief DMX512 send/recv |
okini3939 | 0:cbff6bf41542 | 9 | */ |
okini3939 | 0:cbff6bf41542 | 10 | |
okini3939 | 0:cbff6bf41542 | 11 | #include "mbed.h" |
okini3939 | 0:cbff6bf41542 | 12 | #include "DMX.h" |
okini3939 | 0:cbff6bf41542 | 13 | |
okini3939 | 0:cbff6bf41542 | 14 | DMX::DMX (PinName p_tx, PinName p_rx) : dmx(p_tx, p_rx) { |
okini3939 | 0:cbff6bf41542 | 15 | int i; |
okini3939 | 0:cbff6bf41542 | 16 | |
okini3939 | 0:cbff6bf41542 | 17 | for (i = 0; i < DMX_SIZE; i ++) { |
okini3939 | 0:cbff6bf41542 | 18 | data_tx[i] = 0; |
okini3939 | 0:cbff6bf41542 | 19 | data_rx[i] = 0; |
okini3939 | 0:cbff6bf41542 | 20 | } |
okini3939 | 4:dd0544c80096 | 21 | // mode_tx = DMX_MODE_BEGIN; |
okini3939 | 4:dd0544c80096 | 22 | mode_tx = DMX_MODE_STOP; |
okini3939 | 0:cbff6bf41542 | 23 | mode_rx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 24 | is_recived = 0; |
okini3939 | 0:cbff6bf41542 | 25 | is_sent = 0; |
okini3939 | 0:cbff6bf41542 | 26 | |
okini3939 | 0:cbff6bf41542 | 27 | if (p_tx == p9) { |
okini3939 | 0:cbff6bf41542 | 28 | uart_lcr = &LPC_UART3->LCR; |
okini3939 | 0:cbff6bf41542 | 29 | uart_thr = &LPC_UART3->THR; |
okini3939 | 0:cbff6bf41542 | 30 | } else |
okini3939 | 0:cbff6bf41542 | 31 | if (p_tx == p13) { |
okini3939 | 0:cbff6bf41542 | 32 | uart_lcr = &LPC_UART1->LCR; |
okini3939 | 0:cbff6bf41542 | 33 | uart_thr = &LPC_UART1->THR; |
okini3939 | 0:cbff6bf41542 | 34 | } else |
okini3939 | 0:cbff6bf41542 | 35 | if (p_tx == p28) { |
okini3939 | 0:cbff6bf41542 | 36 | uart_lcr = &LPC_UART2->LCR; |
okini3939 | 0:cbff6bf41542 | 37 | uart_thr = &LPC_UART2->THR; |
okini3939 | 0:cbff6bf41542 | 38 | } |
okini3939 | 0:cbff6bf41542 | 39 | if (p_rx == p10) { |
okini3939 | 0:cbff6bf41542 | 40 | uart_lsr = &LPC_UART3->LSR; |
okini3939 | 0:cbff6bf41542 | 41 | uart_rbr = &LPC_UART3->RBR; |
okini3939 | 0:cbff6bf41542 | 42 | NVIC_SetPriority(UART3_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 43 | } else |
okini3939 | 0:cbff6bf41542 | 44 | if (p_rx == p14) { |
okini3939 | 0:cbff6bf41542 | 45 | uart_lsr = &LPC_UART1->LSR; |
okini3939 | 0:cbff6bf41542 | 46 | uart_rbr = &LPC_UART1->RBR; |
okini3939 | 0:cbff6bf41542 | 47 | NVIC_SetPriority(UART1_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 48 | } else |
okini3939 | 0:cbff6bf41542 | 49 | if (p_rx == p27) { |
okini3939 | 0:cbff6bf41542 | 50 | uart_lsr = &LPC_UART2->LSR; |
okini3939 | 0:cbff6bf41542 | 51 | uart_rbr = &LPC_UART2->RBR; |
okini3939 | 0:cbff6bf41542 | 52 | NVIC_SetPriority(UART2_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 53 | } |
okini3939 | 0:cbff6bf41542 | 54 | |
okini3939 | 0:cbff6bf41542 | 55 | dmx.baud(250000); |
okini3939 | 0:cbff6bf41542 | 56 | dmx.format(8, Serial::None, 2); |
okini3939 | 0:cbff6bf41542 | 57 | dmx.attach(this, &DMX::int_rx, Serial::RxIrq); |
okini3939 | 0:cbff6bf41542 | 58 | |
okini3939 | 4:dd0544c80096 | 59 | // timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 60 | } |
okini3939 | 0:cbff6bf41542 | 61 | |
okini3939 | 0:cbff6bf41542 | 62 | void DMX::put (int ch, int data) { |
okini3939 | 0:cbff6bf41542 | 63 | data_tx[ch] = data; |
okini3939 | 0:cbff6bf41542 | 64 | } |
okini3939 | 0:cbff6bf41542 | 65 | |
okini3939 | 0:cbff6bf41542 | 66 | int DMX::get (int ch) { |
okini3939 | 0:cbff6bf41542 | 67 | return data_rx[ch]; |
okini3939 | 0:cbff6bf41542 | 68 | } |
okini3939 | 0:cbff6bf41542 | 69 | |
okini3939 | 0:cbff6bf41542 | 70 | void DMX::int_timer () { |
okini3939 | 0:cbff6bf41542 | 71 | |
okini3939 | 0:cbff6bf41542 | 72 | switch (mode_tx) { |
okini3939 | 0:cbff6bf41542 | 73 | case DMX_MODE_BEGIN: |
okini3939 | 0:cbff6bf41542 | 74 | // Break Time |
okini3939 | 0:cbff6bf41542 | 75 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 76 | *uart_lcr |= (1 << 6); |
okini3939 | 0:cbff6bf41542 | 77 | mode_tx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 78 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK); |
okini3939 | 0:cbff6bf41542 | 79 | break; |
okini3939 | 0:cbff6bf41542 | 80 | |
okini3939 | 0:cbff6bf41542 | 81 | case DMX_MODE_BREAK: |
okini3939 | 0:cbff6bf41542 | 82 | // Mark After Break |
okini3939 | 0:cbff6bf41542 | 83 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 84 | *uart_lcr &= ~(1 << 6); |
okini3939 | 0:cbff6bf41542 | 85 | mode_tx = DMX_MODE_MAB; |
okini3939 | 0:cbff6bf41542 | 86 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB); |
okini3939 | 0:cbff6bf41542 | 87 | break; |
okini3939 | 0:cbff6bf41542 | 88 | |
okini3939 | 0:cbff6bf41542 | 89 | case DMX_MODE_MAB: |
okini3939 | 0:cbff6bf41542 | 90 | // Start code |
okini3939 | 0:cbff6bf41542 | 91 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 92 | addr_tx = 0; |
okini3939 | 0:cbff6bf41542 | 93 | mode_tx = DMX_MODE_DATA; |
okini3939 | 0:cbff6bf41542 | 94 | dmx.attach(this, &DMX::int_tx, Serial::TxIrq); |
okini3939 | 2:d7677060f8eb | 95 | #ifdef DMX_UART_DIRECT |
okini3939 | 2:d7677060f8eb | 96 | *uart_thr = 0; |
okini3939 | 2:d7677060f8eb | 97 | #else |
okini3939 | 0:cbff6bf41542 | 98 | dmx.putc(0); |
okini3939 | 2:d7677060f8eb | 99 | #endif |
okini3939 | 0:cbff6bf41542 | 100 | break; |
okini3939 | 0:cbff6bf41542 | 101 | } |
okini3939 | 0:cbff6bf41542 | 102 | } |
okini3939 | 0:cbff6bf41542 | 103 | |
okini3939 | 0:cbff6bf41542 | 104 | void DMX::int_tx () { |
okini3939 | 0:cbff6bf41542 | 105 | // Data |
okini3939 | 0:cbff6bf41542 | 106 | if (mode_tx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 107 | if (addr_tx < DMX_SIZE) { |
okini3939 | 1:f0d988e15810 | 108 | #ifdef DMX_UART_DIRECT |
okini3939 | 0:cbff6bf41542 | 109 | *uart_thr = (uint8_t)data_tx[addr_tx]; |
okini3939 | 0:cbff6bf41542 | 110 | #else |
okini3939 | 0:cbff6bf41542 | 111 | dmx.putc(data_tx[addr_tx]); |
okini3939 | 0:cbff6bf41542 | 112 | #endif |
okini3939 | 0:cbff6bf41542 | 113 | addr_tx ++; |
okini3939 | 0:cbff6bf41542 | 114 | } else { |
okini3939 | 0:cbff6bf41542 | 115 | dmx.attach(0, Serial::TxIrq); |
okini3939 | 0:cbff6bf41542 | 116 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 117 | is_sent = 1; |
okini3939 | 0:cbff6bf41542 | 118 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 119 | } |
okini3939 | 0:cbff6bf41542 | 120 | } |
okini3939 | 0:cbff6bf41542 | 121 | } |
okini3939 | 0:cbff6bf41542 | 122 | |
okini3939 | 0:cbff6bf41542 | 123 | void DMX::int_rx () { |
okini3939 | 0:cbff6bf41542 | 124 | int flg, dat; |
okini3939 | 0:cbff6bf41542 | 125 | |
okini3939 | 0:cbff6bf41542 | 126 | flg = *uart_lsr; |
okini3939 | 1:f0d988e15810 | 127 | #ifdef DMX_UART_DIRECT |
okini3939 | 1:f0d988e15810 | 128 | dat = *uart_rbr; |
okini3939 | 1:f0d988e15810 | 129 | #else |
okini3939 | 0:cbff6bf41542 | 130 | dat = dmx.getc(); |
okini3939 | 1:f0d988e15810 | 131 | #endif |
okini3939 | 0:cbff6bf41542 | 132 | |
okini3939 | 0:cbff6bf41542 | 133 | if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) { |
okini3939 | 0:cbff6bf41542 | 134 | // Break Time |
okini3939 | 0:cbff6bf41542 | 135 | if (addr_rx > 0) { |
okini3939 | 0:cbff6bf41542 | 136 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 137 | } |
okini3939 | 0:cbff6bf41542 | 138 | mode_rx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 139 | return; |
okini3939 | 0:cbff6bf41542 | 140 | } |
okini3939 | 0:cbff6bf41542 | 141 | |
okini3939 | 0:cbff6bf41542 | 142 | if (mode_rx == DMX_MODE_BREAK) { |
okini3939 | 0:cbff6bf41542 | 143 | |
okini3939 | 0:cbff6bf41542 | 144 | // Start Code |
okini3939 | 0:cbff6bf41542 | 145 | if (dat == 0) { |
okini3939 | 0:cbff6bf41542 | 146 | addr_rx = 0; |
okini3939 | 0:cbff6bf41542 | 147 | mode_rx = DMX_MODE_DATA; |
okini3939 | 0:cbff6bf41542 | 148 | } else { |
okini3939 | 0:cbff6bf41542 | 149 | mode_rx = DMX_MODE_ERROR; |
okini3939 | 0:cbff6bf41542 | 150 | } |
okini3939 | 0:cbff6bf41542 | 151 | |
okini3939 | 0:cbff6bf41542 | 152 | } else |
okini3939 | 0:cbff6bf41542 | 153 | if (mode_rx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 154 | |
okini3939 | 0:cbff6bf41542 | 155 | // Data |
okini3939 | 0:cbff6bf41542 | 156 | data_rx[addr_rx] = dat; |
okini3939 | 0:cbff6bf41542 | 157 | addr_rx ++; |
okini3939 | 0:cbff6bf41542 | 158 | |
okini3939 | 0:cbff6bf41542 | 159 | if (addr_rx >= DMX_SIZE) { |
okini3939 | 0:cbff6bf41542 | 160 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 161 | mode_rx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 162 | } |
okini3939 | 0:cbff6bf41542 | 163 | } |
okini3939 | 0:cbff6bf41542 | 164 | } |
okini3939 | 0:cbff6bf41542 | 165 | |
okini3939 | 4:dd0544c80096 | 166 | void DMX::start () { |
okini3939 | 4:dd0544c80096 | 167 | if (mode_tx == DMX_MODE_STOP) { |
okini3939 | 4:dd0544c80096 | 168 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 4:dd0544c80096 | 169 | is_sent = 0; |
okini3939 | 4:dd0544c80096 | 170 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 4:dd0544c80096 | 171 | } |
okini3939 | 4:dd0544c80096 | 172 | } |
okini3939 | 4:dd0544c80096 | 173 | |
okini3939 | 4:dd0544c80096 | 174 | void DMX::stop () { |
okini3939 | 3:2eb66b4d99bd | 175 | dmx.attach(0, Serial::TxIrq); |
okini3939 | 3:2eb66b4d99bd | 176 | timeout01.detach(); |
okini3939 | 4:dd0544c80096 | 177 | mode_tx = DMX_MODE_STOP; |
okini3939 | 3:2eb66b4d99bd | 178 | } |