DMX Library for STM devices

Dependents:   DISCO-F746NG_DMX-Test

Fork of DMX by Suga koubou

Committer:
Gottfried
Date:
Sat Dec 12 13:57:52 2015 +0000
Revision:
18:e5e5eca21b4a
Parent:
17:b87addd12d20
some changes

Who changed what in which revision?

UserRevisionLine numberNew 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
Gottfried 17:b87addd12d20 14 // pragma does'nt work?
Gottfried 17:b87addd12d20 15 #pragma GCC diagnostic ignored "-Wreorder"
okini3939 0:cbff6bf41542 16
Gottfried 17:b87addd12d20 17 DMX::DMX (PinName p_tx, PinName p_rx, unsigned int size) :
Gottfried 17:b87addd12d20 18 _dmx(p_tx, p_rx),
Gottfried 17:b87addd12d20 19 myDMXSize(size),
Gottfried 17:b87addd12d20 20 is_recived(0),
Gottfried 17:b87addd12d20 21 is_sent(0),
Gottfried 17:b87addd12d20 22 mode_tx(DMX_MODE_STOP),
Gottfried 18:e5e5eca21b4a 23 mode_rx(DMX_MODE_BEGIN),
Gottfried 18:e5e5eca21b4a 24 addr_tx(0),
Gottfried 18:e5e5eca21b4a 25 addr_rx(0)
Gottfried 17:b87addd12d20 26 {
Gottfried 17:b87addd12d20 27 data_tx = new char[myDMXSize];
Gottfried 17:b87addd12d20 28 data_rx = new char[myDMXSize];
Gottfried 17:b87addd12d20 29
Gottfried 17:b87addd12d20 30 clear();
okini3939 0:cbff6bf41542 31
Gottfried 17:b87addd12d20 32 _uart = new DUMMY_UART_TypeDef;
Gottfried 17:b87addd12d20 33 // NVIC_SetPriority(UART_IRQn, 1);
okini3939 0:cbff6bf41542 34
Gottfried 17:b87addd12d20 35 _dmx.baud(250000);
Gottfried 17:b87addd12d20 36 _dmx.format(8, Serial::None, 2);
Gottfried 17:b87addd12d20 37 _dmx.attach(this, &DMX::int_rx, Serial::RxIrq);
Gottfried 17:b87addd12d20 38 }
Gottfried 17:b87addd12d20 39
Gottfried 17:b87addd12d20 40 DMX::~DMX()
Gottfried 17:b87addd12d20 41 {
Gottfried 17:b87addd12d20 42 delete _uart;
Gottfried 17:b87addd12d20 43 delete[] data_tx;
Gottfried 17:b87addd12d20 44 delete[] data_rx;
okini3939 0:cbff6bf41542 45 }
okini3939 0:cbff6bf41542 46
Gottfried 18:e5e5eca21b4a 47 void DMX::put(int addr, char data)
Gottfried 17:b87addd12d20 48 {
Gottfried 17:b87addd12d20 49 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 50 return;
Gottfried 17:b87addd12d20 51 data_tx[addr] = data;
okini3939 0:cbff6bf41542 52 }
okini3939 0:cbff6bf41542 53
Gottfried 18:e5e5eca21b4a 54 void DMX::put(char *buf, int addr, int len)
Gottfried 17:b87addd12d20 55 {
Gottfried 17:b87addd12d20 56 if(!len)
Gottfried 17:b87addd12d20 57 len = myDMXSize;
Gottfried 17:b87addd12d20 58
Gottfried 17:b87addd12d20 59 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 60 return;
Gottfried 17:b87addd12d20 61 if(len > myDMXSize - addr)
Gottfried 17:b87addd12d20 62 len = myDMXSize - addr;
Gottfried 17:b87addd12d20 63 memcpy(&data_tx[addr], buf, len);
okini3939 6:9e7b4eeac6ec 64 }
okini3939 6:9e7b4eeac6ec 65
Gottfried 17:b87addd12d20 66 int DMX::get(int addr)
Gottfried 17:b87addd12d20 67 {
Gottfried 17:b87addd12d20 68 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 69 return -1;
Gottfried 17:b87addd12d20 70
Gottfried 17:b87addd12d20 71 return data_rx[addr];
okini3939 6:9e7b4eeac6ec 72 }
okini3939 6:9e7b4eeac6ec 73
Gottfried 18:e5e5eca21b4a 74 void DMX::get(char *buf, int addr, int len)
Gottfried 17:b87addd12d20 75 {
Gottfried 17:b87addd12d20 76 if(!len)
Gottfried 17:b87addd12d20 77 len = myDMXSize;
Gottfried 17:b87addd12d20 78
Gottfried 17:b87addd12d20 79 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 80 return;
Gottfried 17:b87addd12d20 81 if(len > myDMXSize - addr)
Gottfried 17:b87addd12d20 82 len = myDMXSize - addr;
Gottfried 17:b87addd12d20 83 memcpy(buf, &data_rx[addr], len);
okini3939 0:cbff6bf41542 84 }
okini3939 0:cbff6bf41542 85
Gottfried 17:b87addd12d20 86 void DMX::int_timer()
Gottfried 17:b87addd12d20 87 {
Gottfried 17:b87addd12d20 88 switch(mode_tx)
Gottfried 17:b87addd12d20 89 {
okini3939 0:cbff6bf41542 90 case DMX_MODE_BEGIN:
okini3939 0:cbff6bf41542 91 // Break Time
okini3939 0:cbff6bf41542 92 timeout01.detach();
okini3939 8:d4a45bba41d2 93 _uart->LCR |= (1 << 6);
okini3939 0:cbff6bf41542 94 mode_tx = DMX_MODE_BREAK;
okini3939 0:cbff6bf41542 95 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK);
okini3939 0:cbff6bf41542 96 break;
okini3939 0:cbff6bf41542 97
okini3939 0:cbff6bf41542 98 case DMX_MODE_BREAK:
okini3939 0:cbff6bf41542 99 // Mark After Break
okini3939 0:cbff6bf41542 100 timeout01.detach();
okini3939 8:d4a45bba41d2 101 _uart->LCR &= ~(1 << 6);
okini3939 0:cbff6bf41542 102 mode_tx = DMX_MODE_MAB;
okini3939 0:cbff6bf41542 103 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB);
okini3939 0:cbff6bf41542 104 break;
okini3939 0:cbff6bf41542 105
okini3939 0:cbff6bf41542 106 case DMX_MODE_MAB:
okini3939 0:cbff6bf41542 107 // Start code
okini3939 0:cbff6bf41542 108 timeout01.detach();
okini3939 0:cbff6bf41542 109 addr_tx = 0;
okini3939 0:cbff6bf41542 110 mode_tx = DMX_MODE_DATA;
okini3939 5:72039cd4c874 111 _dmx.attach(this, &DMX::int_tx, Serial::TxIrq);
okini3939 2:d7677060f8eb 112 #ifdef DMX_UART_DIRECT
okini3939 9:e687f321c428 113 while(!(_uart->LSR & (1<<5)));
okini3939 9:e687f321c428 114 _uart->THR = DMX_START_CODE;
okini3939 2:d7677060f8eb 115 #else
okini3939 9:e687f321c428 116 _dmx.putc(DMX_START_CODE);
okini3939 2:d7677060f8eb 117 #endif
okini3939 0:cbff6bf41542 118 break;
Gottfried 17:b87addd12d20 119 }
Gottfried 17:b87addd12d20 120 }
Gottfried 17:b87addd12d20 121
Gottfried 17:b87addd12d20 122 void DMX::int_tx()
Gottfried 17:b87addd12d20 123 {
Gottfried 17:b87addd12d20 124 // Data
Gottfried 17:b87addd12d20 125 if(mode_tx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 126 {
Gottfried 17:b87addd12d20 127 if(addr_tx < myDMXSize)
Gottfried 17:b87addd12d20 128 {
Gottfried 17:b87addd12d20 129 #ifdef DMX_UART_DIRECT
Gottfried 17:b87addd12d20 130 _uart->THR = (uint8_t)data_tx[addr_tx];
Gottfried 17:b87addd12d20 131 #else
Gottfried 17:b87addd12d20 132 _dmx.putc(data_tx[addr_tx]);
Gottfried 17:b87addd12d20 133 #endif
Gottfried 17:b87addd12d20 134 addr_tx ++;
okini3939 0:cbff6bf41542 135 }
Gottfried 17:b87addd12d20 136 else
Gottfried 17:b87addd12d20 137 {
Gottfried 17:b87addd12d20 138 _dmx.attach(0, Serial::TxIrq);
Gottfried 17:b87addd12d20 139 mode_tx = DMX_MODE_BEGIN;
Gottfried 17:b87addd12d20 140 is_sent = 1;
Gottfried 17:b87addd12d20 141 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
Gottfried 17:b87addd12d20 142 }
Gottfried 17:b87addd12d20 143 }
okini3939 0:cbff6bf41542 144 }
okini3939 0:cbff6bf41542 145
Gottfried 17:b87addd12d20 146 void DMX::int_rx()
Gottfried 17:b87addd12d20 147 {
Gottfried 17:b87addd12d20 148 int flg, dat;
Gottfried 17:b87addd12d20 149
Gottfried 17:b87addd12d20 150 flg = _uart->LSR;
okini3939 1:f0d988e15810 151 #ifdef DMX_UART_DIRECT
Gottfried 17:b87addd12d20 152 dat = _uart->RBR;
okini3939 0:cbff6bf41542 153 #else
Gottfried 17:b87addd12d20 154 dat = _dmx.getc();
okini3939 1:f0d988e15810 155 #endif
okini3939 0:cbff6bf41542 156
Gottfried 17:b87addd12d20 157 if (flg & ((1 << 7)|(1 << 3)|(1 << 4)))
Gottfried 17:b87addd12d20 158 {
Gottfried 17:b87addd12d20 159 // Break Time
Gottfried 17:b87addd12d20 160 if(addr_rx >= 24 && mode_rx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 161 {
Gottfried 17:b87addd12d20 162 is_recived = 1;
Gottfried 17:b87addd12d20 163 }
Gottfried 17:b87addd12d20 164 mode_rx = DMX_MODE_BREAK;
Gottfried 17:b87addd12d20 165 return;
Gottfried 17:b87addd12d20 166 }
Gottfried 17:b87addd12d20 167
Gottfried 17:b87addd12d20 168 if(mode_rx == DMX_MODE_BREAK)
Gottfried 17:b87addd12d20 169 {
Gottfried 17:b87addd12d20 170 // Start Code
Gottfried 17:b87addd12d20 171 if (dat == DMX_START_CODE)
Gottfried 17:b87addd12d20 172 {
Gottfried 17:b87addd12d20 173 addr_rx = 0;
Gottfried 17:b87addd12d20 174 mode_rx = DMX_MODE_DATA;
Gottfried 17:b87addd12d20 175 }
Gottfried 17:b87addd12d20 176 else
Gottfried 17:b87addd12d20 177 {
Gottfried 17:b87addd12d20 178 mode_rx = DMX_MODE_ERROR;
okini3939 0:cbff6bf41542 179 }
okini3939 0:cbff6bf41542 180
Gottfried 17:b87addd12d20 181 }
Gottfried 17:b87addd12d20 182 else if(mode_rx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 183 {
Gottfried 17:b87addd12d20 184 // Data
Gottfried 17:b87addd12d20 185 data_rx[addr_rx] = dat;
Gottfried 17:b87addd12d20 186 addr_rx ++;
okini3939 0:cbff6bf41542 187
Gottfried 17:b87addd12d20 188 if (addr_rx >= myDMXSize)
Gottfried 17:b87addd12d20 189 {
Gottfried 17:b87addd12d20 190 is_recived = 1;
Gottfried 17:b87addd12d20 191 mode_rx = DMX_MODE_BEGIN;
okini3939 0:cbff6bf41542 192 }
Gottfried 17:b87addd12d20 193 }
okini3939 0:cbff6bf41542 194 }
okini3939 0:cbff6bf41542 195
Gottfried 17:b87addd12d20 196 void DMX::start()
Gottfried 17:b87addd12d20 197 {
Gottfried 17:b87addd12d20 198 if (mode_tx == DMX_MODE_STOP)
Gottfried 17:b87addd12d20 199 {
Gottfried 17:b87addd12d20 200 mode_tx = DMX_MODE_BEGIN;
Gottfried 17:b87addd12d20 201 is_sent = 0;
Gottfried 17:b87addd12d20 202 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
Gottfried 17:b87addd12d20 203 }
okini3939 4:dd0544c80096 204 }
okini3939 4:dd0544c80096 205
Gottfried 17:b87addd12d20 206 void DMX::stop()
Gottfried 17:b87addd12d20 207 {
Gottfried 17:b87addd12d20 208 _dmx.attach(0, Serial::TxIrq);
Gottfried 17:b87addd12d20 209 timeout01.detach();
Gottfried 17:b87addd12d20 210 mode_tx = DMX_MODE_STOP;
okini3939 3:2eb66b4d99bd 211 }
okini3939 12:1f176eee2d28 212
Gottfried 17:b87addd12d20 213 void DMX::clear()
Gottfried 17:b87addd12d20 214 {
Gottfried 17:b87addd12d20 215 memset(data_tx, 0, myDMXSize);
Gottfried 17:b87addd12d20 216 memset(data_rx, 0, myDMXSize);
okini3939 12:1f176eee2d28 217 }