Skip to content

Commit 019fbc6

Browse files
committed
- added ping command in i2c-console
- restructure main console scan command
1 parent 8cb0e4f commit 019fbc6

File tree

6 files changed

+74
-16
lines changed

6 files changed

+74
-16
lines changed

c-project/without-os/i2c-console/I2CConsole.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ typedef enum _i2ccommand
1717
SEND = 0,
1818
SENDNRECV,
1919
SET_ADDRESS,
20-
SET_SLOW
20+
SET_SLOW,
21+
PING
2122
} I2CConsoleCommand;
2223

2324
typedef struct _i2cMessage
@@ -44,6 +45,7 @@ typedef struct _i2cMessage
4445
* LOOP 3 TX "hello world" - loop in 3 seconds for sending TX "hello world"
4546
* LOOP 5 RX 6 2 ab 03 - loop in 5 seconds for sending RX 6 2 ab 03
4647
* SLOW 0 - set slow sending off
48+
* PING 23 - check if address 0x23 is alive
4749
*
4850
* @param inputmessage
4951
* @param result
@@ -62,6 +64,7 @@ uint8_t I2CConsoleParser(const char * message, I2CConsoleMessage * result);
6264
* - 1 on failed sending/recv
6365
* - 2 on invalid message
6466
* - 3 on invalid address
67+
* - 4 on timeout of ping
6568
*/
6669
uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command);
6770

c-project/without-os/i2c-console/I2CConsoleIO.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command)
4141
{
4242
_i2c_address = (uint8_t) command->address;
4343
}
44+
else if (command->command == PING)
45+
{
46+
// don't change command address if it's PING
47+
;
48+
}
4449
else
4550
{
4651
command->address = _i2c_address;
@@ -71,7 +76,7 @@ uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command)
7176
command->isDelayBetweenBytes = _i2c_slowTx;
7277
}
7378

74-
if (_i2c_address != 0x00)
79+
if (command->address != 0x00)
7580
{
7681
if (command->command == SEND)
7782
{
@@ -104,6 +109,12 @@ uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command)
104109
command->rx_len, _i2c_slowTx);
105110
}
106111
}
112+
else if (command->command == PING)
113+
{
114+
retVal = (I2CCheckAlive(command->address) == 0) ? retVal : 4;
115+
TRACE_INT(retVal);
116+
LOG("address %x", command->address);
117+
}
107118
}
108119
else
109120
{

c-project/without-os/i2c-console/I2CConsoleParser.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ uint8_t processRX(const char * message, I2CConsoleMessage * result,
237237
return errcode;
238238
}
239239

240+
uint8_t processPing(const char * message, I2CConsoleMessage * result,
241+
uint8_t errcode)
242+
{
243+
if (errcode) return errcode;
244+
245+
// set address
246+
result->command = PING;
247+
if (sscanf(message, "%x", &result->address) != 1)
248+
{
249+
TRACE()
250+
errcode = 1;
251+
}
252+
253+
return errcode;
254+
}
255+
240256
/**
241257
* internal - final touch of result
242258
*
@@ -289,10 +305,11 @@ void parserInit()
289305
registerParserCallback("slow", processSlow);
290306
registerParserCallback("tx", processTX);
291307
registerParserCallback("rx", processRX);
308+
registerParserCallback("ping", processPing);
292309
}
293310

294311
/**
295-
* log - done command: tx rx addr slow
312+
* log - done command: tx rx addr slow ping
296313
*
297314
* wip -
298315
*

c-project/without-os/i2c-console/main.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
#include "I2CConsole.h"
88
#include "SerialDebug.h"
99
#include "PgmStorage.h"
10-
#include "Timer.h"
11-
#include "I2C.h"
1210
#include "Millis.h"
1311

1412
#include <stdio.h>
1513
#include <util/delay.h>
1614

17-
18-
const char PROGMEM helpaddr1[] = "Hint address: LCD 0x28, compass 0x19";
1915
const char PROGMEM help1[] = "Sample command \t\t - explanation (command case insensitive)";
2016
const char PROGMEM help2[] = "ADDR 28 \t\t - set i2c 7-bit address as 0x28";
2117
const char PROGMEM help3[] = "TX 2 00 03 \t\t - send 2 bytes 0x00 and 0x03";
2218
const char PROGMEM help0[] = "TX \"hello world\" \t - send string \"hello world\"";
2319
const char PROGMEM help4[] = "RX 6 2 ab 03 \t\t - send 2 bytes 0xab and 0x03, receive 6 bytes back to rx";
2420
const char PROGMEM help41[] = "SLOW 0 \t\t\t - set slow sending off";
21+
const char PROGMEM help42[] = "PING 23 \t\t - check if address 0x23 is alive";
2522
const char PROGMEM helploop1[] = "LOOP 2 TX 2 00 03 \t - loop the command 'TX 2 00 03' for 2 seconds";
2623
const char PROGMEM helploop2[] = "LOOP 5 RX 6 1 ab \t - loop the command 'RX 6 1 ab' for 5 seconds";
2724
const char PROGMEM help5[] = "---------------";
@@ -38,9 +35,6 @@ void showHelp()
3835
{
3936
char msg[PGM_SIZE];
4037

41-
PgmStorageGet(msg, helpaddr1);
42-
SerialDebugPrint(msg);
43-
4438
PgmStorageGet(msg, help1);
4539
SerialDebugPrint(msg);
4640

@@ -59,6 +53,9 @@ void showHelp()
5953
PgmStorageGet(msg, help41);
6054
SerialDebugPrint(msg);
6155

56+
PgmStorageGet(msg, help42);
57+
SerialDebugPrint(msg);
58+
6259
PgmStorageGet(msg, helploop1);
6360
SerialDebugPrint(msg);
6461

@@ -187,15 +184,38 @@ void showHistory()
187184
void runScan()
188185
{
189186
uint8_t device_addr;
187+
I2CConsoleMessage msg;
188+
char foundDevice = 0;
190189

191190
SerialDebugPrint("Scanning for devices ...");
192191
for (device_addr = 0; device_addr < 128; ++device_addr)
193192
{
194-
if (!I2CCheckAlive(device_addr))
193+
char buff[20];
194+
snprintf(buff, 20, "ping %x", device_addr);
195+
196+
if (!I2CConsoleParser(buff, &msg))
195197
{
196-
SerialDebugPrint("Found device 0x%x", device_addr);
198+
uint8_t sendResult = I2CConsoleSendCommand(&msg);
199+
if (!sendResult)
200+
{
201+
SerialDebugPrint("Found device 0x%x", device_addr);
202+
foundDevice = 1;
203+
}
204+
else
205+
{
206+
LOG("Error code %d for %s", sendResult, buff);
207+
}
208+
}
209+
else
210+
{
211+
LOG("Error parsing %s", buff);
197212
}
198213
}
214+
215+
if (!foundDevice)
216+
{
217+
SerialDebugPrint("No I2C device found");
218+
}
199219
}
200220

201221
void parseNRunMessage(const char * message)
@@ -220,14 +240,14 @@ void parseNRunMessage(const char * message)
220240
uint8_t sendResult = I2CConsoleSendCommand(&cmd);
221241
if (sendResult == 0)
222242
{
223-
SerialDebugPrint("sent %s", message);
243+
SerialDebugPrint("OK - '%s'", message);
224244

225245
if (cmd.command == SENDNRECV)
226246
{
227247
SerialDebugPrint("RX result:");
228248
for (i = 0; i < cmd.rx_len; ++i)
229249
{
230-
SerialDebugPrint(" rx[%d] = 0x%x = %d = '%c'", i, cmd.rx[i],
250+
SerialDebugPrint(" rx[%d] = 0x%x \t= %d \t= '%c'", i, cmd.rx[i],
231251
cmd.rx[i], cmd.rx[i]);
232252
}
233253
}

c-project/without-os/i2c-console/test/I2CConsoleTest.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class MyTS: public CxxTest::TestSuite
3333
const string input3 = "RX 6 2 ab 03";
3434
const string input4 = "TX 10 1 2 3 4 5 6 7 8 9 10";
3535
const string input5 = "slow 0";
36+
const string input6 = "ping 23";
3637

3738
I2CConsoleMessage message;
3839
resetMessage(&message);
@@ -71,13 +72,20 @@ class MyTS: public CxxTest::TestSuite
7172
TS_ASSERT_EQUALS(message.isValid, 1);
7273
TS_ASSERT_EQUALS(message.tx_len, 10);
7374
I2CConsoleDumpCommand(&message);
74-
//
75+
7576
resetMessage(&message);
7677
TS_ASSERT_EQUALS(I2CConsoleParser(input5.c_str(), &message), 0);
7778
TS_ASSERT_EQUALS(message.command, SET_SLOW);
7879
TS_ASSERT_EQUALS(message.isValid, 1);
7980
TS_ASSERT_EQUALS(message.isDelayBetweenBytes, 0);
8081
I2CConsoleDumpCommand(&message);
82+
83+
resetMessage(&message);
84+
TS_ASSERT_EQUALS(I2CConsoleParser(input6.c_str(), &message), 0);
85+
TS_ASSERT_EQUALS(message.command, PING);
86+
TS_ASSERT_EQUALS(message.isValid, 1);
87+
TS_ASSERT_EQUALS(message.address, 0x23);
88+
I2CConsoleDumpCommand(&message);
8189
}
8290

8391
void testConsoleParserMessageSuccess()

common/utils/SerialDebug.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define SERIALDEBUG_H_
1010

1111
#include <stdio.h>
12-
#include <string.h>
1312

1413
#include "MemoryFree.h"
1514
#include "AVRString.h"

0 commit comments

Comments
 (0)