Skip to content

Commit e242d34

Browse files
committed
Removed logging function from sketch example
Changed callback signature to pass in Configurator pointer Modified Configurator to expose log() and changed sketch to use it
1 parent 257db5a commit e242d34

File tree

3 files changed

+40
-48
lines changed

3 files changed

+40
-48
lines changed

ConfigLib.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,15 @@ void Configurator::dumpBlocksToConsole(int startPos)
372372
}
373373

374374
//#!*******************************************************************************************
375-
void Configurator::printConfigCommandHelp(void(*printConfigItemHelp)())
375+
void Configurator::printConfigCommandHelp(void(*printConfigItemHelp)(Configurator*))
376376
{
377377
log(F("Commands are"));
378378
log(F("S:K,V = Set item K to value V"));
379379
log(F("---------------------------------------------"));
380380

381381
if (printConfigItemHelp != NULL) {
382382
log(F("Item Id : Item Name : Value"));
383-
printConfigItemHelp();
383+
printConfigItemHelp(this);
384384
}
385385

386386
log(F("---------------------------------------------"));
@@ -431,9 +431,9 @@ void Configurator::loadConfigFromEEPROM(const char* tag, unsigned char* config,
431431
void Configurator::runConfigUI(const char* configTag,
432432
unsigned char* config,
433433
int configLen,
434-
void(*printConfigItemHelp)(),
435-
void(*printConfig)(),
436-
void(*setConfigItem)(const char*, const char*))
434+
void(*printConfigItemHelp)(Configurator*),
435+
void(*printConfig)(Configurator*),
436+
void(*setConfigItem)(Configurator*,const char*, const char*))
437437
{
438438
char lineBuffer[32];
439439

@@ -490,7 +490,7 @@ void Configurator::runConfigUI(const char* configTag,
490490

491491
// ** PRINT *************************************************************
492492
else if (strcmp(lineBuffer,"P") == 0) {
493-
printConfig();
493+
printConfig(this);
494494
strcpy(lineBuffer, "");
495495
}
496496

@@ -523,7 +523,7 @@ void Configurator::runConfigUI(const char* configTag,
523523

524524
log(F("Setting item [%s] to [%s]"), key, val);
525525

526-
setConfigItem(key, val);
526+
setConfigItem(this, key, val);
527527

528528
strcpy(lineBuffer, "");
529529
}
@@ -541,9 +541,9 @@ void Configurator::runConfigUI(const char* configTag,
541541
void Configurator::initConfig( const char* configTag,
542542
unsigned char* config,
543543
int configLen,
544-
void(*printConfigItemHelp)(),
545-
void(*printConfig)(),
546-
void(*setConfigItem)(const char*, const char*))
544+
void(*printConfigItemHelp)(Configurator*),
545+
void(*printConfig)(Configurator*),
546+
void(*setConfigItem)(Configurator*, const char*, const char*))
547547
{
548548
int initCycleDelay = 500; // cycle time in ms
549549
long initPeriodCountMax = m_configSelectPeriod / initCycleDelay;
@@ -555,7 +555,7 @@ void Configurator::initConfig( const char* configTag,
555555
log(F("Using config"));
556556

557557
loadConfigFromEEPROM(configTag, config, configLen);
558-
printConfig();
558+
printConfig(this);
559559

560560
log(F("Press 'C' and 'Enter' to enter config mode or 'Q' to continue immediately"));
561561

ConfigLib.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,34 @@ class Configurator
6060
void initConfig(const char* configTag,
6161
unsigned char* config,
6262
int configLen,
63-
void(*printConfigItemHelp)(),
64-
void(*printConfig)(),
65-
void(*setConfigItem)(const char*, const char*));
66-
63+
void(*printConfigItemHelp)(Configurator*),
64+
void(*printConfig)(Configurator*),
65+
void(*setConfigItem)(Configurator*,const char*, const char*));
66+
67+
/*
68+
Logs the string to the stream after using
69+
printf to handle the string substitution of the varargs
70+
*/
71+
void log(const __FlashStringHelper * fsh, ...);
72+
73+
6774
protected:
6875

6976
void runConfigUI(const char* configTag,
7077
unsigned char* config,
7178
int configLen,
72-
void(*printConfigItemHelp)(),
73-
void(*printConfig)(),
74-
void(*setConfigItem)(const char*, const char*));
79+
void(*printConfigItemHelp)(Configurator*),
80+
void(*printConfig)(Configurator*),
81+
void(*setConfigItem)(Configurator*, const char*, const char*));
7582

7683
Stream* m_stream = NULL;
7784
int m_logBufferSize;
7885
int m_configSelectPeriod;
7986

80-
void log(const __FlashStringHelper * fsh, ...);
8187
void logToStream(const char* msg);
8288

8389
void dumpBlocksToConsole(int startPos);
84-
void printConfigCommandHelp(void(*printConfigItemHelp)());
90+
void printConfigCommandHelp(void(*printConfigItemHelp)(Configurator*));
8591

8692
void sprintf_vargs(char* buffer, int bufferlen, char * format, ...);
8793
String getField(String* msg, char fieldSep) ;

Examples/Config/Config.ino

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,6 @@
33
#include <ConfigLib.h>
44
#include <EEPROM.h>
55

6-
//:TODO:Move utility function into Configurator class
7-
8-
// Utility function
9-
void print(const __FlashStringHelper * fmt, ...)
10-
{
11-
char msgBuffer[128];
12-
va_list args;
13-
va_start(args, fmt);
14-
#ifdef __AVR__
15-
vsnprintf_P(msgBuffer, sizeof(msgBuffer), (const char *)fmt, args); // progmem for AVR
16-
#else
17-
vsnprintf(msgBuffer, sizeof(msgBuffer), (const char *)fmt, args); // for the rest of the world
18-
#endif
19-
va_end(args);
20-
Serial.println(msgBuffer);
21-
}
22-
236

247
// Config structure which will be read from EEPROM
258
// with some default values in case we can't read any config from EEPROM
@@ -29,23 +12,26 @@ Config config = { 100, 199, "AAA" };
2912
#define CONFIG_TAG "ESWC"
3013

3114
// Callback function which prints out information on the config items and values
32-
void printConfigItemHelp() {
33-
print(F("RFM_NODE_ID RFM_NODE_ID int"));
34-
print(F("RFM_NETWORK_ID RFM_NETWORK_ID int"));
35-
print(F("NODE_ID NODE_ID char[%d]"), sizeof(config.nodeId));
15+
void printConfigItemHelp(Configurator* configurator)
16+
{
17+
configurator->log(F("RFM_NODE_ID RFM_NODE_ID int"));
18+
configurator->log(F("RFM_NETWORK_ID RFM_NETWORK_ID int"));
19+
configurator->log(F("NODE_ID NODE_ID char[%d]"), sizeof(config.nodeId));
3620
}
3721

3822
// Callback function which prints out the config
39-
void printConfig() {
40-
print(F("Current config"));
41-
print(F(" RFM_NODE_ID = [%d]"), config.rfmNodeId);
42-
print(F(" RFM_NETWORK_ID = [%d]"), config.rfmNetworkId);
43-
print(F(" NODE_ID = [%s]"), config.nodeId);
23+
void printConfig(Configurator* configurator)
24+
{
25+
configurator->log(F("Current config"));
26+
configurator->log(F(" RFM_NODE_ID = [%d]"), config.rfmNodeId);
27+
configurator->log(F(" RFM_NETWORK_ID = [%d]"), config.rfmNetworkId);
28+
configurator->log(F(" NODE_ID = [%s]"), config.nodeId);
4429
}
4530

4631

4732
// Callback function which sets the config item key to the value val
48-
void setConfigItem(const char* key, const char* val) {
33+
void setConfigItem(Configurator* configurator, const char* key, const char* val)
34+
{
4935
if (strcmp(key, "RFM_NODE_ID")==0) {
5036
config.rfmNodeId = atoi(val);
5137
}
@@ -56,7 +42,7 @@ void setConfigItem(const char* key, const char* val) {
5642
int _len = strlcpy(config.nodeId, val, sizeof(config.nodeId));
5743
}
5844
else {
59-
print(F("Unknown key type"));
45+
configurator->log(F("Unknown key type"));
6046
}
6147
}
6248

0 commit comments

Comments
 (0)