Skip to content

Commit

Permalink
Merge pull request #313 from Metaln00b/version-compare-improvement
Browse files Browse the repository at this point in the history
Improved version comparison
  • Loading branch information
foorschtbar authored Oct 11, 2023
2 parents 31a63bd + 8dae056 commit 3ab2cb0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ Thanks to these great people for supporting this project.

<!-- ** WORK IN PROGRESS ** -->

### 2.4.0 **WORK IN PROGRESS**
### 2.4.x **WORK IN PROGRESS**

- (foorschtbar) Native support for [Ulanzi TC001 Pixel Clock](https://www.ulanzi.com/products/ulanzi-pixel-smart-clock-2882?aff=1181)\* (**Beta**, see [Releases](https://github.com/pixelit-project/PixelIt/releases)) :tada:
- (foorschtbar) Display `zZz` on the matrix when the device going to sleep
- (foorschtbar) Fixed issue with flickering icons, wenn device wake from sleep mode
- (Metaln00b) Improved new firmware check by doing real version compare (major, minor, patch and prerelease)

### 2.3.2 (2023-09-16)

Expand Down
59 changes: 55 additions & 4 deletions src/PixelIt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ unsigned long checkUpdateScreenPrevMillis = 0;
unsigned long checkUpdatePrevMillis = 0;
String lastReleaseVersion = VERSION;

typedef struct {
int major;
int minor;
int patch;
char prerelease[16];
} Version;

// MP3Player Vars
String OldGetMP3PlayerInfo;

Expand Down Expand Up @@ -375,6 +382,45 @@ String ResetReason()
#endif
}

Version parseVersion(const char* versionStr) {
Version version;

memset(version.prerelease, 0, sizeof(version.prerelease));

int x = sscanf(versionStr, "%d.%d.%d-%s", &version.major, &version.minor, &version.patch, version.prerelease);
return version;
}

int compareVersions(const char* version1, const char* version2) {
if (strlen(version1) == 0 || strlen(version2) == 0)
{
return 0;
}

Version v1 = parseVersion(version1);
Version v2 = parseVersion(version2);

if (v1.major != v2.major) {
return v1.major - v2.major;
}
if (v1.minor != v2.minor) {
return v1.minor - v2.minor;
}
if (v1.patch != v2.patch) {
return v1.patch - v2.patch;
}

if (strlen(v1.prerelease) == 0 && strlen(v2.prerelease) == 0) {
return 0; // Versions are equal
} else if (strlen(v1.prerelease) == 0) {
return 1; // v1 is greater (no prerelease for v1, but prerelease for v2)
} else if (strlen(v2.prerelease) == 0) {
return -1; // v2 is greater (no prerelease for v2, but prerelease for v1)
} else {
return strcmp(v1.prerelease, v2.prerelease); // Compare prerelease strings
}
}

void getBatteryVoltage()
{
uint16_t value = 0;
Expand Down Expand Up @@ -3689,13 +3735,16 @@ void checkUpdate()
if (root.containsKey("version"))
{
lastReleaseVersion = root["version"].as<String>();
if (!lastReleaseVersion.equals(VERSION))

int result = compareVersions(lastReleaseVersion.c_str(), VERSION);

if (result > 0)
{
Log(F("CheckUpdate"), F("New FW available"));
Log(F("CheckUpdate"), "New FW available " + String(VERSION) + " -> " + lastReleaseVersion);
}
else
{
Log(F("CheckUpdate"), F("No new FW available"));
Log(F("CheckUpdate"), "No new FW available " + String(VERSION) + " -> " + lastReleaseVersion);
}
}
}
Expand Down Expand Up @@ -3748,7 +3797,9 @@ void loop()
if (millis() - checkUpdateScreenPrevMillis >= CHECKUPDATESCREEN_INTERVAL)
{
checkUpdateScreenPrevMillis = millis();
if (!lastReleaseVersion.equals(VERSION))

int result = compareVersions(lastReleaseVersion.c_str(), VERSION);
if (result > 0)
{
if (!sleepMode)
{
Expand Down

0 comments on commit 3ab2cb0

Please sign in to comment.