|
@@ -86,32 +86,72 @@ void TWIBus::send() {
|
86
|
86
|
this->reset();
|
87
|
87
|
}
|
88
|
88
|
|
89
|
|
-void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
|
89
|
+// static
|
|
90
|
+void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
|
90
|
91
|
SERIAL_ECHO_START;
|
91
|
92
|
serialprintPGM(prefix);
|
92
|
93
|
SERIAL_ECHOPAIR(": from:", adr);
|
93
|
94
|
SERIAL_ECHOPAIR(" bytes:", bytes);
|
94
|
95
|
SERIAL_ECHOPGM (" data:");
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+// static
|
|
99
|
+void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
|
|
100
|
+ echoprefix(bytes, prefix, adr);
|
95
|
101
|
while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
96
|
102
|
SERIAL_EOL;
|
97
|
103
|
}
|
98
|
104
|
|
99
|
|
-void TWIBus::reqbytes(const uint8_t bytes) {
|
100
|
|
- if (!this->addr) return;
|
|
105
|
+void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
|
|
106
|
+ echoprefix(this->buffer_s, prefix, adr);
|
|
107
|
+ for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
|
|
108
|
+ SERIAL_EOL;
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+bool TWIBus::request(const uint8_t bytes) {
|
|
112
|
+ if (!this->addr) return false;
|
101
|
113
|
|
102
|
114
|
#if ENABLED(DEBUG_TWIBUS)
|
103
|
|
- debug(PSTR("reqbytes"), bytes);
|
|
115
|
+ debug(PSTR("request"), bytes);
|
104
|
116
|
#endif
|
105
|
117
|
|
106
|
118
|
// requestFrom() is a blocking function
|
107
|
119
|
Wire.requestFrom(this->addr, bytes);
|
108
|
120
|
|
109
|
|
- // Wait until all bytes arrive, or timeout
|
|
121
|
+ // Wait for all bytes to arrive
|
110
|
122
|
millis_t t = millis() + this->timeout;
|
111
|
|
- while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
|
|
123
|
+ while (Wire.available() < bytes)
|
|
124
|
+ if (ELAPSED(millis(), t)) {
|
|
125
|
+ #if ENABLED(DEBUG_TWIBUS)
|
|
126
|
+ SERIAL_ECHO_START;
|
|
127
|
+ SERIAL_ECHOLNPGM("i2c timeout");
|
|
128
|
+ #endif
|
|
129
|
+ return false;
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ return true;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+void TWIBus::relay(const uint8_t bytes) {
|
|
136
|
+ #if ENABLED(DEBUG_TWIBUS)
|
|
137
|
+ debug(PSTR("relay"), bytes);
|
|
138
|
+ #endif
|
|
139
|
+
|
|
140
|
+ if (this->request(bytes))
|
|
141
|
+ echodata(bytes, PSTR("i2c-reply"), this->addr);
|
|
142
|
+}
|
|
143
|
+
|
|
144
|
+uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|
|
145
|
+ this->reset();
|
|
146
|
+ uint8_t count = 0;
|
|
147
|
+ while (count < bytes && Wire.available())
|
|
148
|
+ dst[count++] = Wire.read();
|
|
149
|
+ return count;
|
|
150
|
+}
|
112
|
151
|
|
113
|
|
- // Simply echo the data to the bus
|
114
|
|
- this->echodata(bytes, PSTR("i2c-reply"), this->addr);
|
|
152
|
+// static
|
|
153
|
+void TWIBus::flush() {
|
|
154
|
+ while (Wire.available()) Wire.read();
|
115
|
155
|
}
|
116
|
156
|
|
117
|
157
|
#if I2C_SLAVE_ADDRESS > 0
|
|
@@ -120,7 +160,7 @@ void TWIBus::reqbytes(const uint8_t bytes) {
|
120
|
160
|
#if ENABLED(DEBUG_TWIBUS)
|
121
|
161
|
debug(PSTR("receive"), bytes);
|
122
|
162
|
#endif
|
123
|
|
- this->echodata(bytes, PSTR("i2c-receive"), 0);
|
|
163
|
+ echodata(bytes, PSTR("i2c-receive"), 0);
|
124
|
164
|
}
|
125
|
165
|
|
126
|
166
|
void TWIBus::reply(char str[]/*=NULL*/) {
|
|
@@ -142,6 +182,7 @@ void TWIBus::reqbytes(const uint8_t bytes) {
|
142
|
182
|
|
143
|
183
|
#if ENABLED(DEBUG_TWIBUS)
|
144
|
184
|
|
|
185
|
+ // static
|
145
|
186
|
void TWIBus::prefix(const char func[]) {
|
146
|
187
|
SERIAL_ECHOPGM("TWIBus::");
|
147
|
188
|
serialprintPGM(func);
|