|
@@ -49,37 +49,27 @@ void TWIBus::address(const uint8_t adr) {
|
49
|
49
|
|
50
|
50
|
addr = adr;
|
51
|
51
|
|
52
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
53
|
|
- debug(PSTR("address"), adr);
|
54
|
|
- #endif
|
|
52
|
+ debug(PSTR("address"), adr);
|
55
|
53
|
}
|
56
|
54
|
|
57
|
55
|
void TWIBus::addbyte(const char c) {
|
58
|
56
|
if (buffer_s >= COUNT(buffer)) return;
|
59
|
57
|
buffer[buffer_s++] = c;
|
60
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
61
|
|
- debug(PSTR("addbyte"), c);
|
62
|
|
- #endif
|
|
58
|
+ debug(PSTR("addbyte"), c);
|
63
|
59
|
}
|
64
|
60
|
|
65
|
61
|
void TWIBus::addbytes(char src[], uint8_t bytes) {
|
66
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
67
|
|
- debug(PSTR("addbytes"), bytes);
|
68
|
|
- #endif
|
|
62
|
+ debug(PSTR("addbytes"), bytes);
|
69
|
63
|
while (bytes--) addbyte(*src++);
|
70
|
64
|
}
|
71
|
65
|
|
72
|
66
|
void TWIBus::addstring(char str[]) {
|
73
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
74
|
|
- debug(PSTR("addstring"), str);
|
75
|
|
- #endif
|
|
67
|
+ debug(PSTR("addstring"), str);
|
76
|
68
|
while (char c = *str++) addbyte(c);
|
77
|
69
|
}
|
78
|
70
|
|
79
|
71
|
void TWIBus::send() {
|
80
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
81
|
|
- debug(PSTR("send"), addr);
|
82
|
|
- #endif
|
|
72
|
+ debug(PSTR("send"), addr);
|
83
|
73
|
|
84
|
74
|
Wire.beginTransmission(I2C_ADDRESS(addr));
|
85
|
75
|
Wire.write(buffer, buffer_s);
|
|
@@ -89,21 +79,21 @@ void TWIBus::send() {
|
89
|
79
|
}
|
90
|
80
|
|
91
|
81
|
// static
|
92
|
|
-void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
|
|
82
|
+void TWIBus::echoprefix(uint8_t bytes, const char pref[], uint8_t adr) {
|
93
|
83
|
SERIAL_ECHO_START();
|
94
|
|
- serialprintPGM(prefix);
|
|
84
|
+ serialprintPGM(pref);
|
95
|
85
|
SERIAL_ECHOPAIR(": from:", adr, " bytes:", bytes, " data:");
|
96
|
86
|
}
|
97
|
87
|
|
98
|
88
|
// static
|
99
|
|
-void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
100
|
|
- echoprefix(bytes, prefix, adr);
|
|
89
|
+void TWIBus::echodata(uint8_t bytes, const char pref[], uint8_t adr) {
|
|
90
|
+ echoprefix(bytes, pref, adr);
|
101
|
91
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
102
|
92
|
SERIAL_EOL();
|
103
|
93
|
}
|
104
|
94
|
|
105
|
|
-void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
|
106
|
|
- echoprefix(buffer_s, prefix, adr);
|
|
95
|
+void TWIBus::echobuffer(const char pref[], uint8_t adr) {
|
|
96
|
+ echoprefix(buffer_s, pref, adr);
|
107
|
97
|
LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
|
108
|
98
|
SERIAL_EOL();
|
109
|
99
|
}
|
|
@@ -111,15 +101,11 @@ void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
|
111
|
101
|
bool TWIBus::request(const uint8_t bytes) {
|
112
|
102
|
if (!addr) return false;
|
113
|
103
|
|
114
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
115
|
|
- debug(PSTR("request"), bytes);
|
116
|
|
- #endif
|
|
104
|
+ debug(PSTR("request"), bytes);
|
117
|
105
|
|
118
|
106
|
// requestFrom() is a blocking function
|
119
|
107
|
if (Wire.requestFrom(addr, bytes) == 0) {
|
120
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
121
|
|
- debug("request fail", addr);
|
122
|
|
- #endif
|
|
108
|
+ debug("request fail", addr);
|
123
|
109
|
return false;
|
124
|
110
|
}
|
125
|
111
|
|
|
@@ -127,9 +113,7 @@ bool TWIBus::request(const uint8_t bytes) {
|
127
|
113
|
}
|
128
|
114
|
|
129
|
115
|
void TWIBus::relay(const uint8_t bytes) {
|
130
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
131
|
|
- debug(PSTR("relay"), bytes);
|
132
|
|
- #endif
|
|
116
|
+ debug(PSTR("relay"), bytes);
|
133
|
117
|
|
134
|
118
|
if (request(bytes))
|
135
|
119
|
echodata(bytes, PSTR("i2c-reply"), addr);
|
|
@@ -141,9 +125,7 @@ uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|
141
|
125
|
while (count < bytes && Wire.available())
|
142
|
126
|
dst[count++] = Wire.read();
|
143
|
127
|
|
144
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
145
|
|
- debug(PSTR("capture"), count);
|
146
|
|
- #endif
|
|
128
|
+ debug(PSTR("capture"), count);
|
147
|
129
|
|
148
|
130
|
return count;
|
149
|
131
|
}
|
|
@@ -156,16 +138,12 @@ void TWIBus::flush() {
|
156
|
138
|
#if I2C_SLAVE_ADDRESS > 0
|
157
|
139
|
|
158
|
140
|
void TWIBus::receive(uint8_t bytes) {
|
159
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
160
|
|
- debug(PSTR("receive"), bytes);
|
161
|
|
- #endif
|
|
141
|
+ debug(PSTR("receive"), bytes);
|
162
|
142
|
echodata(bytes, PSTR("i2c-receive"), 0);
|
163
|
143
|
}
|
164
|
144
|
|
165
|
145
|
void TWIBus::reply(char str[]/*=nullptr*/) {
|
166
|
|
- #if ENABLED(DEBUG_TWIBUS)
|
167
|
|
- debug(PSTR("reply"), str);
|
168
|
|
- #endif
|
|
146
|
+ debug(PSTR("reply"), str);
|
169
|
147
|
|
170
|
148
|
if (str) {
|
171
|
149
|
reset();
|