color refactoring

This commit is contained in:
Wolfgang Hottgenroth 2020-11-03 17:30:01 +01:00
parent bef20bcaed
commit 6bbe4d7eaf
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 39 additions and 16 deletions

View File

@ -12,6 +12,7 @@ void logFree();
// log a message, make sure it is a null-terminated string
// return value can be ignored, it is only used in test
int logMsg(const char *format, ...);
int errMsg(const char *format, ...);
// reads the ringbuffer and transfers data to output channel
// call this from the idle-loop

View File

@ -60,17 +60,22 @@ static void flashGreenLed(void *handle) {
}
#endif // TEST
int logMsg(const char *format, ...) {
static int innerLogMsg(const char *pre, const char *post, const char *format, va_list vl) {
int res = -1;
char msgBuffer[MSGBUFFER_SIZE];
int offset = 0;
char msgBuffer[MSGBUFFER_SIZE+20];
memset(msgBuffer, 0, MSGBUFFER_SIZE+20);
va_list vl;
va_start(vl, format);
int vcnt = vsnprintf(msgBuffer, MSGBUFFER_SIZE-2, format, vl);
va_end(vl);
if (pre) {
strcpy(msgBuffer, pre);
offset = strlen(pre);
}
int vcnt = vsnprintf(msgBuffer+offset, MSGBUFFER_SIZE, format, vl);
if (vcnt < MSGBUFFER_SIZE) {
strcat(msgBuffer, "\r\n");
if (post) {
strcat(msgBuffer, post);
}
if (-1 == (res = ringbufferPut(&logBuffer, (uint8_t*) msgBuffer, strlen(msgBuffer)))) {
#ifndef TEST
@ -84,3 +89,21 @@ int logMsg(const char *format, ...) {
}
return res;
}
int logMsg(const char *format, ...) {
va_list vl;
va_start(vl, format);
int res = innerLogMsg(NULL, "\r\n", format, vl);
va_end(vl);
return res;
}
int errMsg(const char *format, ...) {
va_list vl;
va_start(vl, format);
int res = innerLogMsg("\x1b[31;1m", "\x1b[0m\r\n", format, vl);
va_end(vl);
return res;
}

View File

@ -118,28 +118,28 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
mbus_data_variable *data_var = &(frame_data.data_var);
logMsg("papf sts: %02x", data_var->header.status);
if ((data_var->header.status & 0x01)) {
logMsg("\x1b[31;1mpapf sts: Application Busy\x1b[0m");
errMsg("papf sts: Application Busy");
}
if ((data_var->header.status & 0x02)) {
logMsg("\x1b[31;1mpapf sts: Any Application Error\x1b[0m");
errMsg("papf sts: Any Application Error");
}
if ((data_var->header.status & 0x04)) {
logMsg("\x1b[31;1mpapf sts: Power Low\x1b[0m");
errMsg("papf sts: Power Low");
}
if ((data_var->header.status & 0x08)) {
logMsg("\x1b[31;1mpapf sts: Permanent Error\x1b[0m");
errMsg("papf sts: Permanent Error");
}
if ((data_var->header.status & 0x10)) {
logMsg("\x1b[31;1mpapf sts: Temporary Error\x1b[0m");
errMsg("papf sts: Temporary Error");
}
if ((data_var->header.status & 0x20)) {
logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 1\x1b[0m");
errMsg("papf sts: Specific to manufacturer Error 1");
}
if ((data_var->header.status & 0x40)) {
logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 2\x1b[0m");
errMsg("papf sts: Specific to manufacturer Error 2");
}
if ((data_var->header.status & 0x80)) {
logMsg("\x1b[31;1mpapf sts: Specific to manufacturer Error 3\x1b[0m");
errMsg("papf sts: Specific to manufacturer Error 3");
}
mbus_data_record *record;
int i;
@ -161,7 +161,6 @@ static void parseAndPrintFrame(t_mbusCommHandle *mbusCommHandle) {
} else {
logMsg("papf err: unable to parse frame");
}
logMsg("\x1b[0m");
}