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