Skip to content

Commit

Permalink
Expose the dmx mode pin as a new parameter in the init function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel committed Dec 10, 2015
1 parent c9e9f4b commit 1e13790
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
34 changes: 27 additions & 7 deletions src/DMXSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// 19.05.2013 ATmega8 compatibility (beta)
// 24.08.2013 Optimizations for speed and size.
// Removed some "volatile" annotations.
// 19.03.2015 DMXModePin as parameter
// - - - - -

#include "Arduino.h"
Expand Down Expand Up @@ -187,7 +188,8 @@ typedef enum {
// These variables are not class members because they have to be reached by the interrupt implementations.
// don't use these variable from outside, use the appropriate methods.

DMXMode _dmxMode; // Mode of Operation
DMXMode _dmxMode; // Mode of Operation
int _dmxModePin; // pin used for I/O direction.

uint8_t _dmxRecvState; // Current State of receiving DMX Bytes
int _dmxChannel; // the next channel byte to be sent.
Expand Down Expand Up @@ -222,7 +224,14 @@ void _DMXSerialWriteByte(uint8_t data);

// (Re)Initialize the specified mode.
// The mode parameter should be a value from enum DMXMode.
void DMXSerialClass::init (int mode)
void DMXSerialClass::init(int mode)
{
init(mode, DMXMODEPIN);
}

// (Re)Initialize the specified mode.
// The mode parameter should be a value from enum DMXMode.
void DMXSerialClass::init (int mode, int dmxModePin)
{
#ifdef SCOPEDEBUG
pinMode(DmxTriggerPin, OUTPUT);
Expand All @@ -231,6 +240,7 @@ void DMXSerialClass::init (int mode)

// initialize global variables
_dmxMode = DMXNone;
_dmxModePin = dmxModePin;
_dmxRecvState= IDLE; // initial state
_dmxChannel = 0;
_dmxDataPtr = _dmxData;
Expand All @@ -246,8 +256,8 @@ void DMXSerialClass::init (int mode)

if (_dmxMode == DMXController) {
// Setup external mode signal
pinMode(DmxModePin, OUTPUT); // enables pin 2 for output to control data direction
digitalWrite(DmxModePin, DmxModeOut); // data Out direction
pinMode(_dmxModePin, OUTPUT); // enables pin 2 for output to control data direction
digitalWrite(_dmxModePin, DmxModeOut); // data Out direction

// Setup Hardware
// Enable transmitter and interrupt
Expand All @@ -260,8 +270,8 @@ void DMXSerialClass::init (int mode)

} else if (_dmxMode == DMXReceiver) {
// Setup external mode signal
pinMode(DmxModePin, OUTPUT); // enables pin 2 for output to control data direction
digitalWrite(DmxModePin, DmxModeIn); // data in direction
pinMode(_dmxModePin, OUTPUT); // enables pin 2 for output to control data direction
digitalWrite(_dmxModePin, DmxModeIn); // data in direction

// Setup Hardware
// Enable receiver and Receive interrupt
Expand Down Expand Up @@ -436,8 +446,18 @@ ISR(USARTn_RX_vect)
if (_dmxDataPtr == _dmxDataLastPtr) { // all channels received.
_dmxRecvState = IDLE; // wait for next break

if ((_dmxUpdated) && (_dmxOnUpdateFunc))
if ((_dmxUpdated) && (_dmxOnUpdateFunc)) {
// stop listening on the serial port for now.
UCSRnB = 0;

// call the update function
_dmxOnUpdateFunc();

// start listening again. Enable receiver and Receive interrupt
UCSRnB = (1<<RXENn) | (1<<RXCIEn);
_DMXSerialBaud(Calcprescale(DMXSPEED), DMXFORMAT); // Enable serial reception with a 250k rate
} // if

_dmxUpdated = false;
} // if
// _dmxChannel++;
Expand Down
8 changes: 6 additions & 2 deletions src/DMXSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// 01.12.2011 include file changed to work with the Arduino 1.0 environment
// 10.05.2012 added method noDataSince to check how long no packet was received
// 12.07.2014 added update flag
// 19.03.2015 DMXModePin as optional parameter
// Until here the maxChannel feature only was used in DMXController mode.
// Now it enables triggering the onUpdate function in DMX receiver mode.
//
Expand All @@ -26,7 +27,7 @@

#define DMXSERIAL_MAX 512 // max. number of supported DMX data channels

#define DmxModePin 2 // Arduino pin 2 for controlling the data direction
#define DMXMODEPIN 2 // Arduino pin 2 for controlling the data direction is the default value.
#define DmxModeOut HIGH // set the level to HIGH for outgoing data direction
#define DmxModeIn LOW // set the level to LOW for incomming data direction

Expand All @@ -49,7 +50,10 @@ class DMXSerialClass
{
public:
// Initialize for specific mode.
void init (int mode);
void init (int mode);

// Initialize for specific mode including a specific mode pin.
void init (int mode, int modePin);

// Set the maximum used channel for DMXController mode.
void maxChannel (int channel);
Expand Down

0 comments on commit 1e13790

Please sign in to comment.