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

View File

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