Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constexpr helpers to identify core version #5269

Merged
merged 10 commits into from
Nov 29, 2018
Prev Previous commit
Next Next commit
introduce namespaces
d-a-v committed Nov 26, 2018
commit 9f3cca994e63815d91e5935c978574f703485922
1 change: 1 addition & 0 deletions cores/esp8266/Esp-version.cpp
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ String EspClass::getFullVersion()
{
return String(F("SDK:")) + system_get_sdk_version()
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
+ F("=") + String(esp8266::coreVersionNumeric())
#if LWIP_VERSION_MAJOR == 1
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
#if LWIP_VERSION_IS_DEVELOPMENT
62 changes: 35 additions & 27 deletions cores/esp8266/core_esp8266_version.h
Original file line number Diff line number Diff line change
@@ -53,20 +53,22 @@ extern "C++"
// case 2.5.0:
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20500000

namespace conststr {

constexpr
bool constexpr_isDecimal (const char c)
bool isDecimal (const char c)
{
return c >= '0' && c <= '9';
}

template<unsigned N> constexpr
bool constexpr_isMinus (const char (&arr) [N], unsigned i)
bool isMinus (const char (&arr) [N], unsigned i)
{
return arr[i] == '-' && constexpr_isDecimal(arr[i+1]);
return arr[i] == '-' && isDecimal(arr[i+1]);
}

template<unsigned N> constexpr
int constexpr_atoi (const char (&arr) [N], unsigned i)
int atoi (const char (&arr) [N], unsigned i)
{
return ({ // <= c++11 requires a "return statement"
int ret = 0;
@@ -76,82 +78,88 @@ int constexpr_atoi (const char (&arr) [N], unsigned i)
sign = -1;
i++;
}
while (constexpr_isDecimal(arr[i]))
while (isDecimal(arr[i]))
ret = 10*ret + arr[i++] - '0';
ret * sign;
});
}

template<unsigned N> constexpr
int constexpr_extract_int (const char (&arr) [N], unsigned f)
int parseNthInteger (const char (&arr) [N], unsigned f)
{
return ({ // <= c++11 requires a "return statement"
unsigned i = 0;
while (f && arr[i])
{
if (constexpr_isMinus(arr, i))
if (isMinus(arr, i))
i++;
for (; constexpr_isDecimal(arr[i]); i++);
for (; isDecimal(arr[i]); i++);
f--;
for (; arr[i] && !constexpr_isMinus(arr, i) && !constexpr_isDecimal(arr[i]); i++);
for (; arr[i] && !isMinus(arr, i) && !isDecimal(arr[i]); i++);
}
constexpr_atoi(arr, i);
atoi(arr, i);
});
}

}; // namespace conststr

namespace esp8266 {

/*
* version major
*/
constexpr
int esp8266CoreVersionMajor ()
int coreVersionMajor ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 0);
return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 0);
}

/*
* version minor
*/
constexpr
int esp8266CoreVersionMinor ()
int coreVersionMinor ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 1);
return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 1);
}

/*
* version revision
*/
constexpr
int esp8266CoreVersionRevision ()
int coreVersionRevision ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 2);
return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 2);
}

/*
* git commit number since last tag (negative)
* or RC-number (positive)
*/
constexpr
int esp8266CoreVersionSubRevision ()
int coreVersionSubRevision ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 3);
return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 3);
}

/*
* unique revision indentifier (never decreases)
*/
constexpr
int esp8266CoreVersionNumeric ()
int coreVersionNumeric ()
{
return esp8266CoreVersionMajor() * 10000000
+ esp8266CoreVersionMinor() * 100000
+ esp8266CoreVersionRevision() * 1000
+ (esp8266CoreVersionSubRevision() < 0 ?
-esp8266CoreVersionSubRevision() :
esp8266CoreVersionSubRevision() ?
esp8266CoreVersionSubRevision() - 100 :
0);
return coreVersionMajor() * 10000000
+ coreVersionMinor() * 100000
+ coreVersionRevision() * 1000
+ (coreVersionSubRevision() < 0 ?
-coreVersionSubRevision() :
coreVersionSubRevision() ?
coreVersionSubRevision() - 100 :
0);
}

}; // namespace esp8266

} // extern "C++"
#endif // __cplusplus
#endif // __CORE_ESP8266_ESP8266_VERSION_H
14 changes: 8 additions & 6 deletions libraries/esp8266/keywords.txt
Original file line number Diff line number Diff line change
@@ -79,9 +79,11 @@ WAKE_RF_DISABLED LITERAL1
ADC_VCC LITERAL1
ADC_TOUT LITERAL1

# constexpr
esp8266CoreVersionMajor LITERAL1
esp8266CoreVersionMinor LITERAL1
esp8266CoreVersionRevision LITERAL1
esp8266CoreVersionSubRevision LITERAL1
esp8266CoreVersionNumeric LITERAL1
#######################################
# namespace esp8266
#######################################
coreVersionMajor LITERAL1
coreVersionMinor LITERAL1
coreVersionRevision LITERAL1
coreVersionSubRevision LITERAL1
coreVersionNumeric LITERAL1