Skip to content

Commit

Permalink
add support for custom serial (on path to #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAwesomeP committed Jul 1, 2019
1 parent 4bbbf52 commit bfd7a56
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Download the latest release ZIP from [here](https://github.com/DaAwesomeP/dmxusb
## Usage
Currently, the library only receives DMX messages from a PC over USB. Please take a look at the [`Simple_Test` sketch](examples/Simple_Test/Simple_Test.ino) for a complete example.

### DMXUSB (serial, baudrate, mode, callback, out_universes)
The DMXUSB class initializes a new instance of the library. The `out_universes` argument is only required for mode 2. Example:
### DMXUSB (serial, baudrate, mode, callback, outUniverses, serialNum)
The DMXUSB class initializes a new instance of the library. The `out_universes` argument is only required for mode 2. The `serialNum` is not required. Example:
```cpp
DMXUSB myDMXUsb(Serial, 115200, 0, myDMXCallback);
```
Expand All @@ -39,24 +39,24 @@ The type of device to emulate:
|-------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 0 | A standard ENTTEC-compatible device with one universe output that emulates the UltraDMX Micro (ESTA ID: 0x6A6B, Device ID: 0x3) |
| 1 | A DMXKing two-universe device that emulates the UltraDMX Pro (0x6A6B, Device ID: 0x2) |
| 2 | A DMXUSB n-universe device similar to the DMXKing two-universe device (requires the `out_universes` argument) **that is in development and not yet supported by OLA** (0x7FF7, Device ID: 0x32) |
| 2 | A DMXUSB n-universe device similar to the DMXKing two-universe device (requires the `outUniverses` argument) **that is in development and not yet supported by OLA** (0x7FF7, Device ID: 0x32) |
The following table shows which universes receive callbacks when different commands are sent to DMXUSB. When label 6 is received in all modes, the callback will be called multiple times and output to all universes as per the DMXKing specification.
| mode | label (command type) | universe 0 | univserse 1 | universes 2 through n |
|------|----------------------|-------------|-------------|------------------------|
| 0 | 6 | Yes | No | No |
| 0 | 100 | No | No | No |
| 0 | 101 | No | No | No |
| 0 | 102 through n | No | No | No |
| 1 | 6 | Yes | Yes | No |
| 1 | 100 | Yes | No | No |
| 1 | 101 | No | Yes | No |
| 1 | 102 through n | No | No | No |
| 2 | 6 | Yes | Yes | Yes |
| 2 | 100 | Yes | No | No |
| 2 | 101 | No | Yes | No |
| 2 | 102 through n | No | No | Yes |
| mode | label (command type) | universe 0 | universe 1 | universes 2 through n |
|------|----------------------|-------------|------------|-----------------------|
| 0 | 6 | Yes | No | No |
| 0 | 100 | No | No | No |
| 0 | 101 | No | No | No |
| 0 | 102 through n | No | No | No |
| 1 | 6 | Yes | Yes | No |
| 1 | 100 | Yes | No | No |
| 1 | 101 | No | Yes | No |
| 1 | 102 through n | No | No | No |
| 2 | 6 | Yes | Yes | Yes |
| 2 | 100 | Yes | No | No |
| 2 | 101 | No | Yes | No |
| 2 | 102 through n | No | No | Yes |
Note that mode 0 only responds to ENTTEC label 6 and outputs to universe 0 in the callback. When mode is 1 or 2, label 6 outputs to either universe 0 and 1 for mode 1 and universes 0 through n for mode 2 (the callback is called multiple times). This mimics the DMXKing specification. With mode 1, label 100 outputs to only universe 0 and label 101 outputs to only universe 1.
Expand All @@ -79,7 +79,7 @@ void myDMXCallback(int universe, char buffer[512]) {
}
```

#### out_universes (int)
#### outUniverses (int)
The number of output universes. This parameter is required only when the mode is 2. **The number of output universes defaults to 0 if mode 2 is used and this parameter is not set.** This argument is ignored for modes 0 and 1.

The maximum number of theoretical universes is found with the following equation:
Expand All @@ -88,6 +88,9 @@ The maximum number of theoretical universes is found with the following equation
```
You must subtract one as seen above because there are a few extra bytes in every DMX packet when sent over USB with this library. This equation does not take into account CPU time and load or how long it takes to process the data or run the rest of your program. **With this math, the Teensy can theoretically achieve 47 universes!**

#### serialNum (uint8_t)
If you are using multiple devices on one computer, then you should set the serial number. The default serial number is `0xFFFFFFFF` if one is not provided. This should be a `uint8_t` array of bytes with a length of 4.

### DMXUSB.listen ()
A function of the class that causes the library to check for messages. This function is typically called at the top of `loop()`. Example:
```cpp
Expand Down
1 change: 0 additions & 1 deletion lib/TeensyID
Submodule TeensyID deleted from ec8c31
25 changes: 13 additions & 12 deletions src/DMXUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
#define STATE_END 5

// Initialize DMXUSB serial port
DMXUSB::DMXUSB(Stream &serial, int baudrate, int mode, void (*dmxInCallback)(int universe, char buffer[512]), int out_universes) {
DMXUSB::DMXUSB(Stream &serial, int baudrate, int mode, void (*dmxInCallback)(int universe, char buffer[512]), int outUniverses, uint8_t serialNum[]) {
_serial = &serial;
_baudrate = baudrate;
_mode = mode;
_dmxInCallback = dmxInCallback;
if (_mode == 0) _out_universes = 1;
else if (_mode == 1) _out_universes = 2;
else if (_mode == 2) _out_universes = out_universes;
if (_mode == 0) _outUniverses = 1;
else if (_mode == 1) _outUniverses = 2;
else if (_mode == 2) _outUniverses = outUniverses;
_serialNum = serialNum;
}

// Poll for incoming DMX messages
Expand Down Expand Up @@ -69,7 +70,7 @@ void DMXUSB::listen() {
// second bit: message label
case STATE_LABEL:
label = b; // record the message label
if (label == 6 || (label >= 100 && label < 100 + _out_universes)) for (int i = 0; i < 512; i++) _buffer[i] = (byte)0x00; // set buffer to all zero values if receiving DMX data
if (label == 6 || (label >= 100 && label < 100 + _outUniverses)) for (int i = 0; i < 512; i++) _buffer[i] = (byte)0x00; // set buffer to all zero values if receiving DMX data
state = STATE_LEN_LSB; // move to next bit
break;

Expand Down Expand Up @@ -156,10 +157,10 @@ void DMXUSB::listen() {
_serial->write(0x0A); // label 10
_serial->write(len & 0xff); // data length LSB: 4
_serial->write((len + 1) >> 8); // data length MSB: 0
_serial->write(0xFF); // for now just use serial number 0xFFFFFFFF
_serial->write(0xFF);
_serial->write(0xFF);
_serial->write(0xFF);
_serial->write(_serialNum[3]);
_serial->write(_serialNum[2]);
_serial->write(_serialNum[1]);
_serial->write(_serialNum[0]);
_serial->write(0xE7); // message footer
}

Expand All @@ -184,19 +185,19 @@ void DMXUSB::listen() {
_serial->write(len & 0xff); // data length LSB: 2
_serial->write((len + 1) >> 8); // data length MSB: 0
// _serial->write((byte)0x00); // out universes
_serial->write((byte)_out_universes); // out universes
_serial->write((byte)_outUniverses); // out universes
_serial->write((byte)0x00); // in universes (not implemented)
_serial->write(0xE7); // message footer
}

else if (label == 6 || (label >= 100 && label < 100 + _out_universes)) { // receive DMX message to all universes
else if (label == 6 || (label >= 100 && label < 100 + _outUniverses)) { // receive DMX message to all universes
//if (index > 1) {
if (label == 6 && _mode == 0) this->DMXUSB::_dmxInCallback(0, _buffer); // receive label==6 DMX message to first universe for Enttec-like ultraDMX Micro device
else if (label == 6 && _mode == 1) { // receive label==6 DMX message to both universes for ultraDMX Pro device
this->DMXUSB::_dmxInCallback(0, _buffer);
this->DMXUSB::_dmxInCallback(1, _buffer);
} else if (label == 6 && _mode == 2) { // receive label==6 DMX message to all universes for DMXUSB device
for (int i = 0; i < _out_universes; i++) this->DMXUSB::_dmxInCallback(i, _buffer);
for (int i = 0; i < _outUniverses; i++) this->DMXUSB::_dmxInCallback(i, _buffer);
} else if (label == 100 && _mode == 1) this->DMXUSB::_dmxInCallback(0, _buffer); // receive label==100 DMX message to first universe for ultraDMX Pro device
else if (label == 101 && _mode == 1) this->DMXUSB::_dmxInCallback(1, _buffer); // receive label==101 DMX message to second universe for ultraDMX Pro device
else if (_mode == 2) this->DMXUSB::_dmxInCallback(label - 100, _buffer); // receive labels 100 through 107 DMX message to each universe for DMXUSB device
Expand Down
21 changes: 5 additions & 16 deletions src/DMXUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,12 @@
#if !defined(CORE_TEENSY)
#include <elapsedMillis.h>
#endif
#if defined(__MK20DX128__) // Teensy 3.0
#define AUTO_SERIAL_AVAILABLE "3.0"
#elif defined(__MK20DX256__) // Teensy 3.1 or 3.2
#define AUTO_SERIAL_AVAILABLE "3.2"
#elif defined(__MKL26Z64__) // Teensy LC
#define AUTO_SERIAL_AVAILABLE "LC"
#elif defined(__MK64FX512__) // Teensy 3.5
#define AUTO_SERIAL_AVAILABLE "3.5"
#elif defined(__MK66FX1M0__) // Teensy 3.6
#define AUTO_SERIAL_AVAILABLE "3.6"
#endif
#if defined(AUTO_SERIAL_AVAILABLE)
#include "../lib/TeensyID/TeensyID.h"
#endif

static uint8_t DMXUSB_SERIALNUM_DEFAULT[4] = {0xff, 0xff, 0xff, 0xff};

class DMXUSB {
public:
DMXUSB(Stream &serial, int baudrate, int mode, void (*dmxInCallback)(int universe, char buffer[512]), int out_universes = 0);
DMXUSB(Stream &serial, int baudrate, int mode, void (*dmxInCallback)(int universe, char buffer[512]), int outUniverses = 0, uint8_t serialNum[] = DMXUSB_SERIALNUM_DEFAULT);
void listen();
private:
char _buffer[512];
Expand All @@ -51,7 +39,8 @@ class DMXUSB {
int _baudrate;
int _mode;
void (*_dmxInCallback)(int universe, char buffer[512]);
int _out_universes;
int _outUniverses;
uint8_t *_serialNum;
};

#endif
Expand Down

0 comments on commit bfd7a56

Please sign in to comment.