Skip to content

Commit

Permalink
receive non blocking, using optional timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel committed Sep 4, 2017
1 parent 52660de commit e30a556
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
20 changes: 13 additions & 7 deletions examples/DmxSerialNeoPixels/DmxSerialNeoPixels.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ void setup () {
// do constantly fetch DMX data and update the neopixels.
void loop() {
// wait for an incomming DMX packet.
DMXSerial.receive();

// read recent DMX values 1..3 and set pwm levels
analogWrite(RedPin, DMXSerial.read(1));
analogWrite(GreenPin, DMXSerial.read(2));
analogWrite(BluePin, DMXSerial.read(3));
if (DMXSerial.receive()) {
analogWrite(RedPin, DMXSerial.read(1));
analogWrite(GreenPin, DMXSerial.read(2));
analogWrite(BluePin, DMXSerial.read(3));
updateNeopixel(DMXSerial.getBuffer() + DMXSTART, PIXELS);

} else {
// don't update the Neopixels but signal a red.
analogWrite(RedPin, 100);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
} // if

updateNeopixel(DMXSerial.getBuffer() + DMXSTART, PIXELS);
} // loop()


// The End.


Expand Down
33 changes: 24 additions & 9 deletions src/DMXSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,22 +353,37 @@ void DMXSerialClass::resetUpdated()


// When mode is DMXProbe this function reads one DMX buffer and then returns.
void DMXSerialClass::receive()
// wait a meaningful time on a packet.
// return true whan a packet has been received.
bool DMXSerialClass::receive() {
return(receive(DMXPROBE_RECV_MAX));
} // receive()


// When mode is DMXProbe this function reads one DMX buffer and then returns.
// wait approximately gives the number of msecs for waiting on a packet.
// return true whan a packet has been received.
bool DMXSerialClass::receive(uint8_t wait)
{
bool ret = false;

if (_dmxMode == DMXProbe) {
_DMXStartReceiving();
// UCSRnA
while (_dmxRecvState != DONE) {
delay(5);
while ((wait > 0) && (_dmxRecvState != DONE)) {
delay(1);
wait--;
} // while

if ((_dmxData[1] == 0) && (_dmxData[2] == 0)) {
// digitalWrite(9, 1);
// delay(500);
// digitalWrite(9, 0);
}
if (_dmxRecvState == DONE) {
ret = true;
} else {
_DMXSerialInit(Calcprescale(DMXSPEED), (1 << RXENn), DMXFORMAT);
} // if
} // if
} // receive()

return(ret);
} // receive(wait)


// Terminate operation
Expand Down
7 changes: 6 additions & 1 deletion src/DMXSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define DmxModeOut HIGH // set the level to HIGH for outgoing data direction
#define DmxModeIn LOW // set the level to LOW for incomming data direction

#define DMXPROBE_RECV_MAX 50 // standard maximum of waiting for a DMX packet in DMXPROBE mode.

// ----- Enumerations -----

// Mode of Operation
Expand Down Expand Up @@ -77,8 +79,11 @@ class DMXSerialClass
void resetUpdated();

// actively wait for an incomming DMX packet.
void receive();
bool receive();

// actively wait for an incomming DMX packet, specifying the maximal waiting time in msecs.
bool receive(uint8_t wait);

// Terminate operation.
void term (void);

Expand Down

0 comments on commit e30a556

Please sign in to comment.