From 64be05f249dcc34fe157bbb4acf8c3179f6899b7 Mon Sep 17 00:00:00 2001 From: knmcguire Date: Mon, 16 Nov 2020 15:27:02 +0100 Subject: [PATCH] (#647) cleaned up Log internal api --- examples/demos/app_push_demo/src/push.c | 10 +-- examples/demos/swarm_demo/app.c | 18 +++--- src/deck/drivers/src/usddeck.c | 2 +- src/modules/interface/log.h | 85 ++++++++++++++++++++++--- src/modules/src/log.c | 29 +++++---- 5 files changed, 109 insertions(+), 35 deletions(-) diff --git a/examples/demos/app_push_demo/src/push.c b/examples/demos/app_push_demo/src/push.c index 1bacd7581d..afa4c4c835 100644 --- a/examples/demos/app_push_demo/src/push.c +++ b/examples/demos/app_push_demo/src/push.c @@ -89,11 +89,11 @@ void appMain() vTaskDelay(M2T(3000)); - uint16_t idUp = logGetVarId("range", "up"); - uint16_t idLeft = logGetVarId("range", "left"); - uint16_t idRight = logGetVarId("range", "right"); - uint16_t idFront = logGetVarId("range", "front"); - uint16_t idBack = logGetVarId("range", "back"); + logVarId_t idUp = logGetVarId("range", "up"); + logVarId_t idLeft = logGetVarId("range", "left"); + logVarId_t idRight = logGetVarId("range", "right"); + logVarId_t idFront = logGetVarId("range", "front"); + logVarId_t idBack = logGetVarId("range", "back"); paramVarId_t idPositioningDeck = paramGetVarId("deck", "bcFlow2"); paramVarId_t idMultiranger = paramGetVarId("deck", "bcMultiranger"); diff --git a/examples/demos/swarm_demo/app.c b/examples/demos/swarm_demo/app.c index 3cd28fb97a..eaa5b436c9 100644 --- a/examples/demos/swarm_demo/app.c +++ b/examples/demos/swarm_demo/app.c @@ -74,15 +74,15 @@ static uint8_t trajectoryCount = 255; static uint8_t remainingTrajectories = 0; // Log and param ids -static uint16_t logIdStateEstimateX; -static uint16_t logIdStateEstimateY; -static uint16_t logIdStateEstimateZ; -static uint16_t logIdKalmanVarPX; -static uint16_t logIdKalmanVarPY; -static uint16_t logIdKalmanVarPZ; -static uint16_t logIdPmState; -static uint16_t logIdlighthouseEstBs0Rt; -static uint16_t logIdlighthouseEstBs1Rt; +static logVarId_t logIdStateEstimateX; +static logVarId_t logIdStateEstimateY; +static logVarId_t logIdStateEstimateZ; +static logVarId_t logIdKalmanVarPX; +static logVarId_t logIdKalmanVarPY; +static logVarId_t logIdKalmanVarPZ; +static logVarId_t logIdPmState; +static logVarId_t logIdlighthouseEstBs0Rt; +static logVarId_t logIdlighthouseEstBs1Rt; static paramVarId_t paramIdStabilizerController; static paramVarId_t paramIdCommanderEnHighLevel; diff --git a/src/deck/drivers/src/usddeck.c b/src/deck/drivers/src/usddeck.c index 04372ef7be..4cd9a6b5bd 100644 --- a/src/deck/drivers/src/usddeck.c +++ b/src/deck/drivers/src/usddeck.c @@ -546,7 +546,7 @@ void usddeckTriggerLogging(void) memcpy(usdLogBuffer, &ticks, 4); int offset = 4; for (int i = 0; i < usdLogConfig.numSlots; ++i) { - int varid = usdLogConfig.varIds[i]; + logVarId_t varid = usdLogConfig.varIds[i]; switch (logGetType(varid)) { case LOG_UINT8: case LOG_INT8: diff --git a/src/modules/interface/log.h b/src/modules/interface/log.h index b1384fbacf..fa061f682d 100644 --- a/src/modules/interface/log.h +++ b/src/modules/interface/log.h @@ -34,15 +34,84 @@ void logInit(void); bool logTest(void); -/* Internal access of log variables */ -int logGetVarId(char* group, char* name); -int logGetType(int varid); -void logGetGroupAndName(int varid, char** group, char** name); -void* logGetAddress(int varid); +/* Public API to access of log variables */ + +/** Variable identifier. + * + * Should be fetched with logGetVarId(). This is to be considered as an + * opaque type, internal structure might change. + * + * Use LOG_VARID_IS_VALID() to check if the ID is valid. + */ +typedef uint16_t logVarId_t; + +/** Get the varId from group and name of variable + * + * @param group Group name of the variable + * @param name Name of the variable + * @return The variable ID or an invalid ID. Use LOG_VARID_IS_VALID() to check validity. + */ +logVarId_t logGetVarId(char* group, char* name); + +/** Check variable ID validity + * + * @param varId variable ID, returned by logGetLogId() + * @return true if the variable ID is valid, false otherwise. + */ +#define LOG_VARID_IS_VALID(varId) (varId != 0xffffu) + +/** Return the logging type + * + * @param varId variable ID, returned by logGetVarId() + * @return Type of the variable. The value correspond to the defines used when + * declaring a param variable. + */ +int logGetType(logVarId_t varid); + +/** Get group and name strings of a parameter + * + * @param varId variable ID, returned by logGetVarId() + * @param group Pointer to a char* that will be filled with the group name + * @param group Pointer to a char* that will be filled with the variable name + * + * The string buffers must be able to hold at least 32 bytes. + */ +void logGetGroupAndName(logVarId_t varid, char** group, char** name); + +/** Get address of the logging variable + * + * @param varId variable ID, returned by logGetVarId() + * @return Address of the location of log variable + * */ +void* logGetAddress(logVarId_t varid); + +/** Get log variable size in byte + * + * @param type Type returned by logGetType() + * @return Size in byte occupied by variable of this type + */ uint8_t logVarSize(int type); -float logGetFloat(int varid); -int logGetInt(int varid); -unsigned int logGetUint(int varid); + +/** Return float value of a logging variable + * + * @param varId variable ID, returned by logGetVarId() + * @return Current value of the variable + */ +float logGetFloat(logVarId_t varid); + +/** Return int value of a logging variable + * + * @param varId variable ID, returned by logGetVarId() + * @return Current value of the variable + */ +int logGetInt(logVarId_t varid); + +/** Return Unsigned int value of a logging variable + * + * @param varId variable ID, returned by paramGetVarId() + * @return Current value of the variable + */ +unsigned int logGetUint(logVarId_t varid); /* Basic log structure */ struct log_s { diff --git a/src/modules/src/log.c b/src/modules/src/log.c index 354521916f..63a96ae26c 100644 --- a/src/modules/src/log.c +++ b/src/modules/src/log.c @@ -960,9 +960,12 @@ static void logReset(void) } /* Public API to access log TOC from within the copter */ -int logGetVarId(char* group, char* name) +static logVarId_t invalidVarId = 0xffffu; + +logVarId_t logGetVarId(char* group, char* name) { int i; + logVarId_t varId = invalidVarId; char * currgroup = ""; for(i=0; i= 0); + ASSERT(LOG_VARID_IS_VALID(varid)); switch(logs[varid].type) { @@ -1047,9 +1052,9 @@ int logGetInt(int varid) return valuei; } -float logGetFloat(int varid) +float logGetFloat(logVarId_t varid) { - ASSERT(varid >= 0); + ASSERT(LOG_VARID_IS_VALID(varid)); if (logs[varid].type == LOG_FLOAT) return *(float *)logs[varid].address; @@ -1057,7 +1062,7 @@ float logGetFloat(int varid) return logGetInt(varid); } -unsigned int logGetUint(int varid) +unsigned int logGetUint(logVarId_t varid) { return (unsigned int)logGetInt(varid); }