Simplify TWIBus debug

This commit is contained in:
Scott Lahteine 2020-03-28 01:19:43 -05:00
parent 53fe572bbd
commit a84990961a
2 changed files with 22 additions and 41 deletions

View File

@ -49,37 +49,27 @@ void TWIBus::address(const uint8_t adr) {
addr = adr;
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("address"), adr);
#endif
}
void TWIBus::addbyte(const char c) {
if (buffer_s >= COUNT(buffer)) return;
buffer[buffer_s++] = c;
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("addbyte"), c);
#endif
}
void TWIBus::addbytes(char src[], uint8_t bytes) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("addbytes"), bytes);
#endif
while (bytes--) addbyte(*src++);
}
void TWIBus::addstring(char str[]) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("addstring"), str);
#endif
while (char c = *str++) addbyte(c);
}
void TWIBus::send() {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("send"), addr);
#endif
Wire.beginTransmission(I2C_ADDRESS(addr));
Wire.write(buffer, buffer_s);
@ -89,21 +79,21 @@ void TWIBus::send() {
}
// static
void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
void TWIBus::echoprefix(uint8_t bytes, const char pref[], uint8_t adr) {
SERIAL_ECHO_START();
serialprintPGM(prefix);
serialprintPGM(pref);
SERIAL_ECHOPAIR(": from:", adr, " bytes:", bytes, " data:");
}
// static
void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
echoprefix(bytes, prefix, adr);
void TWIBus::echodata(uint8_t bytes, const char pref[], uint8_t adr) {
echoprefix(bytes, pref, adr);
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
SERIAL_EOL();
}
void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
echoprefix(buffer_s, prefix, adr);
void TWIBus::echobuffer(const char pref[], uint8_t adr) {
echoprefix(buffer_s, pref, adr);
LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
SERIAL_EOL();
}
@ -111,15 +101,11 @@ void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
bool TWIBus::request(const uint8_t bytes) {
if (!addr) return false;
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("request"), bytes);
#endif
// requestFrom() is a blocking function
if (Wire.requestFrom(addr, bytes) == 0) {
#if ENABLED(DEBUG_TWIBUS)
debug("request fail", addr);
#endif
return false;
}
@ -127,9 +113,7 @@ bool TWIBus::request(const uint8_t bytes) {
}
void TWIBus::relay(const uint8_t bytes) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("relay"), bytes);
#endif
if (request(bytes))
echodata(bytes, PSTR("i2c-reply"), addr);
@ -141,9 +125,7 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
while (count < bytes && Wire.available())
dst[count++] = Wire.read();
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("capture"), count);
#endif
return count;
}
@ -156,16 +138,12 @@ void TWIBus::flush() {
#if I2C_SLAVE_ADDRESS > 0
void TWIBus::receive(uint8_t bytes) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("receive"), bytes);
#endif
echodata(bytes, PSTR("i2c-receive"), 0);
}
void TWIBus::reply(char str[]/*=nullptr*/) {
#if ENABLED(DEBUG_TWIBUS)
debug(PSTR("reply"), str);
#endif
if (str) {
reset();

View File

@ -223,7 +223,6 @@ class TWIBus {
#endif
#if ENABLED(DEBUG_TWIBUS)
/**
* @brief Prints a debug message
* @details Prints a simple debug message "TWIBus::function: value"
@ -233,6 +232,10 @@ class TWIBus {
static void debug(const char func[], char c);
static void debug(const char func[], char adr[]);
static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
#else
static inline void debug(const char[], uint32_t) {}
static inline void debug(const char[], char) {}
static inline void debug(const char[], char[]) {}
static inline void debug(const char[], uint8_t) {}
#endif
};