|
@@ -28,11 +28,17 @@
|
28
|
28
|
|
29
|
29
|
#include <Wire.h>
|
30
|
30
|
|
|
31
|
+#include "../libs/hex_print.h"
|
|
32
|
+
|
31
|
33
|
TWIBus i2c;
|
32
|
34
|
|
33
|
35
|
TWIBus::TWIBus() {
|
34
|
36
|
#if I2C_SLAVE_ADDRESS == 0
|
35
|
|
- Wire.begin(); // No address joins the BUS as the master
|
|
37
|
+ Wire.begin( // No address joins the BUS as the master
|
|
38
|
+ #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
|
|
39
|
+ pin_t(I2C_SDA_PIN), pin_t(I2C_SCL_PIN)
|
|
40
|
+ #endif
|
|
41
|
+ );
|
36
|
42
|
#else
|
37
|
43
|
Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
|
38
|
44
|
#endif
|
|
@@ -88,14 +94,53 @@ void TWIBus::echoprefix(uint8_t bytes, FSTR_P const pref, uint8_t adr) {
|
88
|
94
|
}
|
89
|
95
|
|
90
|
96
|
// static
|
91
|
|
-void TWIBus::echodata(uint8_t bytes, FSTR_P const pref, uint8_t adr) {
|
92
|
|
- echoprefix(bytes, pref, adr);
|
93
|
|
- while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
|
|
97
|
+void TWIBus::echodata(uint8_t bytes, FSTR_P const pref, uint8_t adr, const uint8_t style/*=0*/) {
|
|
98
|
+ union TwoBytesToInt16 { uint8_t bytes[2]; int16_t integervalue; };
|
|
99
|
+ TwoBytesToInt16 ConversionUnion;
|
|
100
|
+
|
|
101
|
+ echoprefix(bytes, pref, adr);
|
|
102
|
+
|
|
103
|
+ while (bytes-- && Wire.available()) {
|
|
104
|
+ int value = Wire.read();
|
|
105
|
+ switch (style) {
|
|
106
|
+
|
|
107
|
+ // Style 1, HEX DUMP
|
|
108
|
+ case 1:
|
|
109
|
+ SERIAL_CHAR(hex_nybble((value & 0xF0) >> 4));
|
|
110
|
+ SERIAL_CHAR(hex_nybble(value & 0x0F));
|
|
111
|
+ if (bytes) SERIAL_CHAR(' ');
|
|
112
|
+ break;
|
|
113
|
+
|
|
114
|
+ // Style 2, signed two byte integer (int16)
|
|
115
|
+ case 2:
|
|
116
|
+ if (bytes == 1)
|
|
117
|
+ ConversionUnion.bytes[1] = (uint8_t)value;
|
|
118
|
+ else if (bytes == 0) {
|
|
119
|
+ ConversionUnion.bytes[0] = (uint8_t)value;
|
|
120
|
+ // Output value in base 10 (standard decimal)
|
|
121
|
+ SERIAL_ECHO(ConversionUnion.integervalue);
|
|
122
|
+ }
|
|
123
|
+ break;
|
|
124
|
+
|
|
125
|
+ // Style 3, unsigned byte, base 10 (uint8)
|
|
126
|
+ case 3:
|
|
127
|
+ SERIAL_ECHO(value);
|
|
128
|
+ if (bytes) SERIAL_CHAR(' ');
|
|
129
|
+ break;
|
|
130
|
+
|
|
131
|
+ // Default style (zero), raw serial output
|
|
132
|
+ default:
|
|
133
|
+ // This can cause issues with some serial consoles, Pronterface is an example where things go wrong
|
|
134
|
+ SERIAL_CHAR(value);
|
|
135
|
+ break;
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+
|
94
|
139
|
SERIAL_EOL();
|
95
|
140
|
}
|
96
|
141
|
|
97
|
|
-void TWIBus::echobuffer(FSTR_P const pref, uint8_t adr) {
|
98
|
|
- echoprefix(buffer_s, pref, adr);
|
|
142
|
+void TWIBus::echobuffer(FSTR_P const prefix, uint8_t adr) {
|
|
143
|
+ echoprefix(buffer_s, prefix, adr);
|
99
|
144
|
LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
|
100
|
145
|
SERIAL_EOL();
|
101
|
146
|
}
|
|
@@ -114,11 +159,11 @@ bool TWIBus::request(const uint8_t bytes) {
|
114
|
159
|
return true;
|
115
|
160
|
}
|
116
|
161
|
|
117
|
|
-void TWIBus::relay(const uint8_t bytes) {
|
|
162
|
+void TWIBus::relay(const uint8_t bytes, const uint8_t style/*=0*/) {
|
118
|
163
|
debug(F("relay"), bytes);
|
119
|
164
|
|
120
|
165
|
if (request(bytes))
|
121
|
|
- echodata(bytes, F("i2c-reply"), addr);
|
|
166
|
+ echodata(bytes, F("i2c-reply"), addr, style);
|
122
|
167
|
}
|
123
|
168
|
|
124
|
169
|
uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
|