|
|
|
@ -49,37 +49,27 @@ void TWIBus::address(const uint8_t adr) {
|
|
|
|
|
|
|
|
|
|
addr = adr;
|
|
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug(PSTR("address"), adr);
|
|
|
|
|
#endif
|
|
|
|
|
debug(PSTR("address"), adr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
debug(PSTR("addbyte"), c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TWIBus::addbytes(char src[], uint8_t bytes) {
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug(PSTR("addbytes"), bytes);
|
|
|
|
|
#endif
|
|
|
|
|
debug(PSTR("addbytes"), bytes);
|
|
|
|
|
while (bytes--) addbyte(*src++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TWIBus::addstring(char str[]) {
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug(PSTR("addstring"), str);
|
|
|
|
|
#endif
|
|
|
|
|
debug(PSTR("addstring"), str);
|
|
|
|
|
while (char c = *str++) addbyte(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TWIBus::send() {
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug(PSTR("send"), addr);
|
|
|
|
|
#endif
|
|
|
|
|
debug(PSTR("send"), addr);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
debug(PSTR("request"), bytes);
|
|
|
|
|
|
|
|
|
|
// requestFrom() is a blocking function
|
|
|
|
|
if (Wire.requestFrom(addr, bytes) == 0) {
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug("request fail", addr);
|
|
|
|
|
#endif
|
|
|
|
|
debug("request fail", addr);
|
|
|
|
|
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
|
|
|
|
|
debug(PSTR("relay"), bytes);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
debug(PSTR("capture"), count);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
debug(PSTR("receive"), bytes);
|
|
|
|
|
echodata(bytes, PSTR("i2c-receive"), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TWIBus::reply(char str[]/*=nullptr*/) {
|
|
|
|
|
#if ENABLED(DEBUG_TWIBUS)
|
|
|
|
|
debug(PSTR("reply"), str);
|
|
|
|
|
#endif
|
|
|
|
|
debug(PSTR("reply"), str);
|
|
|
|
|
|
|
|
|
|
if (str) {
|
|
|
|
|
reset();
|
|
|
|
|