Skip to content

Commit 790c527

Browse files
committed
- complete unit test for stack history i2c console
- warning memory hog of stack history & command dumping - enable DEBUG1 flag to open full debug message - added stop chars in serial debug - moved module test data in i2c console to progmem
1 parent b4f59e2 commit 790c527

File tree

15 files changed

+798
-78
lines changed

15 files changed

+798
-78
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ uint8_t processRX(const char * message, I2CConsoleMessage * result,
224224
return errcode;
225225
}
226226
/**
227-
* log - done command:
227+
* log - done command: tx rx addr slow
228228
*
229-
* wip - tx string
229+
* wip -
230230
*
231231
* @param message
232232
* @param result
@@ -242,6 +242,7 @@ uint8_t I2CConsoleParser(const char * message, I2CConsoleMessage * result)
242242
result->message[0] = '\0';
243243

244244
char command[10];
245+
245246
if (sscanf(message, "%s ", command) != 1)
246247
{
247248
TRACE()
@@ -281,6 +282,7 @@ uint8_t I2CConsoleParser(const char * message, I2CConsoleMessage * result)
281282

282283
void I2CConsoleDumpCommand(const I2CConsoleMessage * command)
283284
{
285+
#ifdef DEBUG1
284286
LOG("========================================================");
285287
LOG("address %x", command->address);
286288
TRACE_INT(command->isDelayBetweenBytes);
@@ -311,6 +313,9 @@ void I2CConsoleDumpCommand(const I2CConsoleMessage * command)
311313
}
312314
}
313315
LOG("========================================================");
316+
#else
317+
(void) command;
318+
#endif
314319
}
315320

316321
#ifndef CXXTEST

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct _i2cMessage
4545
* LOOP 5 RX 6 2 ab 03 - loop in 5 seconds for sending RX 6 2 ab 03
4646
* SLOW 0 - set slow sending off
4747
*
48-
* @param message
48+
* @param inputmessage
4949
* @param result
5050
* @return - 0 on success
5151
* - 1 on format error
@@ -68,6 +68,8 @@ uint8_t I2CConsoleSendCommand(I2CConsoleMessage * command);
6868
/**
6969
* dump I2CConsoleMessage info to serial/stdout
7070
*
71+
* note: very expensive, set DEBUG1 flag to enable this api
72+
*
7173
* @param command
7274
*/
7375
void I2CConsoleDumpCommand(const I2CConsoleMessage * command);
@@ -86,4 +88,52 @@ uint8_t I2CConsoleGetCurrentAddress();
8688
*/
8789
uint8_t I2CConsoleGetSlowSendingStatus();
8890

91+
///////////////////////////////////////////////////////////
92+
/// Command history support
93+
///////////////////////////////////////////////////////////
94+
95+
/**
96+
* get previous saved message, doesn't actually clear it
97+
*
98+
* @return message if exists, NULL if none
99+
*/
100+
const char * I2CConsoleStackMoveUp();
101+
102+
/**
103+
* get next saved message, doesn't actually clear it
104+
*
105+
* @return message if exists, NULL if none
106+
*/
107+
const char * I2CConsoleStackMoveDown();
108+
109+
/**
110+
* Push to stack message history, this will roll over obsolete messages
111+
* Limit to 5 history
112+
*
113+
* @param message - string to push in
114+
*/
115+
void I2CConsoleStackPush(const char * message);
116+
117+
/**
118+
* Reset stack current index to start index
119+
*/
120+
void I2CConsoleStackResetIndex();
121+
122+
/**
123+
* Call this before calling I2CConsoleStack* apis
124+
*/
125+
void I2CConsoleStackInit();
126+
127+
/**
128+
* Force init stack
129+
*/
130+
void I2CConsoleStackReInit();
131+
132+
/**
133+
* get stack count
134+
*
135+
* @return stack count
136+
*/
137+
uint8_t I2CConsoleStackGetCount();
138+
89139
#endif /* TEST_I2C_CONSOLE_I2CCONSOLE_H_ */
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* I2CConsoleStack.c
3+
*
4+
* Created on: May 31, 2017
5+
* Author: dat
6+
*/
7+
8+
#include "I2CConsole.h"
9+
#include <stdio.h>
10+
#include <string.h>
11+
12+
#define HISTORY_COUNT 5
13+
14+
static char g_i2c_history[HISTORY_COUNT][50];
15+
static uint8_t g_i2c_current_index;
16+
static uint8_t g_i2c_start_index; // latest index
17+
static uint8_t g_i2c_stack_count;
18+
19+
/**
20+
* internal - get distance from 2 indexes
21+
*
22+
* @param from
23+
* @param to
24+
* @return - eg. from 1 to 3 is 2, from 4 to 0 is 1
25+
*/
26+
uint8_t getIndexDistance(uint8_t from, uint8_t to)
27+
{
28+
if (from <= to)
29+
{
30+
return to - from;
31+
}
32+
33+
return (to + HISTORY_COUNT - from);
34+
}
35+
36+
/*
37+
* Index rollover, 4->0 since we only support 5 histories
38+
*/
39+
uint8_t getNextIndex(uint8_t index)
40+
{
41+
if (index == HISTORY_COUNT -1)
42+
{
43+
return 0;
44+
}
45+
46+
return index + 1;
47+
}
48+
49+
/*
50+
* Index rewind, 0->4 since we only support 5 histories
51+
*/
52+
uint8_t getPreviousIndex(uint8_t index)
53+
{
54+
if (index == 0)
55+
{
56+
return (HISTORY_COUNT - 1);
57+
}
58+
59+
return index - 1;
60+
}
61+
62+
63+
/**
64+
* get previous saved message, doesn't actually clear it
65+
*
66+
* @return message if exists, NULL if none
67+
*/
68+
const char * I2CConsoleStackMoveUp()
69+
{
70+
// null if nothing from stack
71+
if (g_i2c_stack_count == 0)
72+
{
73+
return NULL;
74+
}
75+
76+
const char * retVal = g_i2c_history[g_i2c_current_index];
77+
78+
// limit current index
79+
if (getIndexDistance(g_i2c_current_index, g_i2c_start_index) > 0)
80+
{
81+
g_i2c_current_index = getNextIndex(g_i2c_current_index);
82+
}
83+
84+
85+
return retVal;
86+
}
87+
88+
/**
89+
* get next saved message, doesn't actually clear it
90+
*
91+
* @return message if exists, NULL if none
92+
*/
93+
const char * I2CConsoleStackMoveDown()
94+
{
95+
// null if nothing from stack
96+
if (g_i2c_stack_count == 0)
97+
{
98+
return NULL;
99+
}
100+
101+
const char * retVal = g_i2c_history[g_i2c_current_index];
102+
103+
// limit current index
104+
if (getIndexDistance(g_i2c_current_index, g_i2c_start_index) < g_i2c_stack_count - 1)
105+
{
106+
g_i2c_current_index = getPreviousIndex(g_i2c_current_index);
107+
}
108+
109+
return retVal;
110+
}
111+
112+
/**
113+
* Push to stack message history, this will roll over obsolete messages
114+
* Limit to 5 history
115+
*
116+
* @param message - string to push in
117+
*/
118+
void I2CConsoleStackPush(const char * message)
119+
{
120+
I2CConsoleStackInit();
121+
122+
// push message to history
123+
124+
// update start index
125+
g_i2c_start_index = (g_i2c_stack_count == 0)? 0 : getNextIndex(g_i2c_start_index);
126+
127+
// update history[start index]
128+
strcpy(g_i2c_history[g_i2c_start_index], message);
129+
130+
// set current index to start index
131+
g_i2c_current_index = g_i2c_start_index;
132+
133+
// limit stack count to history count
134+
if (g_i2c_stack_count < HISTORY_COUNT)
135+
{
136+
++g_i2c_stack_count;
137+
}
138+
}
139+
140+
void I2CConsoleStackResetIndex()
141+
{
142+
g_i2c_current_index = g_i2c_start_index;
143+
}
144+
145+
void I2CConsoleStackInit()
146+
{
147+
static uint8_t _stack_inited = 0;
148+
if (!_stack_inited)
149+
{
150+
I2CConsoleStackReInit();
151+
_stack_inited = 1;
152+
}
153+
}
154+
155+
void I2CConsoleStackReInit()
156+
{
157+
g_i2c_stack_count = 0;
158+
g_i2c_current_index = 0;
159+
g_i2c_start_index = 0;
160+
}
161+
162+
uint8_t I2CConsoleStackGetCount()
163+
{
164+
return g_i2c_stack_count;
165+
}

c-project/without-os/i2c-console/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ INCLUDES = ./
1212

1313
# unit test files
1414
# remember to put cxx test headers in test/*Test.h
15-
TEST_SOURCES = I2CConsole.c
15+
TEST_SOURCES = I2CConsole.c I2CConsoleStack.c
1616
TEST_DEBUG = no
17+
TEST_CFLAGS = -std=gnu++0x
1718

1819
# specify your preferred .mk file
1920
include $(TOP)/common/without-os.mk

0 commit comments

Comments
 (0)