DMX library
Fork of DMX by
DMX.cpp@6:9e7b4eeac6ec, 2013-01-13 (annotated)
- Committer:
- okini3939
- Date:
- Sun Jan 13 01:33:17 2013 +0000
- Revision:
- 6:9e7b4eeac6ec
- Parent:
- 5:72039cd4c874
- Child:
- 7:16d6874076dd
support put/get buffer
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 | 6:9e7b4eeac6ec | 3 | * Copyright (c) 2013 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 | 5:72039cd4c874 | 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 | 5:72039cd4c874 | 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 | 5:72039cd4c874 | 27 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
okini3939 | 0:cbff6bf41542 | 28 | if (p_tx == p9) { |
okini3939 | 0:cbff6bf41542 | 29 | uart_lcr = &LPC_UART3->LCR; |
okini3939 | 0:cbff6bf41542 | 30 | uart_thr = &LPC_UART3->THR; |
okini3939 | 0:cbff6bf41542 | 31 | } else |
okini3939 | 0:cbff6bf41542 | 32 | if (p_tx == p13) { |
okini3939 | 0:cbff6bf41542 | 33 | uart_lcr = &LPC_UART1->LCR; |
okini3939 | 0:cbff6bf41542 | 34 | uart_thr = &LPC_UART1->THR; |
okini3939 | 0:cbff6bf41542 | 35 | } else |
okini3939 | 0:cbff6bf41542 | 36 | if (p_tx == p28) { |
okini3939 | 0:cbff6bf41542 | 37 | uart_lcr = &LPC_UART2->LCR; |
okini3939 | 0:cbff6bf41542 | 38 | uart_thr = &LPC_UART2->THR; |
okini3939 | 0:cbff6bf41542 | 39 | } |
okini3939 | 0:cbff6bf41542 | 40 | if (p_rx == p10) { |
okini3939 | 0:cbff6bf41542 | 41 | uart_lsr = &LPC_UART3->LSR; |
okini3939 | 0:cbff6bf41542 | 42 | uart_rbr = &LPC_UART3->RBR; |
okini3939 | 0:cbff6bf41542 | 43 | NVIC_SetPriority(UART3_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 44 | } else |
okini3939 | 0:cbff6bf41542 | 45 | if (p_rx == p14) { |
okini3939 | 0:cbff6bf41542 | 46 | uart_lsr = &LPC_UART1->LSR; |
okini3939 | 0:cbff6bf41542 | 47 | uart_rbr = &LPC_UART1->RBR; |
okini3939 | 0:cbff6bf41542 | 48 | NVIC_SetPriority(UART1_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 49 | } else |
okini3939 | 0:cbff6bf41542 | 50 | if (p_rx == p27) { |
okini3939 | 0:cbff6bf41542 | 51 | uart_lsr = &LPC_UART2->LSR; |
okini3939 | 0:cbff6bf41542 | 52 | uart_rbr = &LPC_UART2->RBR; |
okini3939 | 0:cbff6bf41542 | 53 | NVIC_SetPriority(UART2_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 54 | } |
okini3939 | 5:72039cd4c874 | 55 | #elif defined(TARGET_LPC11U24) |
okini3939 | 5:72039cd4c874 | 56 | if (p_tx == p9) { |
okini3939 | 5:72039cd4c874 | 57 | uart_lcr = &LPC_USART->LCR; |
okini3939 | 5:72039cd4c874 | 58 | uart_thr = &LPC_USART->THR; |
okini3939 | 5:72039cd4c874 | 59 | } |
okini3939 | 5:72039cd4c874 | 60 | if (p_rx == p10) { |
okini3939 | 5:72039cd4c874 | 61 | uart_lsr = &LPC_USART->LSR; |
okini3939 | 5:72039cd4c874 | 62 | uart_rbr = &LPC_USART->RBR; |
okini3939 | 5:72039cd4c874 | 63 | NVIC_SetPriority(UART_IRQn, 1); |
okini3939 | 5:72039cd4c874 | 64 | } |
okini3939 | 5:72039cd4c874 | 65 | #endif |
okini3939 | 0:cbff6bf41542 | 66 | |
okini3939 | 5:72039cd4c874 | 67 | _dmx.baud(250000); |
okini3939 | 5:72039cd4c874 | 68 | _dmx.format(8, Serial::None, 2); |
okini3939 | 5:72039cd4c874 | 69 | _dmx.attach(this, &DMX::int_rx, Serial::RxIrq); |
okini3939 | 0:cbff6bf41542 | 70 | |
okini3939 | 4:dd0544c80096 | 71 | // timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 72 | } |
okini3939 | 0:cbff6bf41542 | 73 | |
okini3939 | 6:9e7b4eeac6ec | 74 | void DMX::put (int addr, int data) { |
okini3939 | 6:9e7b4eeac6ec | 75 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 76 | data_tx[addr] = data; |
okini3939 | 0:cbff6bf41542 | 77 | } |
okini3939 | 0:cbff6bf41542 | 78 | |
okini3939 | 6:9e7b4eeac6ec | 79 | void DMX::put (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 80 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 81 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 82 | memcpy(&data_tx[addr], buf, len); |
okini3939 | 6:9e7b4eeac6ec | 83 | } |
okini3939 | 6:9e7b4eeac6ec | 84 | |
okini3939 | 6:9e7b4eeac6ec | 85 | int DMX::get (int addr) { |
okini3939 | 6:9e7b4eeac6ec | 86 | if (addr < 0 || addr >= DMX_SIZE) return -1; |
okini3939 | 6:9e7b4eeac6ec | 87 | return data_rx[addr]; |
okini3939 | 6:9e7b4eeac6ec | 88 | } |
okini3939 | 6:9e7b4eeac6ec | 89 | |
okini3939 | 6:9e7b4eeac6ec | 90 | void DMX::get (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 91 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 92 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 93 | memcpy(buf, &data_rx[addr], len); |
okini3939 | 0:cbff6bf41542 | 94 | } |
okini3939 | 0:cbff6bf41542 | 95 | |
okini3939 | 0:cbff6bf41542 | 96 | void DMX::int_timer () { |
okini3939 | 0:cbff6bf41542 | 97 | |
okini3939 | 0:cbff6bf41542 | 98 | switch (mode_tx) { |
okini3939 | 0:cbff6bf41542 | 99 | case DMX_MODE_BEGIN: |
okini3939 | 0:cbff6bf41542 | 100 | // Break Time |
okini3939 | 0:cbff6bf41542 | 101 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 102 | *uart_lcr |= (1 << 6); |
okini3939 | 0:cbff6bf41542 | 103 | mode_tx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 104 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK); |
okini3939 | 0:cbff6bf41542 | 105 | break; |
okini3939 | 0:cbff6bf41542 | 106 | |
okini3939 | 0:cbff6bf41542 | 107 | case DMX_MODE_BREAK: |
okini3939 | 0:cbff6bf41542 | 108 | // Mark After Break |
okini3939 | 0:cbff6bf41542 | 109 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 110 | *uart_lcr &= ~(1 << 6); |
okini3939 | 0:cbff6bf41542 | 111 | mode_tx = DMX_MODE_MAB; |
okini3939 | 0:cbff6bf41542 | 112 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB); |
okini3939 | 0:cbff6bf41542 | 113 | break; |
okini3939 | 0:cbff6bf41542 | 114 | |
okini3939 | 0:cbff6bf41542 | 115 | case DMX_MODE_MAB: |
okini3939 | 0:cbff6bf41542 | 116 | // Start code |
okini3939 | 0:cbff6bf41542 | 117 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 118 | addr_tx = 0; |
okini3939 | 0:cbff6bf41542 | 119 | mode_tx = DMX_MODE_DATA; |
okini3939 | 5:72039cd4c874 | 120 | _dmx.attach(this, &DMX::int_tx, Serial::TxIrq); |
okini3939 | 2:d7677060f8eb | 121 | #ifdef DMX_UART_DIRECT |
okini3939 | 2:d7677060f8eb | 122 | *uart_thr = 0; |
okini3939 | 2:d7677060f8eb | 123 | #else |
okini3939 | 5:72039cd4c874 | 124 | _dmx.putc(0); |
okini3939 | 2:d7677060f8eb | 125 | #endif |
okini3939 | 0:cbff6bf41542 | 126 | break; |
okini3939 | 0:cbff6bf41542 | 127 | } |
okini3939 | 0:cbff6bf41542 | 128 | } |
okini3939 | 0:cbff6bf41542 | 129 | |
okini3939 | 0:cbff6bf41542 | 130 | void DMX::int_tx () { |
okini3939 | 0:cbff6bf41542 | 131 | // Data |
okini3939 | 0:cbff6bf41542 | 132 | if (mode_tx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 133 | if (addr_tx < DMX_SIZE) { |
okini3939 | 1:f0d988e15810 | 134 | #ifdef DMX_UART_DIRECT |
okini3939 | 0:cbff6bf41542 | 135 | *uart_thr = (uint8_t)data_tx[addr_tx]; |
okini3939 | 0:cbff6bf41542 | 136 | #else |
okini3939 | 5:72039cd4c874 | 137 | _dmx.putc(data_tx[addr_tx]); |
okini3939 | 0:cbff6bf41542 | 138 | #endif |
okini3939 | 0:cbff6bf41542 | 139 | addr_tx ++; |
okini3939 | 0:cbff6bf41542 | 140 | } else { |
okini3939 | 5:72039cd4c874 | 141 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 0:cbff6bf41542 | 142 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 143 | is_sent = 1; |
okini3939 | 0:cbff6bf41542 | 144 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 145 | } |
okini3939 | 0:cbff6bf41542 | 146 | } |
okini3939 | 0:cbff6bf41542 | 147 | } |
okini3939 | 0:cbff6bf41542 | 148 | |
okini3939 | 0:cbff6bf41542 | 149 | void DMX::int_rx () { |
okini3939 | 0:cbff6bf41542 | 150 | int flg, dat; |
okini3939 | 0:cbff6bf41542 | 151 | |
okini3939 | 0:cbff6bf41542 | 152 | flg = *uart_lsr; |
okini3939 | 1:f0d988e15810 | 153 | #ifdef DMX_UART_DIRECT |
okini3939 | 1:f0d988e15810 | 154 | dat = *uart_rbr; |
okini3939 | 1:f0d988e15810 | 155 | #else |
okini3939 | 5:72039cd4c874 | 156 | dat = _dmx.getc(); |
okini3939 | 1:f0d988e15810 | 157 | #endif |
okini3939 | 0:cbff6bf41542 | 158 | |
okini3939 | 0:cbff6bf41542 | 159 | if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) { |
okini3939 | 0:cbff6bf41542 | 160 | // Break Time |
okini3939 | 0:cbff6bf41542 | 161 | if (addr_rx > 0) { |
okini3939 | 0:cbff6bf41542 | 162 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 163 | } |
okini3939 | 0:cbff6bf41542 | 164 | mode_rx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 165 | return; |
okini3939 | 0:cbff6bf41542 | 166 | } |
okini3939 | 0:cbff6bf41542 | 167 | |
okini3939 | 0:cbff6bf41542 | 168 | if (mode_rx == DMX_MODE_BREAK) { |
okini3939 | 0:cbff6bf41542 | 169 | |
okini3939 | 0:cbff6bf41542 | 170 | // Start Code |
okini3939 | 0:cbff6bf41542 | 171 | if (dat == 0) { |
okini3939 | 0:cbff6bf41542 | 172 | addr_rx = 0; |
okini3939 | 0:cbff6bf41542 | 173 | mode_rx = DMX_MODE_DATA; |
okini3939 | 0:cbff6bf41542 | 174 | } else { |
okini3939 | 0:cbff6bf41542 | 175 | mode_rx = DMX_MODE_ERROR; |
okini3939 | 0:cbff6bf41542 | 176 | } |
okini3939 | 0:cbff6bf41542 | 177 | |
okini3939 | 0:cbff6bf41542 | 178 | } else |
okini3939 | 0:cbff6bf41542 | 179 | if (mode_rx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 180 | |
okini3939 | 0:cbff6bf41542 | 181 | // Data |
okini3939 | 0:cbff6bf41542 | 182 | data_rx[addr_rx] = dat; |
okini3939 | 0:cbff6bf41542 | 183 | addr_rx ++; |
okini3939 | 0:cbff6bf41542 | 184 | |
okini3939 | 0:cbff6bf41542 | 185 | if (addr_rx >= DMX_SIZE) { |
okini3939 | 0:cbff6bf41542 | 186 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 187 | mode_rx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 188 | } |
okini3939 | 0:cbff6bf41542 | 189 | } |
okini3939 | 0:cbff6bf41542 | 190 | } |
okini3939 | 0:cbff6bf41542 | 191 | |
okini3939 | 4:dd0544c80096 | 192 | void DMX::start () { |
okini3939 | 4:dd0544c80096 | 193 | if (mode_tx == DMX_MODE_STOP) { |
okini3939 | 4:dd0544c80096 | 194 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 4:dd0544c80096 | 195 | is_sent = 0; |
okini3939 | 4:dd0544c80096 | 196 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 4:dd0544c80096 | 197 | } |
okini3939 | 4:dd0544c80096 | 198 | } |
okini3939 | 4:dd0544c80096 | 199 | |
okini3939 | 4:dd0544c80096 | 200 | void DMX::stop () { |
okini3939 | 5:72039cd4c874 | 201 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 3:2eb66b4d99bd | 202 | timeout01.detach(); |
okini3939 | 4:dd0544c80096 | 203 | mode_tx = DMX_MODE_STOP; |
okini3939 | 3:2eb66b4d99bd | 204 | } |