-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
284 lines (247 loc) · 6.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
arduino-I2C-debug-tool v1.0
Copyright (C) 2022 S.Pauthner
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Usage:
* # mastermode:
* [r/w] [address] [data0/sizeOfRequests] [data1] [data2] [data3] ... ([s])
* Seperate the blocks with spaces or commas; the s at the end should not be needed
* e.g. write: w 42 155 20 12 30 (s) or w42,115 20,12 30
* read : r 32 4 (s) or r0x20,4
*/
#include <Arduino.h>
#include <Wire.h>
// Configuration
#define baud 9600 // Baud rate for serial communication
// #define slaveAddress 0x20 // Comment to use master mode !Slave mode supports only listening!
#define monitor // Option to print the actual input; usefull on monitors that don't show the entered characters
#define version 1.0
#define invalid -1
#define inputlength 100
#define datalength 100
#ifndef slaveAddress
// global Variables
char input[inputlength];
uint8_t actualInputIndex = 0;
uint8_t data[datalength];
uint8_t actualDataIndex = 0;
uint8_t address = 0;
bool read = false;
// Programm
void clearInput() {
for (uint8_t i = 0; i < sizeof(input); i++) {
input[i] = invalid;
}
actualInputIndex = 0;
for (uint8_t i = 0; i < sizeof(data); i++) {
data[i] = 0;
}
actualDataIndex = 0;
address = 0;
read = false;
}
void printInput(String title) {
Serial.print(title);
for (uint8_t i = 0; i < actualInputIndex; i++) {
Serial.print(input[i]);
}
Serial.println();
}
void printData(String title) {
Serial.print(title);
for (size_t i = 1; i <= actualDataIndex; i++) {
Serial.print(data[i]);
Serial.print(" ");
}
Serial.println();
}
void makeTidy() { // make eventual bad input to a tidy array (replace , with spaces)(remove multiple spaces)
char array[actualInputIndex];
uint8_t count = 0;
uint8_t newMaxIndex = actualInputIndex;
for (uint8_t i = 0; i < actualInputIndex; i++) { // replace , with spaces
if (input[i] == ',') {
input[i] = ' ';
}
}
for (uint8_t i = 0; i < actualInputIndex; i++) { // remove double spaces
if (input[i] == ' ' && array[count-1] == ' ') {
newMaxIndex--;
}
else{
array[count] = input[i];
count++;
}
}
for (uint8_t i = newMaxIndex; i > 0; i--) { // remove spaces at the end
if (array[i] == ' ') {
newMaxIndex--;
}
else{
break;
}
}
if (array[newMaxIndex-1] == 13) { // remove \r at the end
newMaxIndex--;
}
if (array[newMaxIndex-1] == 10) { // remove eventual \n at the end
newMaxIndex--;
}
actualInputIndex = newMaxIndex; // override input and actualInputIndex
for (size_t i = 0; i < actualInputIndex; i++) {
input[i] = array[i];
}
}
void generateData() {
actualDataIndex = 0;
for (uint8_t i = 0; i < actualInputIndex; i++) {
if ((input[i] == 'r' || input[i] == 'w') && input[i+1] == ' ') {
i++;
}
else if ((input[i] == 'r' || input[i] == 'w') && input[i+1] != ' ') {
}
else if (input[i] == ' ') { // count count up by space
actualDataIndex++;
}
else {
data[actualDataIndex] = data[actualDataIndex]*10 + (input[i]-48); // move old number left and add new number right
}
}
}
void generateAddress() {
address = 0;
uint8_t hex = 0; // 0 = no hex format; else x position
for (size_t i = 0; i < actualInputIndex; i++) {
if (input[i] == 'x') {
hex = i;
break;
}
}
if (hex !=0) { // if I2C address in hex
address += (input[hex+1]-48)*16;
address += (input[hex+2]-48);
}
else {
address = data[0]; // if I2C address in dec
}
}
void generateRW() {
read = false;
for (size_t i = 0; i < actualInputIndex; i++) {
if (input[i] != ' ') {
if (input[i] == 'r' || input[i] == 'R') {
read = true; // Set operation to read if first letter r or R; else operation is write
}
break;
}
}
}
void readI2C() {
Wire.requestFrom(address, data[1]);
Serial.print("Requesting ");
Serial.print(data[1]);
Serial.print(" byte(s) from slave at ");
Serial.println(address);
if (Wire.available()) {
while (Wire.available()) {
Serial.print(Wire.read());
Serial.print(" ");
}
}
else {
Serial.print("No data received!");
}
Serial.println();
}
void writeI2C() {
Wire.beginTransmission(address);
for (uint8_t i = 1; i <= actualDataIndex; i++) {
Wire.write(data[i]);
}
Wire.endTransmission();
Serial.print("Wrote to slave at ");
Serial.print(address);
printData(" data: ");
}
void doI2Cstuff() {
makeTidy();
//printInput("After makeTidy: ");
generateData();
//printData("generated Data array: ");
generateAddress();
//Serial.print("Target address: ");
//Serial.println(address);
generateRW();
//Serial.print("Read? ");
//Serial.println(read);
if (read) {
readI2C();
}
else {
writeI2C();
}
clearInput();
}
void serialEvent() {
while (Serial.available()) {
input[actualInputIndex] = Serial.read();
if (input[actualInputIndex] == 's' || input[actualInputIndex] == 10 ){ // action if 's' or '\n'
doI2Cstuff();
}
else if (input[actualInputIndex] == 8) { // action by backspace
if (actualInputIndex > 0) {
actualInputIndex--;
}
}
else{
actualInputIndex++;
}
}
#ifdef monitor
printInput("Actual input: ");
#endif
}
#else
void receiveEvent(int count) {
Serial.print("Received ");
Serial.print(count);
Serial.println(" new data: ");
while (Wire.available()) {
Serial.print(Wire.read());
Serial.print(" ");
}
Serial.println();
}
#endif
void setup() {
Serial.begin(baud);
#ifndef slaveAddress
clearInput();
Wire.begin();
#else
Wire.begin(slaveAddress);
Wire.onReceive(receiveEvent);
#endif
Serial.print("Welcome to I2C debug tool v");
Serial.println(version);
#ifndef slaveAddress
Serial.println("Usage (master mode): [r/w] [address] [quantityOfBytesToRead/data0] [data1] [data2] ...");
Serial.println("Seperate the blocks with spaces or commas");
#else
Serial.print("Slave mode; Only listening on ");
Serial.println(slaveAddress);
#endif
}
void loop() {
// put your main code here, to run repeatedly:
}