Skip to content

Commit

Permalink
Merge pull request #111 from thirstyice/redefSerial
Browse files Browse the repository at this point in the history
Allow redefinition of where logging goes
  • Loading branch information
hideakitai authored Jul 6, 2024
2 parents 5d7320b + e4dbe53 commit ea329ab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
1 change: 1 addition & 0 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct Destination
uint8_t universe;
};


inline bool operator<(const Destination &rhs, const Destination &lhs)
{
if (rhs.ip < lhs.ip) {
Expand Down
56 changes: 26 additions & 30 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

namespace art_net {

class NoPrint : public Print {
size_t write(uint8_t) {
return 0;
}
};
static NoPrint no_log;

template <typename S>
class Receiver_
{
Expand All @@ -24,8 +31,6 @@ class Receiver_
art_trigger::CallbackType callback_art_trigger;
ArtPollReplyConfig art_poll_reply_config;

bool b_verbose {false};

public:
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
#else
Expand All @@ -48,18 +53,14 @@ class Receiver_
}

if (size > PACKET_SIZE) {
if (this->b_verbose) {
Serial.print(F("Packet size is unexpectedly too large: "));
Serial.println(size);
}
logger->print(F("Packet size is unexpectedly too large: "));
logger->println(size);
size = PACKET_SIZE;
}
this->stream->read(this->packet.data(), size);

if (!checkID()) {
if (this->b_verbose) {
Serial.println(F("Packet ID is not Art-Net"));
}
logger->println(F("Packet ID is not Art-Net"));
return OpCode::ParseFailed;
}

Expand Down Expand Up @@ -120,10 +121,8 @@ class Receiver_
break;
}
default: {
if (this->b_verbose) {
Serial.print(F("Unsupported OpCode: "));
Serial.println(this->getOpCode(), HEX);
}
logger->print(F("Unsupported OpCode: "));
logger->println(this->getOpCode(), HEX);
op_code = OpCode::Unsupported;
break;
}
Expand All @@ -139,21 +138,15 @@ class Receiver_
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
if (net > 0x7F) {
if (this->b_verbose) {
Serial.println(F("net should be less than 0x7F"));
}
logger->println(F("net should be less than 0x7F"));
return;
}
if (subnet > 0xF) {
if (this->b_verbose) {
Serial.println(F("subnet should be less than 0xF"));
}
logger->println(F("subnet should be less than 0xF"));
return;
}
if (universe > 0xF) {
if (this->b_verbose) {
Serial.println(F("universe should be less than 0xF"));
}
logger->println(F("universe should be less than 0xF"));
return;
}
uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe;
Expand Down Expand Up @@ -253,11 +246,11 @@ class Receiver_
n = num;
} else {
n = size / 3;
Serial.println(F("WARN: ArtNet packet size is less than requested LED numbers to forward"));
Serial.print(F(" requested: "));
Serial.print(num * 3);
Serial.print(F(" received : "));
Serial.println(size);
logger->println(F("WARN: ArtNet packet size is less than requested LED numbers to forward"));
logger->print(F(" requested: "));
logger->print(num * 3);
logger->print(F(" received : "));
logger->println(size);
}
for (size_t pixel = 0; pixel < n; ++pixel) {
size_t idx = pixel * 3;
Expand Down Expand Up @@ -288,9 +281,8 @@ class Receiver_
this->art_poll_reply_config.node_report = node_report;
}

void verbose(bool b)
{
this->b_verbose = b;
void setLogger(Print* dest) {
logger = dest;
}

protected:
Expand All @@ -299,7 +291,11 @@ class Receiver_
this->stream = &s;
}


private:

Print* logger = &no_log;

bool checkID() const
{
const char* idptr = reinterpret_cast<const char*>(this->packet.data());
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ void forwardArtDmxDataToFastLED(uint16_t universe, CRGB* leds, uint16_t num);
// set information for artpollreply
// https://art-net.org.uk/how-it-works/discovery-packets/artpollreply/
void setArtPollReplyConfig(uint16_t oem, uint16_t esta_man, uint8_t status1, uint8_t status2, const String &short_name, const String &long_name, const String &node_report);
// others
void verbose(bool b);
// Set where debug output should go (default is nowhere)
void setLogger(Print*);
```
### Note
Expand Down

0 comments on commit ea329ab

Please sign in to comment.